blob: 31d340da68f139d2b594ff42876c3f483e14049e [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));
1104#endif
Ryan Everett7ed542e2024-01-17 11:39:09 +00001105 /* Set the key slot containing the key description's state to
1106 * PENDING_DELETION. This stops new operations from registering
1107 * to read the slot. Current readers can safely continue to access
1108 * the key within the slot; the last registered reader will
1109 * automatically wipe the slot when they call psa_unregister_read().
1110 * If the key is persistent, we can now delete the copy of the key
1111 * from memory. If the key is opaque, we require the driver to
1112 * deal with the deletion. */
Ryan Everettc053d962024-01-25 17:56:32 +00001113 status = psa_key_slot_state_transition(slot, PSA_SLOT_FULL,
1114 PSA_SLOT_PENDING_DELETION);
1115
1116 if (status != PSA_SUCCESS) {
1117 goto exit;
1118 }
Ronald Cronf2911112020-10-29 17:51:10 +01001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001121 /* Refuse the destruction of a read-only key (which may or may not work
1122 * if we attempt it, depending on whether the key is merely read-only
1123 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001124 * Just do the best we can, which is to wipe the copy in memory
1125 * (done in this function's cleanup code). */
1126 overall_status = PSA_ERROR_NOT_PERMITTED;
1127 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001128 }
1129
Gilles Peskine354f7672019-07-12 23:46:38 +02001130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1132 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001133 /* For a key in a secure element, we need to do three things:
1134 * remove the key file in internal storage, destroy the
1135 * key inside the secure element, and update the driver's
1136 * persistent data. Start a transaction that will encompass these
1137 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001139 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001141 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 status = psa_crypto_save_transaction();
1143 if (status != PSA_SUCCESS) {
1144 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001145 /* We should still try to destroy the key in the secure
1146 * element and the key metadata in storage. This is especially
1147 * important if the error is that the storage is full.
1148 * But how to do it exactly without risking an inconsistent
1149 * state after a reset?
1150 * https://github.com/ARMmbed/mbed-crypto/issues/215
1151 */
1152 overall_status = status;
1153 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 status = psa_destroy_se_key(driver,
1157 psa_key_slot_get_slot_number(slot));
1158 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001159 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001161 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001162#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1163
Darryl Greend49a4992018-06-18 17:27:26 +01001164#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ryan Everett4a0ba802024-01-17 14:12:33 +00001166 /* Destroy the copy of the persistent key from storage.
Ryan Everett7ed542e2024-01-17 11:39:09 +00001167 * The slot will still hold a copy of the key until the last reader
1168 * unregisters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 status = psa_destroy_persistent_key(slot->attr.id);
1170 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
Darryl Greend49a4992018-06-18 17:27:26 +01001173 }
1174#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001175
Gilles Peskinefc762652019-07-22 19:30:34 +02001176#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (driver != NULL) {
1178 status = psa_save_se_persistent_data(driver);
1179 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001180 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
1182 status = psa_crypto_stop_transaction();
1183 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001184 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001186 }
1187#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1188
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189exit:
Ryan Everett7ed542e2024-01-17 11:39:09 +00001190 /* Unregister from reading the slot. If we are the last active reader
1191 * then this will wipe the slot. */
1192 status = psa_unregister_read(slot);
1193 /* Prioritize CORRUPTION_DETECTED from unregistering over
1194 * a storage error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001196 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 }
Ryan Everettc053d962024-01-25 17:56:32 +00001198
1199#if defined(MBEDTLS_THREADING_C)
Ryan Everett7fee4f72024-02-09 14:11:27 +00001200 /* Don't overwrite existing errors if the unlock fails. */
1201 status = overall_status;
Ryan Everettc053d962024-01-25 17:56:32 +00001202 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1203 &mbedtls_threading_key_slot_mutex));
1204#endif
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001207}
1208
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001209/** Retrieve all the publicly-accessible attributes of a key.
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1212 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001215 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1220 if (status != PSA_SUCCESS) {
1221 return status;
1222 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001223
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001224 *attributes = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001225
1226#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1228 psa_set_key_slot_number(attributes,
1229 psa_key_slot_get_slot_number(slot));
1230 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001232
Gilles Peskine97c0b2f2024-02-16 00:49:46 +01001233 return psa_unregister_read_under_mutex(slot);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237psa_status_t psa_get_key_slot_number(
1238 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240{
Gilles Peskine972539c2024-02-28 01:49:45 +01001241 if (attributes->has_slot_number) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001242 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 return PSA_SUCCESS;
1244 } else {
1245 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001247}
1248#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1251 size_t key_buffer_size,
1252 uint8_t *data,
1253 size_t data_size,
1254 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001255{
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (key_buffer_size > data_size) {
1257 return PSA_ERROR_BUFFER_TOO_SMALL;
1258 }
1259 memcpy(data, key_buffer, key_buffer_size);
1260 memset(data + key_buffer_size, 0,
1261 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001262 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001264}
1265
Ronald Cron7285cda2020-11-26 14:46:37 +01001266psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 const psa_key_attributes_t *attributes,
1268 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001271 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (key_type_is_raw_bytes(type) ||
1274 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001275 PSA_KEY_TYPE_IS_ECC(type) ||
1276 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return psa_export_key_buffer_internal(
1278 key_buffer, key_buffer_size,
1279 data, data_size, data_length);
1280 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001281 /* This shouldn't happen in the reference implementation, but
1282 it is valid for a special-purpose implementation to omit
1283 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001285 }
1286}
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1289 uint8_t *data,
1290 size_t data_size,
1291 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001292{
1293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1294 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1295 psa_key_slot_t *slot;
1296
Ronald Crone907e552021-01-18 13:22:38 +01001297 /* Reject a zero-length output buffer now, since this can never be a
1298 * valid key representation. This way we know that data must be a valid
1299 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (data_size == 0) {
1301 return PSA_ERROR_BUFFER_TOO_SMALL;
1302 }
Ronald Crone907e552021-01-18 13:22:38 +01001303
Ronald Cron9486f9d2020-11-26 13:36:23 +01001304 /* Set the key to empty now, so that even when there are errors, we always
1305 * set data_length to a value between 0 and data_size. On error, setting
1306 * the key to empty is a good choice because an empty key representation is
1307 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001308 *data_length = 0;
1309
Ronald Cron9486f9d2020-11-26 13:36:23 +01001310 /* Export requires the EXPORT flag. There is an exception for public keys,
1311 * which don't require any flag, but
1312 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1315 PSA_KEY_USAGE_EXPORT, 0);
1316 if (status != PSA_SUCCESS) {
1317 return status;
1318 }
mohammad160306e79202018-03-28 13:17:44 +03001319
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001320 status = psa_driver_wrapper_export_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 slot->key.data, slot->key.bytes,
1322 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001323
Ryan Everetta103ec92024-01-31 13:59:57 +00001324 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001327}
1328
Ronald Cron7285cda2020-11-26 14:46:37 +01001329psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001330 const psa_key_attributes_t *attributes,
1331 const uint8_t *key_buffer,
1332 size_t key_buffer_size,
1333 uint8_t *data,
1334 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001336{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001337 psa_key_type_t type = attributes->type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001338
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001339 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001340 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1341 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001342 /* Exporting public -> public */
1343 return psa_export_key_buffer_internal(
1344 key_buffer, key_buffer_size,
1345 data, data_size, data_length);
1346 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001347#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001348 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1349 return mbedtls_psa_rsa_export_public_key(attributes,
1350 key_buffer,
1351 key_buffer_size,
1352 data,
1353 data_size,
1354 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001356 /* We don't know how to convert a private RSA key to public. */
1357 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001358#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001359 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001360 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001361#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001362 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1363 return mbedtls_psa_ecp_export_public_key(attributes,
1364 key_buffer,
1365 key_buffer_size,
1366 data,
1367 data_size,
1368 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001369#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001370 /* We don't know how to convert a private ECC key to public */
1371 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001372#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001373 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001374 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001375#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001376 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001377 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001378 key_buffer,
1379 key_buffer_size,
1380 data, data_size,
1381 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001382#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001383 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001384#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001385 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001387 (void) key_buffer;
1388 (void) key_buffer_size;
1389 (void) data;
1390 (void) data_size;
1391 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001393 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394}
Ronald Crond18b5f82020-11-25 16:46:05 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1397 uint8_t *data,
1398 size_t data_size,
1399 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001400{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001402 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001403 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001404
Ronald Crone907e552021-01-18 13:22:38 +01001405 /* Reject a zero-length output buffer now, since this can never be a
1406 * valid key representation. This way we know that data must be a valid
1407 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (data_size == 0) {
1409 return PSA_ERROR_BUFFER_TOO_SMALL;
1410 }
Ronald Crone907e552021-01-18 13:22:38 +01001411
Darryl Greendd8fb772018-11-07 16:00:44 +00001412 /* Set the key to empty now, so that even when there are errors, we always
1413 * set data_length to a value between 0 and data_size. On error, setting
1414 * the key to empty is a good choice because an empty key representation is
1415 * unlikely to be accepted anywhere. */
1416 *data_length = 0;
1417
1418 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1420 if (status != PSA_SUCCESS) {
1421 return status;
1422 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1425 status = PSA_ERROR_INVALID_ARGUMENT;
1426 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001427 }
1428
Ronald Cron67227982020-11-26 15:16:05 +01001429 status = psa_driver_wrapper_export_public_key(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01001430 &slot->attr, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001432
1433exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001434 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001437}
1438
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001439/** Validate that a key policy is internally well-formed.
1440 *
1441 * This function only rejects invalid policies. It does not validate the
1442 * consistency of the policy with respect to other attributes of the key
1443 * such as the key type.
1444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001446{
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1448 PSA_KEY_USAGE_COPY |
1449 PSA_KEY_USAGE_ENCRYPT |
1450 PSA_KEY_USAGE_DECRYPT |
1451 PSA_KEY_USAGE_SIGN_MESSAGE |
1452 PSA_KEY_USAGE_VERIFY_MESSAGE |
1453 PSA_KEY_USAGE_SIGN_HASH |
1454 PSA_KEY_USAGE_VERIFY_HASH |
1455 PSA_KEY_USAGE_VERIFY_DERIVATION |
1456 PSA_KEY_USAGE_DERIVE)) != 0) {
1457 return PSA_ERROR_INVALID_ARGUMENT;
1458 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001461}
1462
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001463/** Validate the internal consistency of key attributes.
1464 *
1465 * This function only rejects invalid attribute values. If does not
1466 * validate the consistency of the attributes with any key data that may
1467 * be involved in the creation of the key.
1468 *
1469 * Call this function early in the key creation process.
1470 *
1471 * \param[in] attributes Key attributes for the new key.
1472 * \param[out] p_drv On any return, the driver for the key, if any.
1473 * NULL for a transparent key.
1474 *
1475 */
1476static psa_status_t psa_validate_key_attributes(
1477 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001479{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001480 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1482 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 status = psa_validate_key_location(lifetime, p_drv);
1485 if (status != PSA_SUCCESS) {
1486 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001487 }
1488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 status = psa_validate_key_persistence(lifetime);
1490 if (status != PSA_SUCCESS) {
1491 return status;
1492 }
1493
1494 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1495 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1496 return PSA_ERROR_INVALID_ARGUMENT;
1497 }
1498 } else {
1499 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1500 return PSA_ERROR_INVALID_ARGUMENT;
1501 }
1502 }
1503
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001504 status = psa_validate_key_policy(&attributes->policy);
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (status != PSA_SUCCESS) {
1506 return status;
1507 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001508
1509 /* Refuse to create overly large keys.
1510 * Note that this doesn't trigger on import if the attributes don't
1511 * explicitly specify a size (so psa_get_key_bits returns 0), so
1512 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1514 return PSA_ERROR_NOT_SUPPORTED;
1515 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001518}
1519
Gilles Peskine4747d192019-04-17 15:05:45 +02001520/** Prepare a key slot to receive key material.
1521 *
1522 * This function allocates a key slot and sets its metadata.
1523 *
1524 * If this function fails, call psa_fail_key_creation().
1525 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001526 * This function is intended to be used as follows:
1527 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001528 * it with the specified attributes, and in case of a volatile key assign it
1529 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001530 * -# Populate the slot with the key material.
1531 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1532 * In case of failure at any step, stop the sequence and call
1533 * psa_fail_key_creation().
1534 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001535 * On success, the key slot's state is PSA_SLOT_FILLING.
1536 * It is the responsibility of the caller to change the slot's state to
1537 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001538 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001539 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001540 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001541 * \param[out] p_slot On success, a pointer to the prepared slot.
1542 * \param[out] p_drv On any return, the driver for the key, if any.
1543 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001544 *
1545 * \retval #PSA_SUCCESS
1546 * The key slot is ready to receive key material.
1547 * \return If this function fails, the key slot is an invalid state.
1548 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 */
1550static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001551 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001553 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001555{
1556 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001557 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 psa_key_slot_t *slot;
1559
Gilles Peskinedf179142019-07-15 22:02:14 +02001560 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 *p_drv = NULL;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 status = psa_validate_key_attributes(attributes, p_drv);
1564 if (status != PSA_SUCCESS) {
1565 return status;
1566 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567
Ryan Everett3d8118d2024-01-30 16:58:47 +00001568#if defined(MBEDTLS_THREADING_C)
1569 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1570 &mbedtls_threading_key_slot_mutex));
1571#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001572 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001573#if defined(MBEDTLS_THREADING_C)
1574 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1575 &mbedtls_threading_key_slot_mutex));
1576#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (status != PSA_SUCCESS) {
1578 return status;
1579 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 slot = *p_slot;
1581
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001582 /* We're storing the declared bit-size of the key. It's up to each
1583 * creation mechanism to verify that this information is correct.
1584 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001585 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001586 * is optional (import, copy). In case of a volatile key, assign it the
1587 * volatile key identifier associated to the slot returned to contain its
1588 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001589
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01001590 slot->attr = *attributes;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001592#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1593 slot->attr.id = volatile_key_id;
1594#else
1595 slot->attr.id.key_id = volatile_key_id;
1596#endif
1597 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001598
Gilles Peskinecbaff462019-07-12 23:46:04 +02001599#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001600 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001601 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001602 * create the key file in internal storage, create the
1603 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001604 * persistent data. This is done by starting a transaction that will
1605 * encompass these three actions.
1606 * For registering a volatile key, we just need to find an appropriate
1607 * slot number inside the SE. Since the key is designated volatile, creating
1608 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001609 /* The first thing to do is to find a slot number for the new key.
1610 * We save the slot number in persistent storage as part of the
1611 * transaction data. It will be needed to recover if the power
1612 * fails during the key creation process, to clean up on the secure
1613 * element side after restarting. Obtaining a slot number from the
1614 * secure element driver updates its persistent state, but we do not yet
1615 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001616 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001618 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1620 &slot_number);
1621 if (status != PSA_SUCCESS) {
1622 return status;
1623 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001624
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001625 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001627 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001628 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001629 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 status = psa_crypto_save_transaction();
1631 if (status != PSA_SUCCESS) {
1632 (void) psa_crypto_stop_transaction();
1633 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001634 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001635 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001636
1637 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001639 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001642 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001644 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001648}
Gilles Peskine4747d192019-04-17 15:05:45 +02001649
1650/** Finalize the creation of a key once its key material has been set.
1651 *
1652 * This entails writing the key to persistent storage.
1653 *
1654 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * See the documentation of psa_start_key_creation() for the intended use
1656 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001658 * If the finalization succeeds, the function sets the key slot's state to
1659 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1660 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001661 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 * \param[in,out] slot Pointer to the slot with key material.
1663 * \param[in] driver The secure element driver for the key,
1664 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001665 * \param[out] key On success, identifier of the key. Note that the
1666 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001667 *
1668 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001669 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1671 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1672 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1673 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1674 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1675 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001676 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001677 * \return If this function fails, the key slot is an invalid state.
1678 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001679 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001680static psa_status_t psa_finish_key_creation(
1681 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001682 psa_se_drv_table_entry_t *driver,
1683 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001684{
1685 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001686 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001687 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001688
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001689#if defined(MBEDTLS_THREADING_C)
1690 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1691 &mbedtls_threading_key_slot_mutex));
1692#endif
1693
Gilles Peskine4747d192019-04-17 15:05:45 +02001694#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001698 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001699 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001702 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1703 sizeof(data.slot_number),
1704 "Slot number size does not match psa_se_key_data_storage_t");
1705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1707 status = psa_save_persistent_key(&slot->attr,
1708 (uint8_t *) &data,
1709 sizeof(data));
1710 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001711#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1712 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001713 /* Key material is saved in export representation in the slot, so
1714 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 status = psa_save_persistent_key(&slot->attr,
1716 slot->key.data,
1717 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001718 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001720#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1721
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001723 /* Finish the transaction for a key creation. This does not
1724 * happen when registering an existing key. Detect this case
1725 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001726 * creation of a persistent key in a secure element requires a transaction,
1727 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (driver != NULL &&
1729 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1730 status = psa_save_se_persistent_data(driver);
1731 if (status != PSA_SUCCESS) {
1732 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001733
1734#if defined(MBEDTLS_THREADING_C)
1735 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1736 &mbedtls_threading_key_slot_mutex));
1737#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001739 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001741 }
1742#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001745 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001746 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1747 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001749 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001751 }
Ronald Cron50972942020-11-14 11:28:25 +01001752
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001753#if defined(MBEDTLS_THREADING_C)
1754 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1755 &mbedtls_threading_key_slot_mutex));
1756#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001758}
1759
1760/** Abort the creation of a key.
1761 *
1762 * You may call this function after calling psa_start_key_creation(),
1763 * or after psa_finish_key_creation() fails. In other circumstances, this
1764 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001765 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001766 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 * \param[in,out] slot Pointer to the slot with key material.
1769 * \param[in] driver The secure element driver for the key,
1770 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772static void psa_fail_key_creation(psa_key_slot_t *slot,
1773 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001774{
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 (void) driver;
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001780
Ryan Everettb7101442024-01-23 20:09:49 +00001781#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001782 /* If the lock operation fails we still wipe the slot.
1783 * Operations will no longer work after a failed lock,
1784 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001785 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1786#endif
1787
Gilles Peskine011e4282019-06-26 18:34:38 +02001788#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001789 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001790 * element, and the failure happened later (when saving metadata
1791 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001792 * element.
1793 * https://github.com/ARMmbed/mbed-crypto/issues/217
1794 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001795
Gilles Peskined7729582019-08-05 15:55:54 +02001796 /* Abort the ongoing transaction if any (there may not be one if
1797 * the creation process failed before starting one, or if the
1798 * key creation is a registration of a key in a secure element).
1799 * Earlier functions must already have done what it takes to undo any
1800 * partial creation. All that's left is to update the transaction data
1801 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001803#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001806
1807#if defined(MBEDTLS_THREADING_C)
1808 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1809#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001810}
1811
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001812/** Validate optional attributes during key creation.
1813 *
1814 * Some key attributes are optional during key creation. If they are
1815 * specified in the attributes structure, check that they are consistent
1816 * with the data in the slot.
1817 *
1818 * This function should be called near the end of key creation, after
1819 * the slot in memory is fully populated but before saving persistent data.
1820 */
1821static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001822 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001824{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001825 if (attributes->type != 0) {
1826 if (attributes->type != slot->attr.type) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 return PSA_ERROR_INVALID_ARGUMENT;
1828 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 }
1830
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001831 if (attributes->bits != 0) {
1832 if (attributes->bits != slot->attr.bits) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 return PSA_ERROR_INVALID_ARGUMENT;
1834 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 }
1836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838}
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1841 const uint8_t *data,
1842 size_t data_length,
1843 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001844{
1845 psa_status_t status;
1846 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001847 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001848 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301849 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001850
Ronald Cron81709fc2020-11-14 12:10:32 +01001851 *key = MBEDTLS_SVC_KEY_ID_INIT;
1852
Gilles Peskine0f84d622019-09-12 19:03:13 +02001853 /* Reject zero-length symmetric keys (including raw data key objects).
1854 * This also rejects any key which might be encoded as an empty string,
1855 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 if (data_length == 0) {
1857 return PSA_ERROR_INVALID_ARGUMENT;
1858 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001859
Archana449608b2021-09-08 15:36:05 +05301860 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (data_length > SIZE_MAX / 8) {
1862 return PSA_ERROR_NOT_SUPPORTED;
1863 }
Archana6ed4bda2021-08-04 10:47:15 +05301864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1866 &slot, &driver);
1867 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001871 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301872 * storage ( thus not in the case of importing a key in a secure element
1873 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1874 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001876 if (psa_key_lifetime_is_external(attributes->lifetime)) {
Archana449608b2021-09-08 15:36:05 +05301877 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 attributes, data, data_length, &storage_size);
1879 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05301880 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 }
Archanad8a83dc2021-06-14 10:04:16 +05301882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 status = psa_allocate_buffer_to_slot(slot, storage_size);
1884 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001885 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001887 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001888
1889 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 status = psa_driver_wrapper_import_key(attributes,
1891 data, data_length,
1892 slot->key.data,
1893 slot->key.bytes,
1894 &slot->key.bytes, &bits);
1895 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001896 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001900 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001902 status = PSA_ERROR_INVALID_ARGUMENT;
1903 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001904 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001905
Archana6ed4bda2021-08-04 10:47:15 +05301906 /* Enforce a size limit, and in particular ensure that the bit
1907 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05301909 status = PSA_ERROR_NOT_SUPPORTED;
1910 goto exit;
1911 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 status = psa_validate_optional_attributes(slot, attributes);
1913 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001914 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (status != PSA_SUCCESS) {
1920 psa_fail_key_creation(slot, driver);
1921 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001924}
1925
Gilles Peskined7729582019-08-05 15:55:54 +02001926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1927psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02001929{
1930 psa_status_t status;
1931 psa_key_slot_t *slot = NULL;
1932 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001934
1935 /* Leaving attributes unspecified is not currently supported.
1936 * It could make sense to query the key type and size from the
1937 * secure element, but not all secure elements support this
1938 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
1940 return PSA_ERROR_NOT_SUPPORTED;
1941 }
1942 if (psa_get_key_bits(attributes) == 0) {
1943 return PSA_ERROR_NOT_SUPPORTED;
1944 }
Gilles Peskined7729582019-08-05 15:55:54 +02001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
1947 &slot, &driver);
1948 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02001949 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 }
Gilles Peskined7729582019-08-05 15:55:54 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02001953
1954exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (status != PSA_SUCCESS) {
1956 psa_fail_key_creation(slot, driver);
1957 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001958
Gilles Peskined7729582019-08-05 15:55:54 +02001959 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 psa_close_key(key);
1961 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02001962}
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
1966 const psa_key_attributes_t *specified_attributes,
1967 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01001968{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001970 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001971 psa_key_slot_t *source_slot = NULL;
1972 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001974 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05301975 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01001976
Ronald Cron81709fc2020-11-14 12:10:32 +01001977 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1978
Archana8a180362021-07-05 02:18:48 +05301979 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
1981 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_validate_optional_attributes(source_slot,
1986 specified_attributes);
1987 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990
Archana9d17bf42021-09-10 06:22:44 +05301991 /* The target key type and number of bits have been validated by
1992 * psa_validate_optional_attributes() to be either equal to zero or
1993 * equal to the ones of the source key. So it is safe to inherit
1994 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05301995 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01001996 actual_attributes.bits = source_slot->attr.bits;
1997 actual_attributes.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05301998
1999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 status = psa_restrict_key_policy(source_slot->attr.type,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002001 &actual_attributes.policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 &source_slot->attr.policy);
2003 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2008 &target_slot, &driver);
2009 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 }
2012 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2013 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302014 /*
Archana9d17bf42021-09-10 06:22:44 +05302015 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302016 * the source key would need to be exported as plaintext and re-imported
2017 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302018 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302019 * appropriate API invocations from the application, if needed.
2020 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002021 status = PSA_ERROR_NOT_SUPPORTED;
2022 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002023 }
Archana8a180362021-07-05 02:18:48 +05302024 /*
2025 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302026 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302027 * - For opaque keys this translates to an invocation of the drivers'
2028 * copy_key entry point through the dispatch layer.
2029 * */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002030 if (psa_key_lifetime_is_external(actual_attributes.lifetime)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2032 &storage_size);
2033 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302034 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 }
Archana374fe5b2021-09-08 15:50:28 +05302036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2038 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302039 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Archana374fe5b2021-09-08 15:50:28 +05302041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_driver_wrapper_copy_key(&actual_attributes,
2043 source_slot->key.data,
2044 source_slot->key.bytes,
2045 target_slot->key.data,
2046 target_slot->key.bytes,
2047 &target_slot->key.bytes);
2048 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
2051 } else {
2052 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302053 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 source_slot->key.bytes);
2055 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 }
Archana8a180362021-07-05 02:18:48 +05302058 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002060exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (status != PSA_SUCCESS) {
2062 psa_fail_key_creation(target_slot, driver);
2063 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002064
Ryan Everetta103ec92024-01-31 13:59:57 +00002065 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002068}
2069
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002070
2071
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002072/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002073/* Message digests */
2074/****************************************************************/
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002077{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002078 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (operation->id == 0) {
2080 return PSA_SUCCESS;
2081 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002084 operation->id = 0;
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087}
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2090 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002091{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002093
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002094 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002096 status = PSA_ERROR_BAD_STATE;
2097 goto exit;
2098 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002101 status = PSA_ERROR_INVALID_ARGUMENT;
2102 goto exit;
2103 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002104
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002105 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2106 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002110
2111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if (status != PSA_SUCCESS) {
2113 psa_hash_abort(operation);
2114 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002115
2116 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117}
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2120 const uint8_t *input,
2121 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002126 status = PSA_ERROR_BAD_STATE;
2127 goto exit;
2128 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002129
Steven Cooremanc8288352021-03-04 14:02:19 +01002130 /* Don't require hash implementations to behave correctly on a
2131 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (input_length == 0) {
2133 return PSA_SUCCESS;
2134 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002137
2138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (status != PSA_SUCCESS) {
2140 psa_hash_abort(operation);
2141 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144}
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2147 uint8_t *hash,
2148 size_t hash_size,
2149 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002151 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (operation->id == 0) {
2153 return PSA_ERROR_BAD_STATE;
2154 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002155
Steven Cooreman1e582352021-02-18 17:24:37 +01002156 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 operation, hash, hash_size, hash_length);
2158 psa_hash_abort(operation);
2159 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002160}
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2163 const uint8_t *hash,
2164 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165{
Ronald Cron69a63422021-10-18 09:47:58 +02002166 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002168 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 operation,
2170 actual_hash, sizeof(actual_hash),
2171 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002178 status = PSA_ERROR_INVALID_SIGNATURE;
2179 goto exit;
2180 }
2181
Dave Rodgman78701152023-08-29 14:20:18 +01002182 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002183 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002185
2186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2188 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002189 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193}
2194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195psa_status_t psa_hash_compute(psa_algorithm_t alg,
2196 const uint8_t *input, size_t input_length,
2197 uint8_t *hash, size_t hash_size,
2198 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002199{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002200 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (!PSA_ALG_IS_HASH(alg)) {
2202 return PSA_ERROR_INVALID_ARGUMENT;
2203 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2206 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002207}
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209psa_status_t psa_hash_compare(psa_algorithm_t alg,
2210 const uint8_t *input, size_t input_length,
2211 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002212{
Ronald Cron69a63422021-10-18 09:47:58 +02002213 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002214 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (!PSA_ALG_IS_HASH(alg)) {
2217 return PSA_ERROR_INVALID_ARGUMENT;
2218 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002219
Steven Cooreman1e582352021-02-18 17:24:37 +01002220 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 alg, input, input_length,
2222 actual_hash, sizeof(actual_hash),
2223 &actual_hash_length);
2224 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 }
2227 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002228 status = PSA_ERROR_INVALID_SIGNATURE;
2229 goto exit;
2230 }
Dave Rodgman78701152023-08-29 14:20:18 +01002231 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002232 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002234
2235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2237 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002238}
2239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2241 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002242{
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (source_operation->id == 0 ||
2244 target_operation->id != 0) {
2245 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002246 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2249 target_operation);
2250 if (status != PSA_SUCCESS) {
2251 psa_hash_abort(target_operation);
2252 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002255}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256
2257
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259/* MAC */
2260/****************************************************************/
2261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002263{
Steven Cooremane6804192021-03-19 18:28:56 +01002264 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (operation->id == 0) {
2266 return PSA_SUCCESS;
2267 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002270 operation->mac_size = 0;
2271 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002272 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275}
2276
Ronald Cron2dff3b22021-06-17 16:33:22 +02002277static psa_status_t psa_mac_finalize_alg_and_key_validation(
2278 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002279 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002281{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 psa_key_type_t key_type = psa_get_key_type(attributes);
2284 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (!PSA_ALG_IS_MAC(alg)) {
2287 return PSA_ERROR_INVALID_ARGUMENT;
2288 }
Steven Cooremane6804192021-03-19 18:28:56 +01002289
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002290 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 status = psa_mac_key_can_do(alg, key_type);
2292 if (status != PSA_SUCCESS) {
2293 return status;
2294 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002295
Steven Cooremand1a68f12021-05-10 11:13:41 +02002296 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002300 /* A very short MAC is too short for security since it can be
2301 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2302 * so we make this our minimum, even though 32 bits is still
2303 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002305 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2308 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002309 /* It's impossible to "truncate" to a larger length than the full length
2310 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002312 }
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002315 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2316 * that is disabled in the compile-time configuration. The result can
2317 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2318 * configuration into account. In this case, force a return of
2319 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2320 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2321 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2322 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2323 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002325 }
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002328}
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2331 mbedtls_svc_key_id_t key,
2332 psa_algorithm_t alg,
2333 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002334{
2335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2336 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002337 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002338
2339 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002341 status = PSA_ERROR_BAD_STATE;
2342 goto exit;
2343 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344
2345 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 key,
2347 &slot,
2348 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2349 alg);
2350 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002351 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002354 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 &operation->mac_size);
2356 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002357 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002359
2360 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002361 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (is_sign) {
2363 status = psa_driver_wrapper_mac_sign_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002364 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 slot->key.data,
2366 slot->key.bytes,
2367 alg);
2368 } else {
2369 status = psa_driver_wrapper_mac_verify_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002370 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 slot->key.data,
2372 slot->key.bytes,
2373 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002374 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002375
Gilles Peskinefbfac682018-07-08 20:51:54 +02002376exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (status != PSA_SUCCESS) {
2378 psa_mac_abort(operation);
2379 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002380
Ryan Everettfb9857f2024-02-14 12:16:41 +00002381 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384}
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2387 mbedtls_svc_key_id_t key,
2388 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002389{
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002391}
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2394 mbedtls_svc_key_id_t key,
2395 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002396{
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002398}
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2401 const uint8_t *input,
2402 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002403{
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (operation->id == 0) {
2405 return PSA_ERROR_BAD_STATE;
2406 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002407
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002408 /* Don't require hash implementations to behave correctly on a
2409 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (input_length == 0) {
2411 return PSA_SUCCESS;
2412 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2415 input, input_length);
2416 if (status != PSA_SUCCESS) {
2417 psa_mac_abort(operation);
2418 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002421}
2422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2424 uint8_t *mac,
2425 size_t mac_size,
2426 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002427{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2429 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002432 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002433 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002434 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002437 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002438 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002439 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002440
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002441 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2442 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002444 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002445 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002446 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002449 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002450 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002451 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 status = psa_driver_wrapper_mac_sign_finish(operation,
2454 mac, operation->mac_size,
2455 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002456
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002457exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002458 /* In case of success, set the potential excess room in the output buffer
2459 * to an invalid value, to avoid potentially leaking a longer MAC.
2460 * In case of error, set the output length and content to a safe default,
2461 * such that in case the caller misses an error check, the output would be
2462 * an unachievable MAC.
2463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002465 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002466 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002467 }
mohammad16036df908f2018-04-02 08:34:15 -07002468
Paul Elliottdc42ca82023-02-24 18:11:59 +00002469 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002474}
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2477 const uint8_t *mac,
2478 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2481 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002484 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002485 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002486 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002489 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002490 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002491 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002494 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002495 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002496 }
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 status = psa_driver_wrapper_mac_verify_finish(operation,
2499 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002500
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505}
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2508 psa_algorithm_t alg,
2509 const uint8_t *input,
2510 size_t input_length,
2511 uint8_t *mac,
2512 size_t mac_size,
2513 size_t *mac_length,
2514 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002515{
2516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002517 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2518 psa_key_slot_t *slot;
2519 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520
Ronald Cron51131b52021-06-17 17:17:20 +02002521 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 key,
2523 &slot,
2524 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2525 alg);
2526 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002527 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002529
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002530 status = psa_mac_finalize_alg_and_key_validation(alg, &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 &operation_mac_size);
2532 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002537 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002538 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002539 }
2540
2541 status = psa_driver_wrapper_mac_compute(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002542 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 slot->key.data, slot->key.bytes,
2544 alg,
2545 input, input_length,
2546 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002547
2548exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002549 /* In case of success, set the potential excess room in the output buffer
2550 * to an invalid value, to avoid potentially leaking a longer MAC.
2551 * In case of error, set the output length and content to a safe default,
2552 * such that in case the caller misses an error check, the output would be
2553 * an unachievable MAC.
2554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002556 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002557 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002558 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002559
2560 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002561
Ryan Everetta103ec92024-01-31 13:59:57 +00002562 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002565}
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002568 psa_algorithm_t alg,
2569 const uint8_t *input,
2570 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 uint8_t *mac,
2572 size_t mac_size,
2573 size_t *mac_length)
2574{
2575 return psa_mac_compute_internal(key, alg,
2576 input, input_length,
2577 mac, mac_size, mac_length, 1);
2578}
2579
2580psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2581 psa_algorithm_t alg,
2582 const uint8_t *input,
2583 size_t input_length,
2584 const uint8_t *mac,
2585 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002586{
2587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002588 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2589 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 status = psa_mac_compute_internal(key, alg,
2592 input, input_length,
2593 actual_mac, sizeof(actual_mac),
2594 &actual_mac_length, 0);
2595 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002600 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002601 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002602 }
Dave Rodgman78701152023-08-29 14:20:18 +01002603 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002604 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002605 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002606 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002607
2608exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002612}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002613
Gilles Peskinee59236f2018-01-27 23:32:46 +01002614/****************************************************************/
2615/* Asymmetric cryptography */
2616/****************************************************************/
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2619 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002620{
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (input_is_message) {
2622 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2623 return PSA_ERROR_INVALID_ARGUMENT;
2624 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2627 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2628 return PSA_ERROR_INVALID_ARGUMENT;
2629 }
2630 }
2631 } else {
2632 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2633 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002634 }
2635 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002638}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2641 int input_is_message,
2642 psa_algorithm_t alg,
2643 const uint8_t *input,
2644 size_t input_length,
2645 uint8_t *signature,
2646 size_t signature_size,
2647 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002648{
2649 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2650 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2651 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002652
2653 *signature_length = 0;
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 status = psa_sign_verify_check_alg(input_is_message, alg);
2656 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002657 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002659
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002660 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002661 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002662 * buffer can in principle be empty since it doesn't actually have
2663 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (signature_size == 0) {
2665 return PSA_ERROR_BUFFER_TOO_SMALL;
2666 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002667
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002668 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 key, &slot,
2670 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2671 PSA_KEY_USAGE_SIGN_HASH,
2672 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002679 status = PSA_ERROR_INVALID_ARGUMENT;
2680 goto exit;
2681 }
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002684 status = psa_driver_wrapper_sign_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002685 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002686 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 signature, signature_size, signature_length);
2688 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002689
2690 status = psa_driver_wrapper_sign_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002691 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002692 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002694 }
2695
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002696
2697exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002698 psa_wipe_tag_output_buffer(signature, status, signature_size,
2699 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002700
Ryan Everetta103ec92024-01-31 13:59:57 +00002701 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002704}
2705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2707 int input_is_message,
2708 psa_algorithm_t alg,
2709 const uint8_t *input,
2710 size_t input_length,
2711 const uint8_t *signature,
2712 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002713{
2714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2715 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2716 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 status = psa_sign_verify_check_alg(input_is_message, alg);
2719 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002720 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002722
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002723 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 key, &slot,
2725 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2726 PSA_KEY_USAGE_VERIFY_HASH,
2727 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (status != PSA_SUCCESS) {
2730 return status;
2731 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002734 status = psa_driver_wrapper_verify_message(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002735 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002736 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 signature, signature_length);
2738 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002739 status = psa_driver_wrapper_verify_hash(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01002740 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002741 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002743 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002744
Ryan Everetta103ec92024-01-31 13:59:57 +00002745 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002748
2749}
2750
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002751psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002752 const psa_key_attributes_t *attributes,
2753 const uint8_t *key_buffer,
2754 size_t key_buffer_size,
2755 psa_algorithm_t alg,
2756 const uint8_t *input,
2757 size_t input_length,
2758 uint8_t *signature,
2759 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002761{
2762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002765 size_t hash_length;
2766 uint8_t hash[PSA_HASH_MAX_SIZE];
2767
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002768 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 PSA_ALG_SIGN_GET_HASH(alg),
2770 input, input_length,
2771 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002774 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002776
2777 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 attributes, key_buffer, key_buffer_size,
2779 alg, hash, hash_length,
2780 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002781 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002784}
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2787 psa_algorithm_t alg,
2788 const uint8_t *input,
2789 size_t input_length,
2790 uint8_t *signature,
2791 size_t signature_size,
2792 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002793{
2794 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002795 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002797}
2798
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002799psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002800 const psa_key_attributes_t *attributes,
2801 const uint8_t *key_buffer,
2802 size_t key_buffer_size,
2803 psa_algorithm_t alg,
2804 const uint8_t *input,
2805 size_t input_length,
2806 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002808{
2809 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002812 size_t hash_length;
2813 uint8_t hash[PSA_HASH_MAX_SIZE];
2814
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002815 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ALG_SIGN_GET_HASH(alg),
2817 input, input_length,
2818 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002821 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002823
2824 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 attributes, key_buffer, key_buffer_size,
2826 alg, hash, hash_length,
2827 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002828 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002831}
2832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2834 psa_algorithm_t alg,
2835 const uint8_t *input,
2836 size_t input_length,
2837 const uint8_t *signature,
2838 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002839{
2840 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002841 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843}
2844
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002845psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002846 const psa_key_attributes_t *attributes,
2847 const uint8_t *key_buffer, size_t key_buffer_size,
2848 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002850{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002851 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2853 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002854#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2856 return mbedtls_psa_rsa_sign_hash(
2857 attributes,
2858 key_buffer, key_buffer_size,
2859 alg, hash, hash_length,
2860 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002861#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2862 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 } else {
2864 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002865 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002866 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002868#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2870 return mbedtls_psa_ecdsa_sign_hash(
2871 attributes,
2872 key_buffer, key_buffer_size,
2873 alg, hash, hash_length,
2874 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002875#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2876 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 } else {
2878 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002879 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002880 }
Ronald Cron4501c982021-03-19 20:09:32 +01002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 (void) key_buffer;
2883 (void) key_buffer_size;
2884 (void) hash;
2885 (void) hash_length;
2886 (void) signature;
2887 (void) signature_size;
2888 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002891}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2894 psa_algorithm_t alg,
2895 const uint8_t *hash,
2896 size_t hash_length,
2897 uint8_t *signature,
2898 size_t signature_size,
2899 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002900{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002902 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03002904}
2905
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002906psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002907 const psa_key_attributes_t *attributes,
2908 const uint8_t *key_buffer, size_t key_buffer_size,
2909 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002911{
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002912 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2914 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002915#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2917 return mbedtls_psa_rsa_verify_hash(
2918 attributes,
2919 key_buffer, key_buffer_size,
2920 alg, hash, hash_length,
2921 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002922#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2923 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 } else {
2925 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002926 }
Gilles Peskine2f107ae2024-02-28 01:26:46 +01002927 } else if (PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002929#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2931 return mbedtls_psa_ecdsa_verify_hash(
2932 attributes,
2933 key_buffer, key_buffer_size,
2934 alg, hash, hash_length,
2935 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002936#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2937 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 } else {
2939 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002940 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002941 }
Ronald Cron4501c982021-03-19 20:09:32 +01002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 (void) key_buffer;
2944 (void) key_buffer_size;
2945 (void) hash;
2946 (void) hash_length;
2947 (void) signature;
2948 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002951}
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
2954 psa_algorithm_t alg,
2955 const uint8_t *hash,
2956 size_t hash_length,
2957 const uint8_t *signature,
2958 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03002959{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002960 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002961 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002963}
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
2966 psa_algorithm_t alg,
2967 const uint8_t *input,
2968 size_t input_length,
2969 const uint8_t *salt,
2970 size_t salt_length,
2971 uint8_t *output,
2972 size_t output_size,
2973 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002976 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002977 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002978
Darryl Green5cc689a2018-07-24 15:34:10 +01002979 (void) input;
2980 (void) input_length;
2981 (void) salt;
2982 (void) output;
2983 (void) output_size;
2984
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002985 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
2988 return PSA_ERROR_INVALID_ARGUMENT;
2989 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002990
Valerio Setti5bb454a2024-01-15 10:43:16 +01002991 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
2993 if (status != PSA_SUCCESS) {
2994 return status;
2995 }
2996 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
2997 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02002998 status = PSA_ERROR_INVALID_ARGUMENT;
2999 goto exit;
3000 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003001
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003002 status = psa_driver_wrapper_asymmetric_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003003 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003004 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003006exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003007 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003010}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3013 psa_algorithm_t alg,
3014 const uint8_t *input,
3015 size_t input_length,
3016 const uint8_t *salt,
3017 size_t salt_length,
3018 uint8_t *output,
3019 size_t output_size,
3020 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003023 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003024 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003025
Darryl Green5cc689a2018-07-24 15:34:10 +01003026 (void) input;
3027 (void) input_length;
3028 (void) salt;
3029 (void) output;
3030 (void) output_size;
3031
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003032 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3035 return PSA_ERROR_INVALID_ARGUMENT;
3036 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003037
Valerio Setti5bb454a2024-01-15 10:43:16 +01003038 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3040 if (status != PSA_SUCCESS) {
3041 return status;
3042 }
3043 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003044 status = PSA_ERROR_INVALID_ARGUMENT;
3045 goto exit;
3046 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003048 status = psa_driver_wrapper_asymmetric_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003049 &slot->attr, slot->key.data, slot->key.bytes,
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003050 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003052
3053exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003054 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003057}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003058
Paul Elliott9fe12f62022-11-30 19:16:02 +00003059/****************************************************************/
3060/* Asymmetric interruptible cryptography */
3061/****************************************************************/
3062
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003063static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3064
Paul Elliott9fe12f62022-11-30 19:16:02 +00003065void psa_interruptible_set_max_ops(uint32_t max_ops)
3066{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003067 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003068}
3069
3070uint32_t psa_interruptible_get_max_ops(void)
3071{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003072 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003073}
3074
Paul Elliott9fe12f62022-11-30 19:16:02 +00003075uint32_t psa_sign_hash_get_num_ops(
3076 const psa_sign_hash_interruptible_operation_t *operation)
3077{
Paul Elliott296ede92022-12-15 17:00:30 +00003078 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003079}
3080
3081uint32_t psa_verify_hash_get_num_ops(
3082 const psa_verify_hash_interruptible_operation_t *operation)
3083{
Paul Elliott296ede92022-12-15 17:00:30 +00003084 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003085}
3086
Paul Elliott59ad9452022-12-18 15:09:02 +00003087static psa_status_t psa_sign_hash_abort_internal(
3088 psa_sign_hash_interruptible_operation_t *operation)
3089{
3090 if (operation->id == 0) {
3091 /* The object has (apparently) been initialized but it is not (yet)
3092 * in use. It's ok to call abort on such an object, and there's
3093 * nothing to do. */
3094 return PSA_SUCCESS;
3095 }
3096
3097 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3098
3099 status = psa_driver_wrapper_sign_hash_abort(operation);
3100
3101 operation->id = 0;
3102
Paul Elliotte6145dc2023-02-07 12:51:21 +00003103 /* Do not clear either the error_occurred or num_ops elements here as they
3104 * only want to be cleared by the application calling abort, not by abort
3105 * being called at completion of an operation. */
3106
Paul Elliott59ad9452022-12-18 15:09:02 +00003107 return status;
3108}
3109
Paul Elliott9fe12f62022-11-30 19:16:02 +00003110psa_status_t psa_sign_hash_start(
3111 psa_sign_hash_interruptible_operation_t *operation,
3112 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3113 const uint8_t *hash, size_t hash_length)
3114{
3115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3116 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3117 psa_key_slot_t *slot;
3118
Paul Elliottc9774412023-02-06 15:14:07 +00003119 /* Check that start has not been previously called, or operation has not
3120 * previously errored. */
3121 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003122 return PSA_ERROR_BAD_STATE;
3123 }
3124
Paul Elliott9fe12f62022-11-30 19:16:02 +00003125 status = psa_sign_verify_check_alg(0, alg);
3126 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003127 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003128 return status;
3129 }
3130
3131 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3132 PSA_KEY_USAGE_SIGN_HASH,
3133 alg);
3134
3135 if (status != PSA_SUCCESS) {
3136 goto exit;
3137 }
3138
3139 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3140 status = PSA_ERROR_INVALID_ARGUMENT;
3141 goto exit;
3142 }
3143
Paul Elliott296ede92022-12-15 17:00:30 +00003144 /* Ensure ops count gets reset, in case of operation re-use. */
3145 operation->num_ops = 0;
3146
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003147 status = psa_driver_wrapper_sign_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003148 slot->key.data,
3149 slot->key.bytes, alg,
3150 hash, hash_length);
3151exit:
3152
3153 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003154 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003155 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003156 }
3157
Ryan Everettdcc03d52024-02-14 15:44:13 +00003158 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003159
Paul Elliottc9774412023-02-06 15:14:07 +00003160 if (unlock_status != PSA_SUCCESS) {
3161 operation->error_occurred = 1;
3162 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003163
Paul Elliottc9774412023-02-06 15:14:07 +00003164 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003165}
3166
3167
3168psa_status_t psa_sign_hash_complete(
3169 psa_sign_hash_interruptible_operation_t *operation,
3170 uint8_t *signature, size_t signature_size,
3171 size_t *signature_length)
3172{
3173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3174
3175 *signature_length = 0;
3176
Paul Elliottc9774412023-02-06 15:14:07 +00003177 /* Check that start has been called first, and that operation has not
3178 * previously errored. */
3179 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003180 status = PSA_ERROR_BAD_STATE;
3181 goto exit;
3182 }
3183
Paul Elliott096abc42023-02-03 18:33:23 +00003184 /* Immediately reject a zero-length signature buffer. This guarantees that
3185 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003186 if (signature_size == 0) {
3187 status = PSA_ERROR_BUFFER_TOO_SMALL;
3188 goto exit;
3189 }
3190
3191 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3192 signature_size,
3193 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003194
Paul Elliott296ede92022-12-15 17:00:30 +00003195 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003196 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003197
Paul Elliottebe225c2023-02-10 14:32:53 +00003198exit:
3199
Paul Elliott59200a22023-02-21 15:07:40 +00003200 psa_wipe_tag_output_buffer(signature, status, signature_size,
3201 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003202
Paul Elliott53bb3122023-02-10 14:22:22 +00003203 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003204 if (status != PSA_SUCCESS) {
3205 operation->error_occurred = 1;
3206 }
3207
Paul Elliott59ad9452022-12-18 15:09:02 +00003208 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003209 }
3210
3211 return status;
3212}
3213
3214psa_status_t psa_sign_hash_abort(
3215 psa_sign_hash_interruptible_operation_t *operation)
3216{
Paul Elliott59ad9452022-12-18 15:09:02 +00003217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3218
3219 status = psa_sign_hash_abort_internal(operation);
3220
3221 /* We clear the number of ops done here, so that it is not cleared when
3222 * the operation fails or succeeds, only on manual abort. */
3223 operation->num_ops = 0;
3224
Paul Elliottc9774412023-02-06 15:14:07 +00003225 /* Likewise, failure state. */
3226 operation->error_occurred = 0;
3227
Paul Elliott59ad9452022-12-18 15:09:02 +00003228 return status;
3229}
3230
3231static psa_status_t psa_verify_hash_abort_internal(
3232 psa_verify_hash_interruptible_operation_t *operation)
3233{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003234 if (operation->id == 0) {
3235 /* The object has (apparently) been initialized but it is not (yet)
3236 * in use. It's ok to call abort on such an object, and there's
3237 * nothing to do. */
3238 return PSA_SUCCESS;
3239 }
3240
Paul Elliott59ad9452022-12-18 15:09:02 +00003241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3242
3243 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003244
3245 operation->id = 0;
3246
Paul Elliotte6145dc2023-02-07 12:51:21 +00003247 /* Do not clear either the error_occurred or num_ops elements here as they
3248 * only want to be cleared by the application calling abort, not by abort
3249 * being called at completion of an operation. */
3250
Paul Elliott59ad9452022-12-18 15:09:02 +00003251 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003252}
3253
3254psa_status_t psa_verify_hash_start(
3255 psa_verify_hash_interruptible_operation_t *operation,
3256 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3257 const uint8_t *hash, size_t hash_length,
3258 const uint8_t *signature, size_t signature_length)
3259{
3260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3261 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3262 psa_key_slot_t *slot;
3263
Paul Elliottc9774412023-02-06 15:14:07 +00003264 /* Check that start has not been previously called, or operation has not
3265 * previously errored. */
3266 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003267 return PSA_ERROR_BAD_STATE;
3268 }
3269
3270 status = psa_sign_verify_check_alg(0, alg);
3271 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003272 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003273 return status;
3274 }
3275
3276 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3277 PSA_KEY_USAGE_VERIFY_HASH,
3278 alg);
3279
3280 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003281 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282 return status;
3283 }
3284
Paul Elliott296ede92022-12-15 17:00:30 +00003285 /* Ensure ops count gets reset, in case of operation re-use. */
3286 operation->num_ops = 0;
3287
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003288 status = psa_driver_wrapper_verify_hash_start(operation, &slot->attr,
Paul Elliott9fe12f62022-11-30 19:16:02 +00003289 slot->key.data,
3290 slot->key.bytes,
3291 alg, hash, hash_length,
3292 signature, signature_length);
3293
3294 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003295 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003296 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003297 }
3298
Ryan Everett291267f2024-02-14 15:59:15 +00003299 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300
Paul Elliottc9774412023-02-06 15:14:07 +00003301 if (unlock_status != PSA_SUCCESS) {
3302 operation->error_occurred = 1;
3303 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003304
Paul Elliottc9774412023-02-06 15:14:07 +00003305 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003306}
3307
3308psa_status_t psa_verify_hash_complete(
3309 psa_verify_hash_interruptible_operation_t *operation)
3310{
3311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3312
Paul Elliottc9774412023-02-06 15:14:07 +00003313 /* Check that start has been called first, and that operation has not
3314 * previously errored. */
3315 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316 status = PSA_ERROR_BAD_STATE;
3317 goto exit;
3318 }
3319
3320 status = psa_driver_wrapper_verify_hash_complete(operation);
3321
Paul Elliott296ede92022-12-15 17:00:30 +00003322 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003323 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003324 operation);
3325
Paul Elliottebe225c2023-02-10 14:32:53 +00003326exit:
3327
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003329 if (status != PSA_SUCCESS) {
3330 operation->error_occurred = 1;
3331 }
3332
Paul Elliott59ad9452022-12-18 15:09:02 +00003333 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334 }
3335
3336 return status;
3337}
3338
3339psa_status_t psa_verify_hash_abort(
3340 psa_verify_hash_interruptible_operation_t *operation)
3341{
Paul Elliott59ad9452022-12-18 15:09:02 +00003342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343
Paul Elliott59ad9452022-12-18 15:09:02 +00003344 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345
Paul Elliott59ad9452022-12-18 15:09:02 +00003346 /* We clear the number of ops done here, so that it is not cleared when
3347 * the operation fails or succeeds, only on manual abort. */
3348 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349
Paul Elliottc9774412023-02-06 15:14:07 +00003350 /* Likewise, failure state. */
3351 operation->error_occurred = 0;
3352
Paul Elliott59ad9452022-12-18 15:09:02 +00003353 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354}
3355
Paul Elliott588f8ed2022-12-02 18:10:26 +00003356/****************************************************************/
3357/* Asymmetric interruptible cryptography internal */
3358/* implementations */
3359/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003360
Paul Elliott588f8ed2022-12-02 18:10:26 +00003361void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3362{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003363
Paul Elliott588f8ed2022-12-02 18:10:26 +00003364#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3365 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3366 defined(MBEDTLS_ECP_RESTARTABLE)
3367
3368 /* Internal implementation uses zero to indicate infinite number max ops,
3369 * therefore avoid this value, and set to minimum possible. */
3370 if (max_ops == 0) {
3371 max_ops = 1;
3372 }
3373
Paul Elliott588f8ed2022-12-02 18:10:26 +00003374 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003375#else
3376 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003377#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3378 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3379 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3380}
3381
Paul Elliott588f8ed2022-12-02 18:10:26 +00003382uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003383 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003384{
3385#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3386 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3387 defined(MBEDTLS_ECP_RESTARTABLE)
3388
Paul Elliott93d9ca82023-02-15 18:14:21 +00003389 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003390#else
3391 (void) operation;
3392 return 0;
3393#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3394 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3395 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3396}
3397
3398uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003399 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003400{
3401 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3402 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3403 defined(MBEDTLS_ECP_RESTARTABLE)
3404
Paul Elliott93d9ca82023-02-15 18:14:21 +00003405 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003406#else
3407 (void) operation;
3408 return 0;
3409#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3410 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3411 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3412}
3413
3414psa_status_t mbedtls_psa_sign_hash_start(
3415 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3416 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3417 size_t key_buffer_size, psa_algorithm_t alg,
3418 const uint8_t *hash, size_t hash_length)
3419{
3420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003421 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003422
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003423 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003424 return PSA_ERROR_NOT_SUPPORTED;
3425 }
3426
3427 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003428 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003429 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003430
3431#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003432 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3433 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003434
Paul Elliotta1c94092023-02-15 16:38:04 +00003435 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3436
Paul Elliott93d9ca82023-02-15 18:14:21 +00003437 /* Ensure num_ops is zero'ed in case of context re-use. */
3438 operation->num_ops = 0;
3439
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003440 status = mbedtls_psa_ecp_load_representation(attributes->type,
3441 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003442 key_buffer,
3443 key_buffer_size,
3444 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003445
Paul Elliott068fe072023-01-16 13:59:15 +00003446 if (status != PSA_SUCCESS) {
3447 return status;
3448 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003449
Paul Elliott1bc59df2023-02-05 13:41:57 +00003450 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003451 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003452
Paul Elliott068fe072023-01-16 13:59:15 +00003453 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003454 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003455 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003456
Paul Elliott0290a762023-02-09 14:30:24 +00003457 /* We only need to store the same length of hash as the private key size
3458 * here, it would be truncated by the internal implementation anyway. */
3459 required_hash_length = (hash_length < operation->coordinate_bytes ?
3460 hash_length : operation->coordinate_bytes);
3461
Paul Elliottba70ad42023-02-15 18:23:53 +00003462 if (required_hash_length > sizeof(operation->hash)) {
3463 /* Shouldn't happen, but better safe than sorry. */
3464 return PSA_ERROR_CORRUPTION_DETECTED;
3465 }
3466
Paul Elliott0290a762023-02-09 14:30:24 +00003467 memcpy(operation->hash, hash, required_hash_length);
3468 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003469
3470 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003471
3472#else
Paul Elliott068fe072023-01-16 13:59:15 +00003473 (void) operation;
3474 (void) key_buffer;
3475 (void) key_buffer_size;
3476 (void) alg;
3477 (void) hash;
3478 (void) hash_length;
3479 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003480 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003481
Paul Elliott068fe072023-01-16 13:59:15 +00003482 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003483#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3484 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3485 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003486}
3487
3488psa_status_t mbedtls_psa_sign_hash_complete(
3489 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3490 uint8_t *signature, size_t signature_size,
3491 size_t *signature_length)
3492{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003493#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3494 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3495 defined(MBEDTLS_ECP_RESTARTABLE)
3496
Paul Elliotta1c94092023-02-15 16:38:04 +00003497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003498 mbedtls_mpi r;
3499 mbedtls_mpi s;
3500
Paul Elliott46845252023-02-03 14:59:11 +00003501 mbedtls_mpi_init(&r);
3502 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003503
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003504 /* Ensure max_ops is set to the current value (or default). */
3505 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3506
Paul Elliotta1c94092023-02-15 16:38:04 +00003507 if (signature_size < 2 * operation->coordinate_bytes) {
3508 status = PSA_ERROR_BUFFER_TOO_SMALL;
3509 goto exit;
3510 }
3511
Paul Elliott588f8ed2022-12-02 18:10:26 +00003512 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003513
Paul Elliott588f8ed2022-12-02 18:10:26 +00003514#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3515 status = mbedtls_to_psa_error(
3516 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003517 &r,
3518 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003519 &operation->ctx->d,
3520 operation->hash,
3521 operation->hash_length,
3522 operation->md_alg,
3523 mbedtls_psa_get_random,
3524 MBEDTLS_PSA_RANDOM_STATE,
3525 &operation->restart_ctx));
3526#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003527 status = PSA_ERROR_NOT_SUPPORTED;
3528 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003529#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3530 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003531 status = mbedtls_to_psa_error(
3532 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003533 &r,
3534 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003535 &operation->ctx->d,
3536 operation->hash,
3537 operation->hash_length,
3538 mbedtls_psa_get_random,
3539 MBEDTLS_PSA_RANDOM_STATE,
3540 mbedtls_psa_get_random,
3541 MBEDTLS_PSA_RANDOM_STATE,
3542 &operation->restart_ctx));
3543 }
3544
Paul Elliottf8e5b562023-02-19 18:43:45 +00003545 /* Hide the fact that the restart context only holds a delta of number of
3546 * ops done during the last operation, not an absolute value. */
3547 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3548
Paul Elliott724bd252023-02-08 12:35:08 +00003549 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003550 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003551 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003552 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003553 operation->coordinate_bytes)
3554 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003555
3556 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003557 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003558 }
3559
3560 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003561 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003562 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003563 operation->coordinate_bytes,
3564 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
Paul Elliott1bc59df2023-02-05 13:41:57 +00003571 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003572
Paul Elliott724bd252023-02-08 12:35:08 +00003573 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003574 }
Paul Elliott724bd252023-02-08 12:35:08 +00003575
3576exit:
3577
3578 mbedtls_mpi_free(&r);
3579 mbedtls_mpi_free(&s);
3580 return status;
3581
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582 #else
3583
3584 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003585 (void) signature;
3586 (void) signature_size;
3587 (void) signature_length;
3588
3589 return PSA_ERROR_NOT_SUPPORTED;
3590
3591#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3592 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3593 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3594}
3595
3596psa_status_t mbedtls_psa_sign_hash_abort(
3597 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3598{
3599
3600#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3601 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3602 defined(MBEDTLS_ECP_RESTARTABLE)
3603
3604 if (operation->ctx) {
3605 mbedtls_ecdsa_free(operation->ctx);
3606 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003607 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608 }
3609
3610 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3611
Paul Elliott93d9ca82023-02-15 18:14:21 +00003612 operation->num_ops = 0;
3613
Paul Elliott588f8ed2022-12-02 18:10:26 +00003614 return PSA_SUCCESS;
3615
3616#else
3617
3618 (void) operation;
3619
3620 return PSA_ERROR_NOT_SUPPORTED;
3621
3622#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3623 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3624 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3625}
3626
3627psa_status_t mbedtls_psa_verify_hash_start(
3628 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3629 const psa_key_attributes_t *attributes,
3630 const uint8_t *key_buffer, size_t key_buffer_size,
3631 psa_algorithm_t alg,
3632 const uint8_t *hash, size_t hash_length,
3633 const uint8_t *signature, size_t signature_length)
3634{
3635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003636 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003637 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003639 if (!PSA_KEY_TYPE_IS_ECC(attributes->type)) {
Paul Elliott068fe072023-01-16 13:59:15 +00003640 return PSA_ERROR_NOT_SUPPORTED;
3641 }
3642
3643 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003644 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003645 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646
3647#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003648 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3649 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003650
Paul Elliotta1c94092023-02-15 16:38:04 +00003651 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3652 mbedtls_mpi_init(&operation->r);
3653 mbedtls_mpi_init(&operation->s);
3654
Paul Elliott93d9ca82023-02-15 18:14:21 +00003655 /* Ensure num_ops is zero'ed in case of context re-use. */
3656 operation->num_ops = 0;
3657
Gilles Peskine2f107ae2024-02-28 01:26:46 +01003658 status = mbedtls_psa_ecp_load_representation(attributes->type,
3659 attributes->bits,
Paul Elliott068fe072023-01-16 13:59:15 +00003660 key_buffer,
3661 key_buffer_size,
3662 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663
Paul Elliott068fe072023-01-16 13:59:15 +00003664 if (status != PSA_SUCCESS) {
3665 return status;
3666 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667
Paul Elliottc569fc22023-02-10 13:02:54 +00003668 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669
Paul Elliott1bc59df2023-02-05 13:41:57 +00003670 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003671 return PSA_ERROR_INVALID_SIGNATURE;
3672 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673
Paul Elliott068fe072023-01-16 13:59:15 +00003674 status = mbedtls_to_psa_error(
3675 mbedtls_mpi_read_binary(&operation->r,
3676 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003677 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678
Paul Elliott068fe072023-01-16 13:59:15 +00003679 if (status != PSA_SUCCESS) {
3680 return status;
3681 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682
Paul Elliott068fe072023-01-16 13:59:15 +00003683 status = mbedtls_to_psa_error(
3684 mbedtls_mpi_read_binary(&operation->s,
3685 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003686 coordinate_bytes,
3687 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688
Paul Elliott068fe072023-01-16 13:59:15 +00003689 if (status != PSA_SUCCESS) {
3690 return status;
3691 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003692
Paul Elliott2c9843f2023-02-15 17:32:42 +00003693 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694
Paul Elliott2c9843f2023-02-15 17:32:42 +00003695 if (status != PSA_SUCCESS) {
3696 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003697 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698
Paul Elliott0290a762023-02-09 14:30:24 +00003699 /* We only need to store the same length of hash as the private key size
3700 * here, it would be truncated by the internal implementation anyway. */
3701 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3702 coordinate_bytes);
3703
Paul Elliottba70ad42023-02-15 18:23:53 +00003704 if (required_hash_length > sizeof(operation->hash)) {
3705 /* Shouldn't happen, but better safe than sorry. */
3706 return PSA_ERROR_CORRUPTION_DETECTED;
3707 }
3708
Paul Elliott0290a762023-02-09 14:30:24 +00003709 memcpy(operation->hash, hash, required_hash_length);
3710 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003711
3712 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713#else
Paul Elliott068fe072023-01-16 13:59:15 +00003714 (void) operation;
3715 (void) key_buffer;
3716 (void) key_buffer_size;
3717 (void) alg;
3718 (void) hash;
3719 (void) hash_length;
3720 (void) signature;
3721 (void) signature_length;
3722 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003723 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003724 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003725
Paul Elliott068fe072023-01-16 13:59:15 +00003726 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3728 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3729 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730}
3731
3732psa_status_t mbedtls_psa_verify_hash_complete(
3733 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3734{
3735
3736#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3737 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3738 defined(MBEDTLS_ECP_RESTARTABLE)
3739
Paul Elliottf8e5b562023-02-19 18:43:45 +00003740 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3741
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003742 /* Ensure max_ops is set to the current value (or default). */
3743 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3744
Paul Elliottf8e5b562023-02-19 18:43:45 +00003745 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003746 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3747 operation->hash,
3748 operation->hash_length,
3749 &operation->ctx->Q,
3750 &operation->r,
3751 &operation->s,
3752 &operation->restart_ctx));
3753
Paul Elliottf8e5b562023-02-19 18:43:45 +00003754 /* Hide the fact that the restart context only holds a delta of number of
3755 * ops done during the last operation, not an absolute value. */
3756 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3757
3758 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759#else
3760 (void) operation;
3761
3762 return PSA_ERROR_NOT_SUPPORTED;
3763
3764#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3765 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3766 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3767}
3768
3769psa_status_t mbedtls_psa_verify_hash_abort(
3770 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3771{
3772
3773#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3774 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3775 defined(MBEDTLS_ECP_RESTARTABLE)
3776
3777 if (operation->ctx) {
3778 mbedtls_ecdsa_free(operation->ctx);
3779 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003780 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781 }
3782
3783 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3784
Paul Elliott93d9ca82023-02-15 18:14:21 +00003785 operation->num_ops = 0;
3786
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787 mbedtls_mpi_free(&operation->r);
3788 mbedtls_mpi_free(&operation->s);
3789
3790 return PSA_SUCCESS;
3791
3792#else
3793 (void) operation;
3794
3795 return PSA_ERROR_NOT_SUPPORTED;
3796
3797#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3798 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3799 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3800}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003801
mohammad1603503973b2018-03-12 15:59:30 +02003802/****************************************************************/
3803/* Symmetric cryptography */
3804/****************************************************************/
3805
Gilles Peskine449bd832023-01-11 14:50:10 +01003806static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3807 mbedtls_svc_key_id_t key,
3808 psa_algorithm_t alg,
3809 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003810{
3811 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3812 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003813 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003814 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3815 PSA_KEY_USAGE_ENCRYPT :
3816 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003817
3818 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01003820 status = PSA_ERROR_BAD_STATE;
3821 goto exit;
3822 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003823
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01003825 status = PSA_ERROR_INVALID_ARGUMENT;
3826 goto exit;
3827 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3830 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003831 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003833
Ronald Cron49fafa92021-03-10 08:34:23 +01003834 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003835 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003836 * so we only set it (in the driver wrapper) after resources have been
3837 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003838 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003840 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003842 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 }
3844 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003845
3846 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 if (cipher_operation == MBEDTLS_ENCRYPT) {
3848 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003849 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 slot->key.data,
3851 slot->key.bytes,
3852 alg);
3853 } else {
3854 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01003855 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 slot->key.data,
3857 slot->key.bytes,
3858 alg);
3859 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003860
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 if (status != PSA_SUCCESS) {
3863 psa_cipher_abort(operation);
3864 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003865
Ryan Everettc0053cc2024-02-14 16:27:13 +00003866 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003869}
3870
Gilles Peskine449bd832023-01-11 14:50:10 +01003871psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3872 mbedtls_svc_key_id_t key,
3873 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003874{
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003876}
3877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3879 mbedtls_svc_key_id_t key,
3880 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003881{
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003883}
3884
Gilles Peskine449bd832023-01-11 14:50:10 +01003885psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3886 uint8_t *iv,
3887 size_t iv_size,
3888 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003889{
Ronald Cron6d051732020-10-01 14:10:20 +02003890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02003891 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07003892 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003893
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003895 status = PSA_ERROR_BAD_STATE;
3896 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003897 }
3898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003900 status = PSA_ERROR_BAD_STATE;
3901 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003902 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003903
Ronald Cron2fb90522021-07-05 12:31:44 +02003904 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003906 status = PSA_ERROR_BUFFER_TOO_SMALL;
3907 goto exit;
3908 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02003911 status = PSA_ERROR_GENERIC_ERROR;
3912 goto exit;
3913 }
3914
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 status = psa_generate_random(local_iv, default_iv_length);
3916 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 }
Ronald Cron5618a392021-03-26 09:52:26 +01003919
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 status = psa_driver_wrapper_cipher_set_iv(operation,
3921 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003922
3923exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 if (status == PSA_SUCCESS) {
3925 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02003926 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003927 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02003929 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02003931 }
Ronald Cron6d051732020-10-01 14:10:20 +02003932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003934}
3935
Gilles Peskine449bd832023-01-11 14:50:10 +01003936psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3937 const uint8_t *iv,
3938 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003939{
Ronald Cron6d051732020-10-01 14:10:20 +02003940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003941
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003943 status = PSA_ERROR_BAD_STATE;
3944 goto exit;
3945 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003946
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003948 status = PSA_ERROR_BAD_STATE;
3949 goto exit;
3950 }
Ronald Crona0d68172021-03-26 10:15:08 +01003951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003953 status = PSA_ERROR_INVALID_ARGUMENT;
3954 goto exit;
3955 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003956
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 status = psa_driver_wrapper_cipher_set_iv(operation,
3958 iv,
3959 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003960
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003963 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 } else {
3965 psa_cipher_abort(operation);
3966 }
3967 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003968}
3969
Gilles Peskine449bd832023-01-11 14:50:10 +01003970psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3971 const uint8_t *input,
3972 size_t input_length,
3973 uint8_t *output,
3974 size_t output_size,
3975 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003976{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003977 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003980 status = PSA_ERROR_BAD_STATE;
3981 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003982 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003985 status = PSA_ERROR_BAD_STATE;
3986 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003987 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 status = psa_driver_wrapper_cipher_update(operation,
3990 input,
3991 input_length,
3992 output,
3993 output_size,
3994 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003995
3996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 if (status != PSA_SUCCESS) {
3998 psa_cipher_abort(operation);
3999 }
Ronald Cron6d051732020-10-01 14:10:20 +02004000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004002}
4003
Gilles Peskine449bd832023-01-11 14:50:10 +01004004psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4005 uint8_t *output,
4006 size_t output_size,
4007 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004008{
David Saadab4ecc272019-02-14 13:48:10 +02004009 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004010
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004012 status = PSA_ERROR_BAD_STATE;
4013 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004014 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004017 status = PSA_ERROR_BAD_STATE;
4018 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004019 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 status = psa_driver_wrapper_cipher_finish(operation,
4022 output,
4023 output_size,
4024 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004025
4026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (status == PSA_SUCCESS) {
4028 return psa_cipher_abort(operation);
4029 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004030 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004034 }
mohammad1603503973b2018-03-12 15:59:30 +02004035}
4036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004038{
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004040 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004041 * in use. It's ok to call abort on such an object, and there's
4042 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004044 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004047
Ronald Cron49fafa92021-03-10 08:34:23 +01004048 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004049 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004050 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004051
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004053}
4054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4056 psa_algorithm_t alg,
4057 const uint8_t *input,
4058 size_t input_length,
4059 uint8_t *output,
4060 size_t output_size,
4061 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004062{
4063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004064 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004065 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004066 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4067 size_t default_iv_length = 0;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004068
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004070 status = PSA_ERROR_INVALID_ARGUMENT;
4071 goto exit;
4072 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004073
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4075 PSA_KEY_USAGE_ENCRYPT,
4076 alg);
4077 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4082 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004083 status = PSA_ERROR_GENERIC_ERROR;
4084 goto exit;
4085 }
4086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (default_iv_length > 0) {
4088 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004089 status = PSA_ERROR_BUFFER_TOO_SMALL;
4090 goto exit;
4091 }
4092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 status = psa_generate_random(local_iv, default_iv_length);
4094 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004097 }
4098
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004099 status = psa_driver_wrapper_cipher_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004100 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004101 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004102 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004104
4105exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004106 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004108 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004109 }
Ronald Cron23919522021-07-08 19:00:07 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 if (status == PSA_SUCCESS) {
4112 if (default_iv_length > 0) {
4113 memcpy(output, local_iv, default_iv_length);
4114 }
4115 *output_length += default_iv_length;
4116 } else {
4117 *output_length = 0;
4118 }
4119
4120 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004121}
4122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4124 psa_algorithm_t alg,
4125 const uint8_t *input,
4126 size_t input_length,
4127 uint8_t *output,
4128 size_t output_size,
4129 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004130{
4131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004132 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004133 psa_key_slot_t *slot = NULL;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004136 status = PSA_ERROR_INVALID_ARGUMENT;
4137 goto exit;
4138 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4141 PSA_KEY_USAGE_DECRYPT,
4142 alg);
4143 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004144 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4148 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004149 status = PSA_ERROR_INVALID_ARGUMENT;
4150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004152 status = PSA_ERROR_INVALID_ARGUMENT;
4153 goto exit;
4154 }
4155
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004156 status = psa_driver_wrapper_cipher_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004157 &slot->attr, slot->key.data, slot->key.bytes,
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004158 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004160
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004161exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004162 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004164 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004168 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 }
Ronald Cron23919522021-07-08 19:00:07 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004172}
4173
4174
mohammad16035955c982018-04-26 00:53:03 +03004175/****************************************************************/
4176/* AEAD */
4177/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004178
Paul Elliottbaff51c2021-09-28 17:44:45 +01004179/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004180static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004181{
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004183}
4184
4185/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004186static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4187 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004188{
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004192#if defined(PSA_WANT_ALG_GCM)
4193 case PSA_ALG_GCM:
4194 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004195 * arbitrarily large nonces. Please note that we do not generally
4196 * recommend the usage of nonces of greater length than
4197 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4198 * size, which can then lead to collisions if you encrypt a very
4199 * large number of messages.*/
4200 if (nonce_length != 0) {
4201 return PSA_SUCCESS;
4202 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004203 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004204#endif /* PSA_WANT_ALG_GCM */
4205#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004206 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (nonce_length >= 7 && nonce_length <= 13) {
4208 return PSA_SUCCESS;
4209 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004210 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004211#endif /* PSA_WANT_ALG_CCM */
4212#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004213 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 if (nonce_length == 12) {
4215 return PSA_SUCCESS;
4216 } else if (nonce_length == 8) {
4217 return PSA_ERROR_NOT_SUPPORTED;
4218 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004219 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004220#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004221 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004222 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004224 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004227}
4228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004230{
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4232 return PSA_ERROR_INVALID_ARGUMENT;
4233 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004236}
4237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4239 psa_algorithm_t alg,
4240 const uint8_t *nonce,
4241 size_t nonce_length,
4242 const uint8_t *additional_data,
4243 size_t additional_data_length,
4244 const uint8_t *plaintext,
4245 size_t plaintext_length,
4246 uint8_t *ciphertext,
4247 size_t ciphertext_size,
4248 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004249{
Ronald Cron215633c2021-03-16 17:15:37 +01004250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4251 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004252
mohammad1603f08a5502018-06-03 15:05:47 +03004253 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 status = psa_aead_check_algorithm(alg);
4256 if (status != PSA_SUCCESS) {
4257 return status;
4258 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004259
Ronald Cron9a986162021-03-26 12:40:07 +01004260 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4262 if (status != PSA_SUCCESS) {
4263 return status;
4264 }
mohammad16035955c982018-04-26 00:53:03 +03004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 status = psa_aead_check_nonce_length(alg, nonce_length);
4267 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004270
Ronald Cronde822812021-03-17 16:08:20 +01004271 status = psa_driver_wrapper_aead_encrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004272 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004273 alg,
4274 nonce, nonce_length,
4275 additional_data, additional_data_length,
4276 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4280 memset(ciphertext, 0, ciphertext_size);
4281 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004282
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004283exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004284 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004287}
4288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4290 psa_algorithm_t alg,
4291 const uint8_t *nonce,
4292 size_t nonce_length,
4293 const uint8_t *additional_data,
4294 size_t additional_data_length,
4295 const uint8_t *ciphertext,
4296 size_t ciphertext_length,
4297 uint8_t *plaintext,
4298 size_t plaintext_size,
4299 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004300{
Ronald Cron215633c2021-03-16 17:15:37 +01004301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4302 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004303
Gilles Peskineee652a32018-06-01 19:23:52 +02004304 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 status = psa_aead_check_algorithm(alg);
4307 if (status != PSA_SUCCESS) {
4308 return status;
4309 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004310
Ronald Cron9a986162021-03-26 12:40:07 +01004311 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4313 if (status != PSA_SUCCESS) {
4314 return status;
4315 }
mohammad16035955c982018-04-26 00:53:03 +03004316
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 status = psa_aead_check_nonce_length(alg, nonce_length);
4318 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004319 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004321
Ronald Cronde822812021-03-17 16:08:20 +01004322 status = psa_driver_wrapper_aead_decrypt(
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004323 &slot->attr, slot->key.data, slot->key.bytes,
Ronald Cron215633c2021-03-16 17:15:37 +01004324 alg,
4325 nonce, nonce_length,
4326 additional_data, additional_data_length,
4327 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (status != PSA_SUCCESS && plaintext_size != 0) {
4331 memset(plaintext, 0, plaintext_size);
4332 }
mohammad160360a64d02018-06-03 17:20:42 +03004333
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004334exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004335 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004336
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 return status;
mohammad16035955c982018-04-26 00:53:03 +03004338}
4339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4341{
4342 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004345#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004347 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4349 return PSA_ERROR_INVALID_ARGUMENT;
4350 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004351 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004352#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004353
Przemek Stekiel86679c72022-10-06 17:06:56 +02004354#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004356 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4358 return PSA_ERROR_INVALID_ARGUMENT;
4359 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004360 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004361#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004362
Przemek Stekiel86679c72022-10-06 17:06:56 +02004363#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004365 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (tag_len != 16) {
4367 return PSA_ERROR_INVALID_ARGUMENT;
4368 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004369 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004370#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004371
4372 default:
4373 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004375 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004377}
4378
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004379/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004380static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4381 int is_encrypt,
4382 mbedtls_svc_key_id_t key,
4383 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004384{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4386 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4387 psa_key_slot_t *slot = NULL;
4388 psa_key_usage_t key_usage = 0;
4389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 status = psa_aead_check_algorithm(alg);
4391 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004392 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004396 status = PSA_ERROR_BAD_STATE;
4397 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004398 }
4399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (operation->nonce_set || operation->lengths_set ||
4401 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004402 status = PSA_ERROR_BAD_STATE;
4403 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004404 }
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004407 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004409 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4413 alg);
4414 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004415 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004419 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 if (is_encrypt) {
4423 status = psa_driver_wrapper_aead_encrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004424 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 slot->key.data,
4426 slot->key.bytes,
4427 alg);
4428 } else {
4429 status = psa_driver_wrapper_aead_decrypt_setup(operation,
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004430 &slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 slot->key.data,
4432 slot->key.bytes,
4433 alg);
4434 }
4435 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004438
Gilles Peskine7a5d9202024-02-28 01:18:23 +01004439 operation->key_type = psa_get_key_type(&slot->attr);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004440
4441exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004442 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004445 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004447 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 } else {
4449 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004450 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004453}
4454
Paul Elliott302ff6b2021-04-20 18:10:30 +01004455/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004456psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4457 mbedtls_svc_key_id_t key,
4458 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004459{
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004461}
4462
4463/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004464psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4465 mbedtls_svc_key_id_t key,
4466 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004467{
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004469}
4470
4471/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004472psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4473 uint8_t *nonce,
4474 size_t nonce_size,
4475 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004476{
4477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004478 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004479 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004480
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004481 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004484 status = PSA_ERROR_BAD_STATE;
4485 goto exit;
4486 }
4487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004489 status = PSA_ERROR_BAD_STATE;
4490 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004491 }
4492
Paul Elliotte193ea82021-10-01 13:00:16 +01004493 /* For CCM, this size may not be correct according to the PSA
4494 * specification. The PSA Crypto 1.0.1 specification states:
4495 *
4496 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4497 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4498 *
4499 * However this restriction that L has to be the smallest integer is not
4500 * applied in practice, and it is not implementable here since the
4501 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4503 operation->alg);
4504 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004505 status = PSA_ERROR_BUFFER_TOO_SMALL;
4506 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004507 }
4508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 status = psa_generate_random(local_nonce, required_nonce_size);
4510 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004511 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004515
Paul Elliott39dc6b82021-05-11 19:16:09 +01004516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (status == PSA_SUCCESS) {
4518 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004519 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 } else {
4521 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004522 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004525}
4526
4527/* Set the nonce for a multipart authenticated encryption or decryption
4528 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004529psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4530 const uint8_t *nonce,
4531 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004532{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004536 status = PSA_ERROR_BAD_STATE;
4537 goto exit;
4538 }
4539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004541 status = PSA_ERROR_BAD_STATE;
4542 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004543 }
4544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4546 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004547 status = PSA_ERROR_INVALID_ARGUMENT;
4548 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004549 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4552 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004553
Paul Elliott39dc6b82021-05-11 19:16:09 +01004554exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004556 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 } else {
4558 psa_aead_abort(operation);
4559 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004562}
4563
4564/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004565psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4566 size_t ad_length,
4567 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004568{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004572 status = PSA_ERROR_BAD_STATE;
4573 goto exit;
4574 }
4575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (operation->lengths_set || operation->ad_started ||
4577 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004578 status = PSA_ERROR_BAD_STATE;
4579 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004583#if defined(PSA_WANT_ALG_GCM)
4584 case PSA_ALG_GCM:
4585 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 * bits. Without the guard this code will generate warnings on 32bit
4587 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004588#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (((uint64_t) ad_length) >> 61 != 0 ||
4590 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004591 status = PSA_ERROR_INVALID_ARGUMENT;
4592 goto exit;
4593 }
Paul Elliott325d3742021-09-27 17:56:28 +01004594#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004595 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004596#endif /* PSA_WANT_ALG_GCM */
4597#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004598 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004600 status = PSA_ERROR_INVALID_ARGUMENT;
4601 goto exit;
4602 }
4603 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004604#endif /* PSA_WANT_ALG_CCM */
4605#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004606 case PSA_ALG_CHACHA20_POLY1305:
4607 /* No length restrictions for ChaChaPoly. */
4608 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004609#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004610 default:
4611 break;
4612 }
Paul Elliott325d3742021-09-27 17:56:28 +01004613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4615 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004616
Paul Elliott39dc6b82021-05-11 19:16:09 +01004617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004619 operation->ad_remaining = ad_length;
4620 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004621 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 } else {
4623 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004624 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004627}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004628
4629/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004630psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4631 const uint8_t *input,
4632 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004633{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004637 status = PSA_ERROR_BAD_STATE;
4638 goto exit;
4639 }
4640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004642 status = PSA_ERROR_BAD_STATE;
4643 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004644 }
4645
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 if (operation->lengths_set) {
4647 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004648 status = PSA_ERROR_INVALID_ARGUMENT;
4649 goto exit;
4650 }
4651
4652 operation->ad_remaining -= input_length;
4653 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004654#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004656 status = PSA_ERROR_BAD_STATE;
4657 goto exit;
4658 }
4659#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 status = psa_driver_wrapper_aead_update_ad(operation, input,
4662 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004663
Paul Elliott39dc6b82021-05-11 19:16:09 +01004664exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004666 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 } else {
4668 psa_aead_abort(operation);
4669 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004672}
4673
4674/* Encrypt or decrypt a message fragment in an active multipart AEAD
4675 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004676psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4677 const uint8_t *input,
4678 size_t input_length,
4679 uint8_t *output,
4680 size_t output_size,
4681 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004682{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004684
4685 *output_length = 0;
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004688 status = PSA_ERROR_BAD_STATE;
4689 goto exit;
4690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004693 status = PSA_ERROR_BAD_STATE;
4694 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004695 }
4696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004698 /* Additional data length was supplied, but not all the additional
4699 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004701 status = PSA_ERROR_INVALID_ARGUMENT;
4702 goto exit;
4703 }
4704
4705 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004707 status = PSA_ERROR_INVALID_ARGUMENT;
4708 goto exit;
4709 }
4710
4711 operation->body_remaining -= input_length;
4712 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004713#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004715 status = PSA_ERROR_BAD_STATE;
4716 goto exit;
4717 }
4718#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4721 output, output_size,
4722 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004723
Paul Elliott39dc6b82021-05-11 19:16:09 +01004724exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004726 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 } else {
4728 psa_aead_abort(operation);
4729 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004732}
4733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004735{
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (operation->id == 0 || !operation->nonce_set) {
4737 return PSA_ERROR_BAD_STATE;
4738 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4741 operation->body_remaining != 0)) {
4742 return PSA_ERROR_INVALID_ARGUMENT;
4743 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004746}
4747
Paul Elliott302ff6b2021-04-20 18:10:30 +01004748/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4750 uint8_t *ciphertext,
4751 size_t ciphertext_size,
4752 size_t *ciphertext_length,
4753 uint8_t *tag,
4754 size_t tag_size,
4755 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4758
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004760 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 status = psa_aead_final_checks(operation);
4763 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004768 status = PSA_ERROR_BAD_STATE;
4769 goto exit;
4770 }
4771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
4773 ciphertext_size,
4774 ciphertext_length,
4775 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004776
Paul Elliott39dc6b82021-05-11 19:16:09 +01004777exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00004778
4779
Paul Elliottf47b0952021-05-21 18:02:33 +01004780 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004781 * the zero tag size, make sure the tag is set to something implausible.
4782 * Even if the operation succeeds, make sure we clear the rest of the
4783 * buffer to prevent potential leakage of anything previously placed in
4784 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00004785 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790}
4791
4792/* Finish authenticating and decrypting a message in a multipart AEAD
4793 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004794psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
4795 uint8_t *plaintext,
4796 size_t plaintext_size,
4797 size_t *plaintext_length,
4798 const uint8_t *tag,
4799 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004800{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4802
Paul Elliott302ff6b2021-04-20 18:10:30 +01004803 *plaintext_length = 0;
4804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 status = psa_aead_final_checks(operation);
4806 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01004807 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004811 status = PSA_ERROR_BAD_STATE;
4812 goto exit;
4813 }
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 status = psa_driver_wrapper_aead_verify(operation, plaintext,
4816 plaintext_size,
4817 plaintext_length,
4818 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004819
4820exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004824}
4825
4826/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004827psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004828{
4829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832 /* The object has (apparently) been initialized but it is not (yet)
4833 * in use. It's ok to call abort on such an object, and there's
4834 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004836 }
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004843}
4844
Gilles Peskinee59236f2018-01-27 23:32:46 +01004845/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004846/* Generators */
4847/****************************************************************/
4848
Przemek Stekiel69c46792022-06-10 12:59:51 +02004849#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08004850 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004851 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05304852 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304853 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08004854#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004855#endif /* At least one builtin KDF */
4856
Przemek Stekiel69c46792022-06-10 12:59:51 +02004857#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02004858 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4859 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4860static psa_status_t psa_key_derivation_start_hmac(
4861 psa_mac_operation_t *operation,
4862 psa_algorithm_t hash_alg,
4863 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02004865{
4866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
4869 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
4870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02004871
Steven Cooreman72f736a2021-05-07 14:14:37 +02004872 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 status = psa_driver_wrapper_mac_sign_setup(operation,
4876 &attributes,
4877 hmac_key, hmac_key_length,
4878 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 psa_reset_key_attributes(&attributes);
4881 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02004882}
4883#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004884
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885#define HKDF_STATE_INIT 0 /* no input yet */
4886#define HKDF_STATE_STARTED 1 /* got salt */
4887#define HKDF_STATE_KEYED 2 /* got key */
4888#define HKDF_STATE_OUTPUT 3 /* output started */
4889
Gilles Peskinecbe66502019-05-16 16:59:18 +02004890static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892{
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4894 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
4895 } else {
4896 return operation->alg;
4897 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898}
4899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004901{
4902 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
4904 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004905 /* The object has (apparently) been initialized but it is not
4906 * in use. It's ok to call abort on such an object, and there's
4907 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004909#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
4911 mbedtls_free(operation->ctx.hkdf.info);
4912 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
4913 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02004914#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08004915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4916 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4918 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
4919 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4920 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004921 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02004923 }
4924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004926 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004928 }
4929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004931 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01004933 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004934#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004936 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02004938 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01004939#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02004940 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004941
4942 /* We leave the fields Ai and output_block to be erased safely by the
4943 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004945#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4946 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004947#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
4949 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
4950 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
4951 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04004952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304953#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05304954 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304955 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004956 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304957 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304958 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05304959
4960 status = PSA_SUCCESS;
4961 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05304962#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004963 {
4964 status = PSA_ERROR_BAD_STATE;
4965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 mbedtls_platform_zeroize(operation, sizeof(*operation));
4967 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004968}
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004972{
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004976 }
4977
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004980}
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
4983 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004984{
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 if (operation->alg == 0) {
4986 return PSA_ERROR_BAD_STATE;
4987 }
4988 if (capacity > operation->capacity) {
4989 return PSA_ERROR_INVALID_ARGUMENT;
4990 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004993}
4994
Przemek Stekiel69c46792022-06-10 12:59:51 +02004995#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02004996/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004997static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
4998 psa_algorithm_t kdf_alg,
4999 uint8_t *output,
5000 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005001{
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5003 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005004 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005005 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005006#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005008#else
5009 const uint8_t last_block = 0xff;
5010#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (hkdf->state < HKDF_STATE_KEYED ||
5013 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005014#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005016#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 )) {
5018 return PSA_ERROR_BAD_STATE;
5019 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005020 hkdf->state = HKDF_STATE_OUTPUT;
5021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005023 /* Copy what remains of the current block */
5024 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005026 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 }
5028 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005029 output += n;
5030 output_length -= n;
5031 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005033 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005035 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005036 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005038 * object was corrupted or if this function is called directly
5039 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (hkdf->block_number == last_block) {
5041 return PSA_ERROR_BAD_STATE;
5042 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005043
5044 /* We need a new block */
5045 ++hkdf->block_number;
5046 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5049 hash_alg,
5050 hkdf->prk,
5051 hash_length);
5052 if (status != PSA_SUCCESS) {
5053 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005054 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005055
5056 if (hkdf->block_number != 1) {
5057 status = psa_mac_update(&hkdf->hmac,
5058 hkdf->output_block,
5059 hash_length);
5060 if (status != PSA_SUCCESS) {
5061 return status;
5062 }
5063 }
5064 status = psa_mac_update(&hkdf->hmac,
5065 hkdf->info,
5066 hkdf->info_length);
5067 if (status != PSA_SUCCESS) {
5068 return status;
5069 }
5070 status = psa_mac_update(&hkdf->hmac,
5071 &hkdf->block_number, 1);
5072 if (status != PSA_SUCCESS) {
5073 return status;
5074 }
5075 status = psa_mac_sign_finish(&hkdf->hmac,
5076 hkdf->output_block,
5077 sizeof(hkdf->output_block),
5078 &hmac_output_length);
5079 if (status != PSA_SUCCESS) {
5080 return status;
5081 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005082 }
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005085}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005086#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005087
John Durkop07cc04a2020-11-16 22:08:34 -08005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5089 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005090static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5091 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005093{
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5095 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005096 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5097 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005098 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005099
Janos Follath7742fee2019-06-17 12:58:10 +01005100 /* We can't be wanting more output after block 0xff, otherwise
5101 * the capacity check in psa_key_derivation_output_bytes() would have
5102 * prevented this call. It could happen only if the operation
5103 * object was corrupted or if this function is called directly
5104 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (tls12_prf->block_number == 0xff) {
5106 return PSA_ERROR_CORRUPTION_DETECTED;
5107 }
Janos Follath7742fee2019-06-17 12:58:10 +01005108
5109 /* We need a new block */
5110 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005111 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005112
5113 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5114 *
5115 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5116 *
5117 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5118 * HMAC_hash(secret, A(2) + seed) +
5119 * HMAC_hash(secret, A(3) + seed) + ...
5120 *
5121 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005122 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005123 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005124 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005125 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005126 * is currently extracted as `output_block` and where i is
5127 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005128 */
5129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 status = psa_key_derivation_start_hmac(&hmac,
5131 hash_alg,
5132 tls12_prf->secret,
5133 tls12_prf->secret_length);
5134 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005135 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005137
5138 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005140 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5141 * the variable seed and in this instance means it in the context of the
5142 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 status = psa_mac_update(&hmac,
5144 tls12_prf->label,
5145 tls12_prf->label_length);
5146 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005147 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 }
5149 status = psa_mac_update(&hmac,
5150 tls12_prf->seed,
5151 tls12_prf->seed_length);
5152 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005153 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 }
5155 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005156 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5158 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005159 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005161 }
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 status = psa_mac_sign_finish(&hmac,
5164 tls12_prf->Ai, hash_length,
5165 &hmac_output_length);
5166 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005167 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 }
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 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 status = psa_key_derivation_start_hmac(&hmac,
5175 hash_alg,
5176 tls12_prf->secret,
5177 tls12_prf->secret_length);
5178 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005179 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 }
5181 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5182 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005183 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 }
5185 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5186 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005187 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 }
5189 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5190 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005191 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 }
5193 status = psa_mac_sign_finish(&hmac,
5194 tls12_prf->output_block, hash_length,
5195 &hmac_output_length);
5196 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005197 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005199
Janos Follath7742fee2019-06-17 12:58:10 +01005200
5201cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 cleanup_status = psa_mac_abort(&hmac);
5203 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005204 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005208}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005209
Janos Follath844eb0e2019-06-19 12:10:49 +01005210static psa_status_t psa_key_derivation_tls12_prf_read(
5211 psa_tls12_prf_key_derivation_t *tls12_prf,
5212 psa_algorithm_t alg,
5213 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005215{
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5217 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005218 psa_status_t status;
5219 uint8_t offset, length;
5220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005222 case PSA_TLS12_PRF_STATE_LABEL_SET:
5223 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5224 break;
5225 case PSA_TLS12_PRF_STATE_OUTPUT:
5226 break;
5227 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005229 }
5230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005232 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if (tls12_prf->left_in_block == 0) {
5234 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5235 alg);
5236 if (status != PSA_SUCCESS) {
5237 return status;
5238 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005239
5240 continue;
5241 }
5242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005244 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005246 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005248
5249 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005251 output += length;
5252 output_length -= length;
5253 tls12_prf->left_in_block -= length;
5254 }
5255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005257}
John Durkop07cc04a2020-11-16 22:08:34 -08005258#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5259 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005260
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005261#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5262static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5263 psa_tls12_ecjpake_to_pms_t *ecjpake,
5264 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005266{
5267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005268 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (output_length != 32) {
5271 return PSA_ERROR_INVALID_ARGUMENT;
5272 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5275 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5276 &output_size);
5277 if (status != PSA_SUCCESS) {
5278 return status;
5279 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (output_size != output_length) {
5282 return PSA_ERROR_GENERIC_ERROR;
5283 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005286}
5287#endif
5288
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305289#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305290static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5291 psa_pbkdf2_key_derivation_t *pbkdf2,
5292 psa_algorithm_t prf_alg,
5293 uint8_t prf_output_length,
5294 psa_key_attributes_t *attributes)
5295{
5296 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305297 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305298 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305299 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305300 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305301 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305302 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305303
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305304 mac_operation.is_sign = 1;
5305 mac_operation.mac_size = prf_output_length;
5306 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305307
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305308 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5309 attributes,
5310 pbkdf2->password,
5311 pbkdf2->password_length,
5312 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305313 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305314 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305315 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305316 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5317 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305318 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305319 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305320 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305321 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305322 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305323 }
5324 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5325 &mac_output_length);
5326 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305327 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305328 }
5329
5330 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305331 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305332 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305333 }
5334
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305335 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305336
5337 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305338 /* We are passing prf_output_length as mac_size because the driver
5339 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305340 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305341 status = psa_driver_wrapper_mac_compute(attributes,
5342 pbkdf2->password,
5343 pbkdf2->password_length,
5344 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305345 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305346 &mac_output_length);
5347 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305348 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305349 }
5350
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305351 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305352 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305353
5354cleanup:
5355 /* Zeroise buffers to clear sensitive data from memory. */
5356 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5357 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305358}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305359
5360static psa_status_t psa_key_derivation_pbkdf2_read(
5361 psa_pbkdf2_key_derivation_t *pbkdf2,
5362 psa_algorithm_t kdf_alg,
5363 uint8_t *output,
5364 size_t output_length)
5365{
5366 psa_status_t status;
5367 psa_algorithm_t prf_alg;
5368 uint8_t prf_output_length;
5369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5370 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5371 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5372
5373 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5374 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5375 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5376 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305377 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5378 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305379 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305380 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305381 } else {
5382 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305383 }
5384
5385 switch (pbkdf2->state) {
5386 case PSA_PBKDF2_STATE_PASSWORD_SET:
5387 /* Initially we need a new block so bytes_used is equal to block size*/
5388 pbkdf2->bytes_used = prf_output_length;
5389 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5390 break;
5391 case PSA_PBKDF2_STATE_OUTPUT:
5392 break;
5393 default:
5394 return PSA_ERROR_BAD_STATE;
5395 }
5396
5397 while (output_length != 0) {
5398 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5399 if (n > output_length) {
5400 n = (uint8_t) output_length;
5401 }
5402 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5403 output += n;
5404 output_length -= n;
5405 pbkdf2->bytes_used += n;
5406
5407 if (output_length == 0) {
5408 break;
5409 }
5410
5411 /* We need a new block */
5412 pbkdf2->bytes_used = 0;
5413 pbkdf2->block_number++;
5414
5415 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5416 prf_output_length,
5417 &attributes);
5418 if (status != PSA_SUCCESS) {
5419 return status;
5420 }
5421 }
5422
5423 return PSA_SUCCESS;
5424}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305425#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305426
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005427psa_status_t psa_key_derivation_output_bytes(
5428 psa_key_derivation_operation_t *operation,
5429 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005431{
5432 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005436 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005438 }
5439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005441 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005442 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005444 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005445 goto exit;
5446 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005449 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005450 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5451 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005453 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005455 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005456 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005457
Przemek Stekiel69c46792022-06-10 12:59:51 +02005458#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5460 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5461 output, output_length);
5462 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005463#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005464#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5465 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5467 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5468 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5469 kdf_alg, output,
5470 output_length);
5471 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005472#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5473 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005474#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005476 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5478 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005479#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305480#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305481 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305482 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5483 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305484 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305485#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005486
Gilles Peskineeab56e42018-07-12 17:12:33 +02005487 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005488 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005490 }
5491
5492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005494 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 * This allows us to differentiate between exhausted operations and
5496 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5497 * operations. */
5498 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005500 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005502 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005504}
5505
David Brown7807bf72021-02-09 16:28:23 -07005506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005508{
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 if (data_size >= 8) {
5510 mbedtls_des_key_set_parity(data);
5511 }
5512 if (data_size >= 16) {
5513 mbedtls_des_key_set_parity(data + 8);
5514 }
5515 if (data_size >= 24) {
5516 mbedtls_des_key_set_parity(data + 16);
5517 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005518}
David Brown7807bf72021-02-09 16:28:23 -07005519#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005520
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005521/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 * ECC keys on a Weierstrass elliptic curve require the generation
5523 * of a private key which is an integer
5524 * in the range [1, N - 1], where N is the boundary of the private key domain:
5525 * N is the prime p for Diffie-Hellman, or the order of the
5526 * curve’s base point for ECC.
5527 *
5528 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5529 * This function generates the private key using the following process:
5530 *
5531 * 1. Draw a byte string of length ceiling(m/8) bytes.
5532 * 2. If m is not a multiple of 8, set the most significant
5533 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5534 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5535 * 4. If k > N - 2, discard the result and return to step 1.
5536 * 5. Output k + 1 as the private key.
5537 *
5538 * This method allows compliance to NIST standards, specifically the methods titled
5539 * Key-Pair Generation by Testing Candidates in the following publications:
5540 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5541 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5542 * Diffie-Hellman keys.
5543 *
5544 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5545 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5546 *
5547 * Note: Function allocates memory for *data buffer, so given *data should be
5548 * always NULL.
5549 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005550#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5551#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005552static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005553 psa_key_slot_t *slot,
5554 size_t bits,
5555 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005556 uint8_t **data
5557 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005558{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005559 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005560 mbedtls_mpi k;
5561 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005564 size_t m;
5565 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005566
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 mbedtls_mpi_init(&k);
5568 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005569
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005570 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005572 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005573 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005576 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005577 goto cleanup;
5578 }
5579
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005580 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005584
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005585 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005586 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005587 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005588
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005589 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005590
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005591 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005593
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005594 /* Note: This function is always called with *data == NULL and it
5595 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 *data = mbedtls_calloc(1, m_bytes);
5597 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005598 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005599 goto cleanup;
5600 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005603 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005605 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005607
5608 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005610 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005611 * (8 * ceiling(m/8) - m) bits of the first byte in
5612 * the string to zero.
5613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005615 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005616 }
5617
5618 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 * big-endian byte string.
5620 */
5621 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005622
5623 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 * Result of comparison is returned. When it indicates error
5625 * then this function is called again.
5626 */
5627 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005628 }
5629
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005630 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5632 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005633cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 if (ret != 0) {
5635 status = mbedtls_to_psa_error(ret);
5636 }
5637 if (status != PSA_SUCCESS) {
5638 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005639 *data = NULL;
5640 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 mbedtls_mpi_free(&k);
5642 mbedtls_mpi_free(&diff_N_2);
5643 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005644}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005645
5646/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5647 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5648 *
5649 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5650 * draw a 32-byte string and process it as specified in
5651 * Elliptic Curves for Security [RFC7748] §5.
5652 *
5653 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5654 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5655 *
5656 * Note: Function allocates memory for *data buffer, so given *data should be
5657 * always NULL.
5658 */
5659
5660static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5661 size_t bits,
5662 psa_key_derivation_operation_t *operation,
5663 uint8_t **data
5664 )
5665{
5666 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005670 case 255:
5671 output_length = 32;
5672 break;
5673 case 448:
5674 output_length = 56;
5675 break;
5676 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005678 break;
5679 }
5680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 if (*data == NULL) {
5684 return PSA_ERROR_INSUFFICIENT_MEMORY;
5685 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005690 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005694 case 255:
5695 (*data)[0] &= 248;
5696 (*data)[31] &= 127;
5697 (*data)[31] |= 64;
5698 break;
5699 case 448:
5700 (*data)[0] &= 252;
5701 (*data)[55] |= 128;
5702 break;
5703 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005705 break;
5706 }
5707
5708 return status;
5709}
Valerio Setti86587ab2023-06-27 13:09:30 +02005710#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5711static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5712 psa_key_slot_t *slot, size_t bits,
5713 psa_key_derivation_operation_t *operation, uint8_t **data)
5714{
5715 (void) slot;
5716 (void) bits;
5717 (void) operation;
5718 (void) data;
5719 return PSA_ERROR_NOT_SUPPORTED;
5720}
5721
5722static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5723 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5724{
5725 (void) bits;
5726 (void) operation;
5727 (void) data;
5728 return PSA_ERROR_NOT_SUPPORTED;
5729}
5730#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5731#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005732
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005733static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005734 psa_key_slot_t *slot,
5735 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005737{
5738 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305740 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5744 return PSA_ERROR_INVALID_ARGUMENT;
5745 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005747#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005748 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5750 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5751 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005752 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5754 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005755 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 }
5757 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005758 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
5760 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005761 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005763 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005764 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005765#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02005766 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 if (key_type_is_raw_bytes(slot->attr.type)) {
5768 if (bits % 8 != 0) {
5769 return PSA_ERROR_INVALID_ARGUMENT;
5770 }
5771 data = mbedtls_calloc(1, bytes);
5772 if (data == NULL) {
5773 return PSA_ERROR_INSUFFICIENT_MEMORY;
5774 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 status = psa_key_derivation_output_bytes(operation, data, bytes);
5777 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
David Brown12ca5032021-02-11 11:02:00 -07005780#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 if (slot->attr.type == PSA_KEY_TYPE_DES) {
5782 psa_des_set_key_parity(data, bytes);
5783 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01005784#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 } else {
5786 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01005787 }
Ronald Crondd04d422020-11-28 15:14:42 +01005788
Ronald Cronbf33c932020-11-28 18:06:53 +01005789 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005790
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005791 if (psa_key_lifetime_is_external(slot->attr.lifetime)) {
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005792 status = psa_driver_wrapper_get_key_buffer_size(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 &storage_size);
5794 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305795 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 }
Archanad8a83dc2021-06-14 10:04:16 +05305797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 status = psa_allocate_buffer_to_slot(slot, storage_size);
5799 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05305800 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 }
Archanad8a83dc2021-06-14 10:04:16 +05305802
Gilles Peskine7a5d9202024-02-28 01:18:23 +01005803 status = psa_driver_wrapper_import_key(&slot->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 data, bytes,
5805 slot->key.data,
5806 slot->key.bytes,
5807 &slot->key.bytes, &bits);
5808 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01005809 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005811
5812exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 mbedtls_free(data);
5814 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005815}
5816
Gilles Peskine092ce512024-02-20 12:31:24 +01005817static const psa_key_production_parameters_t default_production_parameters =
5818 PSA_KEY_PRODUCTION_PARAMETERS_INIT;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005819
Gilles Peskine092ce512024-02-20 12:31:24 +01005820int psa_key_production_parameters_are_default(
5821 const psa_key_production_parameters_t *params,
5822 size_t params_data_length)
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005823{
Gilles Peskine092ce512024-02-20 12:31:24 +01005824 if (params->flags != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005825 return 0;
5826 }
Gilles Peskine092ce512024-02-20 12:31:24 +01005827 if (params_data_length != 0) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005828 return 0;
5829 }
5830 return 1;
5831}
5832
5833psa_status_t psa_key_derivation_output_key_ext(
5834 const psa_key_attributes_t *attributes,
5835 psa_key_derivation_operation_t *operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005836 const psa_key_production_parameters_t *params,
5837 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005838 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005839{
5840 psa_status_t status;
5841 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005842 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005843
Ronald Cron81709fc2020-11-14 12:10:32 +01005844 *key = MBEDTLS_SVC_KEY_ID_INIT;
5845
Gilles Peskine0f84d622019-09-12 19:03:13 +02005846 /* Reject any attempt to create a zero-length key so that we don't
5847 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (psa_get_key_bits(attributes) == 0) {
5849 return PSA_ERROR_INVALID_ARGUMENT;
5850 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005851
Gilles Peskine092ce512024-02-20 12:31:24 +01005852 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005853 return PSA_ERROR_INVALID_ARGUMENT;
5854 }
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (operation->alg == PSA_ALG_NONE) {
5857 return PSA_ERROR_BAD_STATE;
5858 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (!operation->can_output_key) {
5861 return PSA_ERROR_NOT_PERMITTED;
5862 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
5865 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005866#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005868 /* Deriving a key in a secure element is not implemented yet. */
5869 status = PSA_ERROR_NOT_SUPPORTED;
5870 }
5871#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 if (status == PSA_SUCCESS) {
5873 status = psa_generate_derived_key_internal(slot,
Gilles Peskine2f107ae2024-02-28 01:26:46 +01005874 attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005876 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (status == PSA_SUCCESS) {
5878 status = psa_finish_key_creation(slot, driver, key);
5879 }
5880 if (status != PSA_SUCCESS) {
5881 psa_fail_key_creation(slot, driver);
5882 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005885}
5886
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005887psa_status_t psa_key_derivation_output_key(
5888 const psa_key_attributes_t *attributes,
5889 psa_key_derivation_operation_t *operation,
5890 mbedtls_svc_key_id_t *key)
5891{
Gilles Peskinec81393b2024-02-14 20:51:28 +01005892 return psa_key_derivation_output_key_ext(attributes, operation,
Gilles Peskine092ce512024-02-20 12:31:24 +01005893 &default_production_parameters, 0,
Gilles Peskinec81393b2024-02-14 20:51:28 +01005894 key);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01005895}
Gilles Peskineeab56e42018-07-12 17:12:33 +02005896
5897
5898/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005899/* Key derivation */
5900/****************************************************************/
5901
Steven Cooreman932ffb72021-02-15 12:14:32 +01005902#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005903static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005904{
5905#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5907 return 1;
5908 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005909#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005910#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
5912 return 1;
5913 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02005914#endif
5915#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
5917 return 1;
5918 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005919#endif
5920#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
5922 return 1;
5923 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005924#endif
5925#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5927 return 1;
5928 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005929#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005930#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5932 return 1;
5933 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005934#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05305935#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5936 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5937 return 1;
5938 }
5939#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05305940#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
5941 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5942 return 1;
5943 }
5944#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02005946}
5947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005949{
5950 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 psa_status_t status = psa_hash_setup(&operation, alg);
5952 psa_hash_abort(&operation);
5953 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02005954}
5955
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305956static psa_status_t psa_key_derivation_set_maximum_capacity(
5957 psa_key_derivation_operation_t *operation,
5958 psa_algorithm_t kdf_alg)
5959{
5960#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
5961 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5962 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
5963 return PSA_SUCCESS;
5964 }
5965#endif
5966#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
5967 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5968#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05305969 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05305970 PSA_KEY_TYPE_AES,
5971 128U,
5972 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305973#else
5974 operation->capacity = SIZE_MAX;
5975#endif
5976 return PSA_SUCCESS;
5977 }
5978#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
5979
5980 /* After this point, if kdf_alg is not valid then value of hash_alg may be
5981 * invalid or meaningless but it does not affect this function */
5982 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
5983 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05305984 if (hash_size == 0) {
5985 return PSA_ERROR_NOT_SUPPORTED;
5986 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05305987
5988 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
5989 * we might fail later, which is somewhat unfriendly and potentially
5990 * risk-prone. */
5991 psa_status_t status = psa_hash_try_support(hash_alg);
5992 if (status != PSA_SUCCESS) {
5993 return status;
5994 }
5995
5996#if defined(PSA_WANT_ALG_HKDF)
5997 if (PSA_ALG_IS_HKDF(kdf_alg)) {
5998 operation->capacity = 255 * hash_size;
5999 } else
6000#endif
6001#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6002 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6003 operation->capacity = hash_size;
6004 } else
6005#endif
6006#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6007 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6008 operation->capacity = 255 * hash_size;
6009 } else
6010#endif
6011#if defined(PSA_WANT_ALG_TLS12_PRF)
6012 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6013 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6014 operation->capacity = SIZE_MAX;
6015 } else
6016#endif
6017#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6018 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6019 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6020 /* Master Secret is always 48 bytes
6021 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6022 operation->capacity = 48U;
6023 } else
6024#endif
6025#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6026 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6027#if (SIZE_MAX > UINT32_MAX)
6028 operation->capacity = UINT32_MAX * hash_size;
6029#else
6030 operation->capacity = SIZE_MAX;
6031#endif
6032 } else
6033#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6034 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306035 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306036 status = PSA_ERROR_NOT_SUPPORTED;
6037 }
6038 return status;
6039}
6040
Gilles Peskine969c5d62019-01-16 15:53:06 +01006041static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006042 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006044{
Janos Follath5fe19732019-06-20 15:09:30 +01006045 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6046 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006048
Gilles Peskine969c5d62019-01-16 15:53:06 +01006049 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (!is_kdf_alg_supported(kdf_alg)) {
6051 return PSA_ERROR_NOT_SUPPORTED;
6052 }
John Durkop07cc04a2020-11-16 22:08:34 -08006053
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306054 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6055 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306056 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006057}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006060{
6061#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 if (alg == PSA_ALG_ECDH) {
6063 return PSA_SUCCESS;
6064 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006065#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006066#if defined(PSA_WANT_ALG_FFDH)
6067 if (alg == PSA_ALG_FFDH) {
6068 return PSA_SUCCESS;
6069 }
6070#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006071 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006073}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006074
6075static int psa_key_derivation_allows_free_form_secret_input(
6076 psa_algorithm_t kdf_alg)
6077{
6078#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6079 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6080 return 0;
6081 }
6082#endif
6083 (void) kdf_alg;
6084 return 1;
6085}
John Durkop07cc04a2020-11-16 22:08:34 -08006086#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6089 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006090{
6091 psa_status_t status;
6092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (operation->alg != 0) {
6094 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006095 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6098 return PSA_ERROR_INVALID_ARGUMENT;
6099 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6100#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6101 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6102 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6103 status = psa_key_agreement_try_support(ka_alg);
6104 if (status != PSA_SUCCESS) {
6105 return status;
6106 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006107 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6108 return PSA_ERROR_INVALID_ARGUMENT;
6109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6111#else
6112 return PSA_ERROR_NOT_SUPPORTED;
6113#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6114 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6115#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6116 status = psa_key_derivation_setup_kdf(operation, alg);
6117#else
6118 return PSA_ERROR_NOT_SUPPORTED;
6119#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6120 } else {
6121 return PSA_ERROR_INVALID_ARGUMENT;
6122 }
6123
6124 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006125 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 }
6127 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006128}
6129
Przemek Stekiel69c46792022-06-10 12:59:51 +02006130#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006131static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6132 psa_algorithm_t kdf_alg,
6133 psa_key_derivation_step_t step,
6134 const uint8_t *data,
6135 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006136{
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006140 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006141#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6143 return PSA_ERROR_INVALID_ARGUMENT;
6144 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006145#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (hkdf->state != HKDF_STATE_INIT) {
6147 return PSA_ERROR_BAD_STATE;
6148 } else {
6149 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6150 hash_alg,
6151 data, data_length);
6152 if (status != PSA_SUCCESS) {
6153 return status;
6154 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006155 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006157 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006158 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006159#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006161 /* We shouldn't be in different state as HKDF_EXPAND only allows
6162 * two inputs: SECRET (this case) and INFO which does not modify
6163 * the state. It could happen only if the hkdf
6164 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (hkdf->state != HKDF_STATE_INIT) {
6166 return PSA_ERROR_BAD_STATE;
6167 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006168
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006169 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6171 return PSA_ERROR_INVALID_ARGUMENT;
6172 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 memcpy(hkdf->prk, data, data_length);
6175 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006176#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006177 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006178 /* HKDF: If no salt was provided, use an empty salt.
6179 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006181#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6183 return PSA_ERROR_BAD_STATE;
6184 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006185#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6187 hash_alg,
6188 NULL, 0);
6189 if (status != PSA_SUCCESS) {
6190 return status;
6191 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006192 hkdf->state = HKDF_STATE_STARTED;
6193 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (hkdf->state != HKDF_STATE_STARTED) {
6195 return PSA_ERROR_BAD_STATE;
6196 }
6197 status = psa_mac_update(&hkdf->hmac,
6198 data, data_length);
6199 if (status != PSA_SUCCESS) {
6200 return status;
6201 }
6202 status = psa_mac_sign_finish(&hkdf->hmac,
6203 hkdf->prk,
6204 sizeof(hkdf->prk),
6205 &data_length);
6206 if (status != PSA_SUCCESS) {
6207 return status;
6208 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006209 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006210
Gilles Peskine2b522db2019-04-12 15:11:49 +02006211 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006212 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006213#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006215 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006217 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006219#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006220 {
6221 /* Block 0 is empty, and the next block will be
6222 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006224 }
6225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006227 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006228#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6230 return PSA_ERROR_INVALID_ARGUMENT;
6231 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006232#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006233#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6235 hkdf->state == HKDF_STATE_INIT) {
6236 return PSA_ERROR_BAD_STATE;
6237 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006238#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 if (hkdf->state == HKDF_STATE_OUTPUT) {
6240 return PSA_ERROR_BAD_STATE;
6241 }
6242 if (hkdf->info_set) {
6243 return PSA_ERROR_BAD_STATE;
6244 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006245 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (data_length != 0) {
6247 hkdf->info = mbedtls_calloc(1, data_length);
6248 if (hkdf->info == NULL) {
6249 return PSA_ERROR_INSUFFICIENT_MEMORY;
6250 }
6251 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006252 }
6253 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006255 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006257 }
6258}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006259#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006260
John Durkop07cc04a2020-11-16 22:08:34 -08006261#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6262 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006263static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6264 const uint8_t *data,
6265 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006266{
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6268 return PSA_ERROR_BAD_STATE;
6269 }
Janos Follathf08e2652019-06-13 09:05:41 +01006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 if (data_length != 0) {
6272 prf->seed = mbedtls_calloc(1, data_length);
6273 if (prf->seed == NULL) {
6274 return PSA_ERROR_INSUFFICIENT_MEMORY;
6275 }
Janos Follathf08e2652019-06-13 09:05:41 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006278 prf->seed_length = data_length;
6279 }
Janos Follathf08e2652019-06-13 09:05:41 +01006280
Gilles Peskine43f958b2020-12-13 14:55:14 +01006281 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006284}
6285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6287 const uint8_t *data,
6288 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006289{
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6291 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6292 return PSA_ERROR_BAD_STATE;
6293 }
Janos Follath81550542019-06-13 14:26:34 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 if (data_length != 0) {
6296 prf->secret = mbedtls_calloc(1, data_length);
6297 if (prf->secret == NULL) {
6298 return PSA_ERROR_INSUFFICIENT_MEMORY;
6299 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006302 prf->secret_length = data_length;
6303 }
Janos Follath81550542019-06-13 14:26:34 +01006304
Gilles Peskine43f958b2020-12-13 14:55:14 +01006305 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006308}
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6311 const uint8_t *data,
6312 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006313{
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6315 return PSA_ERROR_BAD_STATE;
6316 }
Janos Follath63028dd2019-06-13 09:15:47 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (data_length != 0) {
6319 prf->label = mbedtls_calloc(1, data_length);
6320 if (prf->label == NULL) {
6321 return PSA_ERROR_INSUFFICIENT_MEMORY;
6322 }
Janos Follath63028dd2019-06-13 09:15:47 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006325 prf->label_length = data_length;
6326 }
Janos Follath63028dd2019-06-13 09:15:47 +01006327
Gilles Peskine43f958b2020-12-13 14:55:14 +01006328 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006331}
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6334 psa_key_derivation_step_t step,
6335 const uint8_t *data,
6336 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006337{
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006339 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006341 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006343 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006345 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006347 }
6348}
John Durkop07cc04a2020-11-16 22:08:34 -08006349#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6350 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6351
6352#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6353static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6354 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006355 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006357{
6358 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6360 4 + data_length + prf->other_secret_length :
6361 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6364 return PSA_ERROR_INVALID_ARGUMENT;
6365 }
John Durkop07cc04a2020-11-16 22:08:34 -08006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 uint8_t *pms = mbedtls_calloc(1, pms_len);
6368 if (pms == NULL) {
6369 return PSA_ERROR_INSUFFICIENT_MEMORY;
6370 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006371 uint8_t *cur = pms;
6372
6373 /* pure-PSK:
6374 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006375 *
6376 * The premaster secret is formed as follows: if the PSK is N octets
6377 * long, concatenate a uint16 with the value N, N zero octets, a second
6378 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006379 *
6380 * mixed-PSK:
6381 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6382 * follows: concatenate a uint16 with the length of the other secret,
6383 * the other secret itself, uint16 with the length of PSK, and the
6384 * PSK itself.
6385 * For details please check:
6386 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6387 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6388 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006389 */
6390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6392 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6393 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6394 if (prf->other_secret_length != 0) {
6395 memcpy(cur, prf->other_secret, prf->other_secret_length);
6396 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006397 cur += prf->other_secret_length;
6398 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 } else {
6400 *cur++ = MBEDTLS_BYTE_1(data_length);
6401 *cur++ = MBEDTLS_BYTE_0(data_length);
6402 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006403 cur += data_length;
6404 }
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 *cur++ = MBEDTLS_BYTE_1(data_length);
6407 *cur++ = MBEDTLS_BYTE_0(data_length);
6408 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006409 cur += data_length;
6410
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006411 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006412
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006413 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006415}
Janos Follath6660f0e2019-06-17 08:44:03 +01006416
Przemek Stekiele47201b2022-04-19 13:53:28 +02006417static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6418 psa_tls12_prf_key_derivation_t *prf,
6419 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006421{
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6423 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006424 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006425
6426 if (data_length != 0) {
6427 prf->other_secret = mbedtls_calloc(1, data_length);
6428 if (prf->other_secret == NULL) {
6429 return PSA_ERROR_INSUFFICIENT_MEMORY;
6430 }
6431
6432 memcpy(prf->other_secret, data, data_length);
6433 prf->other_secret_length = data_length;
6434 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006435 prf->other_secret_length = 0;
6436 }
6437
6438 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006441}
6442
Janos Follath6660f0e2019-06-17 08:44:03 +01006443static psa_status_t psa_tls12_prf_psk_to_ms_input(
6444 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006445 psa_key_derivation_step_t step,
6446 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006448{
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006450 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 return psa_tls12_prf_psk_to_ms_set_key(prf,
6452 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006453 break;
6454 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6456 data,
6457 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006458 break;
6459 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006461 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006462
Przemek Stekiele47201b2022-04-19 13:53:28 +02006463 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006464}
John Durkop07cc04a2020-11-16 22:08:34 -08006465#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006466
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006467#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6468static psa_status_t psa_tls12_ecjpake_to_pms_input(
6469 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006470 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006471 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006473{
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6475 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6476 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006477 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006478
6479 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 if (data[0] != 0x04) {
6481 return PSA_ERROR_INVALID_ARGUMENT;
6482 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006483
6484 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006488}
6489#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306490
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306491#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306492static psa_status_t psa_pbkdf2_set_input_cost(
6493 psa_pbkdf2_key_derivation_t *pbkdf2,
6494 psa_key_derivation_step_t step,
6495 uint64_t data)
6496{
6497 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6498 return PSA_ERROR_INVALID_ARGUMENT;
6499 }
6500
6501 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6502 return PSA_ERROR_BAD_STATE;
6503 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306504
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306505 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306506 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306507 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306508
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306509 if (data == 0) {
6510 return PSA_ERROR_INVALID_ARGUMENT;
6511 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306512
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306513 pbkdf2->input_cost = data;
6514 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6515
6516 return PSA_SUCCESS;
6517}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306518
6519static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6520 const uint8_t *data,
6521 size_t data_length)
6522{
Gilles Peskineead17662023-08-20 22:02:51 +02006523 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6524 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6525 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6526 /* Appending to existing salt. No state change. */
6527 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306528 return PSA_ERROR_BAD_STATE;
6529 }
6530
Gilles Peskineca57d782023-07-20 19:08:06 +02006531 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006532 /* Appending an empty string, nothing to do. */
6533 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306534 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306535
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306536 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6537 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306538 return PSA_ERROR_INSUFFICIENT_MEMORY;
6539 }
6540
Gilles Peskineead17662023-08-20 22:02:51 +02006541 if (pbkdf2->salt_length != 0) {
6542 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6543 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306544 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306545 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306546 mbedtls_free(pbkdf2->salt);
6547 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306548 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306549 return PSA_SUCCESS;
6550}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306551
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306552#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306553static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306554 const uint8_t *input,
6555 size_t input_len,
6556 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306557 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306558{
6559 psa_status_t status = PSA_SUCCESS;
6560 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6561 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306562 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306563 } else {
6564 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306565 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306566 }
6567 return status;
6568}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306569#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306570
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306571#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306572static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6573 size_t input_len,
6574 uint8_t *output,
6575 size_t *output_len)
6576{
6577 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306578 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306580 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306581 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6582 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6583 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306584 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306585 * mac_size as the driver function sets mac_output_length = mac_size
6586 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306587 status = psa_driver_wrapper_mac_compute(&attributes,
6588 zeros, sizeof(zeros),
6589 PSA_ALG_CMAC, input, input_len,
6590 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306591 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6592 128U,
6593 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306594 output_len);
6595 } else {
6596 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306597 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306598 }
6599 return status;
6600}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306601#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306602
6603static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306604 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306605 const uint8_t *data,
6606 size_t data_length)
6607{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306608 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306609 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6610 return PSA_ERROR_BAD_STATE;
6611 }
6612
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306613#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306614 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6615 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6616 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6617 pbkdf2->password,
6618 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306619 } else
6620#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6621#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6622 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306623 status = psa_pbkdf2_cmac_set_password(data, data_length,
6624 pbkdf2->password,
6625 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306626 } else
6627#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6628 {
6629 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306630 }
6631
6632 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6633
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306634 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306635}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306636
6637static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306638 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306639 psa_key_derivation_step_t step,
6640 const uint8_t *data,
6641 size_t data_length)
6642{
6643 switch (step) {
6644 case PSA_KEY_DERIVATION_INPUT_SALT:
6645 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6646 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306647 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306648 default:
6649 return PSA_ERROR_INVALID_ARGUMENT;
6650 }
6651}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306652#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306653
Gilles Peskineb8965192019-09-24 16:21:10 +02006654/** Check whether the given key type is acceptable for the given
6655 * input step of a key derivation.
6656 *
6657 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6658 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6659 * Both secret and non-secret inputs can alternatively have the type
6660 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6661 * that the input was passed as a buffer rather than via a key object.
6662 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006663static int psa_key_derivation_check_input_type(
6664 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006666{
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006668 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 if (key_type == PSA_KEY_TYPE_DERIVE) {
6670 return PSA_SUCCESS;
6671 }
6672 if (key_type == PSA_KEY_TYPE_NONE) {
6673 return PSA_SUCCESS;
6674 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006675 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006676 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 if (key_type == PSA_KEY_TYPE_DERIVE) {
6678 return PSA_SUCCESS;
6679 }
6680 if (key_type == PSA_KEY_TYPE_NONE) {
6681 return PSA_SUCCESS;
6682 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006683 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006684 case PSA_KEY_DERIVATION_INPUT_LABEL:
6685 case PSA_KEY_DERIVATION_INPUT_SALT:
6686 case PSA_KEY_DERIVATION_INPUT_INFO:
6687 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6689 return PSA_SUCCESS;
6690 }
6691 if (key_type == PSA_KEY_TYPE_NONE) {
6692 return PSA_SUCCESS;
6693 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006694 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306695 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6696 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6697 return PSA_SUCCESS;
6698 }
6699 if (key_type == PSA_KEY_TYPE_DERIVE) {
6700 return PSA_SUCCESS;
6701 }
6702 if (key_type == PSA_KEY_TYPE_NONE) {
6703 return PSA_SUCCESS;
6704 }
6705 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006708}
6709
Janos Follathb80a94e2019-06-12 15:54:46 +01006710static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006711 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006712 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006713 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006714 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006716{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006717 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 status = psa_key_derivation_check_input_type(step, key_type);
6721 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006722 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006724
Przemek Stekiel69c46792022-06-10 12:59:51 +02006725#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6727 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6728 step, data, data_length);
6729 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006730#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006731#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6733 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6734 step, data, data_length);
6735 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006736#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6737#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6739 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6740 step, data, data_length);
6741 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006742#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006745 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6747 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006748#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306749#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306750 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306751 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6752 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306753 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306754#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006755 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006756 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006757 (void) data;
6758 (void) data_length;
6759 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006761 }
6762
Gilles Peskine224b0d62019-09-23 18:13:17 +02006763exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 if (status != PSA_SUCCESS) {
6765 psa_key_derivation_abort(operation);
6766 }
6767 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006768}
6769
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306770static psa_status_t psa_key_derivation_input_integer_internal(
6771 psa_key_derivation_operation_t *operation,
6772 psa_key_derivation_step_t step,
6773 uint64_t value)
6774{
6775 psa_status_t status;
6776 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6777
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306778#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306779 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306780 status = psa_pbkdf2_set_input_cost(
6781 &operation->ctx.pbkdf2, step, value);
6782 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306783#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306784 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306785 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306786 (void) value;
6787 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306788 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306789 }
6790
6791 if (status != PSA_SUCCESS) {
6792 psa_key_derivation_abort(operation);
6793 }
6794 return status;
6795}
6796
Janos Follath51f4a0f2019-06-14 11:35:55 +01006797psa_status_t psa_key_derivation_input_bytes(
6798 psa_key_derivation_operation_t *operation,
6799 psa_key_derivation_step_t step,
6800 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006802{
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 return psa_key_derivation_input_internal(operation, step,
6804 PSA_KEY_TYPE_NONE,
6805 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006806}
6807
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306808psa_status_t psa_key_derivation_input_integer(
6809 psa_key_derivation_operation_t *operation,
6810 psa_key_derivation_step_t step,
6811 uint64_t value)
6812{
6813 return psa_key_derivation_input_integer_internal(operation, step, value);
6814}
6815
Janos Follath51f4a0f2019-06-14 11:35:55 +01006816psa_status_t psa_key_derivation_input_key(
6817 psa_key_derivation_operation_t *operation,
6818 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006820{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006822 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006823 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006824
Ronald Cron5c522922020-11-14 16:35:34 +01006825 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6827 if (status != PSA_SUCCESS) {
6828 psa_key_derivation_abort(operation);
6829 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006830 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006831
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306832 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6833 * permission to output to a key object. */
6834 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6835 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006836 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 status = psa_key_derivation_input_internal(operation,
6840 step, slot->attr.type,
6841 slot->key.data,
6842 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006843
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006844 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006847}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006848
6849
6850
6851/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006852/* Key agreement */
6853/****************************************************************/
6854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6856 const uint8_t *key_buffer,
6857 size_t key_buffer_size,
6858 psa_algorithm_t alg,
6859 const uint8_t *peer_key,
6860 size_t peer_key_length,
6861 uint8_t *shared_secret,
6862 size_t shared_secret_size,
6863 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006864{
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006866#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006867 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6869 key_buffer_size, alg,
6870 peer_key, peer_key_length,
6871 shared_secret,
6872 shared_secret_size,
6873 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006874#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006875
6876#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6877 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006878 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006879 peer_key,
6880 peer_key_length,
6881 key_buffer,
6882 key_buffer_size,
6883 shared_secret,
6884 shared_secret_size,
6885 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006886#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
6887
Gilles Peskine01d718c2018-09-18 12:01:02 +02006888 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01006889 (void) attributes;
6890 (void) key_buffer;
6891 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01006892 (void) peer_key;
6893 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006894 (void) shared_secret;
6895 (void) shared_secret_size;
6896 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006898 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006899}
Gilles Peskine01d718c2018-09-18 12:01:02 +02006900
Aditya Deshpande17845b82022-10-13 17:21:01 +01006901/** Internal function for raw key agreement
6902 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01006903 * to the driver's implementation if a driver is present.
6904 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01006905 * (psa_key_agreement_raw_builtin).
6906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006907static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
6908 psa_key_slot_t *private_key,
6909 const uint8_t *peer_key,
6910 size_t peer_key_length,
6911 uint8_t *shared_secret,
6912 size_t shared_secret_size,
6913 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006914{
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6916 return PSA_ERROR_NOT_SUPPORTED;
6917 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01006918
Gilles Peskine7a5d9202024-02-28 01:18:23 +01006919 return psa_driver_wrapper_key_agreement(&private_key->attr,
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 private_key->key.data,
6921 private_key->key.bytes, alg,
6922 peer_key, peer_key_length,
6923 shared_secret,
6924 shared_secret_size,
6925 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02006926}
6927
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006928/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006929 * to potentially free embedded data structures and wipe confidential data.
6930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006931static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
6932 psa_key_derivation_step_t step,
6933 psa_key_slot_t *private_key,
6934 const uint8_t *peer_key,
6935 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02006936{
6937 psa_status_t status;
Moritz Fischer967f8cd2024-03-02 00:55:06 +00006938 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE] = { 0 };
Gilles Peskine01d718c2018-09-18 12:01:02 +02006939 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006941
6942 /* Step 1: run the secret agreement algorithm to generate the shared
6943 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 status = psa_key_agreement_raw_internal(ka_alg,
6945 private_key,
6946 peer_key, peer_key_length,
6947 shared_secret,
6948 sizeof(shared_secret),
6949 &shared_secret_length);
6950 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02006951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02006953
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006954 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955 * the shared secret. A shared secret is permitted wherever a key
6956 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 status = psa_key_derivation_input_internal(operation, step,
6958 PSA_KEY_TYPE_DERIVE,
6959 shared_secret,
6960 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02006961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
6963 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006964}
6965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
6967 psa_key_derivation_step_t step,
6968 mbedtls_svc_key_id_t private_key,
6969 const uint8_t *peer_key,
6970 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02006971{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006973 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006974 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006975
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
6977 return PSA_ERROR_INVALID_ARGUMENT;
6978 }
Ronald Cron5c522922020-11-14 16:35:34 +01006979 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6981 if (status != PSA_SUCCESS) {
6982 return status;
6983 }
6984 status = psa_key_agreement_internal(operation, step,
6985 slot,
6986 peer_key, peer_key_length);
6987 if (status != PSA_SUCCESS) {
6988 psa_key_derivation_abort(operation);
6989 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006990 /* If a private key has been added as SECRET, we allow the derived
6991 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006993 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006995 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006996
Ryan Everett5ac6fa72024-02-14 17:11:36 +00006997 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006998
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007000}
7001
Gilles Peskine449bd832023-01-11 14:50:10 +01007002psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7003 mbedtls_svc_key_id_t private_key,
7004 const uint8_t *peer_key,
7005 size_t peer_key_length,
7006 uint8_t *output,
7007 size_t output_size,
7008 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007009{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007011 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007012 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007013 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007016 status = PSA_ERROR_INVALID_ARGUMENT;
7017 goto exit;
7018 }
Ronald Cron5c522922020-11-14 16:35:34 +01007019 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7021 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007024
Gilles Peskine3e561302022-04-14 00:17:15 +02007025 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7026 * for the output size. The PSA specification only guarantees that this
7027 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7028 * but it might be nice to allow smaller buffers if the output fits.
7029 * At the time of writing this comment, with only ECDH implemented,
7030 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7031 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7032 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007033 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7035 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007036 status = PSA_ERROR_BUFFER_TOO_SMALL;
7037 goto exit;
7038 }
7039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 status = psa_key_agreement_raw_internal(alg, slot,
7041 peer_key, peer_key_length,
7042 output, output_size,
7043 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007044
7045exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007047 /* If an error happens and is not handled properly, the output
7048 * may be used as a key to protect sensitive data. Arrange for such
7049 * a key to be random, which is likely to result in decryption or
7050 * verification errors. This is better than filling the buffer with
7051 * some constant data such as zeros, which would result in the data
7052 * being protected with a reproducible, easily knowable key.
7053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007055 *output_length = output_size;
7056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007057
Ryan Everetta103ec92024-01-31 13:59:57 +00007058 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007061}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007062
7063
Gilles Peskine30524eb2020-11-13 17:02:26 +01007064
Gilles Peskine86a440b2018-11-12 18:39:40 +01007065/****************************************************************/
7066/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007067/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007068
Gilles Peskine672a7712023-04-28 21:00:28 +02007069#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7070#include "entropy_poll.h"
7071#endif
7072
Gilles Peskine30524eb2020-11-13 17:02:26 +01007073/** Initialize the PSA random generator.
7074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007076{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007077#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007079#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7080
Gilles Peskine30524eb2020-11-13 17:02:26 +01007081 /* Set default configuration if
7082 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007084 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 }
7086 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007087 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007091#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7092 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7093 /* The PSA entropy injection feature depends on using NV seed as an entropy
7094 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 mbedtls_entropy_add_source(&rng->entropy,
7096 mbedtls_nv_seed_poll, NULL,
7097 MBEDTLS_ENTROPY_BLOCK_SIZE,
7098 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007099#endif
7100
Valerio Setti061d4e42024-02-26 12:52:44 +01007101 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007102#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007103}
7104
7105/** Deinitialize the PSA random generator.
7106 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007107static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007108{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007109#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007110 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007111#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007112 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007113 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007114#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007115}
7116
7117/** Seed the PSA random generator.
7118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007119static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007120{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007121#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7122 /* Do nothing: the external RNG seeds itself. */
7123 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007125#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007126 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007127 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 drbg_seed, sizeof(drbg_seed) - 1);
7129 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007130#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007131}
7132
Gilles Peskine449bd832023-01-11 14:50:10 +01007133psa_status_t psa_generate_random(uint8_t *output,
7134 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007135{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007136 GUARD_MODULE_INITIALIZED;
7137
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007138#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7139
7140 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7142 output, output_size,
7143 &output_length);
7144 if (status != PSA_SUCCESS) {
7145 return status;
7146 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007147 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007148 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 if (output_length != output_size) {
7150 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7151 }
7152 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007153
7154#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 while (output_size > 0) {
Valerio Setti718180c2024-02-27 11:58:39 +01007157 int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
Gilles Peskine71ddab92021-01-04 21:01:07 +01007158 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7160 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7161 output_size);
Valerio Setti718180c2024-02-27 11:58:39 +01007162#if defined(MBEDTLS_CTR_DRBG_C)
7163 ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
7164#elif defined(MBEDTLS_HMAC_DRBG_C)
7165 ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
7166#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 if (ret != 0) {
7168 return mbedtls_to_psa_error(ret);
7169 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007170 output_size -= request_size;
7171 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007172 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007174#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007175}
7176
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007177#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007178psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7179 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007180{
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 if (global_data.initialized) {
7182 return PSA_ERROR_NOT_PERMITTED;
7183 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7186 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7187 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7188 return PSA_ERROR_INVALID_ARGUMENT;
7189 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007190
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007192}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007193#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007194
Ronald Cron3772afe2021-02-08 16:10:05 +01007195/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007196 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007197 * \param type The key type
7198 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007199 *
7200 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007201 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007202 * \retval #PSA_ERROR_INVALID_ARGUMENT
7203 * The size in bits of the key is not valid.
7204 * \retval #PSA_ERROR_NOT_SUPPORTED
7205 * The type and/or the size in bits of the key or the combination of
7206 * the two is not supported.
7207 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007208static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007210{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (key_type_is_raw_bytes(type)) {
7214 status = psa_validate_unstructured_key_bit_size(type, bits);
7215 if (status != PSA_SUCCESS) {
7216 return status;
7217 }
7218 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007219#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7221 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7222 return PSA_ERROR_NOT_SUPPORTED;
7223 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007224 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007225 return PSA_ERROR_NOT_SUPPORTED;
7226 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007227
Ronald Cron01b2aba2020-10-05 09:42:02 +02007228 /* Accept only byte-aligned keys, for the same reasons as
7229 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (bits % 8 != 0) {
7231 return PSA_ERROR_NOT_SUPPORTED;
7232 }
7233 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007234#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007235
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007236#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007238 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 return PSA_SUCCESS;
7240 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007241#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007242
Valerio Settia55f0422023-07-10 15:34:41 +02007243#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007244 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007245 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007246 return PSA_ERROR_NOT_SUPPORTED;
7247 }
7248 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007249#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007250 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007252 }
7253
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007255}
7256
Ronald Cron55ed0592020-10-05 10:30:40 +02007257psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007258 const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007259 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007261{
7262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007263 psa_key_type_t type = attributes->type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007264
Gilles Peskine7a18f962024-02-12 16:48:11 +01007265 /* Only used for RSA */
Gilles Peskine092ce512024-02-20 12:31:24 +01007266 (void) params;
7267 (void) params_data_length;
Gilles Peskine7a18f962024-02-12 16:48:11 +01007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 if (key_type_is_raw_bytes(type)) {
7270 status = psa_generate_random(key_buffer, key_buffer_size);
7271 if (status != PSA_SUCCESS) {
7272 return status;
7273 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007274
David Brown12ca5032021-02-11 11:02:00 -07007275#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 if (type == PSA_KEY_TYPE_DES) {
7277 psa_des_set_key_parity(key_buffer, key_buffer_size);
7278 }
David Brown8107e312021-02-12 08:08:46 -07007279#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007281
Valerio Setti76df8c12023-07-11 14:11:28 +02007282#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskinee22f6a92024-02-26 15:45:33 +01007284 return mbedtls_psa_rsa_generate_key(attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +01007285 params, params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01007286 key_buffer,
7287 key_buffer_size,
7288 key_buffer_length);
7289 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007290#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007291
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007292#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7294 return mbedtls_psa_ecp_generate_key(attributes,
7295 key_buffer,
7296 key_buffer_size,
7297 key_buffer_length);
7298 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007299#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007300
Valerio Settia55f0422023-07-10 15:34:41 +02007301#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007302 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7303 return mbedtls_psa_ffdh_generate_key(attributes,
7304 key_buffer,
7305 key_buffer_size,
7306 key_buffer_length);
7307 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007308#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007309 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 (void) key_buffer_length;
7311 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007312 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007315}
Darryl Green0c6575a2018-11-07 16:05:30 +00007316
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007317psa_status_t psa_generate_key_ext(const psa_key_attributes_t *attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007318 const psa_key_production_parameters_t *params,
7319 size_t params_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007320 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007321{
7322 psa_status_t status;
7323 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007324 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007325 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007326
Ronald Cron81709fc2020-11-14 12:10:32 +01007327 *key = MBEDTLS_SVC_KEY_ID_INIT;
7328
Gilles Peskine0f84d622019-09-12 19:03:13 +02007329 /* Reject any attempt to create a zero-length key so that we don't
7330 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 if (psa_get_key_bits(attributes) == 0) {
7332 return PSA_ERROR_INVALID_ARGUMENT;
7333 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007334
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007335 /* Reject any attempt to create a public key. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007336 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 return PSA_ERROR_INVALID_ARGUMENT;
7338 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007339
Gilles Peskine7a18f962024-02-12 16:48:11 +01007340#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007341 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Gilles Peskine092ce512024-02-20 12:31:24 +01007342 if (params->flags != 0) {
Gilles Peskine7a18f962024-02-12 16:48:11 +01007343 return PSA_ERROR_INVALID_ARGUMENT;
7344 }
7345 } else
7346#endif
Gilles Peskine092ce512024-02-20 12:31:24 +01007347 if (!psa_key_production_parameters_are_default(params, params_data_length)) {
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007348 return PSA_ERROR_INVALID_ARGUMENT;
7349 }
7350
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7352 &slot, &driver);
7353 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 }
Gilles Peskine11792082019-08-06 18:36:36 +02007356
Ronald Cron977c2472020-10-13 08:32:21 +02007357 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307358 * storage ( thus not in the case of generating a key in a secure element
7359 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7360 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 if (slot->key.data == NULL) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007362 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->lifetime) ==
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007364 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007365 attributes->type, attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007367 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007369
7370 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine2f107ae2024-02-28 01:26:46 +01007371 attributes->type,
7372 attributes->bits);
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007374 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 attributes, &key_buffer_size);
7376 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 }
Ronald Cron977c2472020-10-13 08:32:21 +02007379 }
Ronald Cron977c2472020-10-13 08:32:21 +02007380
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7382 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 }
Ronald Cron977c2472020-10-13 08:32:21 +02007385 }
7386
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 status = psa_driver_wrapper_generate_key(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007388 params, params_data_length,
Gilles Peskine7a18f962024-02-12 16:48:11 +01007389 slot->key.data, slot->key.bytes,
7390 &slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 if (status != PSA_SUCCESS) {
7392 psa_remove_key_data_from_memory(slot);
7393 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007394
Gilles Peskine11792082019-08-06 18:36:36 +02007395exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 if (status == PSA_SUCCESS) {
7397 status = psa_finish_key_creation(slot, driver, key);
7398 }
7399 if (status != PSA_SUCCESS) {
7400 psa_fail_key_creation(slot, driver);
7401 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007404}
7405
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007406psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7407 mbedtls_svc_key_id_t *key)
7408{
7409 return psa_generate_key_ext(attributes,
Gilles Peskine092ce512024-02-20 12:31:24 +01007410 &default_production_parameters, 0,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01007411 key);
7412}
7413
Gilles Peskinee59236f2018-01-27 23:32:46 +01007414/****************************************************************/
7415/* Module setup */
7416/****************************************************************/
7417
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007418#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007419psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 void (* entropy_init)(mbedtls_entropy_context *ctx),
7421 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007422{
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7424 return PSA_ERROR_BAD_STATE;
7425 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007426 global_data.rng.entropy_init = entropy_init;
7427 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007429}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007430#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007433{
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007435 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7436 mbedtls_psa_random_free(&global_data.rng);
7437 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007438 /* Wipe all remaining data, including configuration.
7439 * In particular, this sets all state indicator to the value
7440 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007442
7443 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007445}
7446
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007447#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7448/** Recover a transaction that was interrupted by a power failure.
7449 *
7450 * This function is called during initialization, before psa_crypto_init()
7451 * returns. If this function returns a failure status, the initialization
7452 * fails.
7453 */
7454static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007456{
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007458 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7459 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 /* TODO - fall through to the failure case until this
7461 * is implemented.
7462 * https://github.com/ARMmbed/mbed-crypto/issues/218
7463 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007464 default:
7465 /* We found an unsupported transaction in the storage.
7466 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007468 }
7469}
7470#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7471
Gilles Peskine449bd832023-01-11 14:50:10 +01007472psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007473{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007474 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007475
Gilles Peskinec6b69072018-11-20 21:42:52 +01007476 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (global_data.initialized != 0) {
7478 return PSA_SUCCESS;
7479 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007480
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007481 /* Init drivers */
7482 status = psa_driver_wrapper_init();
7483 if (status != PSA_SUCCESS) {
7484 goto exit;
7485 }
7486 global_data.drivers_initialized = 1;
7487
Valerio Setti402cfba2023-11-13 10:24:32 +01007488 status = psa_initialize_key_slots();
7489 if (status != PSA_SUCCESS) {
7490 goto exit;
7491 }
7492
Gilles Peskine30524eb2020-11-13 17:02:26 +01007493 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007495 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 status = mbedtls_psa_random_seed(&global_data.rng);
7497 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007498 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007500 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007501
Gilles Peskine4b734222019-07-24 15:56:31 +02007502#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 status = psa_crypto_load_transaction();
7504 if (status == PSA_SUCCESS) {
7505 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7506 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007507 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 }
7509 status = psa_crypto_stop_transaction();
7510 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007511 /* There's no transaction to complete. It's all good. */
7512 status = PSA_SUCCESS;
7513 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007514#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007515
Gilles Peskinec6b69072018-11-20 21:42:52 +01007516 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007517 global_data.initialized = 1;
7518
7519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 if (status != PSA_SUCCESS) {
7521 mbedtls_psa_crypto_free();
7522 }
7523 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007524}
7525
Yanray Wangffc3c482023-07-11 12:01:04 +08007526#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007527psa_status_t psa_crypto_driver_pake_get_password_len(
7528 const psa_crypto_driver_pake_inputs_t *inputs,
7529 size_t *password_len)
7530{
7531 if (inputs->password_len == 0) {
7532 return PSA_ERROR_BAD_STATE;
7533 }
7534
7535 *password_len = inputs->password_len;
7536
7537 return PSA_SUCCESS;
7538}
7539
7540psa_status_t psa_crypto_driver_pake_get_password(
7541 const psa_crypto_driver_pake_inputs_t *inputs,
7542 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7543{
7544 if (inputs->password_len == 0) {
7545 return PSA_ERROR_BAD_STATE;
7546 }
7547
7548 if (buffer_size < inputs->password_len) {
7549 return PSA_ERROR_BUFFER_TOO_SMALL;
7550 }
7551
7552 memcpy(buffer, inputs->password, inputs->password_len);
7553 *buffer_length = inputs->password_len;
7554
7555 return PSA_SUCCESS;
7556}
7557
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007558psa_status_t psa_crypto_driver_pake_get_user_len(
7559 const psa_crypto_driver_pake_inputs_t *inputs,
7560 size_t *user_len)
7561{
7562 if (inputs->user_len == 0) {
7563 return PSA_ERROR_BAD_STATE;
7564 }
7565
7566 *user_len = inputs->user_len;
7567
7568 return PSA_SUCCESS;
7569}
7570
7571psa_status_t psa_crypto_driver_pake_get_user(
7572 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007573 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007574{
7575 if (inputs->user_len == 0) {
7576 return PSA_ERROR_BAD_STATE;
7577 }
7578
Przemek Stekielc0e62502023-03-14 11:49:36 +01007579 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007580 return PSA_ERROR_BUFFER_TOO_SMALL;
7581 }
7582
Przemek Stekielc0e62502023-03-14 11:49:36 +01007583 memcpy(user_id, inputs->user, inputs->user_len);
7584 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007585
7586 return PSA_SUCCESS;
7587}
7588
7589psa_status_t psa_crypto_driver_pake_get_peer_len(
7590 const psa_crypto_driver_pake_inputs_t *inputs,
7591 size_t *peer_len)
7592{
7593 if (inputs->peer_len == 0) {
7594 return PSA_ERROR_BAD_STATE;
7595 }
7596
7597 *peer_len = inputs->peer_len;
7598
7599 return PSA_SUCCESS;
7600}
7601
7602psa_status_t psa_crypto_driver_pake_get_peer(
7603 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007604 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007605{
7606 if (inputs->peer_len == 0) {
7607 return PSA_ERROR_BAD_STATE;
7608 }
7609
Przemek Stekielc0e62502023-03-14 11:49:36 +01007610 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007611 return PSA_ERROR_BUFFER_TOO_SMALL;
7612 }
7613
Przemek Stekielc0e62502023-03-14 11:49:36 +01007614 memcpy(peer_id, inputs->peer, inputs->peer_len);
7615 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007616
7617 return PSA_SUCCESS;
7618}
7619
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007620psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7621 const psa_crypto_driver_pake_inputs_t *inputs,
7622 psa_pake_cipher_suite_t *cipher_suite)
7623{
7624 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7625 return PSA_ERROR_BAD_STATE;
7626 }
7627
7628 *cipher_suite = inputs->cipher_suite;
7629
7630 return PSA_SUCCESS;
7631}
7632
Neil Armstronga7d08c32022-06-01 18:21:20 +02007633psa_status_t psa_pake_setup(
7634 psa_pake_operation_t *operation,
7635 const psa_pake_cipher_suite_t *cipher_suite)
7636{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007637 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007638
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007639 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007640 status = PSA_ERROR_BAD_STATE;
7641 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007642 }
7643
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007644 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007645 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007646 status = PSA_ERROR_INVALID_ARGUMENT;
7647 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007648 }
7649
Przemek Stekiel51eac532022-12-07 11:04:51 +01007650 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7651
Przemek Stekiele12ed362022-12-21 12:54:46 +01007652 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007653 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7654 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007655 operation->data.inputs.cipher_suite = *cipher_suite;
7656
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007657#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007658 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007659 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007660 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007661
David Horstmann16f01512023-06-14 17:21:07 +01007662 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007663 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007664 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007665#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007666 {
7667 status = PSA_ERROR_NOT_SUPPORTED;
7668 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007669 }
7670
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007671 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7672
Przemek Stekiel51eac532022-12-07 11:04:51 +01007673 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007674exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007675 psa_pake_abort(operation);
7676 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007677}
7678
7679psa_status_t psa_pake_set_password_key(
7680 psa_pake_operation_t *operation,
7681 mbedtls_svc_key_id_t password)
7682{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007684 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7685 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007686 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007687
Przemek Stekiel51eac532022-12-07 11:04:51 +01007688 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007689 status = PSA_ERROR_BAD_STATE;
7690 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007691 }
7692
Przemek Stekiel6c764412022-11-22 14:05:12 +01007693 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7694 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007695 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007696 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007697 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007698 }
7699
Gilles Peskine7a5d9202024-02-28 01:18:23 +01007700 type = psa_get_key_type(&slot->attr);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007701
Przemek Stekiel51eac532022-12-07 11:04:51 +01007702 if (type != PSA_KEY_TYPE_PASSWORD &&
7703 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7704 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007705 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007706 }
7707
Przemek Stekiel51eac532022-12-07 11:04:51 +01007708 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7709 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007710 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007711 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007712 }
7713
7714 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7715 operation->data.inputs.password_len = slot->key.bytes;
Gilles Peskine7fad3ef2024-02-28 01:08:27 +01007716 operation->data.inputs.attributes = slot->attr;
7717
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007718exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007719 if (status != PSA_SUCCESS) {
7720 psa_pake_abort(operation);
7721 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007722 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007723 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007724}
7725
7726psa_status_t psa_pake_set_user(
7727 psa_pake_operation_t *operation,
7728 const uint8_t *user_id,
7729 size_t user_id_len)
7730{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007731 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007732
7733 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007734 status = PSA_ERROR_BAD_STATE;
7735 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007736 }
7737
Przemek Stekiel51eac532022-12-07 11:04:51 +01007738 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007739 status = PSA_ERROR_INVALID_ARGUMENT;
7740 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007741 }
7742
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007743 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007744 status = PSA_ERROR_BAD_STATE;
7745 goto exit;
7746 }
7747
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007748 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7749 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007750 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7751 goto exit;
7752 }
7753
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007754 memcpy(operation->data.inputs.user, user_id, user_id_len);
7755 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007756
7757 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007758exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007759 psa_pake_abort(operation);
7760 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007761}
7762
7763psa_status_t psa_pake_set_peer(
7764 psa_pake_operation_t *operation,
7765 const uint8_t *peer_id,
7766 size_t peer_id_len)
7767{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007769
7770 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007771 status = PSA_ERROR_BAD_STATE;
7772 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007773 }
7774
Przemek Stekiel51eac532022-12-07 11:04:51 +01007775 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007776 status = PSA_ERROR_INVALID_ARGUMENT;
7777 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007778 }
7779
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007780 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007781 status = PSA_ERROR_BAD_STATE;
7782 goto exit;
7783 }
7784
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007785 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7786 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007787 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7788 goto exit;
7789 }
7790
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007791 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7792 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007793
7794 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007795exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007796 psa_pake_abort(operation);
7797 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007798}
7799
7800psa_status_t psa_pake_set_role(
7801 psa_pake_operation_t *operation,
7802 psa_pake_role_t role)
7803{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007804 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007805
Przemek Stekiel51eac532022-12-07 11:04:51 +01007806 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007807 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007808 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007809 }
7810
Przemek Stekiel09104b82023-03-06 14:11:51 +01007811 switch (operation->alg) {
7812#if defined(PSA_WANT_ALG_JPAKE)
7813 case PSA_ALG_JPAKE:
7814 if (role == PSA_PAKE_ROLE_NONE) {
7815 return PSA_SUCCESS;
7816 }
7817 status = PSA_ERROR_INVALID_ARGUMENT;
7818 break;
7819#endif
7820 default:
7821 (void) role;
7822 status = PSA_ERROR_NOT_SUPPORTED;
7823 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007824 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007825exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007826 psa_pake_abort(operation);
7827 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007828}
7829
David Horstmanne7f21e62023-05-12 18:17:21 +01007830/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007831#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007832static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007833 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007834{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007835 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007836 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007837 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007838
David Horstmann024e5c52023-06-14 15:48:21 +01007839 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007840 is_x1 = (stage->outputs < 1);
7841 } else {
7842 is_x1 = (stage->inputs < 1);
7843 }
7844
David Horstmann74a3d8c2023-06-14 18:28:19 +01007845 key_share_step = is_x1 ?
7846 PSA_JPAKE_X1_STEP_KEY_SHARE :
7847 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007848 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007849 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7850 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7851 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7852 } else {
7853 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007854 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007855 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007856}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007857#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007858
Przemek Stekiel2797d372022-12-22 11:19:22 +01007859static psa_status_t psa_pake_complete_inputs(
7860 psa_pake_operation_t *operation)
7861{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007863 /* Create copy of the inputs on stack as inputs share memory
7864 with the driver context which will be setup by the driver. */
7865 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007866
Przemek Stekielfde11282023-03-13 16:06:09 +01007867 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007868 return PSA_ERROR_BAD_STATE;
7869 }
7870
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007871 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01007872 if (inputs.user_len == 0 || inputs.peer_len == 0) {
7873 return PSA_ERROR_BAD_STATE;
7874 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01007875 }
7876
Przemek Stekiel18620a32023-01-17 16:34:52 +01007877 /* Clear driver context */
7878 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
7879
7880 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007881
7882 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01007883 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01007884
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007885 /* User and peer are translated to role. */
7886 mbedtls_free(inputs.user);
7887 mbedtls_free(inputs.peer);
7888
Przemek Stekiel2797d372022-12-22 11:19:22 +01007889 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007890#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01007891 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01007892 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007893 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007894#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007895 {
7896 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007897 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007898 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01007899 return status;
7900}
7901
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007902#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01007903static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01007904 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01007905 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007906 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007907{
Przemek Stekiele12ed362022-12-21 12:54:46 +01007908 if (step != PSA_PAKE_STEP_KEY_SHARE &&
7909 step != PSA_PAKE_STEP_ZK_PUBLIC &&
7910 step != PSA_PAKE_STEP_ZK_PROOF) {
7911 return PSA_ERROR_INVALID_ARGUMENT;
7912 }
7913
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007914 psa_jpake_computation_stage_t *computation_stage =
7915 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916
David Horstmann5da95602023-06-08 15:37:12 +01007917 if (computation_stage->round != PSA_JPAKE_FIRST &&
7918 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007919 return PSA_ERROR_BAD_STATE;
7920 }
7921
David Horstmanne7f21e62023-05-12 18:17:21 +01007922 /* Check that the step we are given is the one we were expecting */
7923 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007924 return PSA_ERROR_BAD_STATE;
7925 }
7926
David Horstmanne7f21e62023-05-12 18:17:21 +01007927 if (step == PSA_PAKE_STEP_KEY_SHARE &&
7928 computation_stage->inputs == 0 &&
7929 computation_stage->outputs == 0) {
7930 /* Start of the round, so function decides whether we are inputting
7931 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01007932 computation_stage->io_mode = io_mode;
7933 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007934 /* Middle of the round so the mode we are in must match the function
7935 * called by the user */
7936 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007937 }
7938
7939 return PSA_SUCCESS;
7940}
7941
David Horstmanne7f21e62023-05-12 18:17:21 +01007942static psa_status_t psa_jpake_epilogue(
7943 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01007944 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007945{
David Horstmanne7f21e62023-05-12 18:17:21 +01007946 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007947 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007948
David Horstmanne7f21e62023-05-12 18:17:21 +01007949 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
7950 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01007951 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007952 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01007953 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01007954 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01007955 }
7956 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01007957 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007958 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01007959 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01007960 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01007961 }
7962 }
David Horstmann246ec5a2023-06-27 10:33:06 +01007963 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
7964 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007965 /* End of a round, move to the next round */
7966 stage->inputs = 0;
7967 stage->outputs = 0;
7968 stage->round++;
7969 }
7970 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01007971 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01007972 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007973 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01007974 return PSA_SUCCESS;
7975}
David Horstmanne7f21e62023-05-12 18:17:21 +01007976
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007977#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01007978
Neil Armstronga7d08c32022-06-01 18:21:20 +02007979psa_status_t psa_pake_output(
7980 psa_pake_operation_t *operation,
7981 psa_pake_step_t step,
7982 uint8_t *output,
7983 size_t output_size,
7984 size_t *output_length)
7985{
Przemek Stekiel51eac532022-12-07 11:04:51 +01007986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01007987 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007988 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007989
7990 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007991 status = psa_pake_complete_inputs(operation);
7992 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007994 }
7995 }
7996
7997 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007998 status = PSA_ERROR_BAD_STATE;
7999 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008000 }
8001
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008002 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003 status = PSA_ERROR_INVALID_ARGUMENT;
8004 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008005 }
8006
Przemek Stekiele12ed362022-12-21 12:54:46 +01008007 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008008#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008009 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008010 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008011 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008013 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008014 driver_step = convert_jpake_computation_stage_to_driver_step(
8015 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008016 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008017#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008018 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008019 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 status = PSA_ERROR_NOT_SUPPORTED;
8021 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008022 }
8023
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008024 status = psa_driver_wrapper_pake_output(operation, driver_step,
8025 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008026
8027 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008028 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008029 }
8030
8031 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008032#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008033 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008034 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008035 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008037 }
8038 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008039#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008040 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008041 status = PSA_ERROR_NOT_SUPPORTED;
8042 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008043 }
8044
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008045 return PSA_SUCCESS;
8046exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008047 psa_pake_abort(operation);
8048 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008049}
8050
8051psa_status_t psa_pake_input(
8052 psa_pake_operation_t *operation,
8053 psa_pake_step_t step,
8054 const uint8_t *input,
8055 size_t input_length)
8056{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008058 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008059 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8060 operation->primitive,
8061 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008062
8063 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008064 status = psa_pake_complete_inputs(operation);
8065 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008066 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008067 }
8068 }
8069
8070 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008071 status = PSA_ERROR_BAD_STATE;
8072 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008073 }
8074
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008075 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076 status = PSA_ERROR_INVALID_ARGUMENT;
8077 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008078 }
8079
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008081#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008082 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008083 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008084 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008085 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008086 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008087 driver_step = convert_jpake_computation_stage_to_driver_step(
8088 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008089 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008091 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008092 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008093 status = PSA_ERROR_NOT_SUPPORTED;
8094 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 }
8096
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008097 status = psa_driver_wrapper_pake_input(operation, driver_step,
8098 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099
8100 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008101 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008102 }
8103
8104 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008105#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008106 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008107 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008109 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008110 }
8111 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008112#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008114 status = PSA_ERROR_NOT_SUPPORTED;
8115 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008116 }
8117
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008118 return PSA_SUCCESS;
8119exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008120 psa_pake_abort(operation);
8121 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008122}
8123
8124psa_status_t psa_pake_get_implicit_key(
8125 psa_pake_operation_t *operation,
8126 psa_key_derivation_operation_t *output)
8127{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008128 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008130 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008131 size_t shared_key_len = 0;
8132
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008133 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 status = PSA_ERROR_BAD_STATE;
8135 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008136 }
8137
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008138#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008140 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008141 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008142 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 status = PSA_ERROR_BAD_STATE;
8144 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008145 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008146 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008147#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008148 {
8149 status = PSA_ERROR_NOT_SUPPORTED;
8150 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 }
8152
Przemek Stekiel0c781802022-11-29 14:53:13 +01008153 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8154 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008155 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008156 &shared_key_len);
8157
8158 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008159 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008160 }
8161
8162 status = psa_key_derivation_input_bytes(output,
8163 PSA_KEY_DERIVATION_INPUT_SECRET,
8164 shared_key,
8165 shared_key_len);
8166
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008167 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008168exit:
8169 abort_status = psa_pake_abort(operation);
8170 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008171}
8172
8173psa_status_t psa_pake_abort(
8174 psa_pake_operation_t *operation)
8175{
Przemek Stekield69dca92023-01-31 19:59:20 +01008176 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008177
Przemek Stekield69dca92023-01-31 19:59:20 +01008178 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008179 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008180 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008181
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008182 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8183 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008184 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008185 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008186 }
8187 if (operation->data.inputs.user != NULL) {
8188 mbedtls_free(operation->data.inputs.user);
8189 }
8190 if (operation->data.inputs.peer != NULL) {
8191 mbedtls_free(operation->data.inputs.peer);
8192 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008193 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008194 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008195
Przemek Stekield69dca92023-01-31 19:59:20 +01008196 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008197}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008198#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008199
Gilles Peskinee59236f2018-01-27 23:32:46 +01008200#endif /* MBEDTLS_PSA_CRYPTO_C */