blob: c8dd0d0c1f7b8d83e8748d64b0fb41b1befb6ba0 [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;
566 psa_key_type_t type = attributes->core.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 Peskine449bd832023-01-11 14:50:10 +0100576 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
577 *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
Valerio Settib2bcedb2023-07-10 11:24:00 +02001209#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001210 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001211static psa_status_t psa_get_rsa_public_exponent(
1212 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001214{
1215 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 uint8_t *buffer = NULL;
1218 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1222 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 }
1225 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001226 /* It's the default value, which is reported as an empty string,
1227 * so there's nothing to do. */
1228 goto exit;
1229 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 buflen = mbedtls_mpi_size(&mpi);
1232 buffer = mbedtls_calloc(1, buflen);
1233 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1235 goto exit;
1236 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1238 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001241 attributes->domain_parameters = buffer;
1242 attributes->domain_parameters_size = buflen;
1243
1244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 mbedtls_mpi_free(&mpi);
1246 if (ret != 0) {
1247 mbedtls_free(buffer);
1248 }
1249 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001250}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001251#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001252 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001253
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001254/** Retrieve all the publicly-accessible attributes of a key.
1255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001256psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1257 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001261 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1266 if (status != PSA_SUCCESS) {
1267 return status;
1268 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001269
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001270 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1272 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001273
1274#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1276 psa_set_key_slot_number(attributes,
1277 psa_key_slot_get_slot_number(slot));
1278 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001282#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1283 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001284 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001285 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001286 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001287 /* TODO: This is a temporary situation where domain parameters are deprecated,
1288 * but we need it for namely generating an RSA key with a non-default exponent.
1289 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001292 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001293
Ronald Cron90857082020-11-25 14:58:33 +01001294 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 slot->attr.type,
1296 slot->key.data,
1297 slot->key.bytes,
1298 &rsa);
1299 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001300 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 status = psa_get_rsa_public_exponent(rsa,
1304 attributes);
1305 mbedtls_rsa_free(rsa);
1306 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001307 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001308 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001309#else
1310 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1311 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1312 attributes->domain_parameters = NULL;
1313 attributes->domain_parameters_size = SIZE_MAX;
1314 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001315#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1316 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001317 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001318 default:
1319 /* Nothing else to do. */
1320 break;
1321 }
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (status != PSA_SUCCESS) {
1324 psa_reset_key_attributes(attributes);
1325 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326
Ryan Everetta103ec92024-01-31 13:59:57 +00001327 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
1331
Gilles Peskinec8000c02019-08-02 20:15:51 +02001332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1333psa_status_t psa_get_key_slot_number(
1334 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001336{
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001338 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 return PSA_SUCCESS;
1340 } else {
1341 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001342 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001343}
1344#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1347 size_t key_buffer_size,
1348 uint8_t *data,
1349 size_t data_size,
1350 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001351{
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (key_buffer_size > data_size) {
1353 return PSA_ERROR_BUFFER_TOO_SMALL;
1354 }
1355 memcpy(data, key_buffer, key_buffer_size);
1356 memset(data + key_buffer_size, 0,
1357 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001358 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001360}
1361
Ronald Cron7285cda2020-11-26 14:46:37 +01001362psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001363 const psa_key_attributes_t *attributes,
1364 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366{
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 psa_key_type_t type = attributes->core.type;
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (key_type_is_raw_bytes(type) ||
1370 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001371 PSA_KEY_TYPE_IS_ECC(type) ||
1372 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return psa_export_key_buffer_internal(
1374 key_buffer, key_buffer_size,
1375 data, data_size, data_length);
1376 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001377 /* This shouldn't happen in the reference implementation, but
1378 it is valid for a special-purpose implementation to omit
1379 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001381 }
1382}
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1385 uint8_t *data,
1386 size_t data_size,
1387 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388{
1389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1390 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1391 psa_key_slot_t *slot;
1392
Ronald Crone907e552021-01-18 13:22:38 +01001393 /* Reject a zero-length output buffer now, since this can never be a
1394 * valid key representation. This way we know that data must be a valid
1395 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (data_size == 0) {
1397 return PSA_ERROR_BUFFER_TOO_SMALL;
1398 }
Ronald Crone907e552021-01-18 13:22:38 +01001399
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400 /* Set the key to empty now, so that even when there are errors, we always
1401 * set data_length to a value between 0 and data_size. On error, setting
1402 * the key to empty is a good choice because an empty key representation is
1403 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001404 *data_length = 0;
1405
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406 /* Export requires the EXPORT flag. There is an exception for public keys,
1407 * which don't require any flag, but
1408 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1411 PSA_KEY_USAGE_EXPORT, 0);
1412 if (status != PSA_SUCCESS) {
1413 return status;
1414 }
mohammad160306e79202018-03-28 13:17:44 +03001415
Ronald Crond18b5f82020-11-25 16:46:05 +01001416 psa_key_attributes_t attributes = {
1417 .core = slot->attr
1418 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 status = psa_driver_wrapper_export_key(&attributes,
1420 slot->key.data, slot->key.bytes,
1421 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001422
Ryan Everetta103ec92024-01-31 13:59:57 +00001423 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426}
1427
Ronald Cron7285cda2020-11-26 14:46:37 +01001428psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001429 const psa_key_attributes_t *attributes,
1430 const uint8_t *key_buffer,
1431 size_t key_buffer_size,
1432 uint8_t *data,
1433 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001435{
Ronald Crond18b5f82020-11-25 16:46:05 +01001436 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001437
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001438 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001439 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1440 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001441 /* Exporting public -> public */
1442 return psa_export_key_buffer_internal(
1443 key_buffer, key_buffer_size,
1444 data, data_size, data_length);
1445 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001447 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1448 return mbedtls_psa_rsa_export_public_key(attributes,
1449 key_buffer,
1450 key_buffer_size,
1451 data,
1452 data_size,
1453 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001454#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001455 /* We don't know how to convert a private RSA key to public. */
1456 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001459 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001460#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001461 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1462 return mbedtls_psa_ecp_export_public_key(attributes,
1463 key_buffer,
1464 key_buffer_size,
1465 data,
1466 data_size,
1467 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001468#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001469 /* We don't know how to convert a private ECC key to public */
1470 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001471#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001472 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001473 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001474#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001475 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001476 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001477 key_buffer,
1478 key_buffer_size,
1479 data, data_size,
1480 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001481#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001482 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001486 (void) key_buffer;
1487 (void) key_buffer_size;
1488 (void) data;
1489 (void) data_size;
1490 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001492 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001493}
Ronald Crond18b5f82020-11-25 16:46:05 +01001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1496 uint8_t *data,
1497 size_t data_size,
1498 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001499{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001500 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001501 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001502 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001503 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001504
Ronald Crone907e552021-01-18 13:22:38 +01001505 /* Reject a zero-length output buffer now, since this can never be a
1506 * valid key representation. This way we know that data must be a valid
1507 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (data_size == 0) {
1509 return PSA_ERROR_BUFFER_TOO_SMALL;
1510 }
Ronald Crone907e552021-01-18 13:22:38 +01001511
Darryl Greendd8fb772018-11-07 16:00:44 +00001512 /* Set the key to empty now, so that even when there are errors, we always
1513 * set data_length to a value between 0 and data_size. On error, setting
1514 * the key to empty is a good choice because an empty key representation is
1515 * unlikely to be accepted anywhere. */
1516 *data_length = 0;
1517
1518 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1520 if (status != PSA_SUCCESS) {
1521 return status;
1522 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1525 status = PSA_ERROR_INVALID_ARGUMENT;
1526 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527 }
1528
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001529 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001530 .core = slot->attr
1531 };
Ronald Cron67227982020-11-26 15:16:05 +01001532 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001533 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001535
1536exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00001537 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001540}
1541
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001542MBEDTLS_STATIC_ASSERT(
1543 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1544 "One or more key attribute flag is listed as both external-only and dual-use")
1545MBEDTLS_STATIC_ASSERT(
1546 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1547 "One or more key attribute flag is listed as both internal-only and dual-use")
1548MBEDTLS_STATIC_ASSERT(
1549 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1550 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001551
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552/** Validate that a key policy is internally well-formed.
1553 *
1554 * This function only rejects invalid policies. It does not validate the
1555 * consistency of the policy with respect to other attributes of the key
1556 * such as the key type.
1557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001559{
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1561 PSA_KEY_USAGE_COPY |
1562 PSA_KEY_USAGE_ENCRYPT |
1563 PSA_KEY_USAGE_DECRYPT |
1564 PSA_KEY_USAGE_SIGN_MESSAGE |
1565 PSA_KEY_USAGE_VERIFY_MESSAGE |
1566 PSA_KEY_USAGE_SIGN_HASH |
1567 PSA_KEY_USAGE_VERIFY_HASH |
1568 PSA_KEY_USAGE_VERIFY_DERIVATION |
1569 PSA_KEY_USAGE_DERIVE)) != 0) {
1570 return PSA_ERROR_INVALID_ARGUMENT;
1571 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001574}
1575
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001576/** Validate the internal consistency of key attributes.
1577 *
1578 * This function only rejects invalid attribute values. If does not
1579 * validate the consistency of the attributes with any key data that may
1580 * be involved in the creation of the key.
1581 *
1582 * Call this function early in the key creation process.
1583 *
1584 * \param[in] attributes Key attributes for the new key.
1585 * \param[out] p_drv On any return, the driver for the key, if any.
1586 * NULL for a transparent key.
1587 *
1588 */
1589static psa_status_t psa_validate_key_attributes(
1590 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001592{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001593 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1595 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 status = psa_validate_key_location(lifetime, p_drv);
1598 if (status != PSA_SUCCESS) {
1599 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001600 }
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 status = psa_validate_key_persistence(lifetime);
1603 if (status != PSA_SUCCESS) {
1604 return status;
1605 }
1606
1607 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1608 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1609 return PSA_ERROR_INVALID_ARGUMENT;
1610 }
1611 } else {
1612 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1613 return PSA_ERROR_INVALID_ARGUMENT;
1614 }
1615 }
1616
1617 status = psa_validate_key_policy(&attributes->core.policy);
1618 if (status != PSA_SUCCESS) {
1619 return status;
1620 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001621
1622 /* Refuse to create overly large keys.
1623 * Note that this doesn't trigger on import if the attributes don't
1624 * explicitly specify a size (so psa_get_key_bits returns 0), so
1625 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1627 return PSA_ERROR_NOT_SUPPORTED;
1628 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001629
Gilles Peskine91e8c332019-08-02 19:19:39 +02001630 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1632 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1633 return PSA_ERROR_INVALID_ARGUMENT;
1634 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001637}
1638
Gilles Peskine4747d192019-04-17 15:05:45 +02001639/** Prepare a key slot to receive key material.
1640 *
1641 * This function allocates a key slot and sets its metadata.
1642 *
1643 * If this function fails, call psa_fail_key_creation().
1644 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001645 * This function is intended to be used as follows:
1646 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647 * it with the specified attributes, and in case of a volatile key assign it
1648 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 * -# Populate the slot with the key material.
1650 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1651 * In case of failure at any step, stop the sequence and call
1652 * psa_fail_key_creation().
1653 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001654 * On success, the key slot's state is PSA_SLOT_FILLING.
1655 * It is the responsibility of the caller to change the slot's state to
1656 * PSA_SLOT_EMPTY/FULL once key creation has finished.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001657 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001658 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001660 * \param[out] p_slot On success, a pointer to the prepared slot.
1661 * \param[out] p_drv On any return, the driver for the key, if any.
1662 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001663 *
1664 * \retval #PSA_SUCCESS
1665 * The key slot is ready to receive key material.
1666 * \return If this function fails, the key slot is an invalid state.
1667 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 */
1669static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001670 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001672 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001674{
1675 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001676 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 psa_key_slot_t *slot;
1678
Gilles Peskinedf179142019-07-15 22:02:14 +02001679 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001680 *p_drv = NULL;
1681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 status = psa_validate_key_attributes(attributes, p_drv);
1683 if (status != PSA_SUCCESS) {
1684 return status;
1685 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001686
Ryan Everett3d8118d2024-01-30 16:58:47 +00001687#if defined(MBEDTLS_THREADING_C)
1688 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1689 &mbedtls_threading_key_slot_mutex));
1690#endif
Ryan Everettb69118e2024-01-02 15:54:32 +00001691 status = psa_reserve_free_key_slot(&volatile_key_id, p_slot);
Ryan Everett3d8118d2024-01-30 16:58:47 +00001692#if defined(MBEDTLS_THREADING_C)
1693 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1694 &mbedtls_threading_key_slot_mutex));
1695#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (status != PSA_SUCCESS) {
1697 return status;
1698 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 slot = *p_slot;
1700
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001701 /* We're storing the declared bit-size of the key. It's up to each
1702 * creation mechanism to verify that this information is correct.
1703 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001704 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001705 * is optional (import, copy). In case of a volatile key, assign it the
1706 * volatile key identifier associated to the slot returned to contain its
1707 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001708
1709 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001711#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1712 slot->attr.id = volatile_key_id;
1713#else
1714 slot->attr.id.key_id = volatile_key_id;
1715#endif
1716 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001719 * external-only flags, query `attributes`. Thanks to the check
1720 * in psa_validate_key_attributes(), this leaves the dual-use
Ryan Everettb69118e2024-01-02 15:54:32 +00001721 * flags and any internal flag that psa_reserve_free_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001722 * may have set. */
1723 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001724
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001726 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001728 * create the key file in internal storage, create the
1729 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001730 * persistent data. This is done by starting a transaction that will
1731 * encompass these three actions.
1732 * For registering a volatile key, we just need to find an appropriate
1733 * slot number inside the SE. Since the key is designated volatile, creating
1734 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001735 /* The first thing to do is to find a slot number for the new key.
1736 * We save the slot number in persistent storage as part of the
1737 * transaction data. It will be needed to recover if the power
1738 * fails during the key creation process, to clean up on the secure
1739 * element side after restarting. Obtaining a slot number from the
1740 * secure element driver updates its persistent state, but we do not yet
1741 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001742 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001744 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1746 &slot_number);
1747 if (status != PSA_SUCCESS) {
1748 return status;
1749 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1752 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001753 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001754 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001755 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 status = psa_crypto_save_transaction();
1757 if (status != PSA_SUCCESS) {
1758 (void) psa_crypto_stop_transaction();
1759 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001760 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001761 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001762
1763 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001765 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001768 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001770 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001774}
Gilles Peskine4747d192019-04-17 15:05:45 +02001775
1776/** Finalize the creation of a key once its key material has been set.
1777 *
1778 * This entails writing the key to persistent storage.
1779 *
1780 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001781 * See the documentation of psa_start_key_creation() for the intended use
1782 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 *
Ryan Everettb69118e2024-01-02 15:54:32 +00001784 * If the finalization succeeds, the function sets the key slot's state to
1785 * PSA_SLOT_FULL, and the key slot can no longer be accessed as part of the
1786 * key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001787 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 * \param[in,out] slot Pointer to the slot with key material.
1789 * \param[in] driver The secure element driver for the key,
1790 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001791 * \param[out] key On success, identifier of the key. Note that the
1792 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001793 *
1794 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001795 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1797 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1798 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1799 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1800 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1801 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001802 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001803 * \return If this function fails, the key slot is an invalid state.
1804 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001806static psa_status_t psa_finish_key_creation(
1807 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001808 psa_se_drv_table_entry_t *driver,
1809 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001810{
1811 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001812 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001813 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001814
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001815#if defined(MBEDTLS_THREADING_C)
1816 PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1817 &mbedtls_threading_key_slot_mutex));
1818#endif
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001824 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001825 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001827
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001828 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1829 sizeof(data.slot_number),
1830 "Slot number size does not match psa_se_key_data_storage_t");
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1833 status = psa_save_persistent_key(&slot->attr,
1834 (uint8_t *) &data,
1835 sizeof(data));
1836 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001837#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1838 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001839 /* Key material is saved in export representation in the slot, so
1840 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_save_persistent_key(&slot->attr,
1842 slot->key.data,
1843 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001844 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001846#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1847
Gilles Peskinecbaff462019-07-12 23:46:04 +02001848#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001849 /* Finish the transaction for a key creation. This does not
1850 * happen when registering an existing key. Detect this case
1851 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001852 * creation of a persistent key in a secure element requires a transaction,
1853 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (driver != NULL &&
1855 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1856 status = psa_save_se_persistent_data(driver);
1857 if (status != PSA_SUCCESS) {
1858 psa_destroy_persistent_key(slot->attr.id);
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001859
1860#if defined(MBEDTLS_THREADING_C)
1861 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1862 &mbedtls_threading_key_slot_mutex));
1863#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001867 }
1868#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001871 *key = slot->attr.id;
Ryan Everettb69118e2024-01-02 15:54:32 +00001872 status = psa_key_slot_state_transition(slot, PSA_SLOT_FILLING,
1873 PSA_SLOT_FULL);
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001875 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001877 }
Ronald Cron50972942020-11-14 11:28:25 +01001878
Ryan Everett91ffe5b2024-01-23 20:05:42 +00001879#if defined(MBEDTLS_THREADING_C)
1880 PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1881 &mbedtls_threading_key_slot_mutex));
1882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001884}
1885
1886/** Abort the creation of a key.
1887 *
1888 * You may call this function after calling psa_start_key_creation(),
1889 * or after psa_finish_key_creation() fails. In other circumstances, this
1890 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001891 * See the documentation of psa_start_key_creation() for the intended use
Ryan Everettb69118e2024-01-02 15:54:32 +00001892 * of this function. Sets the slot's state to PSA_SLOT_EMPTY.
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 * \param[in,out] slot Pointer to the slot with key material.
1895 * \param[in] driver The secure element driver for the key,
1896 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001898static void psa_fail_key_creation(psa_key_slot_t *slot,
1899 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001900{
Gilles Peskine011e4282019-06-26 18:34:38 +02001901 (void) driver;
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001906
Ryan Everettb7101442024-01-23 20:09:49 +00001907#if defined(MBEDTLS_THREADING_C)
Ryan Everett73feaf22024-02-14 11:36:41 +00001908 /* If the lock operation fails we still wipe the slot.
1909 * Operations will no longer work after a failed lock,
1910 * but we still need to wipe the slot of confidential data. */
Ryan Everettb7101442024-01-23 20:09:49 +00001911 mbedtls_mutex_lock(&mbedtls_threading_key_slot_mutex);
1912#endif
1913
Gilles Peskine011e4282019-06-26 18:34:38 +02001914#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001915 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001916 * element, and the failure happened later (when saving metadata
1917 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001918 * element.
1919 * https://github.com/ARMmbed/mbed-crypto/issues/217
1920 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001921
Gilles Peskined7729582019-08-05 15:55:54 +02001922 /* Abort the ongoing transaction if any (there may not be one if
1923 * the creation process failed before starting one, or if the
1924 * key creation is a registration of a key in a secure element).
1925 * Earlier functions must already have done what it takes to undo any
1926 * partial creation. All that's left is to update the transaction data
1927 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001929#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 psa_wipe_key_slot(slot);
Ryan Everettb7101442024-01-23 20:09:49 +00001932
1933#if defined(MBEDTLS_THREADING_C)
1934 mbedtls_mutex_unlock(&mbedtls_threading_key_slot_mutex);
1935#endif
Gilles Peskine4747d192019-04-17 15:05:45 +02001936}
1937
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001938/** Validate optional attributes during key creation.
1939 *
1940 * Some key attributes are optional during key creation. If they are
1941 * specified in the attributes structure, check that they are consistent
1942 * with the data in the slot.
1943 *
1944 * This function should be called near the end of key creation, after
1945 * the slot in memory is fully populated but before saving persistent data.
1946 */
1947static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950{
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if (attributes->core.type != 0) {
1952 if (attributes->core.type != slot->attr.type) {
1953 return PSA_ERROR_INVALID_ARGUMENT;
1954 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001958#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1959 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1961 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001962 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001963 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001965
Ronald Cron90857082020-11-25 14:58:33 +01001966 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 slot->attr.type,
1968 slot->key.data,
1969 slot->key.bytes,
1970 &rsa);
1971 if (status != PSA_SUCCESS) {
1972 return status;
1973 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_mpi_init(&actual);
1976 mbedtls_mpi_init(&required);
1977 ret = mbedtls_rsa_export(rsa,
1978 NULL, NULL, NULL, NULL, &actual);
1979 mbedtls_rsa_free(rsa);
1980 mbedtls_free(rsa);
1981 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
1984 ret = mbedtls_mpi_read_binary(&required,
1985 attributes->domain_parameters,
1986 attributes->domain_parameters_size);
1987 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
1990 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
1993rsa_exit:
1994 mbedtls_mpi_free(&actual);
1995 mbedtls_mpi_free(&required);
1996 if (ret != 0) {
1997 return mbedtls_to_psa_error(ret);
1998 }
1999 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002000#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2001 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002002 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005 }
2006 }
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (attributes->core.bits != 0) {
2009 if (attributes->core.bits != slot->attr.bits) {
2010 return PSA_ERROR_INVALID_ARGUMENT;
2011 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012 }
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015}
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2018 const uint8_t *data,
2019 size_t data_length,
2020 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002021{
2022 psa_status_t status;
2023 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002024 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302026 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002027
Ronald Cron81709fc2020-11-14 12:10:32 +01002028 *key = MBEDTLS_SVC_KEY_ID_INIT;
2029
Gilles Peskine0f84d622019-09-12 19:03:13 +02002030 /* Reject zero-length symmetric keys (including raw data key objects).
2031 * This also rejects any key which might be encoded as an empty string,
2032 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (data_length == 0) {
2034 return PSA_ERROR_INVALID_ARGUMENT;
2035 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002036
Archana449608b2021-09-08 15:36:05 +05302037 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (data_length > SIZE_MAX / 8) {
2039 return PSA_ERROR_NOT_SUPPORTED;
2040 }
Archana6ed4bda2021-08-04 10:47:15 +05302041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2043 &slot, &driver);
2044 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002045 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002048 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302049 * storage ( thus not in the case of importing a key in a secure element
2050 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2051 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (slot->key.data == NULL) {
2053 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302054 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 attributes, data, data_length, &storage_size);
2056 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302057 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
Archanad8a83dc2021-06-14 10:04:16 +05302059 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 status = psa_allocate_buffer_to_slot(slot, storage_size);
2061 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002064 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002065
2066 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_driver_wrapper_import_key(attributes,
2068 data, data_length,
2069 slot->key.data,
2070 slot->key.bytes,
2071 &slot->key.bytes, &bits);
2072 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002077 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002079 status = PSA_ERROR_INVALID_ARGUMENT;
2080 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002081 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002082
Archana6ed4bda2021-08-04 10:47:15 +05302083 /* Enforce a size limit, and in particular ensure that the bit
2084 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302086 status = PSA_ERROR_NOT_SUPPORTED;
2087 goto exit;
2088 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 status = psa_validate_optional_attributes(slot, attributes);
2090 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (status != PSA_SUCCESS) {
2097 psa_fail_key_creation(slot, driver);
2098 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002101}
2102
Gilles Peskined7729582019-08-05 15:55:54 +02002103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2104psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002106{
2107 psa_status_t status;
2108 psa_key_slot_t *slot = NULL;
2109 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002111
2112 /* Leaving attributes unspecified is not currently supported.
2113 * It could make sense to query the key type and size from the
2114 * secure element, but not all secure elements support this
2115 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2117 return PSA_ERROR_NOT_SUPPORTED;
2118 }
2119 if (psa_get_key_bits(attributes) == 0) {
2120 return PSA_ERROR_NOT_SUPPORTED;
2121 }
Gilles Peskined7729582019-08-05 15:55:54 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2124 &slot, &driver);
2125 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Gilles Peskined7729582019-08-05 15:55:54 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002130
2131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (status != PSA_SUCCESS) {
2133 psa_fail_key_creation(slot, driver);
2134 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002135
Gilles Peskined7729582019-08-05 15:55:54 +02002136 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 psa_close_key(key);
2138 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002139}
2140#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2143 const psa_key_attributes_t *specified_attributes,
2144 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002145{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002146 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002147 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 psa_key_slot_t *source_slot = NULL;
2149 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002150 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002151 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302152 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002153
Ronald Cron81709fc2020-11-14 12:10:32 +01002154 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2155
Archana8a180362021-07-05 02:18:48 +05302156 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2158 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 status = psa_validate_optional_attributes(source_slot,
2163 specified_attributes);
2164 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002167
Archana9d17bf42021-09-10 06:22:44 +05302168 /* The target key type and number of bits have been validated by
2169 * psa_validate_optional_attributes() to be either equal to zero or
2170 * equal to the ones of the source key. So it is safe to inherit
2171 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302172 * */
Archana9d17bf42021-09-10 06:22:44 +05302173 actual_attributes.core.bits = source_slot->attr.bits;
2174 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302175
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 status = psa_restrict_key_policy(source_slot->attr.type,
2178 &actual_attributes.core.policy,
2179 &source_slot->attr.policy);
2180 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2185 &target_slot, &driver);
2186 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 }
2189 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2190 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302191 /*
Archana9d17bf42021-09-10 06:22:44 +05302192 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302193 * the source key would need to be exported as plaintext and re-imported
2194 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302195 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302196 * appropriate API invocations from the application, if needed.
2197 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002198 status = PSA_ERROR_NOT_SUPPORTED;
2199 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002200 }
Archana8a180362021-07-05 02:18:48 +05302201 /*
2202 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302203 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302204 * - For opaque keys this translates to an invocation of the drivers'
2205 * copy_key entry point through the dispatch layer.
2206 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2208 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2209 &storage_size);
2210 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302211 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 }
Archana374fe5b2021-09-08 15:50:28 +05302213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2215 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 }
Archana374fe5b2021-09-08 15:50:28 +05302218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 status = psa_driver_wrapper_copy_key(&actual_attributes,
2220 source_slot->key.data,
2221 source_slot->key.bytes,
2222 target_slot->key.data,
2223 target_slot->key.bytes,
2224 &target_slot->key.bytes);
2225 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
2228 } else {
2229 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302230 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 source_slot->key.bytes);
2232 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 }
Archana8a180362021-07-05 02:18:48 +05302235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (status != PSA_SUCCESS) {
2239 psa_fail_key_creation(target_slot, driver);
2240 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002241
Ryan Everetta103ec92024-01-31 13:59:57 +00002242 unlock_status = psa_unregister_read_under_mutex(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002245}
2246
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002247
2248
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002249/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002250/* Message digests */
2251/****************************************************************/
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002255 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (operation->id == 0) {
2257 return PSA_SUCCESS;
2258 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002261 operation->id = 0;
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264}
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2267 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002269 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002270
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002271 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273 status = PSA_ERROR_BAD_STATE;
2274 goto exit;
2275 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002278 status = PSA_ERROR_INVALID_ARGUMENT;
2279 goto exit;
2280 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002282 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2283 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002287
2288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (status != PSA_SUCCESS) {
2290 psa_hash_abort(operation);
2291 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002292
2293 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294}
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2297 const uint8_t *input,
2298 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 status = PSA_ERROR_BAD_STATE;
2304 goto exit;
2305 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002306
Steven Cooremanc8288352021-03-04 14:02:19 +01002307 /* Don't require hash implementations to behave correctly on a
2308 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (input_length == 0) {
2310 return PSA_SUCCESS;
2311 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002314
2315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (status != PSA_SUCCESS) {
2317 psa_hash_abort(operation);
2318 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321}
2322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2324 uint8_t *hash,
2325 size_t hash_size,
2326 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002328 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (operation->id == 0) {
2330 return PSA_ERROR_BAD_STATE;
2331 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332
Steven Cooreman1e582352021-02-18 17:24:37 +01002333 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 operation, hash, hash_size, hash_length);
2335 psa_hash_abort(operation);
2336 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337}
2338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2340 const uint8_t *hash,
2341 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342{
Ronald Cron69a63422021-10-18 09:47:58 +02002343 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002345 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 operation,
2347 actual_hash, sizeof(actual_hash),
2348 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002351 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002355 status = PSA_ERROR_INVALID_SIGNATURE;
2356 goto exit;
2357 }
2358
Dave Rodgman78701152023-08-29 14:20:18 +01002359 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002360 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002362
2363exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2365 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002366 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370}
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372psa_status_t psa_hash_compute(psa_algorithm_t alg,
2373 const uint8_t *input, size_t input_length,
2374 uint8_t *hash, size_t hash_size,
2375 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002376{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002377 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (!PSA_ALG_IS_HASH(alg)) {
2379 return PSA_ERROR_INVALID_ARGUMENT;
2380 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2383 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002384}
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386psa_status_t psa_hash_compare(psa_algorithm_t alg,
2387 const uint8_t *input, size_t input_length,
2388 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002389{
Ronald Cron69a63422021-10-18 09:47:58 +02002390 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002391 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (!PSA_ALG_IS_HASH(alg)) {
2394 return PSA_ERROR_INVALID_ARGUMENT;
2395 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002396
Steven Cooreman1e582352021-02-18 17:24:37 +01002397 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 alg, input, input_length,
2399 actual_hash, sizeof(actual_hash),
2400 &actual_hash_length);
2401 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002402 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 }
2404 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002405 status = PSA_ERROR_INVALID_SIGNATURE;
2406 goto exit;
2407 }
Dave Rodgman78701152023-08-29 14:20:18 +01002408 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002409 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002411
2412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2414 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2418 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002419{
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (source_operation->id == 0 ||
2421 target_operation->id != 0) {
2422 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002423 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2426 target_operation);
2427 if (status != PSA_SUCCESS) {
2428 psa_hash_abort(target_operation);
2429 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002432}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433
2434
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436/* MAC */
2437/****************************************************************/
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002440{
Steven Cooremane6804192021-03-19 18:28:56 +01002441 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (operation->id == 0) {
2443 return PSA_SUCCESS;
2444 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002447 operation->mac_size = 0;
2448 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002449 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002452}
2453
Ronald Cron2dff3b22021-06-17 16:33:22 +02002454static psa_status_t psa_mac_finalize_alg_and_key_validation(
2455 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002456 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002459 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 psa_key_type_t key_type = psa_get_key_type(attributes);
2461 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (!PSA_ALG_IS_MAC(alg)) {
2464 return PSA_ERROR_INVALID_ARGUMENT;
2465 }
Steven Cooremane6804192021-03-19 18:28:56 +01002466
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002467 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 status = psa_mac_key_can_do(alg, key_type);
2469 if (status != PSA_SUCCESS) {
2470 return status;
2471 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002472
Steven Cooremand1a68f12021-05-10 11:13:41 +02002473 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002477 /* A very short MAC is too short for security since it can be
2478 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2479 * so we make this our minimum, even though 32 bits is still
2480 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002482 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2485 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002486 /* It's impossible to "truncate" to a larger length than the full length
2487 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002489 }
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002492 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2493 * that is disabled in the compile-time configuration. The result can
2494 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2495 * configuration into account. In this case, force a return of
2496 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2497 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2498 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2499 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2500 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002502 }
2503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002505}
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2508 mbedtls_svc_key_id_t key,
2509 psa_algorithm_t alg,
2510 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002511{
2512 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2513 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002514 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002515 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516
2517 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002519 status = PSA_ERROR_BAD_STATE;
2520 goto exit;
2521 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522
2523 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 key,
2525 &slot,
2526 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2527 alg);
2528 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002532 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533 .core = slot->attr
2534 };
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2537 &operation->mac_size);
2538 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541
2542 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002543 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (is_sign) {
2545 status = psa_driver_wrapper_mac_sign_setup(operation,
2546 &attributes,
2547 slot->key.data,
2548 slot->key.bytes,
2549 alg);
2550 } else {
2551 status = psa_driver_wrapper_mac_verify_setup(operation,
2552 &attributes,
2553 slot->key.data,
2554 slot->key.bytes,
2555 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002557
Gilles Peskinefbfac682018-07-08 20:51:54 +02002558exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (status != PSA_SUCCESS) {
2560 psa_mac_abort(operation);
2561 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002562
Ryan Everettfb9857f2024-02-14 12:16:41 +00002563 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566}
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2569 mbedtls_svc_key_id_t key,
2570 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002571{
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002573}
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2576 mbedtls_svc_key_id_t key,
2577 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002578{
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002580}
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2583 const uint8_t *input,
2584 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585{
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (operation->id == 0) {
2587 return PSA_ERROR_BAD_STATE;
2588 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002590 /* Don't require hash implementations to behave correctly on a
2591 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (input_length == 0) {
2593 return PSA_SUCCESS;
2594 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2597 input, input_length);
2598 if (status != PSA_SUCCESS) {
2599 psa_mac_abort(operation);
2600 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603}
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2606 uint8_t *mac,
2607 size_t mac_size,
2608 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002609{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2611 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002614 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002615 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002616 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002619 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002620 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002621 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002622
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002623 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2624 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002626 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002627 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002628 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002631 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002632 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002633 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 status = psa_driver_wrapper_mac_sign_finish(operation,
2636 mac, operation->mac_size,
2637 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002638
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002639exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002640 /* In case of success, set the potential excess room in the output buffer
2641 * to an invalid value, to avoid potentially leaking a longer MAC.
2642 * In case of error, set the output length and content to a safe default,
2643 * such that in case the caller misses an error check, the output would be
2644 * an unachievable MAC.
2645 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002647 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002648 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002649 }
mohammad16036df908f2018-04-02 08:34:15 -07002650
Paul Elliottdc42ca82023-02-24 18:11:59 +00002651 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002656}
2657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2659 const uint8_t *mac,
2660 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002662 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2663 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002666 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002667 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002668 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002671 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002672 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002673 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002676 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002677 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002678 }
2679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 status = psa_driver_wrapper_mac_verify_finish(operation,
2681 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002682
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687}
2688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2690 psa_algorithm_t alg,
2691 const uint8_t *input,
2692 size_t input_length,
2693 uint8_t *mac,
2694 size_t mac_size,
2695 size_t *mac_length,
2696 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002697{
2698 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002699 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2700 psa_key_slot_t *slot;
2701 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002702 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002703
Ronald Cron51131b52021-06-17 17:17:20 +02002704 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 key,
2706 &slot,
2707 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2708 alg);
2709 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002712
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002713 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002714 .core = slot->attr
2715 };
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2718 &operation_mac_size);
2719 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002724 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002726 }
2727
2728 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 &attributes,
2730 slot->key.data, slot->key.bytes,
2731 alg,
2732 input, input_length,
2733 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002734
2735exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002736 /* In case of success, set the potential excess room in the output buffer
2737 * to an invalid value, to avoid potentially leaking a longer MAC.
2738 * In case of error, set the output length and content to a safe default,
2739 * such that in case the caller misses an error check, the output would be
2740 * an unachievable MAC.
2741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002743 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002744 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002745 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002746
2747 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002748
Ryan Everetta103ec92024-01-31 13:59:57 +00002749 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002752}
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002755 psa_algorithm_t alg,
2756 const uint8_t *input,
2757 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 uint8_t *mac,
2759 size_t mac_size,
2760 size_t *mac_length)
2761{
2762 return psa_mac_compute_internal(key, alg,
2763 input, input_length,
2764 mac, mac_size, mac_length, 1);
2765}
2766
2767psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2768 psa_algorithm_t alg,
2769 const uint8_t *input,
2770 size_t input_length,
2771 const uint8_t *mac,
2772 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002773{
2774 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002775 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2776 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 status = psa_mac_compute_internal(key, alg,
2779 input, input_length,
2780 actual_mac, sizeof(actual_mac),
2781 &actual_mac_length, 0);
2782 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002783 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002787 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002788 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002789 }
Dave Rodgman78701152023-08-29 14:20:18 +01002790 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002791 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002793 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002794
2795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002799}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002800
Gilles Peskinee59236f2018-01-27 23:32:46 +01002801/****************************************************************/
2802/* Asymmetric cryptography */
2803/****************************************************************/
2804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2806 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002807{
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (input_is_message) {
2809 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2810 return PSA_ERROR_INVALID_ARGUMENT;
2811 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2814 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2815 return PSA_ERROR_INVALID_ARGUMENT;
2816 }
2817 }
2818 } else {
2819 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2820 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002821 }
2822 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002825}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2828 int input_is_message,
2829 psa_algorithm_t alg,
2830 const uint8_t *input,
2831 size_t input_length,
2832 uint8_t *signature,
2833 size_t signature_size,
2834 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002835{
2836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2837 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2838 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002839 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002840
2841 *signature_length = 0;
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 status = psa_sign_verify_check_alg(input_is_message, alg);
2844 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002845 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002847
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002848 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002849 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002850 * buffer can in principle be empty since it doesn't actually have
2851 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (signature_size == 0) {
2853 return PSA_ERROR_BUFFER_TOO_SMALL;
2854 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002855
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002856 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 key, &slot,
2858 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2859 PSA_KEY_USAGE_SIGN_HASH,
2860 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002863 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867 status = PSA_ERROR_INVALID_ARGUMENT;
2868 goto exit;
2869 }
2870
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002871 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002873 };
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002876 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002877 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002878 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 signature, signature_size, signature_length);
2880 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881
2882 status = psa_driver_wrapper_sign_hash(
2883 &attributes, slot->key.data, slot->key.bytes,
2884 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002886 }
2887
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002888
2889exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002890 psa_wipe_tag_output_buffer(signature, status, signature_size,
2891 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002892
Ryan Everetta103ec92024-01-31 13:59:57 +00002893 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896}
2897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2899 int input_is_message,
2900 psa_algorithm_t alg,
2901 const uint8_t *input,
2902 size_t input_length,
2903 const uint8_t *signature,
2904 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002905{
2906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2907 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2908 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 status = psa_sign_verify_check_alg(input_is_message, alg);
2911 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002915 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 key, &slot,
2917 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2918 PSA_KEY_USAGE_VERIFY_HASH,
2919 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (status != PSA_SUCCESS) {
2922 return status;
2923 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924
2925 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 };
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002930 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002931 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002932 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 signature, signature_length);
2934 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935 status = psa_driver_wrapper_verify_hash(
2936 &attributes, slot->key.data, slot->key.bytes,
2937 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002939 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002940
Ryan Everetta103ec92024-01-31 13:59:57 +00002941 unlock_status = psa_unregister_read_under_mutex(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944
2945}
2946
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002947psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002948 const psa_key_attributes_t *attributes,
2949 const uint8_t *key_buffer,
2950 size_t key_buffer_size,
2951 psa_algorithm_t alg,
2952 const uint8_t *input,
2953 size_t input_length,
2954 uint8_t *signature,
2955 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002957{
2958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002961 size_t hash_length;
2962 uint8_t hash[PSA_HASH_MAX_SIZE];
2963
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002964 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_ALG_SIGN_GET_HASH(alg),
2966 input, input_length,
2967 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002970 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002972
2973 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 attributes, key_buffer, key_buffer_size,
2975 alg, hash, hash_length,
2976 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002977 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002980}
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2983 psa_algorithm_t alg,
2984 const uint8_t *input,
2985 size_t input_length,
2986 uint8_t *signature,
2987 size_t signature_size,
2988 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002989{
2990 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002991 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993}
2994
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002995psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996 const psa_key_attributes_t *attributes,
2997 const uint8_t *key_buffer,
2998 size_t key_buffer_size,
2999 psa_algorithm_t alg,
3000 const uint8_t *input,
3001 size_t input_length,
3002 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003004{
3005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003008 size_t hash_length;
3009 uint8_t hash[PSA_HASH_MAX_SIZE];
3010
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003011 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 PSA_ALG_SIGN_GET_HASH(alg),
3013 input, input_length,
3014 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003017 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003019
3020 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 attributes, key_buffer, key_buffer_size,
3022 alg, hash, hash_length,
3023 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003024 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003027}
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3030 psa_algorithm_t alg,
3031 const uint8_t *input,
3032 size_t input_length,
3033 const uint8_t *signature,
3034 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003035{
3036 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003037 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003039}
3040
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003041psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003042 const psa_key_attributes_t *attributes,
3043 const uint8_t *key_buffer, size_t key_buffer_size,
3044 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003046{
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3048 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3049 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003050#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3052 return mbedtls_psa_rsa_sign_hash(
3053 attributes,
3054 key_buffer, key_buffer_size,
3055 alg, hash, hash_length,
3056 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003057#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3058 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 } else {
3060 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003061 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3063 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003064#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3066 return mbedtls_psa_ecdsa_sign_hash(
3067 attributes,
3068 key_buffer, key_buffer_size,
3069 alg, hash, hash_length,
3070 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3072 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 } else {
3074 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003075 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003076 }
Ronald Cron4501c982021-03-19 20:09:32 +01003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 (void) key_buffer;
3079 (void) key_buffer_size;
3080 (void) hash;
3081 (void) hash_length;
3082 (void) signature;
3083 (void) signature_size;
3084 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003087}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3090 psa_algorithm_t alg,
3091 const uint8_t *hash,
3092 size_t hash_length,
3093 uint8_t *signature,
3094 size_t signature_size,
3095 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003096{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003097 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003098 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003100}
3101
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003102psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003103 const psa_key_attributes_t *attributes,
3104 const uint8_t *key_buffer, size_t key_buffer_size,
3105 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003107{
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3109 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3110 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3113 return mbedtls_psa_rsa_verify_hash(
3114 attributes,
3115 key_buffer, key_buffer_size,
3116 alg, hash, hash_length,
3117 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003118#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3119 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 } else {
3121 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003122 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3124 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003125#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3127 return mbedtls_psa_ecdsa_verify_hash(
3128 attributes,
3129 key_buffer, key_buffer_size,
3130 alg, hash, hash_length,
3131 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003132#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3133 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 } else {
3135 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003136 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003137 }
Ronald Cron4501c982021-03-19 20:09:32 +01003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 (void) key_buffer;
3140 (void) key_buffer_size;
3141 (void) hash;
3142 (void) hash_length;
3143 (void) signature;
3144 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003147}
3148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3150 psa_algorithm_t alg,
3151 const uint8_t *hash,
3152 size_t hash_length,
3153 const uint8_t *signature,
3154 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003155{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003156 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003157 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003159}
3160
Gilles Peskine449bd832023-01-11 14:50:10 +01003161psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3162 psa_algorithm_t alg,
3163 const uint8_t *input,
3164 size_t input_length,
3165 const uint8_t *salt,
3166 size_t salt_length,
3167 uint8_t *output,
3168 size_t output_size,
3169 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003172 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003173 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003174 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003175
Darryl Green5cc689a2018-07-24 15:34:10 +01003176 (void) input;
3177 (void) input_length;
3178 (void) salt;
3179 (void) output;
3180 (void) output_size;
3181
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003182 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3185 return PSA_ERROR_INVALID_ARGUMENT;
3186 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003187
Valerio Setti5bb454a2024-01-15 10:43:16 +01003188 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3190 if (status != PSA_SUCCESS) {
3191 return status;
3192 }
3193 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3194 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003195 status = PSA_ERROR_INVALID_ARGUMENT;
3196 goto exit;
3197 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003198
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003199 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003201 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003202
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003203 status = psa_driver_wrapper_asymmetric_encrypt(
3204 &attributes, slot->key.data, slot->key.bytes,
3205 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003207exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003208 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003211}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3214 psa_algorithm_t alg,
3215 const uint8_t *input,
3216 size_t input_length,
3217 const uint8_t *salt,
3218 size_t salt_length,
3219 uint8_t *output,
3220 size_t output_size,
3221 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003224 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003225 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003226 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003227
Darryl Green5cc689a2018-07-24 15:34:10 +01003228 (void) input;
3229 (void) input_length;
3230 (void) salt;
3231 (void) output;
3232 (void) output_size;
3233
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003234 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3237 return PSA_ERROR_INVALID_ARGUMENT;
3238 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003239
Valerio Setti5bb454a2024-01-15 10:43:16 +01003240 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3242 if (status != PSA_SUCCESS) {
3243 return status;
3244 }
3245 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003246 status = PSA_ERROR_INVALID_ARGUMENT;
3247 goto exit;
3248 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003250 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003252 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003254 status = psa_driver_wrapper_asymmetric_decrypt(
3255 &attributes, slot->key.data, slot->key.bytes,
3256 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003258
3259exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00003260 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003263}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003264
Paul Elliott9fe12f62022-11-30 19:16:02 +00003265/****************************************************************/
3266/* Asymmetric interruptible cryptography */
3267/****************************************************************/
3268
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003269static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3270
Paul Elliott9fe12f62022-11-30 19:16:02 +00003271void psa_interruptible_set_max_ops(uint32_t max_ops)
3272{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003273 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274}
3275
3276uint32_t psa_interruptible_get_max_ops(void)
3277{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003278 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003279}
3280
Paul Elliott9fe12f62022-11-30 19:16:02 +00003281uint32_t psa_sign_hash_get_num_ops(
3282 const psa_sign_hash_interruptible_operation_t *operation)
3283{
Paul Elliott296ede92022-12-15 17:00:30 +00003284 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003285}
3286
3287uint32_t psa_verify_hash_get_num_ops(
3288 const psa_verify_hash_interruptible_operation_t *operation)
3289{
Paul Elliott296ede92022-12-15 17:00:30 +00003290 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003291}
3292
Paul Elliott59ad9452022-12-18 15:09:02 +00003293static psa_status_t psa_sign_hash_abort_internal(
3294 psa_sign_hash_interruptible_operation_t *operation)
3295{
3296 if (operation->id == 0) {
3297 /* The object has (apparently) been initialized but it is not (yet)
3298 * in use. It's ok to call abort on such an object, and there's
3299 * nothing to do. */
3300 return PSA_SUCCESS;
3301 }
3302
3303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3304
3305 status = psa_driver_wrapper_sign_hash_abort(operation);
3306
3307 operation->id = 0;
3308
Paul Elliotte6145dc2023-02-07 12:51:21 +00003309 /* Do not clear either the error_occurred or num_ops elements here as they
3310 * only want to be cleared by the application calling abort, not by abort
3311 * being called at completion of an operation. */
3312
Paul Elliott59ad9452022-12-18 15:09:02 +00003313 return status;
3314}
3315
Paul Elliott9fe12f62022-11-30 19:16:02 +00003316psa_status_t psa_sign_hash_start(
3317 psa_sign_hash_interruptible_operation_t *operation,
3318 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3319 const uint8_t *hash, size_t hash_length)
3320{
3321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3322 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3323 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003324 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003325
Paul Elliottc9774412023-02-06 15:14:07 +00003326 /* Check that start has not been previously called, or operation has not
3327 * previously errored. */
3328 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329 return PSA_ERROR_BAD_STATE;
3330 }
3331
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 status = psa_sign_verify_check_alg(0, alg);
3333 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003334 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 return status;
3336 }
3337
3338 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3339 PSA_KEY_USAGE_SIGN_HASH,
3340 alg);
3341
3342 if (status != PSA_SUCCESS) {
3343 goto exit;
3344 }
3345
3346 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3347 status = PSA_ERROR_INVALID_ARGUMENT;
3348 goto exit;
3349 }
3350
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003351 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352 .core = slot->attr
3353 };
3354
Paul Elliott296ede92022-12-15 17:00:30 +00003355 /* Ensure ops count gets reset, in case of operation re-use. */
3356 operation->num_ops = 0;
3357
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3359 slot->key.data,
3360 slot->key.bytes, alg,
3361 hash, hash_length);
3362exit:
3363
3364 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003365 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003366 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003367 }
3368
Ryan Everettdcc03d52024-02-14 15:44:13 +00003369 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003370
Paul Elliottc9774412023-02-06 15:14:07 +00003371 if (unlock_status != PSA_SUCCESS) {
3372 operation->error_occurred = 1;
3373 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003374
Paul Elliottc9774412023-02-06 15:14:07 +00003375 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003376}
3377
3378
3379psa_status_t psa_sign_hash_complete(
3380 psa_sign_hash_interruptible_operation_t *operation,
3381 uint8_t *signature, size_t signature_size,
3382 size_t *signature_length)
3383{
3384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3385
3386 *signature_length = 0;
3387
Paul Elliottc9774412023-02-06 15:14:07 +00003388 /* Check that start has been called first, and that operation has not
3389 * previously errored. */
3390 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 status = PSA_ERROR_BAD_STATE;
3392 goto exit;
3393 }
3394
Paul Elliott096abc42023-02-03 18:33:23 +00003395 /* Immediately reject a zero-length signature buffer. This guarantees that
3396 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003397 if (signature_size == 0) {
3398 status = PSA_ERROR_BUFFER_TOO_SMALL;
3399 goto exit;
3400 }
3401
3402 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3403 signature_size,
3404 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003405
Paul Elliott296ede92022-12-15 17:00:30 +00003406 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003407 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003408
Paul Elliottebe225c2023-02-10 14:32:53 +00003409exit:
3410
Paul Elliott59200a22023-02-21 15:07:40 +00003411 psa_wipe_tag_output_buffer(signature, status, signature_size,
3412 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003413
Paul Elliott53bb3122023-02-10 14:22:22 +00003414 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003415 if (status != PSA_SUCCESS) {
3416 operation->error_occurred = 1;
3417 }
3418
Paul Elliott59ad9452022-12-18 15:09:02 +00003419 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003420 }
3421
3422 return status;
3423}
3424
3425psa_status_t psa_sign_hash_abort(
3426 psa_sign_hash_interruptible_operation_t *operation)
3427{
Paul Elliott59ad9452022-12-18 15:09:02 +00003428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3429
3430 status = psa_sign_hash_abort_internal(operation);
3431
3432 /* We clear the number of ops done here, so that it is not cleared when
3433 * the operation fails or succeeds, only on manual abort. */
3434 operation->num_ops = 0;
3435
Paul Elliottc9774412023-02-06 15:14:07 +00003436 /* Likewise, failure state. */
3437 operation->error_occurred = 0;
3438
Paul Elliott59ad9452022-12-18 15:09:02 +00003439 return status;
3440}
3441
3442static psa_status_t psa_verify_hash_abort_internal(
3443 psa_verify_hash_interruptible_operation_t *operation)
3444{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445 if (operation->id == 0) {
3446 /* The object has (apparently) been initialized but it is not (yet)
3447 * in use. It's ok to call abort on such an object, and there's
3448 * nothing to do. */
3449 return PSA_SUCCESS;
3450 }
3451
Paul Elliott59ad9452022-12-18 15:09:02 +00003452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3453
3454 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003455
3456 operation->id = 0;
3457
Paul Elliotte6145dc2023-02-07 12:51:21 +00003458 /* Do not clear either the error_occurred or num_ops elements here as they
3459 * only want to be cleared by the application calling abort, not by abort
3460 * being called at completion of an operation. */
3461
Paul Elliott59ad9452022-12-18 15:09:02 +00003462 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463}
3464
3465psa_status_t psa_verify_hash_start(
3466 psa_verify_hash_interruptible_operation_t *operation,
3467 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3468 const uint8_t *hash, size_t hash_length,
3469 const uint8_t *signature, size_t signature_length)
3470{
3471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3472 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3473 psa_key_slot_t *slot;
3474
Paul Elliottc9774412023-02-06 15:14:07 +00003475 /* Check that start has not been previously called, or operation has not
3476 * previously errored. */
3477 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003478 return PSA_ERROR_BAD_STATE;
3479 }
3480
3481 status = psa_sign_verify_check_alg(0, alg);
3482 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003483 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003484 return status;
3485 }
3486
3487 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3488 PSA_KEY_USAGE_VERIFY_HASH,
3489 alg);
3490
3491 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003492 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003493 return status;
3494 }
3495
3496 psa_key_attributes_t attributes = {
3497 .core = slot->attr
3498 };
3499
Paul Elliott296ede92022-12-15 17:00:30 +00003500 /* Ensure ops count gets reset, in case of operation re-use. */
3501 operation->num_ops = 0;
3502
Paul Elliott9fe12f62022-11-30 19:16:02 +00003503 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3504 slot->key.data,
3505 slot->key.bytes,
3506 alg, hash, hash_length,
3507 signature, signature_length);
3508
3509 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003510 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003511 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512 }
3513
Ryan Everett291267f2024-02-14 15:59:15 +00003514 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003515
Paul Elliottc9774412023-02-06 15:14:07 +00003516 if (unlock_status != PSA_SUCCESS) {
3517 operation->error_occurred = 1;
3518 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519
Paul Elliottc9774412023-02-06 15:14:07 +00003520 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003521}
3522
3523psa_status_t psa_verify_hash_complete(
3524 psa_verify_hash_interruptible_operation_t *operation)
3525{
3526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3527
Paul Elliottc9774412023-02-06 15:14:07 +00003528 /* Check that start has been called first, and that operation has not
3529 * previously errored. */
3530 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003531 status = PSA_ERROR_BAD_STATE;
3532 goto exit;
3533 }
3534
3535 status = psa_driver_wrapper_verify_hash_complete(operation);
3536
Paul Elliott296ede92022-12-15 17:00:30 +00003537 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003538 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003539 operation);
3540
Paul Elliottebe225c2023-02-10 14:32:53 +00003541exit:
3542
Paul Elliott9fe12f62022-11-30 19:16:02 +00003543 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003544 if (status != PSA_SUCCESS) {
3545 operation->error_occurred = 1;
3546 }
3547
Paul Elliott59ad9452022-12-18 15:09:02 +00003548 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003549 }
3550
3551 return status;
3552}
3553
3554psa_status_t psa_verify_hash_abort(
3555 psa_verify_hash_interruptible_operation_t *operation)
3556{
Paul Elliott59ad9452022-12-18 15:09:02 +00003557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003558
Paul Elliott59ad9452022-12-18 15:09:02 +00003559 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560
Paul Elliott59ad9452022-12-18 15:09:02 +00003561 /* We clear the number of ops done here, so that it is not cleared when
3562 * the operation fails or succeeds, only on manual abort. */
3563 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003564
Paul Elliottc9774412023-02-06 15:14:07 +00003565 /* Likewise, failure state. */
3566 operation->error_occurred = 0;
3567
Paul Elliott59ad9452022-12-18 15:09:02 +00003568 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003569}
3570
Paul Elliott588f8ed2022-12-02 18:10:26 +00003571/****************************************************************/
3572/* Asymmetric interruptible cryptography internal */
3573/* implementations */
3574/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003575
Paul Elliott588f8ed2022-12-02 18:10:26 +00003576void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3577{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003578
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3580 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3581 defined(MBEDTLS_ECP_RESTARTABLE)
3582
3583 /* Internal implementation uses zero to indicate infinite number max ops,
3584 * therefore avoid this value, and set to minimum possible. */
3585 if (max_ops == 0) {
3586 max_ops = 1;
3587 }
3588
Paul Elliott588f8ed2022-12-02 18:10:26 +00003589 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003590#else
3591 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3593 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3594 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3595}
3596
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003598 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003599{
3600#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3601 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3602 defined(MBEDTLS_ECP_RESTARTABLE)
3603
Paul Elliott93d9ca82023-02-15 18:14:21 +00003604 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605#else
3606 (void) operation;
3607 return 0;
3608#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3609 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3610 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3611}
3612
3613uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003614 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615{
3616 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3617 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3618 defined(MBEDTLS_ECP_RESTARTABLE)
3619
Paul Elliott93d9ca82023-02-15 18:14:21 +00003620 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003621#else
3622 (void) operation;
3623 return 0;
3624#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3625 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3626 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3627}
3628
3629psa_status_t mbedtls_psa_sign_hash_start(
3630 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3631 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3632 size_t key_buffer_size, psa_algorithm_t alg,
3633 const uint8_t *hash, size_t hash_length)
3634{
3635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003636 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637
Paul Elliott068fe072023-01-16 13:59:15 +00003638 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3639 return PSA_ERROR_NOT_SUPPORTED;
3640 }
3641
3642 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003643 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003644 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645
3646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649
Paul Elliotta1c94092023-02-15 16:38:04 +00003650 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3651
Paul Elliott93d9ca82023-02-15 18:14:21 +00003652 /* Ensure num_ops is zero'ed in case of context re-use. */
3653 operation->num_ops = 0;
3654
Paul Elliott068fe072023-01-16 13:59:15 +00003655 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3656 attributes->core.bits,
3657 key_buffer,
3658 key_buffer_size,
3659 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003660
Paul Elliott068fe072023-01-16 13:59:15 +00003661 if (status != PSA_SUCCESS) {
3662 return status;
3663 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664
Paul Elliott1bc59df2023-02-05 13:41:57 +00003665 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003666 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667
Paul Elliott068fe072023-01-16 13:59:15 +00003668 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003669 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003670 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671
Paul Elliott0290a762023-02-09 14:30:24 +00003672 /* We only need to store the same length of hash as the private key size
3673 * here, it would be truncated by the internal implementation anyway. */
3674 required_hash_length = (hash_length < operation->coordinate_bytes ?
3675 hash_length : operation->coordinate_bytes);
3676
Paul Elliottba70ad42023-02-15 18:23:53 +00003677 if (required_hash_length > sizeof(operation->hash)) {
3678 /* Shouldn't happen, but better safe than sorry. */
3679 return PSA_ERROR_CORRUPTION_DETECTED;
3680 }
3681
Paul Elliott0290a762023-02-09 14:30:24 +00003682 memcpy(operation->hash, hash, required_hash_length);
3683 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003684
3685 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686
3687#else
Paul Elliott068fe072023-01-16 13:59:15 +00003688 (void) operation;
3689 (void) key_buffer;
3690 (void) key_buffer_size;
3691 (void) alg;
3692 (void) hash;
3693 (void) hash_length;
3694 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003695 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696
Paul Elliott068fe072023-01-16 13:59:15 +00003697 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3699 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3700 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701}
3702
3703psa_status_t mbedtls_psa_sign_hash_complete(
3704 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3705 uint8_t *signature, size_t signature_size,
3706 size_t *signature_length)
3707{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3709 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3710 defined(MBEDTLS_ECP_RESTARTABLE)
3711
Paul Elliotta1c94092023-02-15 16:38:04 +00003712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003713 mbedtls_mpi r;
3714 mbedtls_mpi s;
3715
Paul Elliott46845252023-02-03 14:59:11 +00003716 mbedtls_mpi_init(&r);
3717 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003719 /* Ensure max_ops is set to the current value (or default). */
3720 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3721
Paul Elliotta1c94092023-02-15 16:38:04 +00003722 if (signature_size < 2 * operation->coordinate_bytes) {
3723 status = PSA_ERROR_BUFFER_TOO_SMALL;
3724 goto exit;
3725 }
3726
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003728
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3730 status = mbedtls_to_psa_error(
3731 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003732 &r,
3733 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734 &operation->ctx->d,
3735 operation->hash,
3736 operation->hash_length,
3737 operation->md_alg,
3738 mbedtls_psa_get_random,
3739 MBEDTLS_PSA_RANDOM_STATE,
3740 &operation->restart_ctx));
3741#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003742 status = PSA_ERROR_NOT_SUPPORTED;
3743 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3745 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003746 status = mbedtls_to_psa_error(
3747 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003748 &r,
3749 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750 &operation->ctx->d,
3751 operation->hash,
3752 operation->hash_length,
3753 mbedtls_psa_get_random,
3754 MBEDTLS_PSA_RANDOM_STATE,
3755 mbedtls_psa_get_random,
3756 MBEDTLS_PSA_RANDOM_STATE,
3757 &operation->restart_ctx));
3758 }
3759
Paul Elliottf8e5b562023-02-19 18:43:45 +00003760 /* Hide the fact that the restart context only holds a delta of number of
3761 * ops done during the last operation, not an absolute value. */
3762 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3763
Paul Elliott724bd252023-02-08 12:35:08 +00003764 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003766 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003768 operation->coordinate_bytes)
3769 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770
3771 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003772 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773 }
3774
3775 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003776 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003778 operation->coordinate_bytes,
3779 operation->coordinate_bytes)
3780 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781
3782 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003783 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784 }
3785
Paul Elliott1bc59df2023-02-05 13:41:57 +00003786 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliott724bd252023-02-08 12:35:08 +00003788 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003789 }
Paul Elliott724bd252023-02-08 12:35:08 +00003790
3791exit:
3792
3793 mbedtls_mpi_free(&r);
3794 mbedtls_mpi_free(&s);
3795 return status;
3796
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797 #else
3798
3799 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800 (void) signature;
3801 (void) signature_size;
3802 (void) signature_length;
3803
3804 return PSA_ERROR_NOT_SUPPORTED;
3805
3806#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3807 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3808 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3809}
3810
3811psa_status_t mbedtls_psa_sign_hash_abort(
3812 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3813{
3814
3815#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3816 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3817 defined(MBEDTLS_ECP_RESTARTABLE)
3818
3819 if (operation->ctx) {
3820 mbedtls_ecdsa_free(operation->ctx);
3821 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003822 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823 }
3824
3825 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3826
Paul Elliott93d9ca82023-02-15 18:14:21 +00003827 operation->num_ops = 0;
3828
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829 return PSA_SUCCESS;
3830
3831#else
3832
3833 (void) operation;
3834
3835 return PSA_ERROR_NOT_SUPPORTED;
3836
3837#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3838 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3839 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3840}
3841
3842psa_status_t mbedtls_psa_verify_hash_start(
3843 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3844 const psa_key_attributes_t *attributes,
3845 const uint8_t *key_buffer, size_t key_buffer_size,
3846 psa_algorithm_t alg,
3847 const uint8_t *hash, size_t hash_length,
3848 const uint8_t *signature, size_t signature_length)
3849{
3850 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003851 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003852 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliott068fe072023-01-16 13:59:15 +00003854 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3855 return PSA_ERROR_NOT_SUPPORTED;
3856 }
3857
3858 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003859 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003860 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861
3862#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003863 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3864 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865
Paul Elliotta1c94092023-02-15 16:38:04 +00003866 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3867 mbedtls_mpi_init(&operation->r);
3868 mbedtls_mpi_init(&operation->s);
3869
Paul Elliott93d9ca82023-02-15 18:14:21 +00003870 /* Ensure num_ops is zero'ed in case of context re-use. */
3871 operation->num_ops = 0;
3872
Paul Elliott068fe072023-01-16 13:59:15 +00003873 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3874 attributes->core.bits,
3875 key_buffer,
3876 key_buffer_size,
3877 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878
Paul Elliott068fe072023-01-16 13:59:15 +00003879 if (status != PSA_SUCCESS) {
3880 return status;
3881 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliottc569fc22023-02-10 13:02:54 +00003883 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003884
Paul Elliott1bc59df2023-02-05 13:41:57 +00003885 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003886 return PSA_ERROR_INVALID_SIGNATURE;
3887 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888
Paul Elliott068fe072023-01-16 13:59:15 +00003889 status = mbedtls_to_psa_error(
3890 mbedtls_mpi_read_binary(&operation->r,
3891 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003892 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003893
Paul Elliott068fe072023-01-16 13:59:15 +00003894 if (status != PSA_SUCCESS) {
3895 return status;
3896 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003897
Paul Elliott068fe072023-01-16 13:59:15 +00003898 status = mbedtls_to_psa_error(
3899 mbedtls_mpi_read_binary(&operation->s,
3900 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003901 coordinate_bytes,
3902 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003903
Paul Elliott068fe072023-01-16 13:59:15 +00003904 if (status != PSA_SUCCESS) {
3905 return status;
3906 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907
Paul Elliott2c9843f2023-02-15 17:32:42 +00003908 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003909
Paul Elliott2c9843f2023-02-15 17:32:42 +00003910 if (status != PSA_SUCCESS) {
3911 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003912 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003913
Paul Elliott0290a762023-02-09 14:30:24 +00003914 /* We only need to store the same length of hash as the private key size
3915 * here, it would be truncated by the internal implementation anyway. */
3916 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3917 coordinate_bytes);
3918
Paul Elliottba70ad42023-02-15 18:23:53 +00003919 if (required_hash_length > sizeof(operation->hash)) {
3920 /* Shouldn't happen, but better safe than sorry. */
3921 return PSA_ERROR_CORRUPTION_DETECTED;
3922 }
3923
Paul Elliott0290a762023-02-09 14:30:24 +00003924 memcpy(operation->hash, hash, required_hash_length);
3925 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003926
3927 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928#else
Paul Elliott068fe072023-01-16 13:59:15 +00003929 (void) operation;
3930 (void) key_buffer;
3931 (void) key_buffer_size;
3932 (void) alg;
3933 (void) hash;
3934 (void) hash_length;
3935 (void) signature;
3936 (void) signature_length;
3937 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003938 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003939 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003940
Paul Elliott068fe072023-01-16 13:59:15 +00003941 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3943 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3944 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945}
3946
3947psa_status_t mbedtls_psa_verify_hash_complete(
3948 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3949{
3950
3951#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3952 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3953 defined(MBEDTLS_ECP_RESTARTABLE)
3954
Paul Elliottf8e5b562023-02-19 18:43:45 +00003955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3956
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003957 /* Ensure max_ops is set to the current value (or default). */
3958 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3959
Paul Elliottf8e5b562023-02-19 18:43:45 +00003960 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3962 operation->hash,
3963 operation->hash_length,
3964 &operation->ctx->Q,
3965 &operation->r,
3966 &operation->s,
3967 &operation->restart_ctx));
3968
Paul Elliottf8e5b562023-02-19 18:43:45 +00003969 /* Hide the fact that the restart context only holds a delta of number of
3970 * ops done during the last operation, not an absolute value. */
3971 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3972
3973 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974#else
3975 (void) operation;
3976
3977 return PSA_ERROR_NOT_SUPPORTED;
3978
3979#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3980 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3981 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3982}
3983
3984psa_status_t mbedtls_psa_verify_hash_abort(
3985 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3986{
3987
3988#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3989 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3990 defined(MBEDTLS_ECP_RESTARTABLE)
3991
3992 if (operation->ctx) {
3993 mbedtls_ecdsa_free(operation->ctx);
3994 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003995 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003996 }
3997
3998 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3999
Paul Elliott93d9ca82023-02-15 18:14:21 +00004000 operation->num_ops = 0;
4001
Paul Elliott588f8ed2022-12-02 18:10:26 +00004002 mbedtls_mpi_free(&operation->r);
4003 mbedtls_mpi_free(&operation->s);
4004
4005 return PSA_SUCCESS;
4006
4007#else
4008 (void) operation;
4009
4010 return PSA_ERROR_NOT_SUPPORTED;
4011
4012#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4013 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4014 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4015}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004016
mohammad1603503973b2018-03-12 15:59:30 +02004017/****************************************************************/
4018/* Symmetric cryptography */
4019/****************************************************************/
4020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4022 mbedtls_svc_key_id_t key,
4023 psa_algorithm_t alg,
4024 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004025{
4026 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4027 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004028 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4030 PSA_KEY_USAGE_ENCRYPT :
4031 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004032 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004033
4034 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004036 status = PSA_ERROR_BAD_STATE;
4037 goto exit;
4038 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004041 status = PSA_ERROR_INVALID_ARGUMENT;
4042 goto exit;
4043 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4046 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004049
Ronald Cron49fafa92021-03-10 08:34:23 +01004050 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004051 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004052 * so we only set it (in the driver wrapper) after resources have been
4053 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004054 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004056 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004058 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 }
4060 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004061
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004062 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004064 };
4065
Ronald Cronab99ac22020-10-01 16:38:28 +02004066 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (cipher_operation == MBEDTLS_ENCRYPT) {
4068 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4069 &attributes,
4070 slot->key.data,
4071 slot->key.bytes,
4072 alg);
4073 } else {
4074 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4075 &attributes,
4076 slot->key.data,
4077 slot->key.bytes,
4078 alg);
4079 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004080
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004081exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 if (status != PSA_SUCCESS) {
4083 psa_cipher_abort(operation);
4084 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004085
Ryan Everettc0053cc2024-02-14 16:27:13 +00004086 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004089}
4090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4092 mbedtls_svc_key_id_t key,
4093 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004094{
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004096}
4097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4099 mbedtls_svc_key_id_t key,
4100 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004101{
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004103}
4104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4106 uint8_t *iv,
4107 size_t iv_size,
4108 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004109{
Ronald Cron6d051732020-10-01 14:10:20 +02004110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004111 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004112 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004115 status = PSA_ERROR_BAD_STATE;
4116 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004117 }
4118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004120 status = PSA_ERROR_BAD_STATE;
4121 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004122 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004123
Ronald Cron2fb90522021-07-05 12:31:44 +02004124 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004126 status = PSA_ERROR_BUFFER_TOO_SMALL;
4127 goto exit;
4128 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004131 status = PSA_ERROR_GENERIC_ERROR;
4132 goto exit;
4133 }
4134
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 status = psa_generate_random(local_iv, default_iv_length);
4136 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 }
Ronald Cron5618a392021-03-26 09:52:26 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 status = psa_driver_wrapper_cipher_set_iv(operation,
4141 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004142
4143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (status == PSA_SUCCESS) {
4145 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004146 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004147 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004149 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004151 }
Ronald Cron6d051732020-10-01 14:10:20 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004154}
4155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4157 const uint8_t *iv,
4158 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004159{
Ronald Cron6d051732020-10-01 14:10:20 +02004160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004163 status = PSA_ERROR_BAD_STATE;
4164 goto exit;
4165 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004168 status = PSA_ERROR_BAD_STATE;
4169 goto exit;
4170 }
Ronald Crona0d68172021-03-26 10:15:08 +01004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004173 status = PSA_ERROR_INVALID_ARGUMENT;
4174 goto exit;
4175 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 status = psa_driver_wrapper_cipher_set_iv(operation,
4178 iv,
4179 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004180
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004183 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 } else {
4185 psa_cipher_abort(operation);
4186 }
4187 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004188}
4189
Gilles Peskine449bd832023-01-11 14:50:10 +01004190psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4191 const uint8_t *input,
4192 size_t input_length,
4193 uint8_t *output,
4194 size_t output_size,
4195 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004196{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004200 status = PSA_ERROR_BAD_STATE;
4201 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004202 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004205 status = PSA_ERROR_BAD_STATE;
4206 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004207 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_driver_wrapper_cipher_update(operation,
4210 input,
4211 input_length,
4212 output,
4213 output_size,
4214 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004215
4216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 if (status != PSA_SUCCESS) {
4218 psa_cipher_abort(operation);
4219 }
Ronald Cron6d051732020-10-01 14:10:20 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004222}
4223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4225 uint8_t *output,
4226 size_t output_size,
4227 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004228{
David Saadab4ecc272019-02-14 13:48:10 +02004229 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004232 status = PSA_ERROR_BAD_STATE;
4233 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004234 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237 status = PSA_ERROR_BAD_STATE;
4238 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004239 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 status = psa_driver_wrapper_cipher_finish(operation,
4242 output,
4243 output_size,
4244 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004245
4246exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 if (status == PSA_SUCCESS) {
4248 return psa_cipher_abort(operation);
4249 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004250 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004254 }
mohammad1603503973b2018-03-12 15:59:30 +02004255}
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004258{
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004260 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004261 * in use. It's ok to call abort on such an object, and there's
4262 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004264 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004267
Ronald Cron49fafa92021-03-10 08:34:23 +01004268 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004269 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004270 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004273}
4274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4276 psa_algorithm_t alg,
4277 const uint8_t *input,
4278 size_t input_length,
4279 uint8_t *output,
4280 size_t output_size,
4281 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004282{
4283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004284 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004285 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004286 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4287 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004288 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004291 status = PSA_ERROR_INVALID_ARGUMENT;
4292 goto exit;
4293 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4296 PSA_KEY_USAGE_ENCRYPT,
4297 alg);
4298 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004301
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004302 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004304 };
4305
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4307 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004308 status = PSA_ERROR_GENERIC_ERROR;
4309 goto exit;
4310 }
4311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (default_iv_length > 0) {
4313 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004314 status = PSA_ERROR_BUFFER_TOO_SMALL;
4315 goto exit;
4316 }
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 status = psa_generate_random(local_iv, default_iv_length);
4319 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004320 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004322 }
4323
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004324 status = psa_driver_wrapper_cipher_encrypt(
4325 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004326 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004327 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004329
4330exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004331 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004333 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004334 }
Ronald Cron23919522021-07-08 19:00:07 +02004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if (status == PSA_SUCCESS) {
4337 if (default_iv_length > 0) {
4338 memcpy(output, local_iv, default_iv_length);
4339 }
4340 *output_length += default_iv_length;
4341 } else {
4342 *output_length = 0;
4343 }
4344
4345 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346}
4347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4349 psa_algorithm_t alg,
4350 const uint8_t *input,
4351 size_t input_length,
4352 uint8_t *output,
4353 size_t output_size,
4354 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004355{
4356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004358 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004359 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004362 status = PSA_ERROR_INVALID_ARGUMENT;
4363 goto exit;
4364 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4367 PSA_KEY_USAGE_DECRYPT,
4368 alg);
4369 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004370 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004372
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004373 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004375 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4378 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004379 status = PSA_ERROR_INVALID_ARGUMENT;
4380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004382 status = PSA_ERROR_INVALID_ARGUMENT;
4383 goto exit;
4384 }
4385
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004386 status = psa_driver_wrapper_cipher_decrypt(
4387 &attributes, slot->key.data, slot->key.bytes,
4388 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004390
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004391exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004392 unlock_status = psa_unregister_read_under_mutex(slot);
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004394 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004398 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 }
Ronald Cron23919522021-07-08 19:00:07 +02004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004402}
4403
4404
mohammad16035955c982018-04-26 00:53:03 +03004405/****************************************************************/
4406/* AEAD */
4407/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004408
Paul Elliottbaff51c2021-09-28 17:44:45 +01004409/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004411{
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004413}
4414
4415/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004416static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4417 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004418{
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004422#if defined(PSA_WANT_ALG_GCM)
4423 case PSA_ALG_GCM:
4424 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 * arbitrarily large nonces. Please note that we do not generally
4426 * recommend the usage of nonces of greater length than
4427 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4428 * size, which can then lead to collisions if you encrypt a very
4429 * large number of messages.*/
4430 if (nonce_length != 0) {
4431 return PSA_SUCCESS;
4432 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004433 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004434#endif /* PSA_WANT_ALG_GCM */
4435#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004436 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 if (nonce_length >= 7 && nonce_length <= 13) {
4438 return PSA_SUCCESS;
4439 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004440 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004441#endif /* PSA_WANT_ALG_CCM */
4442#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004443 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 if (nonce_length == 12) {
4445 return PSA_SUCCESS;
4446 } else if (nonce_length == 8) {
4447 return PSA_ERROR_NOT_SUPPORTED;
4448 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004449 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004450#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004451 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004452 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004454 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004457}
4458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004460{
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4462 return PSA_ERROR_INVALID_ARGUMENT;
4463 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004466}
4467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4469 psa_algorithm_t alg,
4470 const uint8_t *nonce,
4471 size_t nonce_length,
4472 const uint8_t *additional_data,
4473 size_t additional_data_length,
4474 const uint8_t *plaintext,
4475 size_t plaintext_length,
4476 uint8_t *ciphertext,
4477 size_t ciphertext_size,
4478 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004479{
Ronald Cron215633c2021-03-16 17:15:37 +01004480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4481 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004482
mohammad1603f08a5502018-06-03 15:05:47 +03004483 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 status = psa_aead_check_algorithm(alg);
4486 if (status != PSA_SUCCESS) {
4487 return status;
4488 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004489
Ronald Cron9a986162021-03-26 12:40:07 +01004490 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4492 if (status != PSA_SUCCESS) {
4493 return status;
4494 }
mohammad16035955c982018-04-26 00:53:03 +03004495
Ronald Cron215633c2021-03-16 17:15:37 +01004496 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004498 };
mohammad16035955c982018-04-26 00:53:03 +03004499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 status = psa_aead_check_nonce_length(alg, nonce_length);
4501 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004502 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004504
Ronald Cronde822812021-03-17 16:08:20 +01004505 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004506 &attributes, slot->key.data, slot->key.bytes,
4507 alg,
4508 nonce, nonce_length,
4509 additional_data, additional_data_length,
4510 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4514 memset(ciphertext, 0, ciphertext_size);
4515 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004516
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004517exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004518 psa_unregister_read_under_mutex(slot);
mohammad16035955c982018-04-26 00:53:03 +03004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004521}
4522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4524 psa_algorithm_t alg,
4525 const uint8_t *nonce,
4526 size_t nonce_length,
4527 const uint8_t *additional_data,
4528 size_t additional_data_length,
4529 const uint8_t *ciphertext,
4530 size_t ciphertext_length,
4531 uint8_t *plaintext,
4532 size_t plaintext_size,
4533 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004534{
Ronald Cron215633c2021-03-16 17:15:37 +01004535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4536 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004537
Gilles Peskineee652a32018-06-01 19:23:52 +02004538 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 status = psa_aead_check_algorithm(alg);
4541 if (status != PSA_SUCCESS) {
4542 return status;
4543 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004544
Ronald Cron9a986162021-03-26 12:40:07 +01004545 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4547 if (status != PSA_SUCCESS) {
4548 return status;
4549 }
mohammad16035955c982018-04-26 00:53:03 +03004550
Ronald Cron215633c2021-03-16 17:15:37 +01004551 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004553 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 status = psa_aead_check_nonce_length(alg, nonce_length);
4556 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004557 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004559
Ronald Cronde822812021-03-17 16:08:20 +01004560 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004561 &attributes, slot->key.data, slot->key.bytes,
4562 alg,
4563 nonce, nonce_length,
4564 additional_data, additional_data_length,
4565 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (status != PSA_SUCCESS && plaintext_size != 0) {
4569 memset(plaintext, 0, plaintext_size);
4570 }
mohammad160360a64d02018-06-03 17:20:42 +03004571
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004572exit:
Ryan Everetta103ec92024-01-31 13:59:57 +00004573 psa_unregister_read_under_mutex(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 return status;
mohammad16035955c982018-04-26 00:53:03 +03004576}
4577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4579{
4580 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004583#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004585 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4587 return PSA_ERROR_INVALID_ARGUMENT;
4588 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004589 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004590#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004591
Przemek Stekiel86679c72022-10-06 17:06:56 +02004592#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004594 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4596 return PSA_ERROR_INVALID_ARGUMENT;
4597 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004598 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004599#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004600
Przemek Stekiel86679c72022-10-06 17:06:56 +02004601#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004603 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if (tag_len != 16) {
4605 return PSA_ERROR_INVALID_ARGUMENT;
4606 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004607 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004608#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004609
4610 default:
4611 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004613 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004615}
4616
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004617/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004618static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4619 int is_encrypt,
4620 mbedtls_svc_key_id_t key,
4621 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004622{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4624 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4625 psa_key_slot_t *slot = NULL;
4626 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004627 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 status = psa_aead_check_algorithm(alg);
4630 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635 status = PSA_ERROR_BAD_STATE;
4636 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004637 }
4638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (operation->nonce_set || operation->lengths_set ||
4640 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004641 status = PSA_ERROR_BAD_STATE;
4642 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004643 }
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004646 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004648 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4652 alg);
4653 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004654 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004656
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004657 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004658 .core = slot->attr
4659 };
4660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004662 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (is_encrypt) {
4666 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4667 &attributes,
4668 slot->key.data,
4669 slot->key.bytes,
4670 alg);
4671 } else {
4672 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4673 &attributes,
4674 slot->key.data,
4675 slot->key.bytes,
4676 alg);
4677 }
4678 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004679 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004683
4684exit:
Ryan Everett9af70e52024-02-14 18:38:56 +00004685 unlock_status = psa_unregister_read_under_mutex(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004688 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004690 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 } else {
4692 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004693 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004696}
4697
Paul Elliott302ff6b2021-04-20 18:10:30 +01004698/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004699psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4700 mbedtls_svc_key_id_t key,
4701 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702{
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004704}
4705
4706/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004707psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4708 mbedtls_svc_key_id_t key,
4709 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004710{
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004712}
4713
4714/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004715psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4716 uint8_t *nonce,
4717 size_t nonce_size,
4718 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004719{
4720 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004721 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004722 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004723
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004724 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004727 status = PSA_ERROR_BAD_STATE;
4728 goto exit;
4729 }
4730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004732 status = PSA_ERROR_BAD_STATE;
4733 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004734 }
4735
Paul Elliotte193ea82021-10-01 13:00:16 +01004736 /* For CCM, this size may not be correct according to the PSA
4737 * specification. The PSA Crypto 1.0.1 specification states:
4738 *
4739 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4740 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4741 *
4742 * However this restriction that L has to be the smallest integer is not
4743 * applied in practice, and it is not implementable here since the
4744 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4746 operation->alg);
4747 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004748 status = PSA_ERROR_BUFFER_TOO_SMALL;
4749 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004750 }
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 status = psa_generate_random(local_nonce, required_nonce_size);
4753 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004758
Paul Elliott39dc6b82021-05-11 19:16:09 +01004759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if (status == PSA_SUCCESS) {
4761 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 } else {
4764 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004765 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768}
4769
4770/* Set the nonce for a multipart authenticated encryption or decryption
4771 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004772psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4773 const uint8_t *nonce,
4774 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004776 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004779 status = PSA_ERROR_BAD_STATE;
4780 goto exit;
4781 }
4782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004784 status = PSA_ERROR_BAD_STATE;
4785 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786 }
4787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4789 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004790 status = PSA_ERROR_INVALID_ARGUMENT;
4791 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004792 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4795 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004796
Paul Elliott39dc6b82021-05-11 19:16:09 +01004797exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004799 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 } else {
4801 psa_aead_abort(operation);
4802 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004805}
4806
4807/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004808psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4809 size_t ad_length,
4810 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004815 status = PSA_ERROR_BAD_STATE;
4816 goto exit;
4817 }
4818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (operation->lengths_set || operation->ad_started ||
4820 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 status = PSA_ERROR_BAD_STATE;
4822 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823 }
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004826#if defined(PSA_WANT_ALG_GCM)
4827 case PSA_ALG_GCM:
4828 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 * bits. Without the guard this code will generate warnings on 32bit
4830 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004831#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (((uint64_t) ad_length) >> 61 != 0 ||
4833 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto exit;
4836 }
Paul Elliott325d3742021-09-27 17:56:28 +01004837#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004838 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004839#endif /* PSA_WANT_ALG_GCM */
4840#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004841 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004843 status = PSA_ERROR_INVALID_ARGUMENT;
4844 goto exit;
4845 }
4846 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004847#endif /* PSA_WANT_ALG_CCM */
4848#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004849 case PSA_ALG_CHACHA20_POLY1305:
4850 /* No length restrictions for ChaChaPoly. */
4851 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004852#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004853 default:
4854 break;
4855 }
Paul Elliott325d3742021-09-27 17:56:28 +01004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4858 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004859
Paul Elliott39dc6b82021-05-11 19:16:09 +01004860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004862 operation->ad_remaining = ad_length;
4863 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 } else {
4866 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004867 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004871
4872/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004873psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4874 const uint8_t *input,
4875 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004876{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004880 status = PSA_ERROR_BAD_STATE;
4881 goto exit;
4882 }
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004885 status = PSA_ERROR_BAD_STATE;
4886 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004887 }
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (operation->lengths_set) {
4890 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004891 status = PSA_ERROR_INVALID_ARGUMENT;
4892 goto exit;
4893 }
4894
4895 operation->ad_remaining -= input_length;
4896 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004897#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004899 status = PSA_ERROR_BAD_STATE;
4900 goto exit;
4901 }
4902#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 status = psa_driver_wrapper_aead_update_ad(operation, input,
4905 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004906
Paul Elliott39dc6b82021-05-11 19:16:09 +01004907exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004909 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 } else {
4911 psa_aead_abort(operation);
4912 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004915}
4916
4917/* Encrypt or decrypt a message fragment in an active multipart AEAD
4918 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004919psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4920 const uint8_t *input,
4921 size_t input_length,
4922 uint8_t *output,
4923 size_t output_size,
4924 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004925{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004927
4928 *output_length = 0;
4929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004931 status = PSA_ERROR_BAD_STATE;
4932 goto exit;
4933 }
4934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004936 status = PSA_ERROR_BAD_STATE;
4937 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004938 }
4939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004941 /* Additional data length was supplied, but not all the additional
4942 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004944 status = PSA_ERROR_INVALID_ARGUMENT;
4945 goto exit;
4946 }
4947
4948 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004950 status = PSA_ERROR_INVALID_ARGUMENT;
4951 goto exit;
4952 }
4953
4954 operation->body_remaining -= input_length;
4955 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004956#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004958 status = PSA_ERROR_BAD_STATE;
4959 goto exit;
4960 }
4961#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4964 output, output_size,
4965 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004966
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004969 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 } else {
4971 psa_aead_abort(operation);
4972 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004975}
4976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004978{
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 if (operation->id == 0 || !operation->nonce_set) {
4980 return PSA_ERROR_BAD_STATE;
4981 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4984 operation->body_remaining != 0)) {
4985 return PSA_ERROR_INVALID_ARGUMENT;
4986 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004989}
4990
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004992psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4993 uint8_t *ciphertext,
4994 size_t ciphertext_size,
4995 size_t *ciphertext_length,
4996 uint8_t *tag,
4997 size_t tag_size,
4998 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004999{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5001
Paul Elliott302ff6b2021-04-20 18:10:30 +01005002 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005003 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 status = psa_aead_final_checks(operation);
5006 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005007 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005009
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005011 status = PSA_ERROR_BAD_STATE;
5012 goto exit;
5013 }
5014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5016 ciphertext_size,
5017 ciphertext_length,
5018 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005019
Paul Elliott39dc6b82021-05-11 19:16:09 +01005020exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005021
5022
Paul Elliottf47b0952021-05-21 18:02:33 +01005023 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005024 * the zero tag size, make sure the tag is set to something implausible.
5025 * Even if the operation succeeds, make sure we clear the rest of the
5026 * buffer to prevent potential leakage of anything previously placed in
5027 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005028 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005033}
5034
5035/* Finish authenticating and decrypting a message in a multipart AEAD
5036 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005037psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5038 uint8_t *plaintext,
5039 size_t plaintext_size,
5040 size_t *plaintext_length,
5041 const uint8_t *tag,
5042 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005043{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5045
Paul Elliott302ff6b2021-04-20 18:10:30 +01005046 *plaintext_length = 0;
5047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 status = psa_aead_final_checks(operation);
5049 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005054 status = PSA_ERROR_BAD_STATE;
5055 goto exit;
5056 }
5057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5059 plaintext_size,
5060 plaintext_length,
5061 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005062
5063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005067}
5068
5069/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071{
5072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075 /* The object has (apparently) been initialized but it is not (yet)
5076 * in use. It's ok to call abort on such an object, and there's
5077 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005079 }
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005086}
5087
Gilles Peskinee59236f2018-01-27 23:32:46 +01005088/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005089/* Generators */
5090/****************************************************************/
5091
Przemek Stekiel69c46792022-06-10 12:59:51 +02005092#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005093 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005094 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305095 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305096 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005097#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005098#endif /* At least one builtin KDF */
5099
Przemek Stekiel69c46792022-06-10 12:59:51 +02005100#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005101 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5102 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5103static psa_status_t psa_key_derivation_start_hmac(
5104 psa_mac_operation_t *operation,
5105 psa_algorithm_t hash_alg,
5106 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005108{
5109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5112 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5113 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005114
Steven Cooreman72f736a2021-05-07 14:14:37 +02005115 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 status = psa_driver_wrapper_mac_sign_setup(operation,
5119 &attributes,
5120 hmac_key, hmac_key_length,
5121 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005122
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 psa_reset_key_attributes(&attributes);
5124 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005125}
5126#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005127
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128#define HKDF_STATE_INIT 0 /* no input yet */
5129#define HKDF_STATE_STARTED 1 /* got salt */
5130#define HKDF_STATE_KEYED 2 /* got key */
5131#define HKDF_STATE_OUTPUT 3 /* output started */
5132
Gilles Peskinecbe66502019-05-16 16:59:18 +02005133static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005135{
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5137 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5138 } else {
5139 return operation->alg;
5140 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005141}
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005144{
5145 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5147 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148 /* The object has (apparently) been initialized but it is not
5149 * in use. It's ok to call abort on such an object, and there's
5150 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005152#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5154 mbedtls_free(operation->ctx.hkdf.info);
5155 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5156 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005157#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005158#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5161 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5162 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5163 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005164 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005166 }
5167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005169 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005171 }
5172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005174 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005176 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005177#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005179 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005181 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005182#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005183 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005184
5185 /* We leave the fields Ai and output_block to be erased safely by the
5186 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5189 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005190#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5192 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5193 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5194 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005195#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305196#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305197 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305198 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005199 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305200 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305201 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305202
5203 status = PSA_SUCCESS;
5204 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305205#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005206 {
5207 status = PSA_ERROR_BAD_STATE;
5208 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 mbedtls_platform_zeroize(operation, sizeof(*operation));
5210 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211}
5212
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005215{
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005219 }
5220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223}
5224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5226 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005227{
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 if (operation->alg == 0) {
5229 return PSA_ERROR_BAD_STATE;
5230 }
5231 if (capacity > operation->capacity) {
5232 return PSA_ERROR_INVALID_ARGUMENT;
5233 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005236}
5237
Przemek Stekiel69c46792022-06-10 12:59:51 +02005238#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005239/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005240static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5241 psa_algorithm_t kdf_alg,
5242 uint8_t *output,
5243 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005244{
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5246 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005247 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005248 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005249#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005251#else
5252 const uint8_t last_block = 0xff;
5253#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (hkdf->state < HKDF_STATE_KEYED ||
5256 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005257#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005259#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 )) {
5261 return PSA_ERROR_BAD_STATE;
5262 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263 hkdf->state = HKDF_STATE_OUTPUT;
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005266 /* Copy what remains of the current block */
5267 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005269 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 }
5271 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005272 output += n;
5273 output_length -= n;
5274 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005276 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005278 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005279 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005281 * object was corrupted or if this function is called directly
5282 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (hkdf->block_number == last_block) {
5284 return PSA_ERROR_BAD_STATE;
5285 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005286
5287 /* We need a new block */
5288 ++hkdf->block_number;
5289 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5292 hash_alg,
5293 hkdf->prk,
5294 hash_length);
5295 if (status != PSA_SUCCESS) {
5296 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005298
5299 if (hkdf->block_number != 1) {
5300 status = psa_mac_update(&hkdf->hmac,
5301 hkdf->output_block,
5302 hash_length);
5303 if (status != PSA_SUCCESS) {
5304 return status;
5305 }
5306 }
5307 status = psa_mac_update(&hkdf->hmac,
5308 hkdf->info,
5309 hkdf->info_length);
5310 if (status != PSA_SUCCESS) {
5311 return status;
5312 }
5313 status = psa_mac_update(&hkdf->hmac,
5314 &hkdf->block_number, 1);
5315 if (status != PSA_SUCCESS) {
5316 return status;
5317 }
5318 status = psa_mac_sign_finish(&hkdf->hmac,
5319 hkdf->output_block,
5320 sizeof(hkdf->output_block),
5321 &hmac_output_length);
5322 if (status != PSA_SUCCESS) {
5323 return status;
5324 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005325 }
5326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005328}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005329#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005330
John Durkop07cc04a2020-11-16 22:08:34 -08005331#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5332 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005333static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5334 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005336{
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5338 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005339 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5340 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005341 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005342
Janos Follath7742fee2019-06-17 12:58:10 +01005343 /* We can't be wanting more output after block 0xff, otherwise
5344 * the capacity check in psa_key_derivation_output_bytes() would have
5345 * prevented this call. It could happen only if the operation
5346 * object was corrupted or if this function is called directly
5347 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (tls12_prf->block_number == 0xff) {
5349 return PSA_ERROR_CORRUPTION_DETECTED;
5350 }
Janos Follath7742fee2019-06-17 12:58:10 +01005351
5352 /* We need a new block */
5353 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005354 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005355
5356 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5357 *
5358 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5359 *
5360 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5361 * HMAC_hash(secret, A(2) + seed) +
5362 * HMAC_hash(secret, A(3) + seed) + ...
5363 *
5364 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005366 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005368 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005369 * is currently extracted as `output_block` and where i is
5370 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005371 */
5372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 status = psa_key_derivation_start_hmac(&hmac,
5374 hash_alg,
5375 tls12_prf->secret,
5376 tls12_prf->secret_length);
5377 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005378 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005380
5381 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005383 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5384 * the variable seed and in this instance means it in the context of the
5385 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 status = psa_mac_update(&hmac,
5387 tls12_prf->label,
5388 tls12_prf->label_length);
5389 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005390 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 }
5392 status = psa_mac_update(&hmac,
5393 tls12_prf->seed,
5394 tls12_prf->seed_length);
5395 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005396 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 }
5398 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005399 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5401 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005402 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005404 }
5405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 status = psa_mac_sign_finish(&hmac,
5407 tls12_prf->Ai, hash_length,
5408 &hmac_output_length);
5409 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005410 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 }
5412 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005413 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005415
5416 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 status = psa_key_derivation_start_hmac(&hmac,
5418 hash_alg,
5419 tls12_prf->secret,
5420 tls12_prf->secret_length);
5421 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005422 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
5424 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5425 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005426 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 }
5428 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5429 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005430 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 }
5432 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5433 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 }
5436 status = psa_mac_sign_finish(&hmac,
5437 tls12_prf->output_block, hash_length,
5438 &hmac_output_length);
5439 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005440 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005442
Janos Follath7742fee2019-06-17 12:58:10 +01005443
5444cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 cleanup_status = psa_mac_abort(&hmac);
5446 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005451}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005452
Janos Follath844eb0e2019-06-19 12:10:49 +01005453static psa_status_t psa_key_derivation_tls12_prf_read(
5454 psa_tls12_prf_key_derivation_t *tls12_prf,
5455 psa_algorithm_t alg,
5456 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005458{
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5460 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005461 psa_status_t status;
5462 uint8_t offset, length;
5463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005465 case PSA_TLS12_PRF_STATE_LABEL_SET:
5466 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5467 break;
5468 case PSA_TLS12_PRF_STATE_OUTPUT:
5469 break;
5470 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005472 }
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005475 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 if (tls12_prf->left_in_block == 0) {
5477 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5478 alg);
5479 if (status != PSA_SUCCESS) {
5480 return status;
5481 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005482
5483 continue;
5484 }
5485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005487 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005489 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005491
5492 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005494 output += length;
5495 output_length -= length;
5496 tls12_prf->left_in_block -= length;
5497 }
5498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005500}
John Durkop07cc04a2020-11-16 22:08:34 -08005501#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5502 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005503
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005504#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5505static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5506 psa_tls12_ecjpake_to_pms_t *ecjpake,
5507 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005509{
5510 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005511 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 if (output_length != 32) {
5514 return PSA_ERROR_INVALID_ARGUMENT;
5515 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5518 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5519 &output_size);
5520 if (status != PSA_SUCCESS) {
5521 return status;
5522 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005523
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 if (output_size != output_length) {
5525 return PSA_ERROR_GENERIC_ERROR;
5526 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005529}
5530#endif
5531
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305532#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305533static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5534 psa_pbkdf2_key_derivation_t *pbkdf2,
5535 psa_algorithm_t prf_alg,
5536 uint8_t prf_output_length,
5537 psa_key_attributes_t *attributes)
5538{
5539 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305540 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305541 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305542 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305543 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305544 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305545 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305546
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305547 mac_operation.is_sign = 1;
5548 mac_operation.mac_size = prf_output_length;
5549 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305550
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305551 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5552 attributes,
5553 pbkdf2->password,
5554 pbkdf2->password_length,
5555 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305556 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305557 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305558 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305559 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5560 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305561 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305562 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305563 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305564 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305565 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305566 }
5567 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5568 &mac_output_length);
5569 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305570 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305571 }
5572
5573 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305574 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305575 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305576 }
5577
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305578 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305579
5580 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305581 /* We are passing prf_output_length as mac_size because the driver
5582 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305583 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305584 status = psa_driver_wrapper_mac_compute(attributes,
5585 pbkdf2->password,
5586 pbkdf2->password_length,
5587 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305588 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305589 &mac_output_length);
5590 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305591 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305592 }
5593
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305594 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305595 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305596
5597cleanup:
5598 /* Zeroise buffers to clear sensitive data from memory. */
5599 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5600 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305601}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305602
5603static psa_status_t psa_key_derivation_pbkdf2_read(
5604 psa_pbkdf2_key_derivation_t *pbkdf2,
5605 psa_algorithm_t kdf_alg,
5606 uint8_t *output,
5607 size_t output_length)
5608{
5609 psa_status_t status;
5610 psa_algorithm_t prf_alg;
5611 uint8_t prf_output_length;
5612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5613 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5614 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5615
5616 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5617 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5618 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5619 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305620 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5621 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305622 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305623 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305624 } else {
5625 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305626 }
5627
5628 switch (pbkdf2->state) {
5629 case PSA_PBKDF2_STATE_PASSWORD_SET:
5630 /* Initially we need a new block so bytes_used is equal to block size*/
5631 pbkdf2->bytes_used = prf_output_length;
5632 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5633 break;
5634 case PSA_PBKDF2_STATE_OUTPUT:
5635 break;
5636 default:
5637 return PSA_ERROR_BAD_STATE;
5638 }
5639
5640 while (output_length != 0) {
5641 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5642 if (n > output_length) {
5643 n = (uint8_t) output_length;
5644 }
5645 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5646 output += n;
5647 output_length -= n;
5648 pbkdf2->bytes_used += n;
5649
5650 if (output_length == 0) {
5651 break;
5652 }
5653
5654 /* We need a new block */
5655 pbkdf2->bytes_used = 0;
5656 pbkdf2->block_number++;
5657
5658 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5659 prf_output_length,
5660 &attributes);
5661 if (status != PSA_SUCCESS) {
5662 return status;
5663 }
5664 }
5665
5666 return PSA_SUCCESS;
5667}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305668#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305669
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005670psa_status_t psa_key_derivation_output_bytes(
5671 psa_key_derivation_operation_t *operation,
5672 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005674{
5675 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005681 }
5682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005685 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005687 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005688 goto exit;
5689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005692 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005693 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5694 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005696 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005698 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005699 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005700
Przemek Stekiel69c46792022-06-10 12:59:51 +02005701#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5703 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5704 output, output_length);
5705 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005706#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005707#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5708 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5710 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5711 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5712 kdf_alg, output,
5713 output_length);
5714 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005715#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5716 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005717#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005719 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5721 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005722#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305723#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305724 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305725 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5726 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305727 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305728#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005729
Gilles Peskineeab56e42018-07-12 17:12:33 +02005730 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005731 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005733 }
5734
5735exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005737 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 * This allows us to differentiate between exhausted operations and
5739 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5740 * operations. */
5741 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747}
5748
David Brown7807bf72021-02-09 16:28:23 -07005749#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005750static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005751{
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (data_size >= 8) {
5753 mbedtls_des_key_set_parity(data);
5754 }
5755 if (data_size >= 16) {
5756 mbedtls_des_key_set_parity(data + 8);
5757 }
5758 if (data_size >= 24) {
5759 mbedtls_des_key_set_parity(data + 16);
5760 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005761}
David Brown7807bf72021-02-09 16:28:23 -07005762#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005763
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005764/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 * ECC keys on a Weierstrass elliptic curve require the generation
5766 * of a private key which is an integer
5767 * in the range [1, N - 1], where N is the boundary of the private key domain:
5768 * N is the prime p for Diffie-Hellman, or the order of the
5769 * curve’s base point for ECC.
5770 *
5771 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5772 * This function generates the private key using the following process:
5773 *
5774 * 1. Draw a byte string of length ceiling(m/8) bytes.
5775 * 2. If m is not a multiple of 8, set the most significant
5776 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5777 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5778 * 4. If k > N - 2, discard the result and return to step 1.
5779 * 5. Output k + 1 as the private key.
5780 *
5781 * This method allows compliance to NIST standards, specifically the methods titled
5782 * Key-Pair Generation by Testing Candidates in the following publications:
5783 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5784 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5785 * Diffie-Hellman keys.
5786 *
5787 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5788 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5789 *
5790 * Note: Function allocates memory for *data buffer, so given *data should be
5791 * always NULL.
5792 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005793#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5794#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005795static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005796 psa_key_slot_t *slot,
5797 size_t bits,
5798 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005799 uint8_t **data
5800 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005801{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005802 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005803 mbedtls_mpi k;
5804 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005806 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005807 size_t m;
5808 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 mbedtls_mpi_init(&k);
5811 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005812
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005813 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 mbedtls_ecp_group_id grp_id =
Valerio Settid36c3132023-12-21 14:03:51 +01005816 mbedtls_ecc_group_from_psa(curve, bits);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005819 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005820 goto cleanup;
5821 }
5822
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005823 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005827
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005828 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005829 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005830 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005832 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005833
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005834 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005836
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005837 /* Note: This function is always called with *data == NULL and it
5838 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 *data = mbedtls_calloc(1, m_bytes);
5840 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005841 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005842 goto cleanup;
5843 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005846 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005848 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005850
5851 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005853 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005854 * (8 * ceiling(m/8) - m) bits of the first byte in
5855 * the string to zero.
5856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005858 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005859 }
5860
5861 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 * big-endian byte string.
5863 */
5864 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005865
5866 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 * Result of comparison is returned. When it indicates error
5868 * then this function is called again.
5869 */
5870 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871 }
5872
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5875 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (ret != 0) {
5878 status = mbedtls_to_psa_error(ret);
5879 }
5880 if (status != PSA_SUCCESS) {
5881 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 *data = NULL;
5883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 mbedtls_mpi_free(&k);
5885 mbedtls_mpi_free(&diff_N_2);
5886 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005887}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005888
5889/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5890 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5891 *
5892 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5893 * draw a 32-byte string and process it as specified in
5894 * Elliptic Curves for Security [RFC7748] §5.
5895 *
5896 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5897 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5898 *
5899 * Note: Function allocates memory for *data buffer, so given *data should be
5900 * always NULL.
5901 */
5902
5903static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5904 size_t bits,
5905 psa_key_derivation_operation_t *operation,
5906 uint8_t **data
5907 )
5908{
5909 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005913 case 255:
5914 output_length = 32;
5915 break;
5916 case 448:
5917 output_length = 56;
5918 break;
5919 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005921 break;
5922 }
5923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (*data == NULL) {
5927 return PSA_ERROR_INSUFFICIENT_MEMORY;
5928 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005933 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005937 case 255:
5938 (*data)[0] &= 248;
5939 (*data)[31] &= 127;
5940 (*data)[31] |= 64;
5941 break;
5942 case 448:
5943 (*data)[0] &= 252;
5944 (*data)[55] |= 128;
5945 break;
5946 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005948 break;
5949 }
5950
5951 return status;
5952}
Valerio Setti86587ab2023-06-27 13:09:30 +02005953#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5954static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5955 psa_key_slot_t *slot, size_t bits,
5956 psa_key_derivation_operation_t *operation, uint8_t **data)
5957{
5958 (void) slot;
5959 (void) bits;
5960 (void) operation;
5961 (void) data;
5962 return PSA_ERROR_NOT_SUPPORTED;
5963}
5964
5965static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5966 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5967{
5968 (void) bits;
5969 (void) operation;
5970 (void) data;
5971 return PSA_ERROR_NOT_SUPPORTED;
5972}
5973#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5974#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005975
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005976static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005977 psa_key_slot_t *slot,
5978 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005980{
5981 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305983 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005985 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5988 return PSA_ERROR_INVALID_ARGUMENT;
5989 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005990
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005991#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005992 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5994 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5995 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005996 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5998 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005999 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 }
6001 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006002 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6004 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006005 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006007 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006008 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006009#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006010 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (key_type_is_raw_bytes(slot->attr.type)) {
6012 if (bits % 8 != 0) {
6013 return PSA_ERROR_INVALID_ARGUMENT;
6014 }
6015 data = mbedtls_calloc(1, bytes);
6016 if (data == NULL) {
6017 return PSA_ERROR_INSUFFICIENT_MEMORY;
6018 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 status = psa_key_derivation_output_bytes(operation, data, bytes);
6021 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006022 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 }
David Brown12ca5032021-02-11 11:02:00 -07006024#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6026 psa_des_set_key_parity(data, bytes);
6027 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006028#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 } else {
6030 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006031 }
Ronald Crondd04d422020-11-28 15:14:42 +01006032
Ronald Cronbf33c932020-11-28 18:06:53 +01006033 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006034 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006036 };
6037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6039 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6040 &storage_size);
6041 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 }
Archanad8a83dc2021-06-14 10:04:16 +05306044 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 status = psa_allocate_buffer_to_slot(slot, storage_size);
6046 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 }
Archanad8a83dc2021-06-14 10:04:16 +05306049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 status = psa_driver_wrapper_import_key(&attributes,
6051 data, bytes,
6052 slot->key.data,
6053 slot->key.bytes,
6054 &slot->key.bytes, &bits);
6055 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006056 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006058
6059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 mbedtls_free(data);
6061 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006062}
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6065 psa_key_derivation_operation_t *operation,
6066 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006067{
6068 psa_status_t status;
6069 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006070 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006071
Ronald Cron81709fc2020-11-14 12:10:32 +01006072 *key = MBEDTLS_SVC_KEY_ID_INIT;
6073
Gilles Peskine0f84d622019-09-12 19:03:13 +02006074 /* Reject any attempt to create a zero-length key so that we don't
6075 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (psa_get_key_bits(attributes) == 0) {
6077 return PSA_ERROR_INVALID_ARGUMENT;
6078 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (operation->alg == PSA_ALG_NONE) {
6081 return PSA_ERROR_BAD_STATE;
6082 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (!operation->can_output_key) {
6085 return PSA_ERROR_NOT_PERMITTED;
6086 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6089 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006092 /* Deriving a key in a secure element is not implemented yet. */
6093 status = PSA_ERROR_NOT_SUPPORTED;
6094 }
6095#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (status == PSA_SUCCESS) {
6097 status = psa_generate_derived_key_internal(slot,
6098 attributes->core.bits,
6099 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006100 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (status == PSA_SUCCESS) {
6102 status = psa_finish_key_creation(slot, driver, key);
6103 }
6104 if (status != PSA_SUCCESS) {
6105 psa_fail_key_creation(slot, driver);
6106 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006109}
6110
Gilles Peskineeab56e42018-07-12 17:12:33 +02006111
6112
6113/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006114/* Key derivation */
6115/****************************************************************/
6116
Steven Cooreman932ffb72021-02-15 12:14:32 +01006117#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006118static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006119{
6120#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6122 return 1;
6123 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006124#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006125#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6127 return 1;
6128 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006129#endif
6130#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6132 return 1;
6133 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006134#endif
6135#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6137 return 1;
6138 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006139#endif
6140#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6142 return 1;
6143 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006144#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006145#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6147 return 1;
6148 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006149#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306150#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6151 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6152 return 1;
6153 }
6154#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306155#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6156 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6157 return 1;
6158 }
6159#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006161}
6162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006164{
6165 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 psa_status_t status = psa_hash_setup(&operation, alg);
6167 psa_hash_abort(&operation);
6168 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006169}
6170
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306171static psa_status_t psa_key_derivation_set_maximum_capacity(
6172 psa_key_derivation_operation_t *operation,
6173 psa_algorithm_t kdf_alg)
6174{
6175#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6176 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6177 operation->capacity = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6178 return PSA_SUCCESS;
6179 }
6180#endif
6181#if defined(PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128)
6182 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6183#if (SIZE_MAX > UINT32_MAX)
Kusumit Ghoderao7d4db632023-12-07 16:17:46 +05306184 operation->capacity = UINT32_MAX * (size_t) PSA_MAC_LENGTH(
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306185 PSA_KEY_TYPE_AES,
6186 128U,
6187 PSA_ALG_CMAC);
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306188#else
6189 operation->capacity = SIZE_MAX;
6190#endif
6191 return PSA_SUCCESS;
6192 }
6193#endif /* PSA_WANT_ALG_PBKDF2_AES_CMAC_PRF_128 */
6194
6195 /* After this point, if kdf_alg is not valid then value of hash_alg may be
6196 * invalid or meaningless but it does not affect this function */
6197 psa_algorithm_t hash_alg = PSA_ALG_GET_HASH(kdf_alg);
6198 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306199 if (hash_size == 0) {
6200 return PSA_ERROR_NOT_SUPPORTED;
6201 }
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306202
6203 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6204 * we might fail later, which is somewhat unfriendly and potentially
6205 * risk-prone. */
6206 psa_status_t status = psa_hash_try_support(hash_alg);
6207 if (status != PSA_SUCCESS) {
6208 return status;
6209 }
6210
6211#if defined(PSA_WANT_ALG_HKDF)
6212 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6213 operation->capacity = 255 * hash_size;
6214 } else
6215#endif
6216#if defined(PSA_WANT_ALG_HKDF_EXTRACT)
6217 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6218 operation->capacity = hash_size;
6219 } else
6220#endif
6221#if defined(PSA_WANT_ALG_HKDF_EXPAND)
6222 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6223 operation->capacity = 255 * hash_size;
6224 } else
6225#endif
6226#if defined(PSA_WANT_ALG_TLS12_PRF)
6227 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) &&
6228 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6229 operation->capacity = SIZE_MAX;
6230 } else
6231#endif
6232#if defined(PSA_WANT_ALG_TLS12_PSK_TO_MS)
6233 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg) &&
6234 (hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6235 /* Master Secret is always 48 bytes
6236 * https://datatracker.ietf.org/doc/html/rfc5246.html#section-8.1 */
6237 operation->capacity = 48U;
6238 } else
6239#endif
6240#if defined(PSA_WANT_ALG_PBKDF2_HMAC)
6241 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6242#if (SIZE_MAX > UINT32_MAX)
6243 operation->capacity = UINT32_MAX * hash_size;
6244#else
6245 operation->capacity = SIZE_MAX;
6246#endif
6247 } else
6248#endif /* PSA_WANT_ALG_PBKDF2_HMAC */
6249 {
Kusumit Ghoderaod3f70d32023-12-06 16:20:04 +05306250 (void) hash_size;
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306251 status = PSA_ERROR_NOT_SUPPORTED;
6252 }
6253 return status;
6254}
6255
Gilles Peskine969c5d62019-01-16 15:53:06 +01006256static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006257 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006259{
Janos Follath5fe19732019-06-20 15:09:30 +01006260 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6261 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006263
Gilles Peskine969c5d62019-01-16 15:53:06 +01006264 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 if (!is_kdf_alg_supported(kdf_alg)) {
6266 return PSA_ERROR_NOT_SUPPORTED;
6267 }
John Durkop07cc04a2020-11-16 22:08:34 -08006268
Kusumit Ghoderao86e83dd2023-12-01 16:38:26 +05306269 psa_status_t status = psa_key_derivation_set_maximum_capacity(operation,
6270 kdf_alg);
Kusumit Ghoderao5f3a9382023-09-13 16:28:12 +05306271 return status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006272}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006275{
6276#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 if (alg == PSA_ALG_ECDH) {
6278 return PSA_SUCCESS;
6279 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006280#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006281#if defined(PSA_WANT_ALG_FFDH)
6282 if (alg == PSA_ALG_FFDH) {
6283 return PSA_SUCCESS;
6284 }
6285#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006286 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006288}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006289
6290static int psa_key_derivation_allows_free_form_secret_input(
6291 psa_algorithm_t kdf_alg)
6292{
6293#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6294 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6295 return 0;
6296 }
6297#endif
6298 (void) kdf_alg;
6299 return 1;
6300}
John Durkop07cc04a2020-11-16 22:08:34 -08006301#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6304 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006305{
6306 psa_status_t status;
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 if (operation->alg != 0) {
6309 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006310 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6313 return PSA_ERROR_INVALID_ARGUMENT;
6314 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6315#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6316 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6317 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6318 status = psa_key_agreement_try_support(ka_alg);
6319 if (status != PSA_SUCCESS) {
6320 return status;
6321 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006322 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6323 return PSA_ERROR_INVALID_ARGUMENT;
6324 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6326#else
6327 return PSA_ERROR_NOT_SUPPORTED;
6328#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6329 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6330#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6331 status = psa_key_derivation_setup_kdf(operation, alg);
6332#else
6333 return PSA_ERROR_NOT_SUPPORTED;
6334#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6335 } else {
6336 return PSA_ERROR_INVALID_ARGUMENT;
6337 }
6338
6339 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006340 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 }
6342 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006343}
6344
Przemek Stekiel69c46792022-06-10 12:59:51 +02006345#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006346static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6347 psa_algorithm_t kdf_alg,
6348 psa_key_derivation_step_t step,
6349 const uint8_t *data,
6350 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006351{
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006355 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6358 return PSA_ERROR_INVALID_ARGUMENT;
6359 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006360#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 if (hkdf->state != HKDF_STATE_INIT) {
6362 return PSA_ERROR_BAD_STATE;
6363 } else {
6364 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6365 hash_alg,
6366 data, data_length);
6367 if (status != PSA_SUCCESS) {
6368 return status;
6369 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006370 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006372 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006373 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006374#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006376 /* We shouldn't be in different state as HKDF_EXPAND only allows
6377 * two inputs: SECRET (this case) and INFO which does not modify
6378 * the state. It could happen only if the hkdf
6379 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (hkdf->state != HKDF_STATE_INIT) {
6381 return PSA_ERROR_BAD_STATE;
6382 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006383
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006384 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6386 return PSA_ERROR_INVALID_ARGUMENT;
6387 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 memcpy(hkdf->prk, data, data_length);
6390 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006391#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006392 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006393 /* HKDF: If no salt was provided, use an empty salt.
6394 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6398 return PSA_ERROR_BAD_STATE;
6399 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006400#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6402 hash_alg,
6403 NULL, 0);
6404 if (status != PSA_SUCCESS) {
6405 return status;
6406 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006407 hkdf->state = HKDF_STATE_STARTED;
6408 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (hkdf->state != HKDF_STATE_STARTED) {
6410 return PSA_ERROR_BAD_STATE;
6411 }
6412 status = psa_mac_update(&hkdf->hmac,
6413 data, data_length);
6414 if (status != PSA_SUCCESS) {
6415 return status;
6416 }
6417 status = psa_mac_sign_finish(&hkdf->hmac,
6418 hkdf->prk,
6419 sizeof(hkdf->prk),
6420 &data_length);
6421 if (status != PSA_SUCCESS) {
6422 return status;
6423 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006424 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006425
Gilles Peskine2b522db2019-04-12 15:11:49 +02006426 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006427 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006428#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006430 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006432 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006434#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006435 {
6436 /* Block 0 is empty, and the next block will be
6437 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006439 }
6440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006442 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006443#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6445 return PSA_ERROR_INVALID_ARGUMENT;
6446 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006447#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006448#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6450 hkdf->state == HKDF_STATE_INIT) {
6451 return PSA_ERROR_BAD_STATE;
6452 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006453#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (hkdf->state == HKDF_STATE_OUTPUT) {
6455 return PSA_ERROR_BAD_STATE;
6456 }
6457 if (hkdf->info_set) {
6458 return PSA_ERROR_BAD_STATE;
6459 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006460 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 if (data_length != 0) {
6462 hkdf->info = mbedtls_calloc(1, data_length);
6463 if (hkdf->info == NULL) {
6464 return PSA_ERROR_INSUFFICIENT_MEMORY;
6465 }
6466 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006467 }
6468 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006470 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 }
6473}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006474#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006475
John Durkop07cc04a2020-11-16 22:08:34 -08006476#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6477 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006478static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6479 const uint8_t *data,
6480 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006481{
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6483 return PSA_ERROR_BAD_STATE;
6484 }
Janos Follathf08e2652019-06-13 09:05:41 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (data_length != 0) {
6487 prf->seed = mbedtls_calloc(1, data_length);
6488 if (prf->seed == NULL) {
6489 return PSA_ERROR_INSUFFICIENT_MEMORY;
6490 }
Janos Follathf08e2652019-06-13 09:05:41 +01006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006493 prf->seed_length = data_length;
6494 }
Janos Follathf08e2652019-06-13 09:05:41 +01006495
Gilles Peskine43f958b2020-12-13 14:55:14 +01006496 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006499}
6500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6502 const uint8_t *data,
6503 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006504{
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6506 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6507 return PSA_ERROR_BAD_STATE;
6508 }
Janos Follath81550542019-06-13 14:26:34 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (data_length != 0) {
6511 prf->secret = mbedtls_calloc(1, data_length);
6512 if (prf->secret == NULL) {
6513 return PSA_ERROR_INSUFFICIENT_MEMORY;
6514 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006517 prf->secret_length = data_length;
6518 }
Janos Follath81550542019-06-13 14:26:34 +01006519
Gilles Peskine43f958b2020-12-13 14:55:14 +01006520 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006523}
6524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6526 const uint8_t *data,
6527 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006528{
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6530 return PSA_ERROR_BAD_STATE;
6531 }
Janos Follath63028dd2019-06-13 09:15:47 +01006532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 if (data_length != 0) {
6534 prf->label = mbedtls_calloc(1, data_length);
6535 if (prf->label == NULL) {
6536 return PSA_ERROR_INSUFFICIENT_MEMORY;
6537 }
Janos Follath63028dd2019-06-13 09:15:47 +01006538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006540 prf->label_length = data_length;
6541 }
Janos Follath63028dd2019-06-13 09:15:47 +01006542
Gilles Peskine43f958b2020-12-13 14:55:14 +01006543 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006546}
6547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6549 psa_key_derivation_step_t step,
6550 const uint8_t *data,
6551 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006552{
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006554 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006556 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006558 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006560 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006562 }
6563}
John Durkop07cc04a2020-11-16 22:08:34 -08006564#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6565 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6566
6567#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6568static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6569 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006570 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006572{
6573 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6575 4 + data_length + prf->other_secret_length :
6576 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6579 return PSA_ERROR_INVALID_ARGUMENT;
6580 }
John Durkop07cc04a2020-11-16 22:08:34 -08006581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 uint8_t *pms = mbedtls_calloc(1, pms_len);
6583 if (pms == NULL) {
6584 return PSA_ERROR_INSUFFICIENT_MEMORY;
6585 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006586 uint8_t *cur = pms;
6587
6588 /* pure-PSK:
6589 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006590 *
6591 * The premaster secret is formed as follows: if the PSK is N octets
6592 * long, concatenate a uint16 with the value N, N zero octets, a second
6593 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006594 *
6595 * mixed-PSK:
6596 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6597 * follows: concatenate a uint16 with the length of the other secret,
6598 * the other secret itself, uint16 with the length of PSK, and the
6599 * PSK itself.
6600 * For details please check:
6601 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6602 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6603 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006604 */
6605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6607 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6608 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6609 if (prf->other_secret_length != 0) {
6610 memcpy(cur, prf->other_secret, prf->other_secret_length);
6611 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006612 cur += prf->other_secret_length;
6613 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 } else {
6615 *cur++ = MBEDTLS_BYTE_1(data_length);
6616 *cur++ = MBEDTLS_BYTE_0(data_length);
6617 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006618 cur += data_length;
6619 }
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 *cur++ = MBEDTLS_BYTE_1(data_length);
6622 *cur++ = MBEDTLS_BYTE_0(data_length);
6623 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006624 cur += data_length;
6625
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006626 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006627
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006628 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006630}
Janos Follath6660f0e2019-06-17 08:44:03 +01006631
Przemek Stekiele47201b2022-04-19 13:53:28 +02006632static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6633 psa_tls12_prf_key_derivation_t *prf,
6634 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006635 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006636{
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6638 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006639 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006640
6641 if (data_length != 0) {
6642 prf->other_secret = mbedtls_calloc(1, data_length);
6643 if (prf->other_secret == NULL) {
6644 return PSA_ERROR_INSUFFICIENT_MEMORY;
6645 }
6646
6647 memcpy(prf->other_secret, data, data_length);
6648 prf->other_secret_length = data_length;
6649 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006650 prf->other_secret_length = 0;
6651 }
6652
6653 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006656}
6657
Janos Follath6660f0e2019-06-17 08:44:03 +01006658static psa_status_t psa_tls12_prf_psk_to_ms_input(
6659 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006660 psa_key_derivation_step_t step,
6661 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006663{
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006665 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 return psa_tls12_prf_psk_to_ms_set_key(prf,
6667 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006668 break;
6669 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6671 data,
6672 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006673 break;
6674 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006676 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006677
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006679}
John Durkop07cc04a2020-11-16 22:08:34 -08006680#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006681
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006682#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6683static psa_status_t psa_tls12_ecjpake_to_pms_input(
6684 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006685 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006686 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006688{
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6690 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6691 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006692 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006693
6694 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 if (data[0] != 0x04) {
6696 return PSA_ERROR_INVALID_ARGUMENT;
6697 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006698
6699 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703}
6704#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306705
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306706#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306707static psa_status_t psa_pbkdf2_set_input_cost(
6708 psa_pbkdf2_key_derivation_t *pbkdf2,
6709 psa_key_derivation_step_t step,
6710 uint64_t data)
6711{
6712 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6713 return PSA_ERROR_INVALID_ARGUMENT;
6714 }
6715
6716 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6717 return PSA_ERROR_BAD_STATE;
6718 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306719
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306720 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306721 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306722 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306723
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306724 if (data == 0) {
6725 return PSA_ERROR_INVALID_ARGUMENT;
6726 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306727
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306728 pbkdf2->input_cost = data;
6729 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6730
6731 return PSA_SUCCESS;
6732}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306733
6734static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6735 const uint8_t *data,
6736 size_t data_length)
6737{
Gilles Peskineead17662023-08-20 22:02:51 +02006738 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6739 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6740 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6741 /* Appending to existing salt. No state change. */
6742 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306743 return PSA_ERROR_BAD_STATE;
6744 }
6745
Gilles Peskineca57d782023-07-20 19:08:06 +02006746 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006747 /* Appending an empty string, nothing to do. */
6748 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306749 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306750
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306751 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6752 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306753 return PSA_ERROR_INSUFFICIENT_MEMORY;
6754 }
6755
Gilles Peskineead17662023-08-20 22:02:51 +02006756 if (pbkdf2->salt_length != 0) {
6757 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6758 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306759 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306760 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306761 mbedtls_free(pbkdf2->salt);
6762 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306763 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306764 return PSA_SUCCESS;
6765}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306766
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306767#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306768static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306769 const uint8_t *input,
6770 size_t input_len,
6771 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306772 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306773{
6774 psa_status_t status = PSA_SUCCESS;
6775 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6776 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306777 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306778 } else {
6779 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306780 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306781 }
6782 return status;
6783}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306784#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306785
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306786#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306787static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6788 size_t input_len,
6789 uint8_t *output,
6790 size_t *output_len)
6791{
6792 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306793 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306795 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306796 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6797 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6798 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306799 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306800 * mac_size as the driver function sets mac_output_length = mac_size
6801 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306802 status = psa_driver_wrapper_mac_compute(&attributes,
6803 zeros, sizeof(zeros),
6804 PSA_ALG_CMAC, input, input_len,
6805 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306806 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6807 128U,
6808 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306809 output_len);
6810 } else {
6811 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306812 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306813 }
6814 return status;
6815}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306816#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306817
6818static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306819 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306820 const uint8_t *data,
6821 size_t data_length)
6822{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306823 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306824 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6825 return PSA_ERROR_BAD_STATE;
6826 }
6827
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306828#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306829 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6830 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6831 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6832 pbkdf2->password,
6833 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306834 } else
6835#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6836#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6837 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306838 status = psa_pbkdf2_cmac_set_password(data, data_length,
6839 pbkdf2->password,
6840 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306841 } else
6842#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6843 {
6844 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306845 }
6846
6847 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6848
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306849 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306850}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306851
6852static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306853 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306854 psa_key_derivation_step_t step,
6855 const uint8_t *data,
6856 size_t data_length)
6857{
6858 switch (step) {
6859 case PSA_KEY_DERIVATION_INPUT_SALT:
6860 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6861 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306862 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306863 default:
6864 return PSA_ERROR_INVALID_ARGUMENT;
6865 }
6866}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306867#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306868
Gilles Peskineb8965192019-09-24 16:21:10 +02006869/** Check whether the given key type is acceptable for the given
6870 * input step of a key derivation.
6871 *
6872 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6873 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6874 * Both secret and non-secret inputs can alternatively have the type
6875 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6876 * that the input was passed as a buffer rather than via a key object.
6877 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006878static int psa_key_derivation_check_input_type(
6879 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006881{
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006883 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if (key_type == PSA_KEY_TYPE_DERIVE) {
6885 return PSA_SUCCESS;
6886 }
6887 if (key_type == PSA_KEY_TYPE_NONE) {
6888 return PSA_SUCCESS;
6889 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006890 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006891 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (key_type == PSA_KEY_TYPE_DERIVE) {
6893 return PSA_SUCCESS;
6894 }
6895 if (key_type == PSA_KEY_TYPE_NONE) {
6896 return PSA_SUCCESS;
6897 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006898 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899 case PSA_KEY_DERIVATION_INPUT_LABEL:
6900 case PSA_KEY_DERIVATION_INPUT_SALT:
6901 case PSA_KEY_DERIVATION_INPUT_INFO:
6902 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6904 return PSA_SUCCESS;
6905 }
6906 if (key_type == PSA_KEY_TYPE_NONE) {
6907 return PSA_SUCCESS;
6908 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006909 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306910 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6911 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6912 return PSA_SUCCESS;
6913 }
6914 if (key_type == PSA_KEY_TYPE_DERIVE) {
6915 return PSA_SUCCESS;
6916 }
6917 if (key_type == PSA_KEY_TYPE_NONE) {
6918 return PSA_SUCCESS;
6919 }
6920 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006921 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006923}
6924
Janos Follathb80a94e2019-06-12 15:54:46 +01006925static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006926 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006927 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006928 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006929 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006931{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006932 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 status = psa_key_derivation_check_input_type(step, key_type);
6936 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006937 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939
Przemek Stekiel69c46792022-06-10 12:59:51 +02006940#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6942 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6943 step, data, data_length);
6944 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006945#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006946#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6948 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6949 step, data, data_length);
6950 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006951#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6952#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6954 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6955 step, data, data_length);
6956 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006957#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006958#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006960 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6962 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006963#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306964#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306965 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306966 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6967 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306968 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306969#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006970 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006971 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006972 (void) data;
6973 (void) data_length;
6974 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006976 }
6977
Gilles Peskine224b0d62019-09-23 18:13:17 +02006978exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 if (status != PSA_SUCCESS) {
6980 psa_key_derivation_abort(operation);
6981 }
6982 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006983}
6984
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306985static psa_status_t psa_key_derivation_input_integer_internal(
6986 psa_key_derivation_operation_t *operation,
6987 psa_key_derivation_step_t step,
6988 uint64_t value)
6989{
6990 psa_status_t status;
6991 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6992
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306993#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306994 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306995 status = psa_pbkdf2_set_input_cost(
6996 &operation->ctx.pbkdf2, step, value);
6997 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306998#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306999 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307000 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307001 (void) value;
7002 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307003 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307004 }
7005
7006 if (status != PSA_SUCCESS) {
7007 psa_key_derivation_abort(operation);
7008 }
7009 return status;
7010}
7011
Janos Follath51f4a0f2019-06-14 11:35:55 +01007012psa_status_t psa_key_derivation_input_bytes(
7013 psa_key_derivation_operation_t *operation,
7014 psa_key_derivation_step_t step,
7015 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007017{
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 return psa_key_derivation_input_internal(operation, step,
7019 PSA_KEY_TYPE_NONE,
7020 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007021}
7022
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307023psa_status_t psa_key_derivation_input_integer(
7024 psa_key_derivation_operation_t *operation,
7025 psa_key_derivation_step_t step,
7026 uint64_t value)
7027{
7028 return psa_key_derivation_input_integer_internal(operation, step, value);
7029}
7030
Janos Follath51f4a0f2019-06-14 11:35:55 +01007031psa_status_t psa_key_derivation_input_key(
7032 psa_key_derivation_operation_t *operation,
7033 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007035{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007036 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007037 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007038 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007039
Ronald Cron5c522922020-11-14 16:35:34 +01007040 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7042 if (status != PSA_SUCCESS) {
7043 psa_key_derivation_abort(operation);
7044 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007045 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007046
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307047 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7048 * permission to output to a key object. */
7049 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7050 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007051 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 status = psa_key_derivation_input_internal(operation,
7055 step, slot->attr.type,
7056 slot->key.data,
7057 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007058
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007059 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007062}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007063
7064
7065
7066/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007067/* Key agreement */
7068/****************************************************************/
7069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7071 const uint8_t *key_buffer,
7072 size_t key_buffer_size,
7073 psa_algorithm_t alg,
7074 const uint8_t *peer_key,
7075 size_t peer_key_length,
7076 uint8_t *shared_secret,
7077 size_t shared_secret_size,
7078 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007079{
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007081#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007082 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7084 key_buffer_size, alg,
7085 peer_key, peer_key_length,
7086 shared_secret,
7087 shared_secret_size,
7088 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007089#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007090
7091#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7092 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007093 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007094 peer_key,
7095 peer_key_length,
7096 key_buffer,
7097 key_buffer_size,
7098 shared_secret,
7099 shared_secret_size,
7100 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007101#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7102
Gilles Peskine01d718c2018-09-18 12:01:02 +02007103 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007104 (void) attributes;
7105 (void) key_buffer;
7106 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007107 (void) peer_key;
7108 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007109 (void) shared_secret;
7110 (void) shared_secret_size;
7111 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007113 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007114}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007115
Aditya Deshpande17845b82022-10-13 17:21:01 +01007116/** Internal function for raw key agreement
7117 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007118 * to the driver's implementation if a driver is present.
7119 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007120 * (psa_key_agreement_raw_builtin).
7121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007122static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7123 psa_key_slot_t *private_key,
7124 const uint8_t *peer_key,
7125 size_t peer_key_length,
7126 uint8_t *shared_secret,
7127 size_t shared_secret_size,
7128 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007129{
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7131 return PSA_ERROR_NOT_SUPPORTED;
7132 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007133
7134 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007136 };
7137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 return psa_driver_wrapper_key_agreement(&attributes,
7139 private_key->key.data,
7140 private_key->key.bytes, alg,
7141 peer_key, peer_key_length,
7142 shared_secret,
7143 shared_secret_size,
7144 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007145}
7146
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007147/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007148 * to potentially free embedded data structures and wipe confidential data.
7149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7151 psa_key_derivation_step_t step,
7152 psa_key_slot_t *private_key,
7153 const uint8_t *peer_key,
7154 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007155{
7156 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007157 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007158 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007160
7161 /* Step 1: run the secret agreement algorithm to generate the shared
7162 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 status = psa_key_agreement_raw_internal(ka_alg,
7164 private_key,
7165 peer_key, peer_key_length,
7166 shared_secret,
7167 sizeof(shared_secret),
7168 &shared_secret_length);
7169 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007172
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007173 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007174 * the shared secret. A shared secret is permitted wherever a key
7175 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 status = psa_key_derivation_input_internal(operation, step,
7177 PSA_KEY_TYPE_DERIVE,
7178 shared_secret,
7179 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7182 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007183}
7184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7186 psa_key_derivation_step_t step,
7187 mbedtls_svc_key_id_t private_key,
7188 const uint8_t *peer_key,
7189 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007193 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7196 return PSA_ERROR_INVALID_ARGUMENT;
7197 }
Ronald Cron5c522922020-11-14 16:35:34 +01007198 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7200 if (status != PSA_SUCCESS) {
7201 return status;
7202 }
7203 status = psa_key_agreement_internal(operation, step,
7204 slot,
7205 peer_key, peer_key_length);
7206 if (status != PSA_SUCCESS) {
7207 psa_key_derivation_abort(operation);
7208 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007209 /* If a private key has been added as SECRET, we allow the derived
7210 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007212 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007214 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007215
Ryan Everett5ac6fa72024-02-14 17:11:36 +00007216 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007217
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007219}
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7222 mbedtls_svc_key_id_t private_key,
7223 const uint8_t *peer_key,
7224 size_t peer_key_length,
7225 uint8_t *output,
7226 size_t output_size,
7227 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007228{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007231 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007232 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007235 status = PSA_ERROR_INVALID_ARGUMENT;
7236 goto exit;
7237 }
Ronald Cron5c522922020-11-14 16:35:34 +01007238 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7240 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007243
Gilles Peskine3e561302022-04-14 00:17:15 +02007244 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7245 * for the output size. The PSA specification only guarantees that this
7246 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7247 * but it might be nice to allow smaller buffers if the output fits.
7248 * At the time of writing this comment, with only ECDH implemented,
7249 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7250 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7251 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007252 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7254 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007255 status = PSA_ERROR_BUFFER_TOO_SMALL;
7256 goto exit;
7257 }
7258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 status = psa_key_agreement_raw_internal(alg, slot,
7260 peer_key, peer_key_length,
7261 output, output_size,
7262 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007263
7264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007266 /* If an error happens and is not handled properly, the output
7267 * may be used as a key to protect sensitive data. Arrange for such
7268 * a key to be random, which is likely to result in decryption or
7269 * verification errors. This is better than filling the buffer with
7270 * some constant data such as zeros, which would result in the data
7271 * being protected with a reproducible, easily knowable key.
7272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007274 *output_length = output_size;
7275 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007276
Ryan Everetta103ec92024-01-31 13:59:57 +00007277 unlock_status = psa_unregister_read_under_mutex(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007280}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007281
7282
Gilles Peskine30524eb2020-11-13 17:02:26 +01007283
Gilles Peskine86a440b2018-11-12 18:39:40 +01007284/****************************************************************/
7285/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007286/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007287
Gilles Peskine672a7712023-04-28 21:00:28 +02007288#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7289#include "entropy_poll.h"
7290#endif
7291
Gilles Peskine30524eb2020-11-13 17:02:26 +01007292/** Initialize the PSA random generator.
7293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007294static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007295{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007296#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007298#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7299
Gilles Peskine30524eb2020-11-13 17:02:26 +01007300 /* Set default configuration if
7301 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007303 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 }
7305 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007306 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7311 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7312 /* The PSA entropy injection feature depends on using NV seed as an entropy
7313 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 mbedtls_entropy_add_source(&rng->entropy,
7315 mbedtls_nv_seed_poll, NULL,
7316 MBEDTLS_ENTROPY_BLOCK_SIZE,
7317 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318#endif
7319
Valerio Setti061d4e42024-02-26 12:52:44 +01007320 mbedtls_psa_drbg_init(&rng->drbg);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007321#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007322}
7323
7324/** Deinitialize the PSA random generator.
7325 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007326static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007327{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007328#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007329 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007330#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti061d4e42024-02-26 12:52:44 +01007331 mbedtls_psa_drbg_free(&rng->drbg);
Valerio Setti83e0de82023-11-24 12:13:05 +01007332 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007333#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007334}
7335
7336/** Seed the PSA random generator.
7337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007338static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007339{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007340#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7341 /* Do nothing: the external RNG seeds itself. */
7342 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007344#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007345 const unsigned char drbg_seed[] = "PSA";
Valerio Setti061d4e42024-02-26 12:52:44 +01007346 int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 drbg_seed, sizeof(drbg_seed) - 1);
7348 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007349#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007350}
7351
Gilles Peskine449bd832023-01-11 14:50:10 +01007352psa_status_t psa_generate_random(uint8_t *output,
7353 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007354{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007355 GUARD_MODULE_INITIALIZED;
7356
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007357#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7358
7359 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7361 output, output_size,
7362 &output_length);
7363 if (status != PSA_SUCCESS) {
7364 return status;
7365 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007366 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007367 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 if (output_length != output_size) {
7369 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7370 }
7371 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007372
7373#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007376 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7378 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7379 output_size);
Valerio Setti061d4e42024-02-26 12:52:44 +01007380 int ret = mbedtls_psa_legacy_get_random(&global_data.rng.drbg,
Valerio Settia53e7a52024-02-26 12:03:59 +01007381 output, request_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (ret != 0) {
7383 return mbedtls_to_psa_error(ret);
7384 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007385 output_size -= request_size;
7386 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007387 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007389#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007390}
7391
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007392#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007393psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7394 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007395{
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 if (global_data.initialized) {
7397 return PSA_ERROR_NOT_PERMITTED;
7398 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007399
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7401 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7402 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7403 return PSA_ERROR_INVALID_ARGUMENT;
7404 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007405
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007407}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007408#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007409
Ronald Cron3772afe2021-02-08 16:10:05 +01007410/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007411 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007412 * \param type The key type
7413 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007414 *
7415 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007416 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007417 * \retval #PSA_ERROR_INVALID_ARGUMENT
7418 * The size in bits of the key is not valid.
7419 * \retval #PSA_ERROR_NOT_SUPPORTED
7420 * The type and/or the size in bits of the key or the combination of
7421 * the two is not supported.
7422 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007423static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007425{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007427
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 if (key_type_is_raw_bytes(type)) {
7429 status = psa_validate_unstructured_key_bit_size(type, bits);
7430 if (status != PSA_SUCCESS) {
7431 return status;
7432 }
7433 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007434#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7436 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7437 return PSA_ERROR_NOT_SUPPORTED;
7438 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007439 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007440 return PSA_ERROR_NOT_SUPPORTED;
7441 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007442
Ronald Cron01b2aba2020-10-05 09:42:02 +02007443 /* Accept only byte-aligned keys, for the same reasons as
7444 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (bits % 8 != 0) {
7446 return PSA_ERROR_NOT_SUPPORTED;
7447 }
7448 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007449#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007450
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007451#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007453 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 return PSA_SUCCESS;
7455 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007456#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007457
Valerio Settia55f0422023-07-10 15:34:41 +02007458#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007459 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007460 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007461 return PSA_ERROR_NOT_SUPPORTED;
7462 }
7463 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007464#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007467 }
7468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007470}
7471
Ronald Cron55ed0592020-10-05 10:30:40 +02007472psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007473 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007475{
7476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007477 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if ((attributes->domain_parameters == NULL) &&
7480 (attributes->domain_parameters_size != 0)) {
7481 return PSA_ERROR_INVALID_ARGUMENT;
7482 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007483
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 if (key_type_is_raw_bytes(type)) {
7485 status = psa_generate_random(key_buffer, key_buffer_size);
7486 if (status != PSA_SUCCESS) {
7487 return status;
7488 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007489
David Brown12ca5032021-02-11 11:02:00 -07007490#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 if (type == PSA_KEY_TYPE_DES) {
7492 psa_des_set_key_parity(key_buffer, key_buffer_size);
7493 }
David Brown8107e312021-02-12 08:08:46 -07007494#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007496
Valerio Setti76df8c12023-07-11 14:11:28 +02007497#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7499 return mbedtls_psa_rsa_generate_key(attributes,
7500 key_buffer,
7501 key_buffer_size,
7502 key_buffer_length);
7503 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007504#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007505
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7508 return mbedtls_psa_ecp_generate_key(attributes,
7509 key_buffer,
7510 key_buffer_size,
7511 key_buffer_length);
7512 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007513#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007514
Valerio Settia55f0422023-07-10 15:34:41 +02007515#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007516 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7517 return mbedtls_psa_ffdh_generate_key(attributes,
7518 key_buffer,
7519 key_buffer_size,
7520 key_buffer_length);
7521 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007522#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007523 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 (void) key_buffer_length;
7525 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007526 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007527
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007529}
Darryl Green0c6575a2018-11-07 16:05:30 +00007530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7532 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007533{
7534 psa_status_t status;
7535 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007536 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007537 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007538
Ronald Cron81709fc2020-11-14 12:10:32 +01007539 *key = MBEDTLS_SVC_KEY_ID_INIT;
7540
Gilles Peskine0f84d622019-09-12 19:03:13 +02007541 /* Reject any attempt to create a zero-length key so that we don't
7542 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if (psa_get_key_bits(attributes) == 0) {
7544 return PSA_ERROR_INVALID_ARGUMENT;
7545 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007546
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007547 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7549 return PSA_ERROR_INVALID_ARGUMENT;
7550 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007551
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7553 &slot, &driver);
7554 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 }
Gilles Peskine11792082019-08-06 18:36:36 +02007557
Ronald Cron977c2472020-10-13 08:32:21 +02007558 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307559 * storage ( thus not in the case of generating a key in a secure element
7560 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7561 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 if (slot->key.data == NULL) {
7563 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7564 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007565 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 attributes->core.type, attributes->core.bits);
7567 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007570
7571 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 attributes->core.type,
7573 attributes->core.bits);
7574 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007575 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 attributes, &key_buffer_size);
7577 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007578 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 }
Ronald Cron977c2472020-10-13 08:32:21 +02007580 }
Ronald Cron977c2472020-10-13 08:32:21 +02007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7583 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007584 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 }
Ronald Cron977c2472020-10-13 08:32:21 +02007586 }
7587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 status = psa_driver_wrapper_generate_key(attributes,
7589 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (status != PSA_SUCCESS) {
7592 psa_remove_key_data_from_memory(slot);
7593 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007594
Gilles Peskine11792082019-08-06 18:36:36 +02007595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 if (status == PSA_SUCCESS) {
7597 status = psa_finish_key_creation(slot, driver, key);
7598 }
7599 if (status != PSA_SUCCESS) {
7600 psa_fail_key_creation(slot, driver);
7601 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007604}
7605
Gilles Peskinee59236f2018-01-27 23:32:46 +01007606/****************************************************************/
7607/* Module setup */
7608/****************************************************************/
7609
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007610#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007611psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 void (* entropy_init)(mbedtls_entropy_context *ctx),
7613 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007614{
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7616 return PSA_ERROR_BAD_STATE;
7617 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007618 global_data.rng.entropy_init = entropy_init;
7619 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007621}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007622#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007623
Gilles Peskine449bd832023-01-11 14:50:10 +01007624void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007625{
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007627 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7628 mbedtls_psa_random_free(&global_data.rng);
7629 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007630 /* Wipe all remaining data, including configuration.
7631 * In particular, this sets all state indicator to the value
7632 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007634
7635 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007637}
7638
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007639#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7640/** Recover a transaction that was interrupted by a power failure.
7641 *
7642 * This function is called during initialization, before psa_crypto_init()
7643 * returns. If this function returns a failure status, the initialization
7644 * fails.
7645 */
7646static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007648{
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007650 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7651 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 /* TODO - fall through to the failure case until this
7653 * is implemented.
7654 * https://github.com/ARMmbed/mbed-crypto/issues/218
7655 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007656 default:
7657 /* We found an unsupported transaction in the storage.
7658 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007660 }
7661}
7662#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7663
Gilles Peskine449bd832023-01-11 14:50:10 +01007664psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007665{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007666 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007667
Gilles Peskinec6b69072018-11-20 21:42:52 +01007668 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 if (global_data.initialized != 0) {
7670 return PSA_SUCCESS;
7671 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007672
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007673 /* Init drivers */
7674 status = psa_driver_wrapper_init();
7675 if (status != PSA_SUCCESS) {
7676 goto exit;
7677 }
7678 global_data.drivers_initialized = 1;
7679
Valerio Setti402cfba2023-11-13 10:24:32 +01007680 status = psa_initialize_key_slots();
7681 if (status != PSA_SUCCESS) {
7682 goto exit;
7683 }
7684
Gilles Peskine30524eb2020-11-13 17:02:26 +01007685 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007687 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 status = mbedtls_psa_random_seed(&global_data.rng);
7689 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007692 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007693
Gilles Peskine4b734222019-07-24 15:56:31 +02007694#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 status = psa_crypto_load_transaction();
7696 if (status == PSA_SUCCESS) {
7697 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7698 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 }
7701 status = psa_crypto_stop_transaction();
7702 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007703 /* There's no transaction to complete. It's all good. */
7704 status = PSA_SUCCESS;
7705 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007706#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007707
Gilles Peskinec6b69072018-11-20 21:42:52 +01007708 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007709 global_data.initialized = 1;
7710
7711exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 if (status != PSA_SUCCESS) {
7713 mbedtls_psa_crypto_free();
7714 }
7715 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007716}
7717
Yanray Wangffc3c482023-07-11 12:01:04 +08007718#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007719psa_status_t psa_crypto_driver_pake_get_password_len(
7720 const psa_crypto_driver_pake_inputs_t *inputs,
7721 size_t *password_len)
7722{
7723 if (inputs->password_len == 0) {
7724 return PSA_ERROR_BAD_STATE;
7725 }
7726
7727 *password_len = inputs->password_len;
7728
7729 return PSA_SUCCESS;
7730}
7731
7732psa_status_t psa_crypto_driver_pake_get_password(
7733 const psa_crypto_driver_pake_inputs_t *inputs,
7734 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7735{
7736 if (inputs->password_len == 0) {
7737 return PSA_ERROR_BAD_STATE;
7738 }
7739
7740 if (buffer_size < inputs->password_len) {
7741 return PSA_ERROR_BUFFER_TOO_SMALL;
7742 }
7743
7744 memcpy(buffer, inputs->password, inputs->password_len);
7745 *buffer_length = inputs->password_len;
7746
7747 return PSA_SUCCESS;
7748}
7749
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007750psa_status_t psa_crypto_driver_pake_get_user_len(
7751 const psa_crypto_driver_pake_inputs_t *inputs,
7752 size_t *user_len)
7753{
7754 if (inputs->user_len == 0) {
7755 return PSA_ERROR_BAD_STATE;
7756 }
7757
7758 *user_len = inputs->user_len;
7759
7760 return PSA_SUCCESS;
7761}
7762
7763psa_status_t psa_crypto_driver_pake_get_user(
7764 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007765 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007766{
7767 if (inputs->user_len == 0) {
7768 return PSA_ERROR_BAD_STATE;
7769 }
7770
Przemek Stekielc0e62502023-03-14 11:49:36 +01007771 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007772 return PSA_ERROR_BUFFER_TOO_SMALL;
7773 }
7774
Przemek Stekielc0e62502023-03-14 11:49:36 +01007775 memcpy(user_id, inputs->user, inputs->user_len);
7776 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007777
7778 return PSA_SUCCESS;
7779}
7780
7781psa_status_t psa_crypto_driver_pake_get_peer_len(
7782 const psa_crypto_driver_pake_inputs_t *inputs,
7783 size_t *peer_len)
7784{
7785 if (inputs->peer_len == 0) {
7786 return PSA_ERROR_BAD_STATE;
7787 }
7788
7789 *peer_len = inputs->peer_len;
7790
7791 return PSA_SUCCESS;
7792}
7793
7794psa_status_t psa_crypto_driver_pake_get_peer(
7795 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007796 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007797{
7798 if (inputs->peer_len == 0) {
7799 return PSA_ERROR_BAD_STATE;
7800 }
7801
Przemek Stekielc0e62502023-03-14 11:49:36 +01007802 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007803 return PSA_ERROR_BUFFER_TOO_SMALL;
7804 }
7805
Przemek Stekielc0e62502023-03-14 11:49:36 +01007806 memcpy(peer_id, inputs->peer, inputs->peer_len);
7807 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007808
7809 return PSA_SUCCESS;
7810}
7811
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007812psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7813 const psa_crypto_driver_pake_inputs_t *inputs,
7814 psa_pake_cipher_suite_t *cipher_suite)
7815{
7816 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7817 return PSA_ERROR_BAD_STATE;
7818 }
7819
7820 *cipher_suite = inputs->cipher_suite;
7821
7822 return PSA_SUCCESS;
7823}
7824
Neil Armstronga7d08c32022-06-01 18:21:20 +02007825psa_status_t psa_pake_setup(
7826 psa_pake_operation_t *operation,
7827 const psa_pake_cipher_suite_t *cipher_suite)
7828{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007831 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007832 status = PSA_ERROR_BAD_STATE;
7833 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007834 }
7835
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007836 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007837 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007838 status = PSA_ERROR_INVALID_ARGUMENT;
7839 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007840 }
7841
Przemek Stekiel51eac532022-12-07 11:04:51 +01007842 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7843
Przemek Stekiele12ed362022-12-21 12:54:46 +01007844 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007845 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7846 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007847 operation->data.inputs.cipher_suite = *cipher_suite;
7848
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007849#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007850 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007851 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007852 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007853
David Horstmann16f01512023-06-14 17:21:07 +01007854 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007855 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007856 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007857#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007858 {
7859 status = PSA_ERROR_NOT_SUPPORTED;
7860 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861 }
7862
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007863 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7864
Przemek Stekiel51eac532022-12-07 11:04:51 +01007865 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007866exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007867 psa_pake_abort(operation);
7868 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007869}
7870
7871psa_status_t psa_pake_set_password_key(
7872 psa_pake_operation_t *operation,
7873 mbedtls_svc_key_id_t password)
7874{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007876 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7877 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007878 psa_key_attributes_t attributes;
7879 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007880
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882 status = PSA_ERROR_BAD_STATE;
7883 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007884 }
7885
Przemek Stekiel6c764412022-11-22 14:05:12 +01007886 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7887 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007888 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007889 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007891 }
7892
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007893 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007894 .core = slot->attr
7895 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007896
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007897 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007898
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899 if (type != PSA_KEY_TYPE_PASSWORD &&
7900 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7901 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007903 }
7904
Przemek Stekiel51eac532022-12-07 11:04:51 +01007905 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7906 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007907 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007908 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007909 }
7910
7911 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7912 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007913 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007915 if (status != PSA_SUCCESS) {
7916 psa_pake_abort(operation);
7917 }
Ryan Everettbbedfce2024-02-14 18:22:09 +00007918 unlock_status = psa_unregister_read_under_mutex(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007919 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_user(
7923 psa_pake_operation_t *operation,
7924 const uint8_t *user_id,
7925 size_t user_id_len)
7926{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007928
7929 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007930 status = PSA_ERROR_BAD_STATE;
7931 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932 }
7933
Przemek Stekiel51eac532022-12-07 11:04:51 +01007934 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007935 status = PSA_ERROR_INVALID_ARGUMENT;
7936 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007937 }
7938
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007939 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007940 status = PSA_ERROR_BAD_STATE;
7941 goto exit;
7942 }
7943
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007944 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7945 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007946 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7947 goto exit;
7948 }
7949
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007950 memcpy(operation->data.inputs.user, user_id, user_id_len);
7951 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007952
7953 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007955 psa_pake_abort(operation);
7956 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007957}
7958
7959psa_status_t psa_pake_set_peer(
7960 psa_pake_operation_t *operation,
7961 const uint8_t *peer_id,
7962 size_t peer_id_len)
7963{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007965
7966 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 status = PSA_ERROR_BAD_STATE;
7968 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007969 }
7970
Przemek Stekiel51eac532022-12-07 11:04:51 +01007971 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 status = PSA_ERROR_INVALID_ARGUMENT;
7973 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007974 }
7975
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007976 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007977 status = PSA_ERROR_BAD_STATE;
7978 goto exit;
7979 }
7980
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007981 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7982 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007983 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7984 goto exit;
7985 }
7986
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007987 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7988 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007989
7990 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007991exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007992 psa_pake_abort(operation);
7993 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007994}
7995
7996psa_status_t psa_pake_set_role(
7997 psa_pake_operation_t *operation,
7998 psa_pake_role_t role)
7999{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008001
Przemek Stekiel51eac532022-12-07 11:04:51 +01008002 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008003 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008004 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008005 }
8006
Przemek Stekiel09104b82023-03-06 14:11:51 +01008007 switch (operation->alg) {
8008#if defined(PSA_WANT_ALG_JPAKE)
8009 case PSA_ALG_JPAKE:
8010 if (role == PSA_PAKE_ROLE_NONE) {
8011 return PSA_SUCCESS;
8012 }
8013 status = PSA_ERROR_INVALID_ARGUMENT;
8014 break;
8015#endif
8016 default:
8017 (void) role;
8018 status = PSA_ERROR_NOT_SUPPORTED;
8019 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008020 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008021exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008022 psa_pake_abort(operation);
8023 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008024}
8025
David Horstmanne7f21e62023-05-12 18:17:21 +01008026/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008027#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008028static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008029 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008030{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008031 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008032 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008033 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008034
David Horstmann024e5c52023-06-14 15:48:21 +01008035 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008036 is_x1 = (stage->outputs < 1);
8037 } else {
8038 is_x1 = (stage->inputs < 1);
8039 }
8040
David Horstmann74a3d8c2023-06-14 18:28:19 +01008041 key_share_step = is_x1 ?
8042 PSA_JPAKE_X1_STEP_KEY_SHARE :
8043 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008044 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008045 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8046 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8047 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8048 } else {
8049 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008050 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008051 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008052}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008053#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008054
Przemek Stekiel2797d372022-12-22 11:19:22 +01008055static psa_status_t psa_pake_complete_inputs(
8056 psa_pake_operation_t *operation)
8057{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008059 /* Create copy of the inputs on stack as inputs share memory
8060 with the driver context which will be setup by the driver. */
8061 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008062
Przemek Stekielfde11282023-03-13 16:06:09 +01008063 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008064 return PSA_ERROR_BAD_STATE;
8065 }
8066
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008067 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008068 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8069 return PSA_ERROR_BAD_STATE;
8070 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008071 }
8072
Przemek Stekiel18620a32023-01-17 16:34:52 +01008073 /* Clear driver context */
8074 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8075
8076 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008077
8078 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008079 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008080
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008081 /* User and peer are translated to role. */
8082 mbedtls_free(inputs.user);
8083 mbedtls_free(inputs.peer);
8084
Przemek Stekiel2797d372022-12-22 11:19:22 +01008085 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008086#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008087 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008088 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008089 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008090#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008091 {
8092 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008093 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008094 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008095 return status;
8096}
8097
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008098#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008099static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008100 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008101 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008102 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008103{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008104 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8105 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8106 step != PSA_PAKE_STEP_ZK_PROOF) {
8107 return PSA_ERROR_INVALID_ARGUMENT;
8108 }
8109
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008110 psa_jpake_computation_stage_t *computation_stage =
8111 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008112
David Horstmann5da95602023-06-08 15:37:12 +01008113 if (computation_stage->round != PSA_JPAKE_FIRST &&
8114 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008115 return PSA_ERROR_BAD_STATE;
8116 }
8117
David Horstmanne7f21e62023-05-12 18:17:21 +01008118 /* Check that the step we are given is the one we were expecting */
8119 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008120 return PSA_ERROR_BAD_STATE;
8121 }
8122
David Horstmanne7f21e62023-05-12 18:17:21 +01008123 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8124 computation_stage->inputs == 0 &&
8125 computation_stage->outputs == 0) {
8126 /* Start of the round, so function decides whether we are inputting
8127 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008128 computation_stage->io_mode = io_mode;
8129 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008130 /* Middle of the round so the mode we are in must match the function
8131 * called by the user */
8132 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008133 }
8134
8135 return PSA_SUCCESS;
8136}
8137
David Horstmanne7f21e62023-05-12 18:17:21 +01008138static psa_status_t psa_jpake_epilogue(
8139 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008140 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141{
David Horstmanne7f21e62023-05-12 18:17:21 +01008142 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008143 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144
David Horstmanne7f21e62023-05-12 18:17:21 +01008145 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8146 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008147 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008148 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008149 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008150 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008151 }
8152 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008153 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008154 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008155 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008156 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008157 }
8158 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008159 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8160 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008161 /* End of a round, move to the next round */
8162 stage->inputs = 0;
8163 stage->outputs = 0;
8164 stage->round++;
8165 }
8166 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008167 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008168 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 return PSA_SUCCESS;
8171}
David Horstmanne7f21e62023-05-12 18:17:21 +01008172
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008173#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008174
Neil Armstronga7d08c32022-06-01 18:21:20 +02008175psa_status_t psa_pake_output(
8176 psa_pake_operation_t *operation,
8177 psa_pake_step_t step,
8178 uint8_t *output,
8179 size_t output_size,
8180 size_t *output_length)
8181{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008183 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008184 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008185
8186 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008187 status = psa_pake_complete_inputs(operation);
8188 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008190 }
8191 }
8192
8193 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008194 status = PSA_ERROR_BAD_STATE;
8195 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008196 }
8197
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008198 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008199 status = PSA_ERROR_INVALID_ARGUMENT;
8200 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008201 }
8202
Przemek Stekiele12ed362022-12-21 12:54:46 +01008203 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008204#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008205 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008206 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008208 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008210 driver_step = convert_jpake_computation_stage_to_driver_step(
8211 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008212 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008213#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008214 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008215 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008216 status = PSA_ERROR_NOT_SUPPORTED;
8217 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 }
8219
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008220 status = psa_driver_wrapper_pake_output(operation, driver_step,
8221 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222
8223 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008224 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225 }
8226
8227 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008228#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008229 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008230 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008231 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 }
8234 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008235#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008236 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 status = PSA_ERROR_NOT_SUPPORTED;
8238 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 }
8240
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008241 return PSA_SUCCESS;
8242exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008243 psa_pake_abort(operation);
8244 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008245}
8246
8247psa_status_t psa_pake_input(
8248 psa_pake_operation_t *operation,
8249 psa_pake_step_t step,
8250 const uint8_t *input,
8251 size_t input_length)
8252{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008254 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008255 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8256 operation->primitive,
8257 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008258
8259 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008260 status = psa_pake_complete_inputs(operation);
8261 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008263 }
8264 }
8265
8266 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_BAD_STATE;
8268 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008269 }
8270
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008271 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 status = PSA_ERROR_INVALID_ARGUMENT;
8273 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008274 }
8275
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008277#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008278 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008279 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008283 driver_step = convert_jpake_computation_stage_to_driver_step(
8284 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008286#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008288 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008289 status = PSA_ERROR_NOT_SUPPORTED;
8290 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008291 }
8292
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008293 status = psa_driver_wrapper_pake_input(operation, driver_step,
8294 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008295
8296 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008297 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008298 }
8299
8300 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008301#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008302 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008303 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008304 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008305 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008306 }
8307 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008308#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008309 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008310 status = PSA_ERROR_NOT_SUPPORTED;
8311 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008312 }
8313
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008314 return PSA_SUCCESS;
8315exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008316 psa_pake_abort(operation);
8317 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008318}
8319
8320psa_status_t psa_pake_get_implicit_key(
8321 psa_pake_operation_t *operation,
8322 psa_key_derivation_operation_t *output)
8323{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008325 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008326 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008327 size_t shared_key_len = 0;
8328
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008329 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008330 status = PSA_ERROR_BAD_STATE;
8331 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008332 }
8333
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008334#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008336 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008337 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008338 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008339 status = PSA_ERROR_BAD_STATE;
8340 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008341 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008342 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008343#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008344 {
8345 status = PSA_ERROR_NOT_SUPPORTED;
8346 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 }
8348
Przemek Stekiel0c781802022-11-29 14:53:13 +01008349 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8350 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008351 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008352 &shared_key_len);
8353
8354 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008355 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008356 }
8357
8358 status = psa_key_derivation_input_bytes(output,
8359 PSA_KEY_DERIVATION_INPUT_SECRET,
8360 shared_key,
8361 shared_key_len);
8362
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008363 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008364exit:
8365 abort_status = psa_pake_abort(operation);
8366 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008367}
8368
8369psa_status_t psa_pake_abort(
8370 psa_pake_operation_t *operation)
8371{
Przemek Stekield69dca92023-01-31 19:59:20 +01008372 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008373
Przemek Stekield69dca92023-01-31 19:59:20 +01008374 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008375 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008376 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008377
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008378 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8379 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008380 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008381 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008382 }
8383 if (operation->data.inputs.user != NULL) {
8384 mbedtls_free(operation->data.inputs.user);
8385 }
8386 if (operation->data.inputs.peer != NULL) {
8387 mbedtls_free(operation->data.inputs.peer);
8388 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008389 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008390 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008391
Przemek Stekield69dca92023-01-31 19:59:20 +01008392 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008393}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008394#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008395
Gilles Peskinee59236f2018-01-27 23:32:46 +01008396#endif /* MBEDTLS_PSA_CRYPTO_C */