blob: a9ba787d0fe082f1501daf3e186271d3f142f504 [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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100113int psa_can_do_hash(psa_algorithm_t hash_alg)
114{
115 (void) hash_alg;
116 return global_data.drivers_initialized;
117}
Valerio Settic6f004f2023-12-12 11:27:36 +0100118
119int psa_can_do_cipher(psa_algorithm_t cipher_alg)
120{
121 (void) cipher_alg;
122 return global_data.drivers_initialized;
123}
124
125
Valerio Settia55f0422023-07-10 15:34:41 +0200126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200128 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200129static int psa_is_dh_key_size_valid(size_t bits)
130{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200131 if (bits != 2048 && bits != 3072 && bits != 4096 &&
132 bits != 6144 && bits != 8192) {
133 return 0;
134 }
135
136 return 1;
137}
Valerio Settia55f0422023-07-10 15:34:41 +0200138#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200139 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200140 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100144 /* Mbed TLS error codes can combine a high-level error code and a
145 * low-level error code. The low-level error usually reflects the
146 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 int low_level_ret = -(-ret & 0x007f);
148 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100151
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100152#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
154 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100156 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
157 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100158#endif
159
160#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
162 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
163 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
164 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
165 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100171#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200172
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100173#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000174 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100177#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100178
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100179#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100184#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100185
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100186#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200187 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100189#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200190
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100191#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100196#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200197
Dave Rodgman509b5672023-08-16 19:26:23 +0100198#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100213#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
216 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100217 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
218 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
222 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100227
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100228#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100231#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Gilles Peskinee59236f2018-01-27 23:32:46 +0100233 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
234 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
235 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100238#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200241 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100245#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
Gilles Peskine82e57d12020-11-13 21:31:17 +0100247#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100249 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
250 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100251 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
254 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100258#endif
259
Dave Rodgmandea266f2023-08-31 11:52:43 +0100260#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100265 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100267#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100270#endif
271#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100272
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#if defined(MBEDTLS_BIGNUM_C)
274#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100275 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100277#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100288 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100290 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100292#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100293
Dave Rodgman509b5672023-08-16 19:26:23 +0100294#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100295 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
298 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100300#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
301 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100304#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
306 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
311 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100313 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 case MBEDTLS_ERR_PK_INVALID_ALG:
316 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
317 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200321 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100323#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100324
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200327 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200329
Dave Rodgman509b5672023-08-16 19:26:23 +0100330#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
340 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100348#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200349
Dave Rodgman1dab4452023-09-01 09:59:51 +0100350#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300351 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300352 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300354 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300356 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300358 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
359 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100363 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100365
Dave Rodgman509b5672023-08-16 19:26:23 +0100366#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000367 case MBEDTLS_ERR_ECP_IN_PROGRESS:
368 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100369#endif
370#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000371
Janos Follatha13b9052019-11-22 12:48:59 +0000372 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100377 }
378}
379
Paul Elliottdc42ca82023-02-24 18:11:59 +0000380/**
381 * \brief For output buffers which contain "tags"
382 * (outputs that may be checked for validity like
383 * hashes, MACs and signatures), fill the unused
384 * part of the output buffer (the whole buffer on
385 * error, the trailing part on success) with
386 * something that isn't a valid tag (barring an
387 * attack on the tag and deliberately-crafted
388 * input), in case the caller doesn't check the
389 * return status properly.
390 *
391 * \param output_buffer Pointer to buffer to wipe. May not be NULL
392 * unless \p output_buffer_size is zero.
393 * \param status Status of function called to generate
394 * output_buffer originally
395 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
396 * could be NULL.
397 * \param output_buffer_length Length of data written to output_buffer, must be
398 * less than \p output_buffer_size
399 */
400static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000401 size_t output_buffer_size, size_t output_buffer_length)
402{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000403 size_t offset = 0;
404
405 if (output_buffer_size == 0) {
406 /* If output_buffer_size is 0 then we have nothing to do. We must not
407 call memset because output_buffer may be NULL in this case */
408 return;
409 }
410
411 if (status == PSA_SUCCESS) {
412 offset = output_buffer_length;
413 }
414
415 memset(output_buffer + offset, '!', output_buffer_size - offset);
416}
417
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200418
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200419
420
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421/****************************************************************/
422/* Key management */
423/****************************************************************/
424
Valerio Settia9aab1a2023-06-19 13:39:54 +0200425#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200426psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
427 size_t *bits)
428{
429 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200430#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200431 case MBEDTLS_ECP_DP_SECP192R1:
432 *bits = 192;
433 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100434#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200435#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200436 case MBEDTLS_ECP_DP_SECP224R1:
437 *bits = 224;
438 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100439#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200440#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200441 case MBEDTLS_ECP_DP_SECP256R1:
442 *bits = 256;
443 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100444#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200445#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200446 case MBEDTLS_ECP_DP_SECP384R1:
447 *bits = 384;
448 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100449#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200450#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200451 case MBEDTLS_ECP_DP_SECP521R1:
452 *bits = 521;
453 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100454#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200455#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200456 case MBEDTLS_ECP_DP_BP256R1:
457 *bits = 256;
458 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100459#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200460#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200461 case MBEDTLS_ECP_DP_BP384R1:
462 *bits = 384;
463 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100464#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200465#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200466 case MBEDTLS_ECP_DP_BP512R1:
467 *bits = 512;
468 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100469#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200470#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200471 case MBEDTLS_ECP_DP_CURVE25519:
472 *bits = 255;
473 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100474#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200475#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200476 case MBEDTLS_ECP_DP_SECP192K1:
477 *bits = 192;
478 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100479#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200480#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200481 case MBEDTLS_ECP_DP_SECP224K1:
482 *bits = 224;
483 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100484#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200485#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200486 case MBEDTLS_ECP_DP_SECP256K1:
487 *bits = 256;
488 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100489#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200490#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200491 case MBEDTLS_ECP_DP_CURVE448:
492 *bits = 448;
493 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100494#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200495 default:
496 *bits = 0;
497 return 0;
498 }
499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
502 size_t bits,
503 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200504{
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100506 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100508#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#endif
512#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100519#endif
520#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100523#endif
524#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100525 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (bits_is_sloppy) {
529 return MBEDTLS_ECP_DP_SECP521R1;
530 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100532#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100533 }
534 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100535
Paul Elliott8ff510a2020-06-02 17:19:28 +0100536 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100538#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100539 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#endif
542#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100543 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100545#endif
546#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100547 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100549#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100550 }
551 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100552
Paul Elliott8ff510a2020-06-02 17:19:28 +0100553 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100556 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (bits_is_sloppy) {
560 return MBEDTLS_ECP_DP_CURVE25519;
561 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100563#endif
564#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100565 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100567#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100568 }
569 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100570
Paul Elliott8ff510a2020-06-02 17:19:28 +0100571 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100573#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100574 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#endif
577#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100578 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100580#endif
581#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100582 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100584#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100585 }
586 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200587 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100588
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100589 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200591}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200592#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
595 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596{
597 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200599 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200601 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200602 case PSA_KEY_TYPE_PASSWORD:
603 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200604 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100605#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (bits != 128 && bits != 192 && bits != 256) {
608 return PSA_ERROR_INVALID_ARGUMENT;
609 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200610 break;
611#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200612#if defined(PSA_WANT_KEY_TYPE_ARIA)
613 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (bits != 128 && bits != 192 && bits != 256) {
615 return PSA_ERROR_INVALID_ARGUMENT;
616 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200617 break;
618#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100619#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200620 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (bits != 128 && bits != 192 && bits != 256) {
622 return PSA_ERROR_INVALID_ARGUMENT;
623 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200624 break;
625#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100626#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (bits != 64 && bits != 128 && bits != 192) {
629 return PSA_ERROR_INVALID_ARGUMENT;
630 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200631 break;
632#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100633#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200634 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (bits != 256) {
636 return PSA_ERROR_INVALID_ARGUMENT;
637 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200638 break;
639#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200640 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200642 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (bits % 8 != 0) {
644 return PSA_ERROR_INVALID_ARGUMENT;
645 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200648}
649
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100650/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100651 *
Steven Cooremancd640932021-03-08 12:00:27 +0100652 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
653 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100654 *
655 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100656 * \param[in] key_type The key type of the key to be used with the
657 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100658 *
659 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100660 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100661 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100662 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100663 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100664MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100665 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100667{
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (PSA_ALG_IS_HMAC(algorithm)) {
669 if (key_type == PSA_KEY_TYPE_HMAC) {
670 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100671 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100672 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
675 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
676 * key. */
677 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
678 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
679 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
680 * the block length (larger than 1) for block ciphers. */
681 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
682 return PSA_SUCCESS;
683 }
684 }
685 }
686
687 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
691 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200692{
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (slot->key.data != NULL) {
694 return PSA_ERROR_ALREADY_EXISTS;
695 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 slot->key.data = mbedtls_calloc(1, buffer_length);
698 if (slot->key.data == NULL) {
699 return PSA_ERROR_INSUFFICIENT_MEMORY;
700 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200701
Ronald Cronea0f8a62020-11-25 17:52:23 +0100702 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200704}
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
707 const uint8_t *data,
708 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200709{
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 psa_status_t status = psa_allocate_buffer_to_slot(slot,
711 data_length);
712 if (status != PSA_SUCCESS) {
713 return status;
714 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 memcpy(slot->key.data, data, data_length);
717 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200718}
719
Ronald Crona0fe59f2020-11-29 11:14:53 +0100720psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100721 const psa_key_attributes_t *attributes,
722 const uint8_t *data, size_t data_length,
723 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
727 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200729 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (data_length == 0) {
731 return PSA_ERROR_NOT_SUPPORTED;
732 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (key_type_is_raw_bytes(type)) {
735 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
738 *bits);
739 if (status != PSA_SUCCESS) {
740 return status;
741 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200742
Ronald Crondd04d422020-11-28 15:14:42 +0100743 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100745 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return PSA_SUCCESS;
749 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200750#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200751 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100752 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200753 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100754 return PSA_ERROR_INVALID_ARGUMENT;
755 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200756 return mbedtls_psa_ffdh_import_key(attributes,
757 data, data_length,
758 key_buffer, key_buffer_size,
759 key_buffer_length,
760 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100761 }
Valerio Settia55f0422023-07-10 15:34:41 +0200762#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200763 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200764#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
766 if (PSA_KEY_TYPE_IS_ECC(type)) {
767 return mbedtls_psa_ecp_import_key(attributes,
768 data, data_length,
769 key_buffer, key_buffer_size,
770 key_buffer_length,
771 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100772 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200773#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800774 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200775#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
776 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
778 if (PSA_KEY_TYPE_IS_RSA(type)) {
779 return mbedtls_psa_rsa_import_key(attributes,
780 data, data_length,
781 key_buffer, key_buffer_size,
782 key_buffer_length,
783 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200784 }
Valerio Settife478902023-07-25 12:27:19 +0200785#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
786 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800787 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100788 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100791}
792
Gilles Peskinef603c712019-01-19 13:40:11 +0100793/** Calculate the intersection of two algorithm usage policies.
794 *
795 * Return 0 (which allows no operation) on incompatibility.
796 */
797static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100798 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100799 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100801{
Gilles Peskine549ea862019-05-22 11:45:59 +0200802 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (alg1 == alg2) {
804 return alg1;
805 }
Steven Cooreman03488022021-02-18 12:07:20 +0100806 /* If the policies are from the same hash-and-sign family, check
807 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
809 PSA_ALG_IS_SIGN_HASH(alg2) &&
810 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
811 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
812 return alg2;
813 }
814 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
815 return alg1;
816 }
Steven Cooreman03488022021-02-18 12:07:20 +0100817 }
818 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100819 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100820 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
822 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
823 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
824 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
825 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100826 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100827
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100828 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
830 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
831 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
832 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100833 }
834 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
836 (alg1_len <= alg2_len)) {
837 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100838 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
840 (alg2_len <= alg1_len)) {
841 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100842 }
843 }
844 /* If the policies are from the same MAC family, check whether one
845 * of them is a minimum-MAC-length policy. Calculate the most
846 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
848 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
849 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100850 /* Validate the combination of key type and algorithm. Since the base
851 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
853 return 0;
854 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100855
Steven Cooreman31a876d2021-03-03 20:47:40 +0100856 /* Get the (exact or at-least) output lengths for both sides of the
857 * requested intersection. None of the currently supported algorithms
858 * have an output length dependent on the actual key size, so setting it
859 * to a bogus value of 0 is currently OK.
860 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100861 * Note that for at-least-this-length wildcard algorithms, the output
862 * length is set to the shortest allowed length, which allows us to
863 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
865 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100866 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867
Steven Cooremana1d83222021-02-25 10:20:29 +0100868 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
870 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
871 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100872 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100873
874 /* If only one is an at-least-this-length policy, the intersection would
875 * be the other (fixed-length) policy as long as said fixed length is
876 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
878 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100879 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
881 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100882 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100883
Steven Cooremancd640932021-03-08 12:00:27 +0100884 /* If none of them are wildcards, check whether they define the same tag
885 * length. This is still possible here when one is default-length and
886 * the other specific-length. Ensure to always return the
887 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (alg1_len == alg2_len) {
889 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
890 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100891 }
892 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100894}
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896static int psa_key_algorithm_permits(psa_key_type_t key_type,
897 psa_algorithm_t policy_alg,
898 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200899{
Gilles Peskine549ea862019-05-22 11:45:59 +0200900 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (requested_alg == policy_alg) {
902 return 1;
903 }
Steven Cooreman03488022021-02-18 12:07:20 +0100904 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
905 * and requested_alg is the same hash-and-sign family with any hash,
906 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
908 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
909 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
910 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100911 }
912 /* If policy_alg is a wildcard AEAD algorithm of the same base as
913 * the requested algorithm, check the requested tag length to be
914 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (PSA_ALG_IS_AEAD(policy_alg) &&
916 PSA_ALG_IS_AEAD(requested_alg) &&
917 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
918 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
919 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
920 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
921 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100922 }
Steven Cooremancd640932021-03-08 12:00:27 +0100923 /* If policy_alg is a MAC algorithm of the same base as the requested
924 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (PSA_ALG_IS_MAC(policy_alg) &&
926 PSA_ALG_IS_MAC(requested_alg) &&
927 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
928 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100929 /* Validate the combination of key type and algorithm. Since the policy
930 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
932 return 0;
933 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100934
Steven Cooreman31a876d2021-03-03 20:47:40 +0100935 /* Get both the requested output length for the algorithm which is to be
936 * verified, and the default output length for the base algorithm.
937 * Note that none of the currently supported algorithms have an output
938 * length dependent on actual key size, so setting it to a bogus value
939 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100940 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100942 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 key_type, 0,
944 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100945
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100946 /* If the policy is default-length, only allow an algorithm with
947 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
949 return requested_output_length == default_output_length;
950 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100951
952 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100953 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
955 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
956 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957 }
958
Steven Cooremancd640932021-03-08 12:00:27 +0100959 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
960 * check for the requested MAC length to be equal to or longer than the
961 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
963 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
964 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100965 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200966 }
Steven Cooremance48e852020-10-05 16:02:45 +0200967 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200968 * a key derivation with that key agreement should also be allowed. This
969 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
971 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
972 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
973 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200974 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100975 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200977}
978
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100979/** Test whether a policy permits an algorithm.
980 *
981 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100982 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100983 * \note This function requires providing the key type for which the policy is
984 * being validated, since some algorithm policy definitions (e.g. MAC)
985 * have different properties depending on what kind of cipher it is
986 * combined with.
987 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100988 * \retval PSA_SUCCESS When \p alg is a specific algorithm
989 * allowed by the \p policy.
990 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
991 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
992 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
995 psa_key_type_t key_type,
996 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100997{
Steven Cooremand788fab2021-02-25 11:29:17 +0100998 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (alg == 0) {
1000 return PSA_ERROR_INVALID_ARGUMENT;
1001 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001002
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (PSA_ALG_IS_WILDCARD(alg)) {
1005 return PSA_ERROR_INVALID_ARGUMENT;
1006 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1009 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1010 return PSA_SUCCESS;
1011 } else {
1012 return PSA_ERROR_NOT_PERMITTED;
1013 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001014}
1015
Gilles Peskinef603c712019-01-19 13:40:11 +01001016/** Restrict a key policy based on a constraint.
1017 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001018 * \note This function requires providing the key type for which the policy is
1019 * being restricted, since some algorithm policy definitions (e.g. MAC)
1020 * have different properties depending on what kind of cipher it is
1021 * combined with.
1022 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001023 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001024 * \param[in,out] policy The policy to restrict.
1025 * \param[in] constraint The policy constraint to apply.
1026 *
1027 * \retval #PSA_SUCCESS
1028 * \c *policy contains the intersection of the original value of
1029 * \c *policy and \c *constraint.
1030 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001031 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001032 * \c *policy is unchanged.
1033 */
1034static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001035 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001036 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001038{
1039 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1041 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001042 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1044 constraint->alg2);
1045 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1046 return PSA_ERROR_INVALID_ARGUMENT;
1047 }
1048 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1049 return PSA_ERROR_INVALID_ARGUMENT;
1050 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001051 policy->usage &= constraint->usage;
1052 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001053 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001055}
1056
Przemek Stekiel061f6942022-11-30 10:51:35 +01001057/** Get the description of a key given its identifier and policy constraints
1058 * and lock it.
1059 *
1060 * The key must have allow all the usage flags set in \p usage. If \p alg is
1061 * nonzero, the key must allow operations with this algorithm. If \p alg is
1062 * zero, the algorithm is not checked.
1063 *
1064 * In case of a persistent key, the function loads the description of the key
1065 * into a key slot if not already done.
1066 *
1067 * On success, the returned key slot is locked. It is the responsibility of
1068 * the caller to unlock the key slot when it does not access it anymore.
1069 */
1070static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001071 mbedtls_svc_key_id_t key,
1072 psa_key_slot_t **p_slot,
1073 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001075{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001077 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 status = psa_get_and_lock_key_slot(key, p_slot);
1080 if (status != PSA_SUCCESS) {
1081 return status;
1082 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001083 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001084
1085 /* Enforce that usage policy for the key slot contains all the flags
1086 * required by the usage parameter. There is one exception: public
1087 * keys can always be exported, so we treat public key objects as
1088 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001090 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001094 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001095 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001096 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001098 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (alg != 0) {
1100 status = psa_key_policy_permits(&slot->attr.policy,
1101 slot->attr.type,
1102 alg);
1103 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001104 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001106 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001109
1110error:
1111 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001115}
Darryl Green940d72c2018-07-13 13:18:51 +01001116
Ronald Cron5c522922020-11-14 16:35:34 +01001117/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001118 *
1119 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001120 * available, as opposed to a key in a secure element and/or to be used
1121 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001123 * This is a temporary function that may be used instead of
1124 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1125 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001126 *
Ronald Cron5c522922020-11-14 16:35:34 +01001127 * On success, the returned key slot is locked. It is the responsibility of the
1128 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001129 */
Ronald Cron5c522922020-11-14 16:35:34 +01001130static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1131 mbedtls_svc_key_id_t key,
1132 psa_key_slot_t **p_slot,
1133 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001135{
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1137 usage, alg);
1138 if (status != PSA_SUCCESS) {
1139 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001140 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1143 psa_unlock_key_slot(*p_slot);
1144 *p_slot = NULL;
1145 return PSA_ERROR_NOT_SUPPORTED;
1146 }
1147
1148 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001149}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001152{
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001154 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001156
Ronald Cronea0f8a62020-11-25 17:52:23 +01001157 slot->key.data = NULL;
1158 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001161}
1162
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001163/** Completely wipe a slot in memory, including its policy.
1164 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001166{
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 /*
1170 * As the return error code may not be handled in case of multiple errors,
1171 * do our best to report an unexpected lock counter. Assert with
1172 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1173 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1174 * function is called as part of the execution of a test suite, the
1175 * execution of the test suite is stopped in error if the assertion fails.
1176 */
1177 if (slot->lock_count != 1) {
1178 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001179 status = PSA_ERROR_CORRUPTION_DETECTED;
1180 }
1181
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001182 /* Multipart operations may still be using the key. This is safe
1183 * because all multipart operation objects are independent from
1184 * the key slot: if they need to access the key after the setup
1185 * phase, they have a copy of the key. Note that this means that
1186 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001187 /* At this point, key material and other type-specific content has
1188 * been wiped. Clear remaining metadata. We can call memset and not
1189 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 memset(slot, 0, sizeof(*slot));
1191 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001192}
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001196 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 psa_status_t status; /* status of the last operation */
1198 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001199#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1200 psa_se_drv_table_entry_t *driver;
1201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (mbedtls_svc_key_id_is_null(key)) {
1204 return PSA_SUCCESS;
1205 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001206
Ronald Cronf2911112020-10-29 17:51:10 +01001207 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001208 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001209 * key, this will load the key description from persistent memory if not
1210 * done yet. We cannot avoid this loading as without it we don't know if
1211 * the key is operated by an SE or not and this information is needed by
1212 * the current implementation.
1213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 status = psa_get_and_lock_key_slot(key, &slot);
1215 if (status != PSA_SUCCESS) {
1216 return status;
1217 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001218
Ronald Cronf2911112020-10-29 17:51:10 +01001219 /*
1220 * If the key slot containing the key description is under access by the
1221 * library (apart from the present access), the key cannot be destroyed
1222 * yet. For the time being, just return in error. Eventually (to be
1223 * implemented), the key should be destroyed when all accesses have
1224 * stopped.
1225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (slot->lock_count > 1) {
1227 psa_unlock_key_slot(slot);
1228 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001232 /* Refuse the destruction of a read-only key (which may or may not work
1233 * if we attempt it, depending on whether the key is merely read-only
1234 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001235 * Just do the best we can, which is to wipe the copy in memory
1236 * (done in this function's cleanup code). */
1237 overall_status = PSA_ERROR_NOT_PERMITTED;
1238 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001239 }
1240
Gilles Peskine354f7672019-07-12 23:46:38 +02001241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1243 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001244 /* For a key in a secure element, we need to do three things:
1245 * remove the key file in internal storage, destroy the
1246 * key inside the secure element, and update the driver's
1247 * persistent data. Start a transaction that will encompass these
1248 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001250 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001252 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 status = psa_crypto_save_transaction();
1254 if (status != PSA_SUCCESS) {
1255 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001256 /* We should still try to destroy the key in the secure
1257 * element and the key metadata in storage. This is especially
1258 * important if the error is that the storage is full.
1259 * But how to do it exactly without risking an inconsistent
1260 * state after a reset?
1261 * https://github.com/ARMmbed/mbed-crypto/issues/215
1262 */
1263 overall_status = status;
1264 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001265 }
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 status = psa_destroy_se_key(driver,
1268 psa_key_slot_get_slot_number(slot));
1269 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001270 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001272 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1274
Darryl Greend49a4992018-06-18 17:27:26 +01001275#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1277 status = psa_destroy_persistent_key(slot->attr.id);
1278 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001279 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001281
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001282 /* TODO: other slots may have a copy of the same key. We should
1283 * invalidate them.
1284 * https://github.com/ARMmbed/mbed-crypto/issues/214
1285 */
Darryl Greend49a4992018-06-18 17:27:26 +01001286 }
1287#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001288
Gilles Peskinefc762652019-07-22 19:30:34 +02001289#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (driver != NULL) {
1291 status = psa_save_se_persistent_data(driver);
1292 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001293 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 }
1295 status = psa_crypto_stop_transaction();
1296 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001299 }
1300#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1301
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001304 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001306 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 }
1308 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309}
1310
Valerio Settib2bcedb2023-07-10 11:24:00 +02001311#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001312 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001313static psa_status_t psa_get_rsa_public_exponent(
1314 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001316{
1317 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001319 uint8_t *buffer = NULL;
1320 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1324 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 }
1327 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001328 /* It's the default value, which is reported as an empty string,
1329 * so there's nothing to do. */
1330 goto exit;
1331 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 buflen = mbedtls_mpi_size(&mpi);
1334 buffer = mbedtls_calloc(1, buflen);
1335 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001336 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1337 goto exit;
1338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1340 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001343 attributes->domain_parameters = buffer;
1344 attributes->domain_parameters_size = buflen;
1345
1346exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 mbedtls_mpi_free(&mpi);
1348 if (ret != 0) {
1349 mbedtls_free(buffer);
1350 }
1351 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001352}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001353#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001354 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001355
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001356/** Retrieve all the publicly-accessible attributes of a key.
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1359 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001361 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001362 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001363 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1368 if (status != PSA_SUCCESS) {
1369 return status;
1370 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001371
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001372 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1374 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001375
1376#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1378 psa_set_key_slot_number(attributes,
1379 psa_key_slot_get_slot_number(slot));
1380 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001384#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1385 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001386 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001387 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001388 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Pengyu Lvf75893b2023-12-08 17:21:39 +08001389 /* TODO: This is a temporary situation where domain parameters are deprecated,
1390 * but we need it for namely generating an RSA key with a non-default exponent.
1391 * This would be improved after https://github.com/Mbed-TLS/mbedtls/issues/6494.
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001394 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001395
Ronald Cron90857082020-11-25 14:58:33 +01001396 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 slot->attr.type,
1398 slot->key.data,
1399 slot->key.bytes,
1400 &rsa);
1401 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001402 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 status = psa_get_rsa_public_exponent(rsa,
1406 attributes);
1407 mbedtls_rsa_free(rsa);
1408 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001409 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001410 break;
Pengyu Lve9efbc22023-12-08 16:59:08 +08001411#else
1412 case PSA_KEY_TYPE_RSA_KEY_PAIR:
1413 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
1414 attributes->domain_parameters = NULL;
1415 attributes->domain_parameters_size = SIZE_MAX;
1416 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001417#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1418 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001420 default:
1421 /* Nothing else to do. */
1422 break;
1423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (status != PSA_SUCCESS) {
1426 psa_reset_key_attributes(attributes);
1427 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
1433
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1435psa_status_t psa_get_key_slot_number(
1436 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438{
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001440 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return PSA_SUCCESS;
1442 } else {
1443 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001444 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445}
1446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1449 size_t key_buffer_size,
1450 uint8_t *data,
1451 size_t data_size,
1452 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001453{
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (key_buffer_size > data_size) {
1455 return PSA_ERROR_BUFFER_TOO_SMALL;
1456 }
1457 memcpy(data, key_buffer, key_buffer_size);
1458 memset(data + key_buffer_size, 0,
1459 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001462}
1463
Ronald Cron7285cda2020-11-26 14:46:37 +01001464psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 const psa_key_attributes_t *attributes,
1466 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468{
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 psa_key_type_t type = attributes->core.type;
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (key_type_is_raw_bytes(type) ||
1472 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001473 PSA_KEY_TYPE_IS_ECC(type) ||
1474 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return psa_export_key_buffer_internal(
1476 key_buffer, key_buffer_size,
1477 data, data_size, data_length);
1478 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479 /* This shouldn't happen in the reference implementation, but
1480 it is valid for a special-purpose implementation to omit
1481 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483 }
1484}
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1487 uint8_t *data,
1488 size_t data_size,
1489 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490{
1491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1492 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1493 psa_key_slot_t *slot;
1494
Ronald Crone907e552021-01-18 13:22:38 +01001495 /* Reject a zero-length output buffer now, since this can never be a
1496 * valid key representation. This way we know that data must be a valid
1497 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (data_size == 0) {
1499 return PSA_ERROR_BUFFER_TOO_SMALL;
1500 }
Ronald Crone907e552021-01-18 13:22:38 +01001501
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 /* Set the key to empty now, so that even when there are errors, we always
1503 * set data_length to a value between 0 and data_size. On error, setting
1504 * the key to empty is a good choice because an empty key representation is
1505 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001506 *data_length = 0;
1507
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 /* Export requires the EXPORT flag. There is an exception for public keys,
1509 * which don't require any flag, but
1510 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1513 PSA_KEY_USAGE_EXPORT, 0);
1514 if (status != PSA_SUCCESS) {
1515 return status;
1516 }
mohammad160306e79202018-03-28 13:17:44 +03001517
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 psa_key_attributes_t attributes = {
1519 .core = slot->attr
1520 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 status = psa_driver_wrapper_export_key(&attributes,
1522 slot->key.data, slot->key.bytes,
1523 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528}
1529
Ronald Cron7285cda2020-11-26 14:46:37 +01001530psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001531 const psa_key_attributes_t *attributes,
1532 const uint8_t *key_buffer,
1533 size_t key_buffer_size,
1534 uint8_t *data,
1535 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001537{
Ronald Crond18b5f82020-11-25 16:46:05 +01001538 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001539
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001540 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001541 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1542 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001543 /* Exporting public -> public */
1544 return psa_export_key_buffer_internal(
1545 key_buffer, key_buffer_size,
1546 data, data_size, data_length);
1547 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1550 return mbedtls_psa_rsa_export_public_key(attributes,
1551 key_buffer,
1552 key_buffer_size,
1553 data,
1554 data_size,
1555 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001556#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001557 /* We don't know how to convert a private RSA key to public. */
1558 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001560 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001561 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001563 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1564 return mbedtls_psa_ecp_export_public_key(attributes,
1565 key_buffer,
1566 key_buffer_size,
1567 data,
1568 data_size,
1569 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001571 /* We don't know how to convert a private ECC key to public */
1572 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001574 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001575 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001578 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001579 key_buffer,
1580 key_buffer_size,
1581 data, data_size,
1582 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001583#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001584 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001585#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001586 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001588 (void) key_buffer;
1589 (void) key_buffer_size;
1590 (void) data;
1591 (void) data_size;
1592 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595}
Ronald Crond18b5f82020-11-25 16:46:05 +01001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1598 uint8_t *data,
1599 size_t data_size,
1600 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001601{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001603 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001604 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001605 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001606
Ronald Crone907e552021-01-18 13:22:38 +01001607 /* Reject a zero-length output buffer now, since this can never be a
1608 * valid key representation. This way we know that data must be a valid
1609 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (data_size == 0) {
1611 return PSA_ERROR_BUFFER_TOO_SMALL;
1612 }
Ronald Crone907e552021-01-18 13:22:38 +01001613
Darryl Greendd8fb772018-11-07 16:00:44 +00001614 /* Set the key to empty now, so that even when there are errors, we always
1615 * set data_length to a value between 0 and data_size. On error, setting
1616 * the key to empty is a good choice because an empty key representation is
1617 * unlikely to be accepted anywhere. */
1618 *data_length = 0;
1619
1620 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1622 if (status != PSA_SUCCESS) {
1623 return status;
1624 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1627 status = PSA_ERROR_INVALID_ARGUMENT;
1628 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001629 }
1630
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001631 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001632 .core = slot->attr
1633 };
Ronald Cron67227982020-11-26 15:16:05 +01001634 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001635 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001637
1638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001642}
1643
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001644MBEDTLS_STATIC_ASSERT(
1645 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1646 "One or more key attribute flag is listed as both external-only and dual-use")
1647MBEDTLS_STATIC_ASSERT(
1648 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1649 "One or more key attribute flag is listed as both internal-only and dual-use")
1650MBEDTLS_STATIC_ASSERT(
1651 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1652 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate that a key policy is internally well-formed.
1655 *
1656 * This function only rejects invalid policies. It does not validate the
1657 * consistency of the policy with respect to other attributes of the key
1658 * such as the key type.
1659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001661{
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1663 PSA_KEY_USAGE_COPY |
1664 PSA_KEY_USAGE_ENCRYPT |
1665 PSA_KEY_USAGE_DECRYPT |
1666 PSA_KEY_USAGE_SIGN_MESSAGE |
1667 PSA_KEY_USAGE_VERIFY_MESSAGE |
1668 PSA_KEY_USAGE_SIGN_HASH |
1669 PSA_KEY_USAGE_VERIFY_HASH |
1670 PSA_KEY_USAGE_VERIFY_DERIVATION |
1671 PSA_KEY_USAGE_DERIVE)) != 0) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676}
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate the internal consistency of key attributes.
1679 *
1680 * This function only rejects invalid attribute values. If does not
1681 * validate the consistency of the attributes with any key data that may
1682 * be involved in the creation of the key.
1683 *
1684 * Call this function early in the key creation process.
1685 *
1686 * \param[in] attributes Key attributes for the new key.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
1689 *
1690 */
1691static psa_status_t psa_validate_key_attributes(
1692 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001694{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1697 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 status = psa_validate_key_location(lifetime, p_drv);
1700 if (status != PSA_SUCCESS) {
1701 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 status = psa_validate_key_persistence(lifetime);
1705 if (status != PSA_SUCCESS) {
1706 return status;
1707 }
1708
1709 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1710 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
1713 } else {
1714 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1715 return PSA_ERROR_INVALID_ARGUMENT;
1716 }
1717 }
1718
1719 status = psa_validate_key_policy(&attributes->core.policy);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
1724 /* Refuse to create overly large keys.
1725 * Note that this doesn't trigger on import if the attributes don't
1726 * explicitly specify a size (so psa_get_key_bits returns 0), so
1727 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1729 return PSA_ERROR_NOT_SUPPORTED;
1730 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1734 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1735 return PSA_ERROR_INVALID_ARGUMENT;
1736 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001739}
1740
Gilles Peskine4747d192019-04-17 15:05:45 +02001741/** Prepare a key slot to receive key material.
1742 *
1743 * This function allocates a key slot and sets its metadata.
1744 *
1745 * If this function fails, call psa_fail_key_creation().
1746 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001747 * This function is intended to be used as follows:
1748 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001749 * it with the specified attributes, and in case of a volatile key assign it
1750 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 * -# Populate the slot with the key material.
1752 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1753 * In case of failure at any step, stop the sequence and call
1754 * psa_fail_key_creation().
1755 *
Ronald Cron5c522922020-11-14 16:35:34 +01001756 * On success, the key slot is locked. It is the responsibility of the caller
1757 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001758 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001759 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[out] p_slot On success, a pointer to the prepared slot.
1762 * \param[out] p_drv On any return, the driver for the key, if any.
1763 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 *
1765 * \retval #PSA_SUCCESS
1766 * The key slot is ready to receive key material.
1767 * \return If this function fails, the key slot is an invalid state.
1768 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 */
1770static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001771 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001775{
1776 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001777 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_key_slot_t *slot;
1779
Gilles Peskinedf179142019-07-15 22:02:14 +02001780 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 *p_drv = NULL;
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 status = psa_validate_key_attributes(attributes, p_drv);
1784 if (status != PSA_SUCCESS) {
1785 return status;
1786 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1789 if (status != PSA_SUCCESS) {
1790 return status;
1791 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 slot = *p_slot;
1793
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001794 /* We're storing the declared bit-size of the key. It's up to each
1795 * creation mechanism to verify that this information is correct.
1796 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001798 * is optional (import, copy). In case of a volatile key, assign it the
1799 * volatile key identifier associated to the slot returned to contain its
1800 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001801
1802 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001804#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1805 slot->attr.id = volatile_key_id;
1806#else
1807 slot->attr.id.key_id = volatile_key_id;
1808#endif
1809 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001810
Gilles Peskine91e8c332019-08-02 19:19:39 +02001811 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001812 * external-only flags, query `attributes`. Thanks to the check
1813 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001814 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001815 * may have set. */
1816 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001817
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001819 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001821 * create the key file in internal storage, create the
1822 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * persistent data. This is done by starting a transaction that will
1824 * encompass these three actions.
1825 * For registering a volatile key, we just need to find an appropriate
1826 * slot number inside the SE. Since the key is designated volatile, creating
1827 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001828 /* The first thing to do is to find a slot number for the new key.
1829 * We save the slot number in persistent storage as part of the
1830 * transaction data. It will be needed to recover if the power
1831 * fails during the key creation process, to clean up on the secure
1832 * element side after restarting. Obtaining a slot number from the
1833 * secure element driver updates its persistent state, but we do not yet
1834 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001835 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001837 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1839 &slot_number);
1840 if (status != PSA_SUCCESS) {
1841 return status;
1842 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1845 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 status = psa_crypto_save_transaction();
1850 if (status != PSA_SUCCESS) {
1851 (void) psa_crypto_stop_transaction();
1852 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001853 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001854 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001855
1856 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001858 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001864#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1865
Ryan Everett975d4112023-11-16 13:37:51 +00001866 slot->status = PSA_SLOT_OCCUPIED;
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001869}
Gilles Peskine4747d192019-04-17 15:05:45 +02001870
1871/** Finalize the creation of a key once its key material has been set.
1872 *
1873 * This entails writing the key to persistent storage.
1874 *
1875 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001876 * See the documentation of psa_start_key_creation() for the intended use
1877 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 *
Ronald Cron5c522922020-11-14 16:35:34 +01001879 * If the finalization succeeds, the function unlocks the key slot (it was
1880 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1881 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001882 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001883 * \param[in,out] slot Pointer to the slot with key material.
1884 * \param[in] driver The secure element driver for the key,
1885 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001886 * \param[out] key On success, identifier of the key. Note that the
1887 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001888 *
1889 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001890 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1892 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1893 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1894 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1895 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1896 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001897 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001898 * \return If this function fails, the key slot is an invalid state.
1899 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001901static psa_status_t psa_finish_key_creation(
1902 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001903 psa_se_drv_table_entry_t *driver,
1904 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001905{
1906 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001907 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001909
1910#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001912#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001914 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001915 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001917
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001918 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1919 sizeof(data.slot_number),
1920 "Slot number size does not match psa_se_key_data_storage_t");
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1923 status = psa_save_persistent_key(&slot->attr,
1924 (uint8_t *) &data,
1925 sizeof(data));
1926 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001927#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1928 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001929 /* Key material is saved in export representation in the slot, so
1930 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 status = psa_save_persistent_key(&slot->attr,
1932 slot->key.data,
1933 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001934 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001935 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001936#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1937
Gilles Peskinecbaff462019-07-12 23:46:04 +02001938#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001939 /* Finish the transaction for a key creation. This does not
1940 * happen when registering an existing key. Detect this case
1941 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001942 * creation of a persistent key in a secure element requires a transaction,
1943 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (driver != NULL &&
1945 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1946 status = psa_save_se_persistent_data(driver);
1947 if (status != PSA_SUCCESS) {
1948 psa_destroy_persistent_key(slot->attr.id);
1949 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001950 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001952 }
1953#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001956 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 status = psa_unlock_key_slot(slot);
1958 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001959 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001961 }
Ronald Cron50972942020-11-14 11:28:25 +01001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001964}
1965
1966/** Abort the creation of a key.
1967 *
1968 * You may call this function after calling psa_start_key_creation(),
1969 * or after psa_finish_key_creation() fails. In other circumstances, this
1970 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001971 * See the documentation of psa_start_key_creation() for the intended use
1972 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001973 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001974 * \param[in,out] slot Pointer to the slot with key material.
1975 * \param[in] driver The secure element driver for the key,
1976 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978static void psa_fail_key_creation(psa_key_slot_t *slot,
1979 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001980{
Gilles Peskine011e4282019-06-26 18:34:38 +02001981 (void) driver;
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001984 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001986
1987#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001988 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001989 * element, and the failure happened later (when saving metadata
1990 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001991 * element.
1992 * https://github.com/ARMmbed/mbed-crypto/issues/217
1993 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001994
Gilles Peskined7729582019-08-05 15:55:54 +02001995 /* Abort the ongoing transaction if any (there may not be one if
1996 * the creation process failed before starting one, or if the
1997 * key creation is a registration of a key in a secure element).
1998 * Earlier functions must already have done what it takes to undo any
1999 * partial creation. All that's left is to update the transaction data
2000 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002002#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002005}
2006
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002007/** Validate optional attributes during key creation.
2008 *
2009 * Some key attributes are optional during key creation. If they are
2010 * specified in the attributes structure, check that they are consistent
2011 * with the data in the slot.
2012 *
2013 * This function should be called near the end of key creation, after
2014 * the slot in memory is fully populated but before saving persistent data.
2015 */
2016static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019{
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (attributes->core.type != 0) {
2021 if (attributes->core.type != slot->attr.type) {
2022 return PSA_ERROR_INVALID_ARGUMENT;
2023 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 }
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002027#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2028 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2030 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002031 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002032 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002034
Ronald Cron90857082020-11-25 14:58:33 +01002035 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 slot->attr.type,
2037 slot->key.data,
2038 slot->key.bytes,
2039 &rsa);
2040 if (status != PSA_SUCCESS) {
2041 return status;
2042 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 mbedtls_mpi_init(&actual);
2045 mbedtls_mpi_init(&required);
2046 ret = mbedtls_rsa_export(rsa,
2047 NULL, NULL, NULL, NULL, &actual);
2048 mbedtls_rsa_free(rsa);
2049 mbedtls_free(rsa);
2050 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 }
2053 ret = mbedtls_mpi_read_binary(&required,
2054 attributes->domain_parameters,
2055 attributes->domain_parameters_size);
2056 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
2059 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
2062rsa_exit:
2063 mbedtls_mpi_free(&actual);
2064 mbedtls_mpi_free(&required);
2065 if (ret != 0) {
2066 return mbedtls_to_psa_error(ret);
2067 }
2068 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002069#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2070 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002071 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074 }
2075 }
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (attributes->core.bits != 0) {
2078 if (attributes->core.bits != slot->attr.bits) {
2079 return PSA_ERROR_INVALID_ARGUMENT;
2080 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002081 }
2082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002084}
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2087 const uint8_t *data,
2088 size_t data_length,
2089 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002090{
2091 psa_status_t status;
2092 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002093 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002094 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302095 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096
Ronald Cron81709fc2020-11-14 12:10:32 +01002097 *key = MBEDTLS_SVC_KEY_ID_INIT;
2098
Gilles Peskine0f84d622019-09-12 19:03:13 +02002099 /* Reject zero-length symmetric keys (including raw data key objects).
2100 * This also rejects any key which might be encoded as an empty string,
2101 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (data_length == 0) {
2103 return PSA_ERROR_INVALID_ARGUMENT;
2104 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002105
Archana449608b2021-09-08 15:36:05 +05302106 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (data_length > SIZE_MAX / 8) {
2108 return PSA_ERROR_NOT_SUPPORTED;
2109 }
Archana6ed4bda2021-08-04 10:47:15 +05302110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2112 &slot, &driver);
2113 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002117 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302118 * storage ( thus not in the case of importing a key in a secure element
2119 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2120 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (slot->key.data == NULL) {
2122 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302123 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 attributes, data, data_length, &storage_size);
2125 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Archanad8a83dc2021-06-14 10:04:16 +05302128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 status = psa_allocate_buffer_to_slot(slot, storage_size);
2130 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002131 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002133 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002134
2135 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 status = psa_driver_wrapper_import_key(attributes,
2137 data, data_length,
2138 slot->key.data,
2139 slot->key.bytes,
2140 &slot->key.bytes, &bits);
2141 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002146 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002148 status = PSA_ERROR_INVALID_ARGUMENT;
2149 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002150 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002151
Archana6ed4bda2021-08-04 10:47:15 +05302152 /* Enforce a size limit, and in particular ensure that the bit
2153 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302155 status = PSA_ERROR_NOT_SUPPORTED;
2156 goto exit;
2157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_validate_optional_attributes(slot, attributes);
2159 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (status != PSA_SUCCESS) {
2166 psa_fail_key_creation(slot, driver);
2167 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002170}
2171
Gilles Peskined7729582019-08-05 15:55:54 +02002172#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2173psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002175{
2176 psa_status_t status;
2177 psa_key_slot_t *slot = NULL;
2178 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002180
2181 /* Leaving attributes unspecified is not currently supported.
2182 * It could make sense to query the key type and size from the
2183 * secure element, but not all secure elements support this
2184 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2186 return PSA_ERROR_NOT_SUPPORTED;
2187 }
2188 if (psa_get_key_bits(attributes) == 0) {
2189 return PSA_ERROR_NOT_SUPPORTED;
2190 }
Gilles Peskined7729582019-08-05 15:55:54 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2193 &slot, &driver);
2194 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 }
Gilles Peskined7729582019-08-05 15:55:54 +02002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002199
2200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (status != PSA_SUCCESS) {
2202 psa_fail_key_creation(slot, driver);
2203 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002204
Gilles Peskined7729582019-08-05 15:55:54 +02002205 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 psa_close_key(key);
2207 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002208}
2209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2212 const psa_key_attributes_t *specified_attributes,
2213 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002214{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002215 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002216 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002217 psa_key_slot_t *source_slot = NULL;
2218 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002219 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002220 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302221 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002222
Ronald Cron81709fc2020-11-14 12:10:32 +01002223 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2224
Archana8a180362021-07-05 02:18:48 +05302225 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2227 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 status = psa_validate_optional_attributes(source_slot,
2232 specified_attributes);
2233 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236
Archana9d17bf42021-09-10 06:22:44 +05302237 /* The target key type and number of bits have been validated by
2238 * psa_validate_optional_attributes() to be either equal to zero or
2239 * equal to the ones of the source key. So it is safe to inherit
2240 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302241 * */
Archana9d17bf42021-09-10 06:22:44 +05302242 actual_attributes.core.bits = source_slot->attr.bits;
2243 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302244
2245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 status = psa_restrict_key_policy(source_slot->attr.type,
2247 &actual_attributes.core.policy,
2248 &source_slot->attr.policy);
2249 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2254 &target_slot, &driver);
2255 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 }
2258 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2259 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302260 /*
Archana9d17bf42021-09-10 06:22:44 +05302261 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302262 * the source key would need to be exported as plaintext and re-imported
2263 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302264 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302265 * appropriate API invocations from the application, if needed.
2266 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002267 status = PSA_ERROR_NOT_SUPPORTED;
2268 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002269 }
Archana8a180362021-07-05 02:18:48 +05302270 /*
2271 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302272 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302273 * - For opaque keys this translates to an invocation of the drivers'
2274 * copy_key entry point through the dispatch layer.
2275 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2277 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2278 &storage_size);
2279 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
Archana374fe5b2021-09-08 15:50:28 +05302282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2284 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 }
Archana374fe5b2021-09-08 15:50:28 +05302287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 status = psa_driver_wrapper_copy_key(&actual_attributes,
2289 source_slot->key.data,
2290 source_slot->key.bytes,
2291 target_slot->key.data,
2292 target_slot->key.bytes,
2293 &target_slot->key.bytes);
2294 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302295 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 }
2297 } else {
2298 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302299 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 source_slot->key.bytes);
2301 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 }
Archana8a180362021-07-05 02:18:48 +05302304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (status != PSA_SUCCESS) {
2308 psa_fail_key_creation(target_slot, driver);
2309 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002314}
2315
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002316
2317
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002318/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002319/* Message digests */
2320/****************************************************************/
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002324 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (operation->id == 0) {
2326 return PSA_SUCCESS;
2327 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002330 operation->id = 0;
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333}
2334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2336 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002338 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002339
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002342 status = PSA_ERROR_BAD_STATE;
2343 goto exit;
2344 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002347 status = PSA_ERROR_INVALID_ARGUMENT;
2348 goto exit;
2349 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002350
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002351 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2352 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356
2357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (status != PSA_SUCCESS) {
2359 psa_hash_abort(operation);
2360 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002361
2362 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363}
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2366 const uint8_t *input,
2367 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002372 status = PSA_ERROR_BAD_STATE;
2373 goto exit;
2374 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002375
Steven Cooremanc8288352021-03-04 14:02:19 +01002376 /* Don't require hash implementations to behave correctly on a
2377 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (input_length == 0) {
2379 return PSA_SUCCESS;
2380 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002383
2384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (status != PSA_SUCCESS) {
2386 psa_hash_abort(operation);
2387 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390}
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2393 uint8_t *hash,
2394 size_t hash_size,
2395 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002397 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (operation->id == 0) {
2399 return PSA_ERROR_BAD_STATE;
2400 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401
Steven Cooreman1e582352021-02-18 17:24:37 +01002402 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 operation, hash, hash_size, hash_length);
2404 psa_hash_abort(operation);
2405 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406}
2407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2409 const uint8_t *hash,
2410 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411{
Ronald Cron69a63422021-10-18 09:47:58 +02002412 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002414 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 operation,
2416 actual_hash, sizeof(actual_hash),
2417 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002424 status = PSA_ERROR_INVALID_SIGNATURE;
2425 goto exit;
2426 }
2427
Dave Rodgman78701152023-08-29 14:20:18 +01002428 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002429 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002431
2432exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2434 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002435 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439}
2440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441psa_status_t psa_hash_compute(psa_algorithm_t alg,
2442 const uint8_t *input, size_t input_length,
2443 uint8_t *hash, size_t hash_size,
2444 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002445{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002446 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (!PSA_ALG_IS_HASH(alg)) {
2448 return PSA_ERROR_INVALID_ARGUMENT;
2449 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2452 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002453}
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455psa_status_t psa_hash_compare(psa_algorithm_t alg,
2456 const uint8_t *input, size_t input_length,
2457 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002458{
Ronald Cron69a63422021-10-18 09:47:58 +02002459 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002460 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (!PSA_ALG_IS_HASH(alg)) {
2463 return PSA_ERROR_INVALID_ARGUMENT;
2464 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002465
Steven Cooreman1e582352021-02-18 17:24:37 +01002466 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 alg, input, input_length,
2468 actual_hash, sizeof(actual_hash),
2469 &actual_hash_length);
2470 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002471 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 }
2473 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002474 status = PSA_ERROR_INVALID_SIGNATURE;
2475 goto exit;
2476 }
Dave Rodgman78701152023-08-29 14:20:18 +01002477 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002478 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002480
2481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2483 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002484}
2485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2487 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002488{
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (source_operation->id == 0 ||
2490 target_operation->id != 0) {
2491 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002492 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2495 target_operation);
2496 if (status != PSA_SUCCESS) {
2497 psa_hash_abort(target_operation);
2498 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002501}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502
2503
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505/* MAC */
2506/****************************************************************/
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002509{
Steven Cooremane6804192021-03-19 18:28:56 +01002510 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (operation->id == 0) {
2512 return PSA_SUCCESS;
2513 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002516 operation->mac_size = 0;
2517 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002518 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002521}
2522
Ronald Cron2dff3b22021-06-17 16:33:22 +02002523static psa_status_t psa_mac_finalize_alg_and_key_validation(
2524 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002525 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 psa_key_type_t key_type = psa_get_key_type(attributes);
2530 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (!PSA_ALG_IS_MAC(alg)) {
2533 return PSA_ERROR_INVALID_ARGUMENT;
2534 }
Steven Cooremane6804192021-03-19 18:28:56 +01002535
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002536 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 status = psa_mac_key_can_do(alg, key_type);
2538 if (status != PSA_SUCCESS) {
2539 return status;
2540 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002541
Steven Cooremand1a68f12021-05-10 11:13:41 +02002542 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002546 /* A very short MAC is too short for security since it can be
2547 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2548 * so we make this our minimum, even though 32 bits is still
2549 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002551 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2554 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002555 /* It's impossible to "truncate" to a larger length than the full length
2556 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002558 }
2559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002561 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2562 * that is disabled in the compile-time configuration. The result can
2563 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2564 * configuration into account. In this case, force a return of
2565 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2566 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2567 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2568 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2569 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002571 }
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002574}
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2577 mbedtls_svc_key_id_t key,
2578 psa_algorithm_t alg,
2579 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002580{
2581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2582 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002583 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002584 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585
2586 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002588 status = PSA_ERROR_BAD_STATE;
2589 goto exit;
2590 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002591
2592 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 key,
2594 &slot,
2595 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2596 alg);
2597 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002601 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602 .core = slot->attr
2603 };
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2606 &operation->mac_size);
2607 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610
2611 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002612 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 if (is_sign) {
2614 status = psa_driver_wrapper_mac_sign_setup(operation,
2615 &attributes,
2616 slot->key.data,
2617 slot->key.bytes,
2618 alg);
2619 } else {
2620 status = psa_driver_wrapper_mac_verify_setup(operation,
2621 &attributes,
2622 slot->key.data,
2623 slot->key.bytes,
2624 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002626
Gilles Peskinefbfac682018-07-08 20:51:54 +02002627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (status != PSA_SUCCESS) {
2629 psa_mac_abort(operation);
2630 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635}
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2638 mbedtls_svc_key_id_t key,
2639 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640{
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002642}
2643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2645 mbedtls_svc_key_id_t key,
2646 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002647{
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002649}
2650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2652 const uint8_t *input,
2653 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654{
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (operation->id == 0) {
2656 return PSA_ERROR_BAD_STATE;
2657 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002659 /* Don't require hash implementations to behave correctly on a
2660 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (input_length == 0) {
2662 return PSA_SUCCESS;
2663 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2666 input, input_length);
2667 if (status != PSA_SUCCESS) {
2668 psa_mac_abort(operation);
2669 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672}
2673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2675 uint8_t *mac,
2676 size_t mac_size,
2677 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002678{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2680 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002684 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002685 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002689 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002690 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002691
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002692 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2693 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002695 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002696 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002697 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002700 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002701 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002702 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 status = psa_driver_wrapper_mac_sign_finish(operation,
2705 mac, operation->mac_size,
2706 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002707
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002708exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002709 /* In case of success, set the potential excess room in the output buffer
2710 * to an invalid value, to avoid potentially leaking a longer MAC.
2711 * In case of error, set the output length and content to a safe default,
2712 * such that in case the caller misses an error check, the output would be
2713 * an unachievable MAC.
2714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002716 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002717 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002718 }
mohammad16036df908f2018-04-02 08:34:15 -07002719
Paul Elliottdc42ca82023-02-24 18:11:59 +00002720 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002725}
2726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2728 const uint8_t *mac,
2729 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002731 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2732 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002735 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002736 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002737 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002740 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002741 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002742 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002745 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002746 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002747 }
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 status = psa_driver_wrapper_mac_verify_finish(operation,
2750 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002751
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756}
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2759 psa_algorithm_t alg,
2760 const uint8_t *input,
2761 size_t input_length,
2762 uint8_t *mac,
2763 size_t mac_size,
2764 size_t *mac_length,
2765 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002766{
2767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002768 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2769 psa_key_slot_t *slot;
2770 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002771 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772
Ronald Cron51131b52021-06-17 17:17:20 +02002773 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 key,
2775 &slot,
2776 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2777 alg);
2778 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002779 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002781
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002782 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002783 .core = slot->attr
2784 };
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2787 &operation_mac_size);
2788 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002793 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002794 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002795 }
2796
2797 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 &attributes,
2799 slot->key.data, slot->key.bytes,
2800 alg,
2801 input, input_length,
2802 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002803
2804exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002805 /* In case of success, set the potential excess room in the output buffer
2806 * to an invalid value, to avoid potentially leaking a longer MAC.
2807 * In case of error, set the output length and content to a safe default,
2808 * such that in case the caller misses an error check, the output would be
2809 * an unachievable MAC.
2810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002812 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002813 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002814 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002815
2816 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002821}
2822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002824 psa_algorithm_t alg,
2825 const uint8_t *input,
2826 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 uint8_t *mac,
2828 size_t mac_size,
2829 size_t *mac_length)
2830{
2831 return psa_mac_compute_internal(key, alg,
2832 input, input_length,
2833 mac, mac_size, mac_length, 1);
2834}
2835
2836psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2837 psa_algorithm_t alg,
2838 const uint8_t *input,
2839 size_t input_length,
2840 const uint8_t *mac,
2841 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002842{
2843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002844 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2845 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 status = psa_mac_compute_internal(key, alg,
2848 input, input_length,
2849 actual_mac, sizeof(actual_mac),
2850 &actual_mac_length, 0);
2851 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002856 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002857 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002858 }
Dave Rodgman78701152023-08-29 14:20:18 +01002859 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002860 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002861 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002862 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002863
2864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002868}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002869
Gilles Peskinee59236f2018-01-27 23:32:46 +01002870/****************************************************************/
2871/* Asymmetric cryptography */
2872/****************************************************************/
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2875 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876{
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (input_is_message) {
2878 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2879 return PSA_ERROR_INVALID_ARGUMENT;
2880 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2883 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2884 return PSA_ERROR_INVALID_ARGUMENT;
2885 }
2886 }
2887 } else {
2888 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2889 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890 }
2891 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002894}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2897 int input_is_message,
2898 psa_algorithm_t alg,
2899 const uint8_t *input,
2900 size_t input_length,
2901 uint8_t *signature,
2902 size_t signature_size,
2903 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904{
2905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2906 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2907 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002908 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909
2910 *signature_length = 0;
2911
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 status = psa_sign_verify_check_alg(input_is_message, alg);
2913 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002914 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002916
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002918 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919 * buffer can in principle be empty since it doesn't actually have
2920 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (signature_size == 0) {
2922 return PSA_ERROR_BUFFER_TOO_SMALL;
2923 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 key, &slot,
2927 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2928 PSA_KEY_USAGE_SIGN_HASH,
2929 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002936 status = PSA_ERROR_INVALID_ARGUMENT;
2937 goto exit;
2938 }
2939
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002940 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942 };
2943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002945 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002946 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002947 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 signature, signature_size, signature_length);
2949 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002950
2951 status = psa_driver_wrapper_sign_hash(
2952 &attributes, slot->key.data, slot->key.bytes,
2953 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002955 }
2956
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002957
2958exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002959 psa_wipe_tag_output_buffer(signature, status, signature_size,
2960 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002965}
2966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2968 int input_is_message,
2969 psa_algorithm_t alg,
2970 const uint8_t *input,
2971 size_t input_length,
2972 const uint8_t *signature,
2973 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002974{
2975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2976 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2977 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 status = psa_sign_verify_check_alg(input_is_message, alg);
2980 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002981 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002983
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002984 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 key, &slot,
2986 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2987 PSA_KEY_USAGE_VERIFY_HASH,
2988 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 if (status != PSA_SUCCESS) {
2991 return status;
2992 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002993
2994 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002996 };
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002999 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003000 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003001 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 signature, signature_length);
3003 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003004 status = psa_driver_wrapper_verify_hash(
3005 &attributes, slot->key.data, slot->key.bytes,
3006 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003008 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003013
3014}
3015
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003016psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003017 const psa_key_attributes_t *attributes,
3018 const uint8_t *key_buffer,
3019 size_t key_buffer_size,
3020 psa_algorithm_t alg,
3021 const uint8_t *input,
3022 size_t input_length,
3023 uint8_t *signature,
3024 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003026{
3027 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003030 size_t hash_length;
3031 uint8_t hash[PSA_HASH_MAX_SIZE];
3032
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003033 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 PSA_ALG_SIGN_GET_HASH(alg),
3035 input, input_length,
3036 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003039 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003041
3042 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 attributes, key_buffer, key_buffer_size,
3044 alg, hash, hash_length,
3045 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003046 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003049}
3050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3052 psa_algorithm_t alg,
3053 const uint8_t *input,
3054 size_t input_length,
3055 uint8_t *signature,
3056 size_t signature_size,
3057 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003058{
3059 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003060 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003062}
3063
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003064psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003065 const psa_key_attributes_t *attributes,
3066 const uint8_t *key_buffer,
3067 size_t key_buffer_size,
3068 psa_algorithm_t alg,
3069 const uint8_t *input,
3070 size_t input_length,
3071 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003073{
3074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003077 size_t hash_length;
3078 uint8_t hash[PSA_HASH_MAX_SIZE];
3079
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003080 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 PSA_ALG_SIGN_GET_HASH(alg),
3082 input, input_length,
3083 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003086 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003088
3089 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 attributes, key_buffer, key_buffer_size,
3091 alg, hash, hash_length,
3092 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003093 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003096}
3097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3099 psa_algorithm_t alg,
3100 const uint8_t *input,
3101 size_t input_length,
3102 const uint8_t *signature,
3103 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003104{
3105 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003106 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003108}
3109
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003110psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003111 const psa_key_attributes_t *attributes,
3112 const uint8_t *key_buffer, size_t key_buffer_size,
3113 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003115{
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3117 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3118 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3121 return mbedtls_psa_rsa_sign_hash(
3122 attributes,
3123 key_buffer, key_buffer_size,
3124 alg, hash, hash_length,
3125 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003126#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3127 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else {
3129 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3132 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003133#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3135 return mbedtls_psa_ecdsa_sign_hash(
3136 attributes,
3137 key_buffer, key_buffer_size,
3138 alg, hash, hash_length,
3139 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003140#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3141 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 } else {
3143 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003144 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003145 }
Ronald Cron4501c982021-03-19 20:09:32 +01003146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 (void) key_buffer;
3148 (void) key_buffer_size;
3149 (void) hash;
3150 (void) hash_length;
3151 (void) signature;
3152 (void) signature_size;
3153 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003156}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003157
Gilles Peskine449bd832023-01-11 14:50:10 +01003158psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3159 psa_algorithm_t alg,
3160 const uint8_t *hash,
3161 size_t hash_length,
3162 uint8_t *signature,
3163 size_t signature_size,
3164 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003165{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003166 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003167 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003169}
3170
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003171psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003172 const psa_key_attributes_t *attributes,
3173 const uint8_t *key_buffer, size_t key_buffer_size,
3174 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003176{
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3178 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3179 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3182 return mbedtls_psa_rsa_verify_hash(
3183 attributes,
3184 key_buffer, key_buffer_size,
3185 alg, hash, hash_length,
3186 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003187#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3188 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 } else {
3190 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3193 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003194#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3196 return mbedtls_psa_ecdsa_verify_hash(
3197 attributes,
3198 key_buffer, key_buffer_size,
3199 alg, hash, hash_length,
3200 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003201#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3202 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 } else {
3204 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003205 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003206 }
Ronald Cron4501c982021-03-19 20:09:32 +01003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 (void) key_buffer;
3209 (void) key_buffer_size;
3210 (void) hash;
3211 (void) hash_length;
3212 (void) signature;
3213 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003216}
3217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3219 psa_algorithm_t alg,
3220 const uint8_t *hash,
3221 size_t hash_length,
3222 const uint8_t *signature,
3223 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003224{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003225 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003226 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003228}
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3231 psa_algorithm_t alg,
3232 const uint8_t *input,
3233 size_t input_length,
3234 const uint8_t *salt,
3235 size_t salt_length,
3236 uint8_t *output,
3237 size_t output_size,
3238 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003241 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003242 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003243 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003244
Darryl Green5cc689a2018-07-24 15:34:10 +01003245 (void) input;
3246 (void) input_length;
3247 (void) salt;
3248 (void) output;
3249 (void) output_size;
3250
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003251 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3254 return PSA_ERROR_INVALID_ARGUMENT;
3255 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003256
Ronald Cron5c522922020-11-14 16:35:34 +01003257 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3259 if (status != PSA_SUCCESS) {
3260 return status;
3261 }
3262 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3263 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003264 status = PSA_ERROR_INVALID_ARGUMENT;
3265 goto exit;
3266 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003268 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003270 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003271
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003272 status = psa_driver_wrapper_asymmetric_encrypt(
3273 &attributes, slot->key.data, slot->key.bytes,
3274 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003276exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003280}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3283 psa_algorithm_t alg,
3284 const uint8_t *input,
3285 size_t input_length,
3286 const uint8_t *salt,
3287 size_t salt_length,
3288 uint8_t *output,
3289 size_t output_size,
3290 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003293 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003294 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003295 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003296
Darryl Green5cc689a2018-07-24 15:34:10 +01003297 (void) input;
3298 (void) input_length;
3299 (void) salt;
3300 (void) output;
3301 (void) output_size;
3302
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003303 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3306 return PSA_ERROR_INVALID_ARGUMENT;
3307 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003308
Ronald Cron5c522922020-11-14 16:35:34 +01003309 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3311 if (status != PSA_SUCCESS) {
3312 return status;
3313 }
3314 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003315 status = PSA_ERROR_INVALID_ARGUMENT;
3316 goto exit;
3317 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003318
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003319 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003321 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003323 status = psa_driver_wrapper_asymmetric_decrypt(
3324 &attributes, slot->key.data, slot->key.bytes,
3325 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003327
3328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003332}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003333
Paul Elliott9fe12f62022-11-30 19:16:02 +00003334/****************************************************************/
3335/* Asymmetric interruptible cryptography */
3336/****************************************************************/
3337
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003338static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3339
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340void psa_interruptible_set_max_ops(uint32_t max_ops)
3341{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003342 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343}
3344
3345uint32_t psa_interruptible_get_max_ops(void)
3346{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003347 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348}
3349
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350uint32_t psa_sign_hash_get_num_ops(
3351 const psa_sign_hash_interruptible_operation_t *operation)
3352{
Paul Elliott296ede92022-12-15 17:00:30 +00003353 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003354}
3355
3356uint32_t psa_verify_hash_get_num_ops(
3357 const psa_verify_hash_interruptible_operation_t *operation)
3358{
Paul Elliott296ede92022-12-15 17:00:30 +00003359 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003360}
3361
Paul Elliott59ad9452022-12-18 15:09:02 +00003362static psa_status_t psa_sign_hash_abort_internal(
3363 psa_sign_hash_interruptible_operation_t *operation)
3364{
3365 if (operation->id == 0) {
3366 /* The object has (apparently) been initialized but it is not (yet)
3367 * in use. It's ok to call abort on such an object, and there's
3368 * nothing to do. */
3369 return PSA_SUCCESS;
3370 }
3371
3372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3373
3374 status = psa_driver_wrapper_sign_hash_abort(operation);
3375
3376 operation->id = 0;
3377
Paul Elliotte6145dc2023-02-07 12:51:21 +00003378 /* Do not clear either the error_occurred or num_ops elements here as they
3379 * only want to be cleared by the application calling abort, not by abort
3380 * being called at completion of an operation. */
3381
Paul Elliott59ad9452022-12-18 15:09:02 +00003382 return status;
3383}
3384
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385psa_status_t psa_sign_hash_start(
3386 psa_sign_hash_interruptible_operation_t *operation,
3387 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3388 const uint8_t *hash, size_t hash_length)
3389{
3390 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3391 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3392 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003393 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394
Paul Elliottc9774412023-02-06 15:14:07 +00003395 /* Check that start has not been previously called, or operation has not
3396 * previously errored. */
3397 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398 return PSA_ERROR_BAD_STATE;
3399 }
3400
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401 status = psa_sign_verify_check_alg(0, alg);
3402 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003403 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003404 return status;
3405 }
3406
3407 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3408 PSA_KEY_USAGE_SIGN_HASH,
3409 alg);
3410
3411 if (status != PSA_SUCCESS) {
3412 goto exit;
3413 }
3414
3415 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3416 status = PSA_ERROR_INVALID_ARGUMENT;
3417 goto exit;
3418 }
3419
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003420 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003421 .core = slot->attr
3422 };
3423
Paul Elliott296ede92022-12-15 17:00:30 +00003424 /* Ensure ops count gets reset, in case of operation re-use. */
3425 operation->num_ops = 0;
3426
Paul Elliott9fe12f62022-11-30 19:16:02 +00003427 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3428 slot->key.data,
3429 slot->key.bytes, alg,
3430 hash, hash_length);
3431exit:
3432
3433 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003434 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003435 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 }
3437
3438 unlock_status = psa_unlock_key_slot(slot);
3439
Paul Elliottc9774412023-02-06 15:14:07 +00003440 if (unlock_status != PSA_SUCCESS) {
3441 operation->error_occurred = 1;
3442 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443
Paul Elliottc9774412023-02-06 15:14:07 +00003444 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003445}
3446
3447
3448psa_status_t psa_sign_hash_complete(
3449 psa_sign_hash_interruptible_operation_t *operation,
3450 uint8_t *signature, size_t signature_size,
3451 size_t *signature_length)
3452{
3453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3454
3455 *signature_length = 0;
3456
Paul Elliottc9774412023-02-06 15:14:07 +00003457 /* Check that start has been called first, and that operation has not
3458 * previously errored. */
3459 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003460 status = PSA_ERROR_BAD_STATE;
3461 goto exit;
3462 }
3463
Paul Elliott096abc42023-02-03 18:33:23 +00003464 /* Immediately reject a zero-length signature buffer. This guarantees that
3465 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466 if (signature_size == 0) {
3467 status = PSA_ERROR_BUFFER_TOO_SMALL;
3468 goto exit;
3469 }
3470
3471 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3472 signature_size,
3473 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003474
Paul Elliott296ede92022-12-15 17:00:30 +00003475 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003476 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003477
Paul Elliottebe225c2023-02-10 14:32:53 +00003478exit:
3479
Paul Elliott59200a22023-02-21 15:07:40 +00003480 psa_wipe_tag_output_buffer(signature, status, signature_size,
3481 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003482
Paul Elliott53bb3122023-02-10 14:22:22 +00003483 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003484 if (status != PSA_SUCCESS) {
3485 operation->error_occurred = 1;
3486 }
3487
Paul Elliott59ad9452022-12-18 15:09:02 +00003488 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003489 }
3490
3491 return status;
3492}
3493
3494psa_status_t psa_sign_hash_abort(
3495 psa_sign_hash_interruptible_operation_t *operation)
3496{
Paul Elliott59ad9452022-12-18 15:09:02 +00003497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3498
3499 status = psa_sign_hash_abort_internal(operation);
3500
3501 /* We clear the number of ops done here, so that it is not cleared when
3502 * the operation fails or succeeds, only on manual abort. */
3503 operation->num_ops = 0;
3504
Paul Elliottc9774412023-02-06 15:14:07 +00003505 /* Likewise, failure state. */
3506 operation->error_occurred = 0;
3507
Paul Elliott59ad9452022-12-18 15:09:02 +00003508 return status;
3509}
3510
3511static psa_status_t psa_verify_hash_abort_internal(
3512 psa_verify_hash_interruptible_operation_t *operation)
3513{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514 if (operation->id == 0) {
3515 /* The object has (apparently) been initialized but it is not (yet)
3516 * in use. It's ok to call abort on such an object, and there's
3517 * nothing to do. */
3518 return PSA_SUCCESS;
3519 }
3520
Paul Elliott59ad9452022-12-18 15:09:02 +00003521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3522
3523 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003524
3525 operation->id = 0;
3526
Paul Elliotte6145dc2023-02-07 12:51:21 +00003527 /* Do not clear either the error_occurred or num_ops elements here as they
3528 * only want to be cleared by the application calling abort, not by abort
3529 * being called at completion of an operation. */
3530
Paul Elliott59ad9452022-12-18 15:09:02 +00003531 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003532}
3533
3534psa_status_t psa_verify_hash_start(
3535 psa_verify_hash_interruptible_operation_t *operation,
3536 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3537 const uint8_t *hash, size_t hash_length,
3538 const uint8_t *signature, size_t signature_length)
3539{
3540 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3541 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3542 psa_key_slot_t *slot;
3543
Paul Elliottc9774412023-02-06 15:14:07 +00003544 /* Check that start has not been previously called, or operation has not
3545 * previously errored. */
3546 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003547 return PSA_ERROR_BAD_STATE;
3548 }
3549
3550 status = psa_sign_verify_check_alg(0, alg);
3551 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003552 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003553 return status;
3554 }
3555
3556 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3557 PSA_KEY_USAGE_VERIFY_HASH,
3558 alg);
3559
3560 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003561 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003562 return status;
3563 }
3564
3565 psa_key_attributes_t attributes = {
3566 .core = slot->attr
3567 };
3568
Paul Elliott296ede92022-12-15 17:00:30 +00003569 /* Ensure ops count gets reset, in case of operation re-use. */
3570 operation->num_ops = 0;
3571
Paul Elliott9fe12f62022-11-30 19:16:02 +00003572 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3573 slot->key.data,
3574 slot->key.bytes,
3575 alg, hash, hash_length,
3576 signature, signature_length);
3577
3578 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003579 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003580 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003581 }
3582
3583 unlock_status = psa_unlock_key_slot(slot);
3584
Paul Elliottc9774412023-02-06 15:14:07 +00003585 if (unlock_status != PSA_SUCCESS) {
3586 operation->error_occurred = 1;
3587 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003588
Paul Elliottc9774412023-02-06 15:14:07 +00003589 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003590}
3591
3592psa_status_t psa_verify_hash_complete(
3593 psa_verify_hash_interruptible_operation_t *operation)
3594{
3595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3596
Paul Elliottc9774412023-02-06 15:14:07 +00003597 /* Check that start has been called first, and that operation has not
3598 * previously errored. */
3599 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003600 status = PSA_ERROR_BAD_STATE;
3601 goto exit;
3602 }
3603
3604 status = psa_driver_wrapper_verify_hash_complete(operation);
3605
Paul Elliott296ede92022-12-15 17:00:30 +00003606 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003607 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003608 operation);
3609
Paul Elliottebe225c2023-02-10 14:32:53 +00003610exit:
3611
Paul Elliott9fe12f62022-11-30 19:16:02 +00003612 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003613 if (status != PSA_SUCCESS) {
3614 operation->error_occurred = 1;
3615 }
3616
Paul Elliott59ad9452022-12-18 15:09:02 +00003617 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003618 }
3619
3620 return status;
3621}
3622
3623psa_status_t psa_verify_hash_abort(
3624 psa_verify_hash_interruptible_operation_t *operation)
3625{
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627
Paul Elliott59ad9452022-12-18 15:09:02 +00003628 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003629
Paul Elliott59ad9452022-12-18 15:09:02 +00003630 /* We clear the number of ops done here, so that it is not cleared when
3631 * the operation fails or succeeds, only on manual abort. */
3632 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003633
Paul Elliottc9774412023-02-06 15:14:07 +00003634 /* Likewise, failure state. */
3635 operation->error_occurred = 0;
3636
Paul Elliott59ad9452022-12-18 15:09:02 +00003637 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003638}
3639
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640/****************************************************************/
3641/* Asymmetric interruptible cryptography internal */
3642/* implementations */
3643/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003644
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3646{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003647
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3649 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3650 defined(MBEDTLS_ECP_RESTARTABLE)
3651
3652 /* Internal implementation uses zero to indicate infinite number max ops,
3653 * therefore avoid this value, and set to minimum possible. */
3654 if (max_ops == 0) {
3655 max_ops = 1;
3656 }
3657
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003659#else
3660 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3662 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3663 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3664}
3665
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003667 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668{
3669#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3670 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3671 defined(MBEDTLS_ECP_RESTARTABLE)
3672
Paul Elliott93d9ca82023-02-15 18:14:21 +00003673 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674#else
3675 (void) operation;
3676 return 0;
3677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3678 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3679 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3680}
3681
3682uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003683 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684{
3685 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3686 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3687 defined(MBEDTLS_ECP_RESTARTABLE)
3688
Paul Elliott93d9ca82023-02-15 18:14:21 +00003689 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690#else
3691 (void) operation;
3692 return 0;
3693#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3694 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3695 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3696}
3697
3698psa_status_t mbedtls_psa_sign_hash_start(
3699 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3700 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3701 size_t key_buffer_size, psa_algorithm_t alg,
3702 const uint8_t *hash, size_t hash_length)
3703{
3704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003705 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706
Paul Elliott068fe072023-01-16 13:59:15 +00003707 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3708 return PSA_ERROR_NOT_SUPPORTED;
3709 }
3710
3711 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003712 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003713 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
3715#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003716 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3717 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
Paul Elliotta1c94092023-02-15 16:38:04 +00003719 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3720
Paul Elliott93d9ca82023-02-15 18:14:21 +00003721 /* Ensure num_ops is zero'ed in case of context re-use. */
3722 operation->num_ops = 0;
3723
Paul Elliott068fe072023-01-16 13:59:15 +00003724 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3725 attributes->core.bits,
3726 key_buffer,
3727 key_buffer_size,
3728 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott068fe072023-01-16 13:59:15 +00003730 if (status != PSA_SUCCESS) {
3731 return status;
3732 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733
Paul Elliott1bc59df2023-02-05 13:41:57 +00003734 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003735 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736
Paul Elliott068fe072023-01-16 13:59:15 +00003737 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003738 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003739 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740
Paul Elliott0290a762023-02-09 14:30:24 +00003741 /* We only need to store the same length of hash as the private key size
3742 * here, it would be truncated by the internal implementation anyway. */
3743 required_hash_length = (hash_length < operation->coordinate_bytes ?
3744 hash_length : operation->coordinate_bytes);
3745
Paul Elliottba70ad42023-02-15 18:23:53 +00003746 if (required_hash_length > sizeof(operation->hash)) {
3747 /* Shouldn't happen, but better safe than sorry. */
3748 return PSA_ERROR_CORRUPTION_DETECTED;
3749 }
3750
Paul Elliott0290a762023-02-09 14:30:24 +00003751 memcpy(operation->hash, hash, required_hash_length);
3752 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003753
3754 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
3756#else
Paul Elliott068fe072023-01-16 13:59:15 +00003757 (void) operation;
3758 (void) key_buffer;
3759 (void) key_buffer_size;
3760 (void) alg;
3761 (void) hash;
3762 (void) hash_length;
3763 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003764 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
Paul Elliott068fe072023-01-16 13:59:15 +00003766 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3768 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3769 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770}
3771
3772psa_status_t mbedtls_psa_sign_hash_complete(
3773 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3774 uint8_t *signature, size_t signature_size,
3775 size_t *signature_length)
3776{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3778 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3779 defined(MBEDTLS_ECP_RESTARTABLE)
3780
Paul Elliotta1c94092023-02-15 16:38:04 +00003781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003782 mbedtls_mpi r;
3783 mbedtls_mpi s;
3784
Paul Elliott46845252023-02-03 14:59:11 +00003785 mbedtls_mpi_init(&r);
3786 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003788 /* Ensure max_ops is set to the current value (or default). */
3789 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3790
Paul Elliotta1c94092023-02-15 16:38:04 +00003791 if (signature_size < 2 * operation->coordinate_bytes) {
3792 status = PSA_ERROR_BUFFER_TOO_SMALL;
3793 goto exit;
3794 }
3795
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003797
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3799 status = mbedtls_to_psa_error(
3800 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003801 &r,
3802 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803 &operation->ctx->d,
3804 operation->hash,
3805 operation->hash_length,
3806 operation->md_alg,
3807 mbedtls_psa_get_random,
3808 MBEDTLS_PSA_RANDOM_STATE,
3809 &operation->restart_ctx));
3810#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003811 status = PSA_ERROR_NOT_SUPPORTED;
3812 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3814 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815 status = mbedtls_to_psa_error(
3816 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003817 &r,
3818 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819 &operation->ctx->d,
3820 operation->hash,
3821 operation->hash_length,
3822 mbedtls_psa_get_random,
3823 MBEDTLS_PSA_RANDOM_STATE,
3824 mbedtls_psa_get_random,
3825 MBEDTLS_PSA_RANDOM_STATE,
3826 &operation->restart_ctx));
3827 }
3828
Paul Elliottf8e5b562023-02-19 18:43:45 +00003829 /* Hide the fact that the restart context only holds a delta of number of
3830 * ops done during the last operation, not an absolute value. */
3831 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3832
Paul Elliott724bd252023-02-08 12:35:08 +00003833 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003835 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003837 operation->coordinate_bytes)
3838 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
3840 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003841 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842 }
3843
3844 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003845 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003847 operation->coordinate_bytes,
3848 operation->coordinate_bytes)
3849 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
3851 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003852 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 }
3854
Paul Elliott1bc59df2023-02-05 13:41:57 +00003855 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliott724bd252023-02-08 12:35:08 +00003857 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858 }
Paul Elliott724bd252023-02-08 12:35:08 +00003859
3860exit:
3861
3862 mbedtls_mpi_free(&r);
3863 mbedtls_mpi_free(&s);
3864 return status;
3865
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 #else
3867
3868 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869 (void) signature;
3870 (void) signature_size;
3871 (void) signature_length;
3872
3873 return PSA_ERROR_NOT_SUPPORTED;
3874
3875#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3876 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3877 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3878}
3879
3880psa_status_t mbedtls_psa_sign_hash_abort(
3881 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3882{
3883
3884#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3885 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3886 defined(MBEDTLS_ECP_RESTARTABLE)
3887
3888 if (operation->ctx) {
3889 mbedtls_ecdsa_free(operation->ctx);
3890 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003891 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892 }
3893
3894 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3895
Paul Elliott93d9ca82023-02-15 18:14:21 +00003896 operation->num_ops = 0;
3897
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898 return PSA_SUCCESS;
3899
3900#else
3901
3902 (void) operation;
3903
3904 return PSA_ERROR_NOT_SUPPORTED;
3905
3906#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3907 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3908 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3909}
3910
3911psa_status_t mbedtls_psa_verify_hash_start(
3912 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3913 const psa_key_attributes_t *attributes,
3914 const uint8_t *key_buffer, size_t key_buffer_size,
3915 psa_algorithm_t alg,
3916 const uint8_t *hash, size_t hash_length,
3917 const uint8_t *signature, size_t signature_length)
3918{
3919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003920 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003921 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003922
Paul Elliott068fe072023-01-16 13:59:15 +00003923 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3924 return PSA_ERROR_NOT_SUPPORTED;
3925 }
3926
3927 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003928 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003929 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930
3931#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003932 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3933 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934
Paul Elliotta1c94092023-02-15 16:38:04 +00003935 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3936 mbedtls_mpi_init(&operation->r);
3937 mbedtls_mpi_init(&operation->s);
3938
Paul Elliott93d9ca82023-02-15 18:14:21 +00003939 /* Ensure num_ops is zero'ed in case of context re-use. */
3940 operation->num_ops = 0;
3941
Paul Elliott068fe072023-01-16 13:59:15 +00003942 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3943 attributes->core.bits,
3944 key_buffer,
3945 key_buffer_size,
3946 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliott068fe072023-01-16 13:59:15 +00003948 if (status != PSA_SUCCESS) {
3949 return status;
3950 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliottc569fc22023-02-10 13:02:54 +00003952 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003953
Paul Elliott1bc59df2023-02-05 13:41:57 +00003954 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003955 return PSA_ERROR_INVALID_SIGNATURE;
3956 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957
Paul Elliott068fe072023-01-16 13:59:15 +00003958 status = mbedtls_to_psa_error(
3959 mbedtls_mpi_read_binary(&operation->r,
3960 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003961 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott068fe072023-01-16 13:59:15 +00003963 if (status != PSA_SUCCESS) {
3964 return status;
3965 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliott068fe072023-01-16 13:59:15 +00003967 status = mbedtls_to_psa_error(
3968 mbedtls_mpi_read_binary(&operation->s,
3969 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003970 coordinate_bytes,
3971 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Paul Elliott068fe072023-01-16 13:59:15 +00003973 if (status != PSA_SUCCESS) {
3974 return status;
3975 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliott2c9843f2023-02-15 17:32:42 +00003977 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978
Paul Elliott2c9843f2023-02-15 17:32:42 +00003979 if (status != PSA_SUCCESS) {
3980 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003981 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003982
Paul Elliott0290a762023-02-09 14:30:24 +00003983 /* We only need to store the same length of hash as the private key size
3984 * here, it would be truncated by the internal implementation anyway. */
3985 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3986 coordinate_bytes);
3987
Paul Elliottba70ad42023-02-15 18:23:53 +00003988 if (required_hash_length > sizeof(operation->hash)) {
3989 /* Shouldn't happen, but better safe than sorry. */
3990 return PSA_ERROR_CORRUPTION_DETECTED;
3991 }
3992
Paul Elliott0290a762023-02-09 14:30:24 +00003993 memcpy(operation->hash, hash, required_hash_length);
3994 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003995
3996 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997#else
Paul Elliott068fe072023-01-16 13:59:15 +00003998 (void) operation;
3999 (void) key_buffer;
4000 (void) key_buffer_size;
4001 (void) alg;
4002 (void) hash;
4003 (void) hash_length;
4004 (void) signature;
4005 (void) signature_length;
4006 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004007 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004008 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009
Paul Elliott068fe072023-01-16 13:59:15 +00004010 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004011#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4012 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4013 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004014}
4015
4016psa_status_t mbedtls_psa_verify_hash_complete(
4017 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4018{
4019
4020#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4021 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4022 defined(MBEDTLS_ECP_RESTARTABLE)
4023
Paul Elliottf8e5b562023-02-19 18:43:45 +00004024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4025
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004026 /* Ensure max_ops is set to the current value (or default). */
4027 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4028
Paul Elliottf8e5b562023-02-19 18:43:45 +00004029 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004030 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4031 operation->hash,
4032 operation->hash_length,
4033 &operation->ctx->Q,
4034 &operation->r,
4035 &operation->s,
4036 &operation->restart_ctx));
4037
Paul Elliottf8e5b562023-02-19 18:43:45 +00004038 /* Hide the fact that the restart context only holds a delta of number of
4039 * ops done during the last operation, not an absolute value. */
4040 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4041
4042 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004043#else
4044 (void) operation;
4045
4046 return PSA_ERROR_NOT_SUPPORTED;
4047
4048#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4049 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4050 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4051}
4052
4053psa_status_t mbedtls_psa_verify_hash_abort(
4054 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4055{
4056
4057#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4058 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4059 defined(MBEDTLS_ECP_RESTARTABLE)
4060
4061 if (operation->ctx) {
4062 mbedtls_ecdsa_free(operation->ctx);
4063 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004064 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004065 }
4066
4067 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4068
Paul Elliott93d9ca82023-02-15 18:14:21 +00004069 operation->num_ops = 0;
4070
Paul Elliott588f8ed2022-12-02 18:10:26 +00004071 mbedtls_mpi_free(&operation->r);
4072 mbedtls_mpi_free(&operation->s);
4073
4074 return PSA_SUCCESS;
4075
4076#else
4077 (void) operation;
4078
4079 return PSA_ERROR_NOT_SUPPORTED;
4080
4081#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4082 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4083 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4084}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004085
mohammad1603503973b2018-03-12 15:59:30 +02004086/****************************************************************/
4087/* Symmetric cryptography */
4088/****************************************************************/
4089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4091 mbedtls_svc_key_id_t key,
4092 psa_algorithm_t alg,
4093 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004094{
4095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4096 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004097 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4099 PSA_KEY_USAGE_ENCRYPT :
4100 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004101 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004102
4103 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
4107 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004110 status = PSA_ERROR_INVALID_ARGUMENT;
4111 goto exit;
4112 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4115 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004118
Ronald Cron49fafa92021-03-10 08:34:23 +01004119 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004121 * so we only set it (in the driver wrapper) after resources have been
4122 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004123 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004125 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004127 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 }
4129 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004130
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004131 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004133 };
4134
Ronald Cronab99ac22020-10-01 16:38:28 +02004135 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (cipher_operation == MBEDTLS_ENCRYPT) {
4137 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4138 &attributes,
4139 slot->key.data,
4140 slot->key.bytes,
4141 alg);
4142 } else {
4143 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4144 &attributes,
4145 slot->key.data,
4146 slot->key.bytes,
4147 alg);
4148 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004149
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (status != PSA_SUCCESS) {
4152 psa_cipher_abort(operation);
4153 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4161 mbedtls_svc_key_id_t key,
4162 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004163{
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004165}
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4168 mbedtls_svc_key_id_t key,
4169 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004170{
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004172}
4173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4175 uint8_t *iv,
4176 size_t iv_size,
4177 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004178{
Ronald Cron6d051732020-10-01 14:10:20 +02004179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004180 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004181 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004184 status = PSA_ERROR_BAD_STATE;
4185 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004186 }
4187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004189 status = PSA_ERROR_BAD_STATE;
4190 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004191 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004192
Ronald Cron2fb90522021-07-05 12:31:44 +02004193 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004195 status = PSA_ERROR_BUFFER_TOO_SMALL;
4196 goto exit;
4197 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004200 status = PSA_ERROR_GENERIC_ERROR;
4201 goto exit;
4202 }
4203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 status = psa_generate_random(local_iv, default_iv_length);
4205 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 }
Ronald Cron5618a392021-03-26 09:52:26 +01004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_driver_wrapper_cipher_set_iv(operation,
4210 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004211
4212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (status == PSA_SUCCESS) {
4214 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004215 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004216 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004218 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004220 }
Ronald Cron6d051732020-10-01 14:10:20 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004223}
4224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4226 const uint8_t *iv,
4227 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004228{
Ronald Cron6d051732020-10-01 14:10:20 +02004229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +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;
4234 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237 status = PSA_ERROR_BAD_STATE;
4238 goto exit;
4239 }
Ronald Crona0d68172021-03-26 10:15:08 +01004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004242 status = PSA_ERROR_INVALID_ARGUMENT;
4243 goto exit;
4244 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 status = psa_driver_wrapper_cipher_set_iv(operation,
4247 iv,
4248 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004249
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 } else {
4254 psa_cipher_abort(operation);
4255 }
4256 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004257}
4258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4260 const uint8_t *input,
4261 size_t input_length,
4262 uint8_t *output,
4263 size_t output_size,
4264 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004265{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269 status = PSA_ERROR_BAD_STATE;
4270 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004271 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004274 status = PSA_ERROR_BAD_STATE;
4275 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004276 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 status = psa_driver_wrapper_cipher_update(operation,
4279 input,
4280 input_length,
4281 output,
4282 output_size,
4283 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004284
4285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (status != PSA_SUCCESS) {
4287 psa_cipher_abort(operation);
4288 }
Ronald Cron6d051732020-10-01 14:10:20 +02004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004291}
4292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004297{
David Saadab4ecc272019-02-14 13:48:10 +02004298 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301 status = PSA_ERROR_BAD_STATE;
4302 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004303 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004306 status = PSA_ERROR_BAD_STATE;
4307 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004308 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_driver_wrapper_cipher_finish(operation,
4311 output,
4312 output_size,
4313 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004314
4315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (status == PSA_SUCCESS) {
4317 return psa_cipher_abort(operation);
4318 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004319 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004323 }
mohammad1603503973b2018-03-12 15:59:30 +02004324}
4325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004327{
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004329 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004330 * in use. It's ok to call abort on such an object, and there's
4331 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004333 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004336
Ronald Cron49fafa92021-03-10 08:34:23 +01004337 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004338 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004339 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004342}
4343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4345 psa_algorithm_t alg,
4346 const uint8_t *input,
4347 size_t input_length,
4348 uint8_t *output,
4349 size_t output_size,
4350 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004351{
4352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004353 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004354 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004355 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4356 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004357 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004360 status = PSA_ERROR_INVALID_ARGUMENT;
4361 goto exit;
4362 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4365 PSA_KEY_USAGE_ENCRYPT,
4366 alg);
4367 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004370
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004371 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004373 };
4374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4376 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004377 status = PSA_ERROR_GENERIC_ERROR;
4378 goto exit;
4379 }
4380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (default_iv_length > 0) {
4382 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004383 status = PSA_ERROR_BUFFER_TOO_SMALL;
4384 goto exit;
4385 }
4386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_generate_random(local_iv, default_iv_length);
4388 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004391 }
4392
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004393 status = psa_driver_wrapper_cipher_encrypt(
4394 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004395 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004396 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004398
4399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 unlock_status = psa_unlock_key_slot(slot);
4401 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004402 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004403 }
Ronald Cron23919522021-07-08 19:00:07 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status == PSA_SUCCESS) {
4406 if (default_iv_length > 0) {
4407 memcpy(output, local_iv, default_iv_length);
4408 }
4409 *output_length += default_iv_length;
4410 } else {
4411 *output_length = 0;
4412 }
4413
4414 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004415}
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4418 psa_algorithm_t alg,
4419 const uint8_t *input,
4420 size_t input_length,
4421 uint8_t *output,
4422 size_t output_size,
4423 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004424{
4425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004426 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004427 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004428 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004431 status = PSA_ERROR_INVALID_ARGUMENT;
4432 goto exit;
4433 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4436 PSA_KEY_USAGE_DECRYPT,
4437 alg);
4438 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004441
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004442 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004444 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4447 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004448 status = PSA_ERROR_INVALID_ARGUMENT;
4449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004451 status = PSA_ERROR_INVALID_ARGUMENT;
4452 goto exit;
4453 }
4454
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004455 status = psa_driver_wrapper_cipher_decrypt(
4456 &attributes, slot->key.data, slot->key.bytes,
4457 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004459
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004460exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 unlock_status = psa_unlock_key_slot(slot);
4462 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004463 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004467 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 }
Ronald Cron23919522021-07-08 19:00:07 +02004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004471}
4472
4473
mohammad16035955c982018-04-26 00:53:03 +03004474/****************************************************************/
4475/* AEAD */
4476/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004477
Paul Elliottbaff51c2021-09-28 17:44:45 +01004478/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004479static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004480{
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004482}
4483
4484/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004485static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4486 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004487{
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004491#if defined(PSA_WANT_ALG_GCM)
4492 case PSA_ALG_GCM:
4493 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 * arbitrarily large nonces. Please note that we do not generally
4495 * recommend the usage of nonces of greater length than
4496 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4497 * size, which can then lead to collisions if you encrypt a very
4498 * large number of messages.*/
4499 if (nonce_length != 0) {
4500 return PSA_SUCCESS;
4501 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004502 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004503#endif /* PSA_WANT_ALG_GCM */
4504#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004505 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (nonce_length >= 7 && nonce_length <= 13) {
4507 return PSA_SUCCESS;
4508 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004510#endif /* PSA_WANT_ALG_CCM */
4511#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004512 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (nonce_length == 12) {
4514 return PSA_SUCCESS;
4515 } else if (nonce_length == 8) {
4516 return PSA_ERROR_NOT_SUPPORTED;
4517 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004518 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004519#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004520 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004521 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004523 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004526}
4527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004529{
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4531 return PSA_ERROR_INVALID_ARGUMENT;
4532 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004535}
4536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4538 psa_algorithm_t alg,
4539 const uint8_t *nonce,
4540 size_t nonce_length,
4541 const uint8_t *additional_data,
4542 size_t additional_data_length,
4543 const uint8_t *plaintext,
4544 size_t plaintext_length,
4545 uint8_t *ciphertext,
4546 size_t ciphertext_size,
4547 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004548{
Ronald Cron215633c2021-03-16 17:15:37 +01004549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4550 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004551
mohammad1603f08a5502018-06-03 15:05:47 +03004552 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 status = psa_aead_check_algorithm(alg);
4555 if (status != PSA_SUCCESS) {
4556 return status;
4557 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004558
Ronald Cron9a986162021-03-26 12:40:07 +01004559 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4561 if (status != PSA_SUCCESS) {
4562 return status;
4563 }
mohammad16035955c982018-04-26 00:53:03 +03004564
Ronald Cron215633c2021-03-16 17:15:37 +01004565 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004567 };
mohammad16035955c982018-04-26 00:53:03 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 status = psa_aead_check_nonce_length(alg, nonce_length);
4570 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004573
Ronald Cronde822812021-03-17 16:08:20 +01004574 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004575 &attributes, slot->key.data, slot->key.bytes,
4576 alg,
4577 nonce, nonce_length,
4578 additional_data, additional_data_length,
4579 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4583 memset(ciphertext, 0, ciphertext_size);
4584 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004585
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004590}
4591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4593 psa_algorithm_t alg,
4594 const uint8_t *nonce,
4595 size_t nonce_length,
4596 const uint8_t *additional_data,
4597 size_t additional_data_length,
4598 const uint8_t *ciphertext,
4599 size_t ciphertext_length,
4600 uint8_t *plaintext,
4601 size_t plaintext_size,
4602 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004603{
Ronald Cron215633c2021-03-16 17:15:37 +01004604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4605 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004606
Gilles Peskineee652a32018-06-01 19:23:52 +02004607 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 status = psa_aead_check_algorithm(alg);
4610 if (status != PSA_SUCCESS) {
4611 return status;
4612 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004613
Ronald Cron9a986162021-03-26 12:40:07 +01004614 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4616 if (status != PSA_SUCCESS) {
4617 return status;
4618 }
mohammad16035955c982018-04-26 00:53:03 +03004619
Ronald Cron215633c2021-03-16 17:15:37 +01004620 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004622 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 status = psa_aead_check_nonce_length(alg, nonce_length);
4625 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004628
Ronald Cronde822812021-03-17 16:08:20 +01004629 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004630 &attributes, slot->key.data, slot->key.bytes,
4631 alg,
4632 nonce, nonce_length,
4633 additional_data, additional_data_length,
4634 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if (status != PSA_SUCCESS && plaintext_size != 0) {
4638 memset(plaintext, 0, plaintext_size);
4639 }
mohammad160360a64d02018-06-03 17:20:42 +03004640
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 return status;
mohammad16035955c982018-04-26 00:53:03 +03004645}
4646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4648{
4649 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004652#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4656 return PSA_ERROR_INVALID_ARGUMENT;
4657 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004659#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004660
Przemek Stekiel86679c72022-10-06 17:06:56 +02004661#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4665 return PSA_ERROR_INVALID_ARGUMENT;
4666 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004668#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669
Przemek Stekiel86679c72022-10-06 17:06:56 +02004670#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (tag_len != 16) {
4674 return PSA_ERROR_INVALID_ARGUMENT;
4675 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004677#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004678
4679 default:
4680 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004684}
4685
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004687static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4688 int is_encrypt,
4689 mbedtls_svc_key_id_t key,
4690 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004691{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004692 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4693 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4694 psa_key_slot_t *slot = NULL;
4695 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004696 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 status = psa_aead_check_algorithm(alg);
4699 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704 status = PSA_ERROR_BAD_STATE;
4705 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004706 }
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (operation->nonce_set || operation->lengths_set ||
4709 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004710 status = PSA_ERROR_BAD_STATE;
4711 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004712 }
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4721 alg);
4722 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004725
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004726 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004727 .core = slot->attr
4728 };
4729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (is_encrypt) {
4735 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4736 &attributes,
4737 slot->key.data,
4738 slot->key.bytes,
4739 alg);
4740 } else {
4741 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4742 &attributes,
4743 slot->key.data,
4744 slot->key.bytes,
4745 alg);
4746 }
4747 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752
4753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004759 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 } else {
4761 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004762 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004765}
4766
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4769 mbedtls_svc_key_id_t key,
4770 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771{
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773}
4774
4775/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004776psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4777 mbedtls_svc_key_id_t key,
4778 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779{
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004781}
4782
4783/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004784psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4785 uint8_t *nonce,
4786 size_t nonce_size,
4787 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788{
4789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004790 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004791 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004793 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004796 status = PSA_ERROR_BAD_STATE;
4797 goto exit;
4798 }
4799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 status = PSA_ERROR_BAD_STATE;
4802 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004803 }
4804
Paul Elliotte193ea82021-10-01 13:00:16 +01004805 /* For CCM, this size may not be correct according to the PSA
4806 * specification. The PSA Crypto 1.0.1 specification states:
4807 *
4808 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4809 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4810 *
4811 * However this restriction that L has to be the smallest integer is not
4812 * applied in practice, and it is not implementable here since the
4813 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4815 operation->alg);
4816 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817 status = PSA_ERROR_BUFFER_TOO_SMALL;
4818 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819 }
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 status = psa_generate_random(local_nonce, required_nonce_size);
4822 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827
Paul Elliott39dc6b82021-05-11 19:16:09 +01004828exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (status == PSA_SUCCESS) {
4830 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 } else {
4833 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004834 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004837}
4838
4839/* Set the nonce for a multipart authenticated encryption or decryption
4840 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004841psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4842 const uint8_t *nonce,
4843 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004844{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004848 status = PSA_ERROR_BAD_STATE;
4849 goto exit;
4850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853 status = PSA_ERROR_BAD_STATE;
4854 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004855 }
4856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4858 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004859 status = PSA_ERROR_INVALID_ARGUMENT;
4860 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004861 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4864 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004865
Paul Elliott39dc6b82021-05-11 19:16:09 +01004866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 } else {
4870 psa_aead_abort(operation);
4871 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874}
4875
4876/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4878 size_t ad_length,
4879 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004884 status = PSA_ERROR_BAD_STATE;
4885 goto exit;
4886 }
4887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (operation->lengths_set || operation->ad_started ||
4889 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004890 status = PSA_ERROR_BAD_STATE;
4891 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004892 }
4893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004895#if defined(PSA_WANT_ALG_GCM)
4896 case PSA_ALG_GCM:
4897 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 * bits. Without the guard this code will generate warnings on 32bit
4899 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004900#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (((uint64_t) ad_length) >> 61 != 0 ||
4902 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004903 status = PSA_ERROR_INVALID_ARGUMENT;
4904 goto exit;
4905 }
Paul Elliott325d3742021-09-27 17:56:28 +01004906#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004908#endif /* PSA_WANT_ALG_GCM */
4909#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004912 status = PSA_ERROR_INVALID_ARGUMENT;
4913 goto exit;
4914 }
4915 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004916#endif /* PSA_WANT_ALG_CCM */
4917#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004918 case PSA_ALG_CHACHA20_POLY1305:
4919 /* No length restrictions for ChaChaPoly. */
4920 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004921#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004922 default:
4923 break;
4924 }
Paul Elliott325d3742021-09-27 17:56:28 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4927 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004928
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004931 operation->ad_remaining = ad_length;
4932 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 } else {
4935 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004936 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004940
4941/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004942psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4943 const uint8_t *input,
4944 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004956 }
4957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 if (operation->lengths_set) {
4959 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004960 status = PSA_ERROR_INVALID_ARGUMENT;
4961 goto exit;
4962 }
4963
4964 operation->ad_remaining -= input_length;
4965 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004966#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004968 status = PSA_ERROR_BAD_STATE;
4969 goto exit;
4970 }
4971#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 status = psa_driver_wrapper_aead_update_ad(operation, input,
4974 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004975
Paul Elliott39dc6b82021-05-11 19:16:09 +01004976exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004978 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 } else {
4980 psa_aead_abort(operation);
4981 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984}
4985
4986/* Encrypt or decrypt a message fragment in an active multipart AEAD
4987 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004988psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4989 const uint8_t *input,
4990 size_t input_length,
4991 uint8_t *output,
4992 size_t output_size,
4993 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004996
4997 *output_length = 0;
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005000 status = PSA_ERROR_BAD_STATE;
5001 goto exit;
5002 }
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005005 status = PSA_ERROR_BAD_STATE;
5006 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007 }
5008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005010 /* Additional data length was supplied, but not all the additional
5011 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005013 status = PSA_ERROR_INVALID_ARGUMENT;
5014 goto exit;
5015 }
5016
5017 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005019 status = PSA_ERROR_INVALID_ARGUMENT;
5020 goto exit;
5021 }
5022
5023 operation->body_remaining -= input_length;
5024 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005025#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005027 status = PSA_ERROR_BAD_STATE;
5028 goto exit;
5029 }
5030#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5033 output, output_size,
5034 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005035
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005038 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 } else {
5040 psa_aead_abort(operation);
5041 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005044}
5045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005047{
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (operation->id == 0 || !operation->nonce_set) {
5049 return PSA_ERROR_BAD_STATE;
5050 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5053 operation->body_remaining != 0)) {
5054 return PSA_ERROR_INVALID_ARGUMENT;
5055 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005058}
5059
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005061psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5062 uint8_t *ciphertext,
5063 size_t ciphertext_size,
5064 size_t *ciphertext_length,
5065 uint8_t *tag,
5066 size_t tag_size,
5067 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5070
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005072 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_aead_final_checks(operation);
5075 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005080 status = PSA_ERROR_BAD_STATE;
5081 goto exit;
5082 }
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5085 ciphertext_size,
5086 ciphertext_length,
5087 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005088
Paul Elliott39dc6b82021-05-11 19:16:09 +01005089exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005090
5091
Paul Elliottf47b0952021-05-21 18:02:33 +01005092 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005093 * the zero tag size, make sure the tag is set to something implausible.
5094 * Even if the operation succeeds, make sure we clear the rest of the
5095 * buffer to prevent potential leakage of anything previously placed in
5096 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005097 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102}
5103
5104/* Finish authenticating and decrypting a message in a multipart AEAD
5105 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005106psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5107 uint8_t *plaintext,
5108 size_t plaintext_size,
5109 size_t *plaintext_length,
5110 const uint8_t *tag,
5111 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005112{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5114
Paul Elliott302ff6b2021-04-20 18:10:30 +01005115 *plaintext_length = 0;
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 status = psa_aead_final_checks(operation);
5118 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005123 status = PSA_ERROR_BAD_STATE;
5124 goto exit;
5125 }
5126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5128 plaintext_size,
5129 plaintext_length,
5130 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005131
5132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136}
5137
5138/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140{
5141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144 /* The object has (apparently) been initialized but it is not (yet)
5145 * in use. It's ok to call abort on such an object, and there's
5146 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005148 }
5149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005155}
5156
Gilles Peskinee59236f2018-01-27 23:32:46 +01005157/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158/* Generators */
5159/****************************************************************/
5160
Przemek Stekiel69c46792022-06-10 12:59:51 +02005161#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305164 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305165 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005166#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005167#endif /* At least one builtin KDF */
5168
Przemek Stekiel69c46792022-06-10 12:59:51 +02005169#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005170 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5171 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5172static psa_status_t psa_key_derivation_start_hmac(
5173 psa_mac_operation_t *operation,
5174 psa_algorithm_t hash_alg,
5175 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005177{
5178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5181 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005183
Steven Cooreman72f736a2021-05-07 14:14:37 +02005184 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 status = psa_driver_wrapper_mac_sign_setup(operation,
5188 &attributes,
5189 hmac_key, hmac_key_length,
5190 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 psa_reset_key_attributes(&attributes);
5193 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005194}
5195#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005196
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005197#define HKDF_STATE_INIT 0 /* no input yet */
5198#define HKDF_STATE_STARTED 1 /* got salt */
5199#define HKDF_STATE_KEYED 2 /* got key */
5200#define HKDF_STATE_OUTPUT 3 /* output started */
5201
Gilles Peskinecbe66502019-05-16 16:59:18 +02005202static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204{
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5206 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5207 } else {
5208 return operation->alg;
5209 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210}
5211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213{
5214 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5216 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217 /* The object has (apparently) been initialized but it is not
5218 * in use. It's ok to call abort on such an object, and there's
5219 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005221#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5223 mbedtls_free(operation->ctx.hkdf.info);
5224 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5225 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005226#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005227#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5228 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5230 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5231 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5232 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005233 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005235 }
5236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005238 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005240 }
5241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005243 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005245 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005248 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005250 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005251#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005252 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005253
5254 /* We leave the fields Ai and output_block to be erased safely by the
5255 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005257#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5258 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005259#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5261 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5262 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5263 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005264#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305265#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305266 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005268 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305269 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305270 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305271
5272 status = PSA_SUCCESS;
5273 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305274#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005275 {
5276 status = PSA_ERROR_BAD_STATE;
5277 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 mbedtls_platform_zeroize(operation, sizeof(*operation));
5279 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280}
5281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284{
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005288 }
5289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292}
5293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5295 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005296{
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 if (operation->alg == 0) {
5298 return PSA_ERROR_BAD_STATE;
5299 }
5300 if (capacity > operation->capacity) {
5301 return PSA_ERROR_INVALID_ARGUMENT;
5302 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005305}
5306
Przemek Stekiel69c46792022-06-10 12:59:51 +02005307#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005308/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5310 psa_algorithm_t kdf_alg,
5311 uint8_t *output,
5312 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005313{
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5315 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005316 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005317 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005320#else
5321 const uint8_t last_block = 0xff;
5322#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (hkdf->state < HKDF_STATE_KEYED ||
5325 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005326#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005328#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 )) {
5330 return PSA_ERROR_BAD_STATE;
5331 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 hkdf->state = HKDF_STATE_OUTPUT;
5333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335 /* Copy what remains of the current block */
5336 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005338 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
5340 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341 output += n;
5342 output_length -= n;
5343 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005347 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005348 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005350 * object was corrupted or if this function is called directly
5351 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 if (hkdf->block_number == last_block) {
5353 return PSA_ERROR_BAD_STATE;
5354 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005355
5356 /* We need a new block */
5357 ++hkdf->block_number;
5358 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5361 hash_alg,
5362 hkdf->prk,
5363 hash_length);
5364 if (status != PSA_SUCCESS) {
5365 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005366 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005367
5368 if (hkdf->block_number != 1) {
5369 status = psa_mac_update(&hkdf->hmac,
5370 hkdf->output_block,
5371 hash_length);
5372 if (status != PSA_SUCCESS) {
5373 return status;
5374 }
5375 }
5376 status = psa_mac_update(&hkdf->hmac,
5377 hkdf->info,
5378 hkdf->info_length);
5379 if (status != PSA_SUCCESS) {
5380 return status;
5381 }
5382 status = psa_mac_update(&hkdf->hmac,
5383 &hkdf->block_number, 1);
5384 if (status != PSA_SUCCESS) {
5385 return status;
5386 }
5387 status = psa_mac_sign_finish(&hkdf->hmac,
5388 hkdf->output_block,
5389 sizeof(hkdf->output_block),
5390 &hmac_output_length);
5391 if (status != PSA_SUCCESS) {
5392 return status;
5393 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005398#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005399
John Durkop07cc04a2020-11-16 22:08:34 -08005400#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5401 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005402static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5403 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005405{
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5407 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005408 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5409 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005410 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005411
Janos Follath7742fee2019-06-17 12:58:10 +01005412 /* We can't be wanting more output after block 0xff, otherwise
5413 * the capacity check in psa_key_derivation_output_bytes() would have
5414 * prevented this call. It could happen only if the operation
5415 * object was corrupted or if this function is called directly
5416 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (tls12_prf->block_number == 0xff) {
5418 return PSA_ERROR_CORRUPTION_DETECTED;
5419 }
Janos Follath7742fee2019-06-17 12:58:10 +01005420
5421 /* We need a new block */
5422 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005423 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005424
5425 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5426 *
5427 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5428 *
5429 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5430 * HMAC_hash(secret, A(2) + seed) +
5431 * HMAC_hash(secret, A(3) + seed) + ...
5432 *
5433 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005435 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005436 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005437 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005438 * is currently extracted as `output_block` and where i is
5439 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005440 */
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 status = psa_key_derivation_start_hmac(&hmac,
5443 hash_alg,
5444 tls12_prf->secret,
5445 tls12_prf->secret_length);
5446 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005449
5450 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5453 * the variable seed and in this instance means it in the context of the
5454 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_mac_update(&hmac,
5456 tls12_prf->label,
5457 tls12_prf->label_length);
5458 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 }
5461 status = psa_mac_update(&hmac,
5462 tls12_prf->seed,
5463 tls12_prf->seed_length);
5464 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 }
5467 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5470 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 status = psa_mac_sign_finish(&hmac,
5476 tls12_prf->Ai, hash_length,
5477 &hmac_output_length);
5478 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005479 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 }
5481 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005482 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005484
5485 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 status = psa_key_derivation_start_hmac(&hmac,
5487 hash_alg,
5488 tls12_prf->secret,
5489 tls12_prf->secret_length);
5490 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005491 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 }
5493 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
5497 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5498 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 }
5501 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5502 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 }
5505 status = psa_mac_sign_finish(&hmac,
5506 tls12_prf->output_block, hash_length,
5507 &hmac_output_length);
5508 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005509 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005511
Janos Follath7742fee2019-06-17 12:58:10 +01005512
5513cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 cleanup_status = psa_mac_abort(&hmac);
5515 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005516 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005520}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005521
Janos Follath844eb0e2019-06-19 12:10:49 +01005522static psa_status_t psa_key_derivation_tls12_prf_read(
5523 psa_tls12_prf_key_derivation_t *tls12_prf,
5524 psa_algorithm_t alg,
5525 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005527{
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5529 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005530 psa_status_t status;
5531 uint8_t offset, length;
5532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005534 case PSA_TLS12_PRF_STATE_LABEL_SET:
5535 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5536 break;
5537 case PSA_TLS12_PRF_STATE_OUTPUT:
5538 break;
5539 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005541 }
5542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005544 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 if (tls12_prf->left_in_block == 0) {
5546 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5547 alg);
5548 if (status != PSA_SUCCESS) {
5549 return status;
5550 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005551
5552 continue;
5553 }
5554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005556 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005558 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005560
5561 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005563 output += length;
5564 output_length -= length;
5565 tls12_prf->left_in_block -= length;
5566 }
5567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005569}
John Durkop07cc04a2020-11-16 22:08:34 -08005570#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5571 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005572
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005573#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5574static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5575 psa_tls12_ecjpake_to_pms_t *ecjpake,
5576 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005578{
5579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005580 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 if (output_length != 32) {
5583 return PSA_ERROR_INVALID_ARGUMENT;
5584 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5587 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5588 &output_size);
5589 if (status != PSA_SUCCESS) {
5590 return status;
5591 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if (output_size != output_length) {
5594 return PSA_ERROR_GENERIC_ERROR;
5595 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005598}
5599#endif
5600
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305601#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305602static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5603 psa_pbkdf2_key_derivation_t *pbkdf2,
5604 psa_algorithm_t prf_alg,
5605 uint8_t prf_output_length,
5606 psa_key_attributes_t *attributes)
5607{
5608 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305609 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305610 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305611 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305612 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305615
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305616 mac_operation.is_sign = 1;
5617 mac_operation.mac_size = prf_output_length;
5618 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305619
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5621 attributes,
5622 pbkdf2->password,
5623 pbkdf2->password_length,
5624 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305625 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305626 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305627 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305628 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5629 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305630 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305632 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305633 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305634 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 }
5636 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5637 &mac_output_length);
5638 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305639 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305640 }
5641
5642 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305643 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305644 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305645 }
5646
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305647 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648
5649 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305650 /* We are passing prf_output_length as mac_size because the driver
5651 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305652 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653 status = psa_driver_wrapper_mac_compute(attributes,
5654 pbkdf2->password,
5655 pbkdf2->password_length,
5656 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305657 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305658 &mac_output_length);
5659 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305660 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305661 }
5662
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305663 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305664 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305665
5666cleanup:
5667 /* Zeroise buffers to clear sensitive data from memory. */
5668 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5669 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305670}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305671
5672static psa_status_t psa_key_derivation_pbkdf2_read(
5673 psa_pbkdf2_key_derivation_t *pbkdf2,
5674 psa_algorithm_t kdf_alg,
5675 uint8_t *output,
5676 size_t output_length)
5677{
5678 psa_status_t status;
5679 psa_algorithm_t prf_alg;
5680 uint8_t prf_output_length;
5681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5682 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5683 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5684
5685 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5686 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5687 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5688 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305689 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5690 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305691 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305692 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305693 } else {
5694 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305695 }
5696
5697 switch (pbkdf2->state) {
5698 case PSA_PBKDF2_STATE_PASSWORD_SET:
5699 /* Initially we need a new block so bytes_used is equal to block size*/
5700 pbkdf2->bytes_used = prf_output_length;
5701 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5702 break;
5703 case PSA_PBKDF2_STATE_OUTPUT:
5704 break;
5705 default:
5706 return PSA_ERROR_BAD_STATE;
5707 }
5708
5709 while (output_length != 0) {
5710 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5711 if (n > output_length) {
5712 n = (uint8_t) output_length;
5713 }
5714 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5715 output += n;
5716 output_length -= n;
5717 pbkdf2->bytes_used += n;
5718
5719 if (output_length == 0) {
5720 break;
5721 }
5722
5723 /* We need a new block */
5724 pbkdf2->bytes_used = 0;
5725 pbkdf2->block_number++;
5726
5727 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5728 prf_output_length,
5729 &attributes);
5730 if (status != PSA_SUCCESS) {
5731 return status;
5732 }
5733 }
5734
5735 return PSA_SUCCESS;
5736}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305737#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305738
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005739psa_status_t psa_key_derivation_output_bytes(
5740 psa_key_derivation_operation_t *operation,
5741 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005743{
5744 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005750 }
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005756 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757 goto exit;
5758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005761 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005762 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5763 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005765 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005769
Przemek Stekiel69c46792022-06-10 12:59:51 +02005770#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5772 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5773 output, output_length);
5774 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005775#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005776#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5777 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5779 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5780 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5781 kdf_alg, output,
5782 output_length);
5783 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005784#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5785 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005786#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005788 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5790 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005791#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305792#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305793 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305794 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5795 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305796 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305797#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005798
Gilles Peskineeab56e42018-07-12 17:12:33 +02005799 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005800 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802 }
5803
5804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005806 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 * This allows us to differentiate between exhausted operations and
5808 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5809 * operations. */
5810 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005812 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005816}
5817
David Brown7807bf72021-02-09 16:28:23 -07005818#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005819static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005820{
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (data_size >= 8) {
5822 mbedtls_des_key_set_parity(data);
5823 }
5824 if (data_size >= 16) {
5825 mbedtls_des_key_set_parity(data + 8);
5826 }
5827 if (data_size >= 24) {
5828 mbedtls_des_key_set_parity(data + 16);
5829 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005830}
David Brown7807bf72021-02-09 16:28:23 -07005831#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005832
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 * ECC keys on a Weierstrass elliptic curve require the generation
5835 * of a private key which is an integer
5836 * in the range [1, N - 1], where N is the boundary of the private key domain:
5837 * N is the prime p for Diffie-Hellman, or the order of the
5838 * curve’s base point for ECC.
5839 *
5840 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5841 * This function generates the private key using the following process:
5842 *
5843 * 1. Draw a byte string of length ceiling(m/8) bytes.
5844 * 2. If m is not a multiple of 8, set the most significant
5845 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5846 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5847 * 4. If k > N - 2, discard the result and return to step 1.
5848 * 5. Output k + 1 as the private key.
5849 *
5850 * This method allows compliance to NIST standards, specifically the methods titled
5851 * Key-Pair Generation by Testing Candidates in the following publications:
5852 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5853 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5854 * Diffie-Hellman keys.
5855 *
5856 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5857 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5858 *
5859 * Note: Function allocates memory for *data buffer, so given *data should be
5860 * always NULL.
5861 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005862#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5863#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005864static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865 psa_key_slot_t *slot,
5866 size_t bits,
5867 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005868 uint8_t **data
5869 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005871 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 mbedtls_mpi k;
5873 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005876 size_t m;
5877 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 mbedtls_mpi_init(&k);
5880 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005881
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005888 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005889 goto cleanup;
5890 }
5891
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005897 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005899 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005900
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005901 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005903 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005905
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906 /* Note: This function is always called with *data == NULL and it
5907 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 *data = mbedtls_calloc(1, m_bytes);
5909 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005911 goto cleanup;
5912 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005915 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005917 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919
5920 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005922 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 * (8 * ceiling(m/8) - m) bits of the first byte in
5924 * the string to zero.
5925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005927 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005928 }
5929
5930 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 * big-endian byte string.
5932 */
5933 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005934
5935 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 * Result of comparison is returned. When it indicates error
5937 * then this function is called again.
5938 */
5939 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005940 }
5941
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5944 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005945cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (ret != 0) {
5947 status = mbedtls_to_psa_error(ret);
5948 }
5949 if (status != PSA_SUCCESS) {
5950 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005951 *data = NULL;
5952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 mbedtls_mpi_free(&k);
5954 mbedtls_mpi_free(&diff_N_2);
5955 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005956}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005957
5958/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5959 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5960 *
5961 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5962 * draw a 32-byte string and process it as specified in
5963 * Elliptic Curves for Security [RFC7748] §5.
5964 *
5965 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5966 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5967 *
5968 * Note: Function allocates memory for *data buffer, so given *data should be
5969 * always NULL.
5970 */
5971
5972static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5973 size_t bits,
5974 psa_key_derivation_operation_t *operation,
5975 uint8_t **data
5976 )
5977{
5978 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005982 case 255:
5983 output_length = 32;
5984 break;
5985 case 448:
5986 output_length = 56;
5987 break;
5988 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990 break;
5991 }
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (*data == NULL) {
5996 return PSA_ERROR_INSUFFICIENT_MEMORY;
5997 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006004
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006006 case 255:
6007 (*data)[0] &= 248;
6008 (*data)[31] &= 127;
6009 (*data)[31] |= 64;
6010 break;
6011 case 448:
6012 (*data)[0] &= 252;
6013 (*data)[55] |= 128;
6014 break;
6015 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006017 break;
6018 }
6019
6020 return status;
6021}
Valerio Setti86587ab2023-06-27 13:09:30 +02006022#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6023static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6024 psa_key_slot_t *slot, size_t bits,
6025 psa_key_derivation_operation_t *operation, uint8_t **data)
6026{
6027 (void) slot;
6028 (void) bits;
6029 (void) operation;
6030 (void) data;
6031 return PSA_ERROR_NOT_SUPPORTED;
6032}
6033
6034static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6035 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6036{
6037 (void) bits;
6038 (void) operation;
6039 (void) data;
6040 return PSA_ERROR_NOT_SUPPORTED;
6041}
6042#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6043#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006044
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006045static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006046 psa_key_slot_t *slot,
6047 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049{
6050 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306052 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006054 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6057 return PSA_ERROR_INVALID_ARGUMENT;
6058 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006059
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006060#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006061 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6063 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6064 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006065 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6067 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 }
6070 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006071 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6073 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006076 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006077 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006078#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006079 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (key_type_is_raw_bytes(slot->attr.type)) {
6081 if (bits % 8 != 0) {
6082 return PSA_ERROR_INVALID_ARGUMENT;
6083 }
6084 data = mbedtls_calloc(1, bytes);
6085 if (data == NULL) {
6086 return PSA_ERROR_INSUFFICIENT_MEMORY;
6087 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 status = psa_key_derivation_output_bytes(operation, data, bytes);
6090 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 }
David Brown12ca5032021-02-11 11:02:00 -07006093#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6095 psa_des_set_key_parity(data, bytes);
6096 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006097#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 } else {
6099 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006100 }
Ronald Crondd04d422020-11-28 15:14:42 +01006101
Ronald Cronbf33c932020-11-28 18:06:53 +01006102 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006103 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006105 };
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6108 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6109 &storage_size);
6110 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
Archanad8a83dc2021-06-14 10:04:16 +05306113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 status = psa_allocate_buffer_to_slot(slot, storage_size);
6115 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 }
Archanad8a83dc2021-06-14 10:04:16 +05306118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 status = psa_driver_wrapper_import_key(&attributes,
6120 data, bytes,
6121 slot->key.data,
6122 slot->key.bytes,
6123 &slot->key.bytes, &bits);
6124 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006125 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006127
6128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 mbedtls_free(data);
6130 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006131}
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6134 psa_key_derivation_operation_t *operation,
6135 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006136{
6137 psa_status_t status;
6138 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006139 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006140
Ronald Cron81709fc2020-11-14 12:10:32 +01006141 *key = MBEDTLS_SVC_KEY_ID_INIT;
6142
Gilles Peskine0f84d622019-09-12 19:03:13 +02006143 /* Reject any attempt to create a zero-length key so that we don't
6144 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (psa_get_key_bits(attributes) == 0) {
6146 return PSA_ERROR_INVALID_ARGUMENT;
6147 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (operation->alg == PSA_ALG_NONE) {
6150 return PSA_ERROR_BAD_STATE;
6151 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (!operation->can_output_key) {
6154 return PSA_ERROR_NOT_PERMITTED;
6155 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6158 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006161 /* Deriving a key in a secure element is not implemented yet. */
6162 status = PSA_ERROR_NOT_SUPPORTED;
6163 }
6164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (status == PSA_SUCCESS) {
6166 status = psa_generate_derived_key_internal(slot,
6167 attributes->core.bits,
6168 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (status == PSA_SUCCESS) {
6171 status = psa_finish_key_creation(slot, driver, key);
6172 }
6173 if (status != PSA_SUCCESS) {
6174 psa_fail_key_creation(slot, driver);
6175 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006178}
6179
Gilles Peskineeab56e42018-07-12 17:12:33 +02006180
6181
6182/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006183/* Key derivation */
6184/****************************************************************/
6185
Steven Cooreman932ffb72021-02-15 12:14:32 +01006186#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006187static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006188{
6189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6191 return 1;
6192 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006193#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6196 return 1;
6197 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006198#endif
6199#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6201 return 1;
6202 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006203#endif
6204#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6206 return 1;
6207 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006208#endif
6209#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6211 return 1;
6212 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006213#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006214#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6216 return 1;
6217 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006218#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306219#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6220 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6221 return 1;
6222 }
6223#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306224#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6225 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6226 return 1;
6227 }
6228#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006230}
6231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006233{
6234 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 psa_status_t status = psa_hash_setup(&operation, alg);
6236 psa_hash_abort(&operation);
6237 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006238}
6239
Gilles Peskine969c5d62019-01-16 15:53:06 +01006240static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006241 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006243{
Janos Follath5fe19732019-06-20 15:09:30 +01006244 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6245 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006247
Gilles Peskine969c5d62019-01-16 15:53:06 +01006248 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (!is_kdf_alg_supported(kdf_alg)) {
6250 return PSA_ERROR_NOT_SUPPORTED;
6251 }
John Durkop07cc04a2020-11-16 22:08:34 -08006252
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006253 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306254 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6256 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306257 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6258 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6259 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306260 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306261 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (hash_size == 0) {
6263 return PSA_ERROR_NOT_SUPPORTED;
6264 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006265
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006266 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6267 * we might fail later, which is somewhat unfriendly and potentially
6268 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_status_t status = psa_hash_try_support(hash_alg);
6270 if (status != PSA_SUCCESS) {
6271 return status;
6272 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006273 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6276 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6277 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006279 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006281 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6283 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006286#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6287 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 operation->capacity = 255 * hash_size;
6289 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006290}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006293{
6294#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 if (alg == PSA_ALG_ECDH) {
6296 return PSA_SUCCESS;
6297 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006298#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006299#if defined(PSA_WANT_ALG_FFDH)
6300 if (alg == PSA_ALG_FFDH) {
6301 return PSA_SUCCESS;
6302 }
6303#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006304 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006306}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006307
6308static int psa_key_derivation_allows_free_form_secret_input(
6309 psa_algorithm_t kdf_alg)
6310{
6311#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6312 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6313 return 0;
6314 }
6315#endif
6316 (void) kdf_alg;
6317 return 1;
6318}
John Durkop07cc04a2020-11-16 22:08:34 -08006319#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6322 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006323{
6324 psa_status_t status;
6325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (operation->alg != 0) {
6327 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006328 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6331 return PSA_ERROR_INVALID_ARGUMENT;
6332 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6333#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6334 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6335 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6336 status = psa_key_agreement_try_support(ka_alg);
6337 if (status != PSA_SUCCESS) {
6338 return status;
6339 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006340 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6341 return PSA_ERROR_INVALID_ARGUMENT;
6342 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6344#else
6345 return PSA_ERROR_NOT_SUPPORTED;
6346#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6347 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6348#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6349 status = psa_key_derivation_setup_kdf(operation, alg);
6350#else
6351 return PSA_ERROR_NOT_SUPPORTED;
6352#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6353 } else {
6354 return PSA_ERROR_INVALID_ARGUMENT;
6355 }
6356
6357 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006358 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 }
6360 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361}
6362
Przemek Stekiel69c46792022-06-10 12:59:51 +02006363#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006364static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6365 psa_algorithm_t kdf_alg,
6366 psa_key_derivation_step_t step,
6367 const uint8_t *data,
6368 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006369{
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006371 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006373 case PSA_KEY_DERIVATION_INPUT_SALT:
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)) {
6376 return PSA_ERROR_INVALID_ARGUMENT;
6377 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006378#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (hkdf->state != HKDF_STATE_INIT) {
6380 return PSA_ERROR_BAD_STATE;
6381 } else {
6382 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6383 hash_alg,
6384 data, data_length);
6385 if (status != PSA_SUCCESS) {
6386 return status;
6387 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006388 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006390 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006391 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006392#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006394 /* We shouldn't be in different state as HKDF_EXPAND only allows
6395 * two inputs: SECRET (this case) and INFO which does not modify
6396 * the state. It could happen only if the hkdf
6397 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (hkdf->state != HKDF_STATE_INIT) {
6399 return PSA_ERROR_BAD_STATE;
6400 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006401
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006402 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6404 return PSA_ERROR_INVALID_ARGUMENT;
6405 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 memcpy(hkdf->prk, data, data_length);
6408 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006409#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006410 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006411 /* HKDF: If no salt was provided, use an empty salt.
6412 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006414#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6416 return PSA_ERROR_BAD_STATE;
6417 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006418#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6420 hash_alg,
6421 NULL, 0);
6422 if (status != PSA_SUCCESS) {
6423 return status;
6424 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006425 hkdf->state = HKDF_STATE_STARTED;
6426 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 if (hkdf->state != HKDF_STATE_STARTED) {
6428 return PSA_ERROR_BAD_STATE;
6429 }
6430 status = psa_mac_update(&hkdf->hmac,
6431 data, data_length);
6432 if (status != PSA_SUCCESS) {
6433 return status;
6434 }
6435 status = psa_mac_sign_finish(&hkdf->hmac,
6436 hkdf->prk,
6437 sizeof(hkdf->prk),
6438 &data_length);
6439 if (status != PSA_SUCCESS) {
6440 return status;
6441 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006442 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006443
Gilles Peskine2b522db2019-04-12 15:11:49 +02006444 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006445 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006446#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006448 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006450 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006452#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006453 {
6454 /* Block 0 is empty, and the next block will be
6455 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006457 }
6458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006460 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006461#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6463 return PSA_ERROR_INVALID_ARGUMENT;
6464 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006465#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006466#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6468 hkdf->state == HKDF_STATE_INIT) {
6469 return PSA_ERROR_BAD_STATE;
6470 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006471#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 if (hkdf->state == HKDF_STATE_OUTPUT) {
6473 return PSA_ERROR_BAD_STATE;
6474 }
6475 if (hkdf->info_set) {
6476 return PSA_ERROR_BAD_STATE;
6477 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 if (data_length != 0) {
6480 hkdf->info = mbedtls_calloc(1, data_length);
6481 if (hkdf->info == NULL) {
6482 return PSA_ERROR_INSUFFICIENT_MEMORY;
6483 }
6484 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006485 }
6486 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006490 }
6491}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006492#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006493
John Durkop07cc04a2020-11-16 22:08:34 -08006494#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6495 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006496static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6497 const uint8_t *data,
6498 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006499{
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6501 return PSA_ERROR_BAD_STATE;
6502 }
Janos Follathf08e2652019-06-13 09:05:41 +01006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (data_length != 0) {
6505 prf->seed = mbedtls_calloc(1, data_length);
6506 if (prf->seed == NULL) {
6507 return PSA_ERROR_INSUFFICIENT_MEMORY;
6508 }
Janos Follathf08e2652019-06-13 09:05:41 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006511 prf->seed_length = data_length;
6512 }
Janos Follathf08e2652019-06-13 09:05:41 +01006513
Gilles Peskine43f958b2020-12-13 14:55:14 +01006514 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006517}
6518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6520 const uint8_t *data,
6521 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006522{
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6524 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6525 return PSA_ERROR_BAD_STATE;
6526 }
Janos Follath81550542019-06-13 14:26:34 +01006527
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 if (data_length != 0) {
6529 prf->secret = mbedtls_calloc(1, data_length);
6530 if (prf->secret == NULL) {
6531 return PSA_ERROR_INSUFFICIENT_MEMORY;
6532 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006533
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006535 prf->secret_length = data_length;
6536 }
Janos Follath81550542019-06-13 14:26:34 +01006537
Gilles Peskine43f958b2020-12-13 14:55:14 +01006538 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006541}
6542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6544 const uint8_t *data,
6545 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006546{
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6548 return PSA_ERROR_BAD_STATE;
6549 }
Janos Follath63028dd2019-06-13 09:15:47 +01006550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 if (data_length != 0) {
6552 prf->label = mbedtls_calloc(1, data_length);
6553 if (prf->label == NULL) {
6554 return PSA_ERROR_INSUFFICIENT_MEMORY;
6555 }
Janos Follath63028dd2019-06-13 09:15:47 +01006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006558 prf->label_length = data_length;
6559 }
Janos Follath63028dd2019-06-13 09:15:47 +01006560
Gilles Peskine43f958b2020-12-13 14:55:14 +01006561 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006564}
6565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6567 psa_key_derivation_step_t step,
6568 const uint8_t *data,
6569 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006570{
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006572 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006574 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006576 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006578 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006580 }
6581}
John Durkop07cc04a2020-11-16 22:08:34 -08006582#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6583 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6584
6585#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6586static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6587 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006588 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006590{
6591 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6593 4 + data_length + prf->other_secret_length :
6594 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6597 return PSA_ERROR_INVALID_ARGUMENT;
6598 }
John Durkop07cc04a2020-11-16 22:08:34 -08006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 uint8_t *pms = mbedtls_calloc(1, pms_len);
6601 if (pms == NULL) {
6602 return PSA_ERROR_INSUFFICIENT_MEMORY;
6603 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006604 uint8_t *cur = pms;
6605
6606 /* pure-PSK:
6607 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006608 *
6609 * The premaster secret is formed as follows: if the PSK is N octets
6610 * long, concatenate a uint16 with the value N, N zero octets, a second
6611 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006612 *
6613 * mixed-PSK:
6614 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6615 * follows: concatenate a uint16 with the length of the other secret,
6616 * the other secret itself, uint16 with the length of PSK, and the
6617 * PSK itself.
6618 * For details please check:
6619 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6620 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6621 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006622 */
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6625 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6626 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6627 if (prf->other_secret_length != 0) {
6628 memcpy(cur, prf->other_secret, prf->other_secret_length);
6629 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006630 cur += prf->other_secret_length;
6631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 } else {
6633 *cur++ = MBEDTLS_BYTE_1(data_length);
6634 *cur++ = MBEDTLS_BYTE_0(data_length);
6635 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006636 cur += data_length;
6637 }
6638
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 *cur++ = MBEDTLS_BYTE_1(data_length);
6640 *cur++ = MBEDTLS_BYTE_0(data_length);
6641 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006642 cur += data_length;
6643
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006644 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006645
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006646 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006648}
Janos Follath6660f0e2019-06-17 08:44:03 +01006649
Przemek Stekiele47201b2022-04-19 13:53:28 +02006650static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6651 psa_tls12_prf_key_derivation_t *prf,
6652 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006654{
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6656 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006658
6659 if (data_length != 0) {
6660 prf->other_secret = mbedtls_calloc(1, data_length);
6661 if (prf->other_secret == NULL) {
6662 return PSA_ERROR_INSUFFICIENT_MEMORY;
6663 }
6664
6665 memcpy(prf->other_secret, data, data_length);
6666 prf->other_secret_length = data_length;
6667 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006668 prf->other_secret_length = 0;
6669 }
6670
6671 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006674}
6675
Janos Follath6660f0e2019-06-17 08:44:03 +01006676static psa_status_t psa_tls12_prf_psk_to_ms_input(
6677 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006678 psa_key_derivation_step_t step,
6679 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006681{
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006683 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 return psa_tls12_prf_psk_to_ms_set_key(prf,
6685 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006686 break;
6687 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6689 data,
6690 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006691 break;
6692 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006694 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006695
Przemek Stekiele47201b2022-04-19 13:53:28 +02006696 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006697}
John Durkop07cc04a2020-11-16 22:08:34 -08006698#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006699
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6701static psa_status_t psa_tls12_ecjpake_to_pms_input(
6702 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006703 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006704 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006706{
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6708 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6709 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006710 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006711
6712 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (data[0] != 0x04) {
6714 return PSA_ERROR_INVALID_ARGUMENT;
6715 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006716
6717 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006721}
6722#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306723
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306724#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306725static psa_status_t psa_pbkdf2_set_input_cost(
6726 psa_pbkdf2_key_derivation_t *pbkdf2,
6727 psa_key_derivation_step_t step,
6728 uint64_t data)
6729{
6730 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6731 return PSA_ERROR_INVALID_ARGUMENT;
6732 }
6733
6734 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6735 return PSA_ERROR_BAD_STATE;
6736 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306737
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306738 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306739 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306740 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306741
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306742 if (data == 0) {
6743 return PSA_ERROR_INVALID_ARGUMENT;
6744 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306745
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306746 pbkdf2->input_cost = data;
6747 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6748
6749 return PSA_SUCCESS;
6750}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306751
6752static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6753 const uint8_t *data,
6754 size_t data_length)
6755{
Gilles Peskineead17662023-08-20 22:02:51 +02006756 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6757 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6758 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6759 /* Appending to existing salt. No state change. */
6760 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306761 return PSA_ERROR_BAD_STATE;
6762 }
6763
Gilles Peskineca57d782023-07-20 19:08:06 +02006764 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006765 /* Appending an empty string, nothing to do. */
6766 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306767 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306768
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306769 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6770 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306771 return PSA_ERROR_INSUFFICIENT_MEMORY;
6772 }
6773
Gilles Peskineead17662023-08-20 22:02:51 +02006774 if (pbkdf2->salt_length != 0) {
6775 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6776 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306777 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306778 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306779 mbedtls_free(pbkdf2->salt);
6780 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306781 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306782 return PSA_SUCCESS;
6783}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306784
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306785#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306787 const uint8_t *input,
6788 size_t input_len,
6789 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306790 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791{
6792 psa_status_t status = PSA_SUCCESS;
6793 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6794 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306795 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306796 } else {
6797 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306798 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306799 }
6800 return status;
6801}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306802#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306803
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306804#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306805static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6806 size_t input_len,
6807 uint8_t *output,
6808 size_t *output_len)
6809{
6810 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306811 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306813 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306814 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6815 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6816 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306817 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306818 * mac_size as the driver function sets mac_output_length = mac_size
6819 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306820 status = psa_driver_wrapper_mac_compute(&attributes,
6821 zeros, sizeof(zeros),
6822 PSA_ALG_CMAC, input, input_len,
6823 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306824 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6825 128U,
6826 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306827 output_len);
6828 } else {
6829 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306830 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306831 }
6832 return status;
6833}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306834#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306835
6836static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306837 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306838 const uint8_t *data,
6839 size_t data_length)
6840{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306841 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306842 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6843 return PSA_ERROR_BAD_STATE;
6844 }
6845
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306846#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306847 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6848 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6849 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6850 pbkdf2->password,
6851 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306852 } else
6853#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6854#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6855 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306856 status = psa_pbkdf2_cmac_set_password(data, data_length,
6857 pbkdf2->password,
6858 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306859 } else
6860#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6861 {
6862 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306863 }
6864
6865 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6866
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306867 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306868}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306869
6870static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306871 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306872 psa_key_derivation_step_t step,
6873 const uint8_t *data,
6874 size_t data_length)
6875{
6876 switch (step) {
6877 case PSA_KEY_DERIVATION_INPUT_SALT:
6878 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6879 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306880 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306881 default:
6882 return PSA_ERROR_INVALID_ARGUMENT;
6883 }
6884}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306885#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306886
Gilles Peskineb8965192019-09-24 16:21:10 +02006887/** Check whether the given key type is acceptable for the given
6888 * input step of a key derivation.
6889 *
6890 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6891 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6892 * Both secret and non-secret inputs can alternatively have the type
6893 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6894 * that the input was passed as a buffer rather than via a key object.
6895 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006896static int psa_key_derivation_check_input_type(
6897 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899{
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006901 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (key_type == PSA_KEY_TYPE_DERIVE) {
6903 return PSA_SUCCESS;
6904 }
6905 if (key_type == PSA_KEY_TYPE_NONE) {
6906 return PSA_SUCCESS;
6907 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006908 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006909 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 if (key_type == PSA_KEY_TYPE_DERIVE) {
6911 return PSA_SUCCESS;
6912 }
6913 if (key_type == PSA_KEY_TYPE_NONE) {
6914 return PSA_SUCCESS;
6915 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006916 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006917 case PSA_KEY_DERIVATION_INPUT_LABEL:
6918 case PSA_KEY_DERIVATION_INPUT_SALT:
6919 case PSA_KEY_DERIVATION_INPUT_INFO:
6920 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6922 return PSA_SUCCESS;
6923 }
6924 if (key_type == PSA_KEY_TYPE_NONE) {
6925 return PSA_SUCCESS;
6926 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006927 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306928 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6929 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6930 return PSA_SUCCESS;
6931 }
6932 if (key_type == PSA_KEY_TYPE_DERIVE) {
6933 return PSA_SUCCESS;
6934 }
6935 if (key_type == PSA_KEY_TYPE_NONE) {
6936 return PSA_SUCCESS;
6937 }
6938 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941}
6942
Janos Follathb80a94e2019-06-12 15:54:46 +01006943static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006944 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006945 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006946 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006947 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006949{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006950 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 status = psa_key_derivation_check_input_type(step, key_type);
6954 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006957
Przemek Stekiel69c46792022-06-10 12:59:51 +02006958#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6960 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6961 step, data, data_length);
6962 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006963#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006964#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6966 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6967 step, data, data_length);
6968 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006969#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6970#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6972 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6973 step, data, data_length);
6974 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006975#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006978 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6980 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006981#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306982#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306983 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306984 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6985 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306986 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306987#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006988 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006989 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006990 (void) data;
6991 (void) data_length;
6992 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006994 }
6995
Gilles Peskine224b0d62019-09-23 18:13:17 +02006996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (status != PSA_SUCCESS) {
6998 psa_key_derivation_abort(operation);
6999 }
7000 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007001}
7002
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307003static psa_status_t psa_key_derivation_input_integer_internal(
7004 psa_key_derivation_operation_t *operation,
7005 psa_key_derivation_step_t step,
7006 uint64_t value)
7007{
7008 psa_status_t status;
7009 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7010
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307011#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307012 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307013 status = psa_pbkdf2_set_input_cost(
7014 &operation->ctx.pbkdf2, step, value);
7015 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307016#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307017 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307018 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307019 (void) value;
7020 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307021 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307022 }
7023
7024 if (status != PSA_SUCCESS) {
7025 psa_key_derivation_abort(operation);
7026 }
7027 return status;
7028}
7029
Janos Follath51f4a0f2019-06-14 11:35:55 +01007030psa_status_t psa_key_derivation_input_bytes(
7031 psa_key_derivation_operation_t *operation,
7032 psa_key_derivation_step_t step,
7033 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007035{
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 return psa_key_derivation_input_internal(operation, step,
7037 PSA_KEY_TYPE_NONE,
7038 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007039}
7040
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307041psa_status_t psa_key_derivation_input_integer(
7042 psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
7044 uint64_t value)
7045{
7046 return psa_key_derivation_input_integer_internal(operation, step, value);
7047}
7048
Janos Follath51f4a0f2019-06-14 11:35:55 +01007049psa_status_t psa_key_derivation_input_key(
7050 psa_key_derivation_operation_t *operation,
7051 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007053{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007056 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007057
Ronald Cron5c522922020-11-14 16:35:34 +01007058 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7060 if (status != PSA_SUCCESS) {
7061 psa_key_derivation_abort(operation);
7062 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007063 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007064
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307065 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7066 * permission to output to a key object. */
7067 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7068 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007069 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 status = psa_key_derivation_input_internal(operation,
7073 step, slot->attr.type,
7074 slot->key.data,
7075 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007080}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007081
7082
7083
7084/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007085/* Key agreement */
7086/****************************************************************/
7087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7089 const uint8_t *key_buffer,
7090 size_t key_buffer_size,
7091 psa_algorithm_t alg,
7092 const uint8_t *peer_key,
7093 size_t peer_key_length,
7094 uint8_t *shared_secret,
7095 size_t shared_secret_size,
7096 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007097{
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007099#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007100 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7102 key_buffer_size, alg,
7103 peer_key, peer_key_length,
7104 shared_secret,
7105 shared_secret_size,
7106 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007107#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007108
7109#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7110 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007111 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007112 peer_key,
7113 peer_key_length,
7114 key_buffer,
7115 key_buffer_size,
7116 shared_secret,
7117 shared_secret_size,
7118 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007119#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7120
Gilles Peskine01d718c2018-09-18 12:01:02 +02007121 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007122 (void) attributes;
7123 (void) key_buffer;
7124 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007125 (void) peer_key;
7126 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 (void) shared_secret;
7128 (void) shared_secret_size;
7129 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007131 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007132}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007133
Aditya Deshpande17845b82022-10-13 17:21:01 +01007134/** Internal function for raw key agreement
7135 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007136 * to the driver's implementation if a driver is present.
7137 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138 * (psa_key_agreement_raw_builtin).
7139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007140static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7141 psa_key_slot_t *private_key,
7142 const uint8_t *peer_key,
7143 size_t peer_key_length,
7144 uint8_t *shared_secret,
7145 size_t shared_secret_size,
7146 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007147{
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7149 return PSA_ERROR_NOT_SUPPORTED;
7150 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007151
7152 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007154 };
7155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 return psa_driver_wrapper_key_agreement(&attributes,
7157 private_key->key.data,
7158 private_key->key.bytes, alg,
7159 peer_key, peer_key_length,
7160 shared_secret,
7161 shared_secret_size,
7162 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007163}
7164
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007165/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007166 * to potentially free embedded data structures and wipe confidential data.
7167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7169 psa_key_derivation_step_t step,
7170 psa_key_slot_t *private_key,
7171 const uint8_t *peer_key,
7172 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173{
7174 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007175 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007178
7179 /* Step 1: run the secret agreement algorithm to generate the shared
7180 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 status = psa_key_agreement_raw_internal(ka_alg,
7182 private_key,
7183 peer_key, peer_key_length,
7184 shared_secret,
7185 sizeof(shared_secret),
7186 &shared_secret_length);
7187 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007190
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007191 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007192 * the shared secret. A shared secret is permitted wherever a key
7193 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 status = psa_key_derivation_input_internal(operation, step,
7195 PSA_KEY_TYPE_DERIVE,
7196 shared_secret,
7197 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7200 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007201}
7202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7204 psa_key_derivation_step_t step,
7205 mbedtls_svc_key_id_t private_key,
7206 const uint8_t *peer_key,
7207 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007208{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007210 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007211 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7214 return PSA_ERROR_INVALID_ARGUMENT;
7215 }
Ronald Cron5c522922020-11-14 16:35:34 +01007216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7218 if (status != PSA_SUCCESS) {
7219 return status;
7220 }
7221 status = psa_key_agreement_internal(operation, step,
7222 slot,
7223 peer_key, peer_key_length);
7224 if (status != PSA_SUCCESS) {
7225 psa_key_derivation_abort(operation);
7226 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007227 /* If a private key has been added as SECRET, we allow the derived
7228 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007230 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007232 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007237}
7238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7240 mbedtls_svc_key_id_t private_key,
7241 const uint8_t *peer_key,
7242 size_t peer_key_length,
7243 uint8_t *output,
7244 size_t output_size,
7245 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007249 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007250 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007253 status = PSA_ERROR_INVALID_ARGUMENT;
7254 goto exit;
7255 }
Ronald Cron5c522922020-11-14 16:35:34 +01007256 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7258 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007261
Gilles Peskine3e561302022-04-14 00:17:15 +02007262 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7263 * for the output size. The PSA specification only guarantees that this
7264 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7265 * but it might be nice to allow smaller buffers if the output fits.
7266 * At the time of writing this comment, with only ECDH implemented,
7267 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7268 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7269 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007270 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7272 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007273 status = PSA_ERROR_BUFFER_TOO_SMALL;
7274 goto exit;
7275 }
7276
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 status = psa_key_agreement_raw_internal(alg, slot,
7278 peer_key, peer_key_length,
7279 output, output_size,
7280 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007281
7282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284 /* If an error happens and is not handled properly, the output
7285 * may be used as a key to protect sensitive data. Arrange for such
7286 * a key to be random, which is likely to result in decryption or
7287 * verification errors. This is better than filling the buffer with
7288 * some constant data such as zeros, which would result in the data
7289 * being protected with a reproducible, easily knowable key.
7290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007292 *output_length = output_size;
7293 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007294
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007298}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007299
7300
Gilles Peskine30524eb2020-11-13 17:02:26 +01007301
Gilles Peskine86a440b2018-11-12 18:39:40 +01007302/****************************************************************/
7303/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007304/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007305
Gilles Peskine672a7712023-04-28 21:00:28 +02007306#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7307#include "entropy_poll.h"
7308#endif
7309
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310/** Initialize the PSA random generator.
7311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007312static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007314#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007316#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7317
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318 /* Set default configuration if
7319 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 }
7323 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7329 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7330 /* The PSA entropy injection feature depends on using NV seed as an entropy
7331 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 mbedtls_entropy_add_source(&rng->entropy,
7333 mbedtls_nv_seed_poll, NULL,
7334 MBEDTLS_ENTROPY_BLOCK_SIZE,
7335 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007336#endif
7337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007339#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007340}
7341
7342/** Deinitialize the PSA random generator.
7343 */
Valerio Setti83e0de82023-11-24 12:13:05 +01007344static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007345{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Valerio Setti83e0de82023-11-24 12:13:05 +01007347 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Valerio Setti83e0de82023-11-24 12:13:05 +01007349 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7350 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352}
7353
7354/** Seed the PSA random generator.
7355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007356static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007357{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007358#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7359 /* Do nothing: the external RNG seeds itself. */
7360 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007362#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007363 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7365 drbg_seed, sizeof(drbg_seed) - 1);
7366 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007367#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007368}
7369
Gilles Peskine449bd832023-01-11 14:50:10 +01007370psa_status_t psa_generate_random(uint8_t *output,
7371 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007373 GUARD_MODULE_INITIALIZED;
7374
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007375#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7376
7377 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7379 output, output_size,
7380 &output_length);
7381 if (status != PSA_SUCCESS) {
7382 return status;
7383 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007384 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007385 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 if (output_length != output_size) {
7387 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7388 }
7389 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007390
7391#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7392
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007394 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7396 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7397 output_size);
7398 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7399 output, request_size);
7400 if (ret != 0) {
7401 return mbedtls_to_psa_error(ret);
7402 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007403 output_size -= request_size;
7404 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007407#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007408}
7409
Gilles Peskine8814fc42020-12-14 15:33:44 +01007410/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007411 *
7412 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7413 * `psa_generate_random(...)`. The state parameter is ignored since the
7414 * PSA API doesn't support passing an explicit state.
7415 *
7416 * In the non-external case, psa_generate_random() calls an
7417 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7418 * and semantics as mbedtls_psa_get_random(). As an optimization,
7419 * instead of doing this back-and-forth between the PSA API and the
7420 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7421 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007423#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7424int mbedtls_psa_get_random(void *p_rng,
7425 unsigned char *output,
7426 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007427{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007428 /* This function takes a pointer to the RNG state because that's what
7429 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7430 * its own state internally and doesn't let the caller access that state.
7431 * So we just ignore the state parameter, and in practice we'll pass
7432 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007433 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 psa_status_t status = psa_generate_random(output, output_size);
7435 if (status == PSA_SUCCESS) {
7436 return 0;
7437 } else {
7438 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7439 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007440}
7441#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7442
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007443#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007444psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7445 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007446{
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (global_data.initialized) {
7448 return PSA_ERROR_NOT_PERMITTED;
7449 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7452 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7453 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7454 return PSA_ERROR_INVALID_ARGUMENT;
7455 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007458}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007459#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007460
Ronald Cron3772afe2021-02-08 16:10:05 +01007461/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007463 * \param type The key type
7464 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 *
7466 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007467 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007468 * \retval #PSA_ERROR_INVALID_ARGUMENT
7469 * The size in bits of the key is not valid.
7470 * \retval #PSA_ERROR_NOT_SUPPORTED
7471 * The type and/or the size in bits of the key or the combination of
7472 * the two is not supported.
7473 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007474static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007476{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if (key_type_is_raw_bytes(type)) {
7480 status = psa_validate_unstructured_key_bit_size(type, bits);
7481 if (status != PSA_SUCCESS) {
7482 return status;
7483 }
7484 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007485#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7487 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7488 return PSA_ERROR_NOT_SUPPORTED;
7489 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007490 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007491 return PSA_ERROR_NOT_SUPPORTED;
7492 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007493
Ronald Cron01b2aba2020-10-05 09:42:02 +02007494 /* Accept only byte-aligned keys, for the same reasons as
7495 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (bits % 8 != 0) {
7497 return PSA_ERROR_NOT_SUPPORTED;
7498 }
7499 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007500#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007501
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007502#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007504 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return PSA_SUCCESS;
7506 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007507#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007508
Valerio Settia55f0422023-07-10 15:34:41 +02007509#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007510 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007511 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007512 return PSA_ERROR_NOT_SUPPORTED;
7513 }
7514 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007515#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518 }
7519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521}
7522
Ronald Cron55ed0592020-10-05 10:30:40 +02007523psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007524 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007526{
7527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007528 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if ((attributes->domain_parameters == NULL) &&
7531 (attributes->domain_parameters_size != 0)) {
7532 return PSA_ERROR_INVALID_ARGUMENT;
7533 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007534
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (key_type_is_raw_bytes(type)) {
7536 status = psa_generate_random(key_buffer, key_buffer_size);
7537 if (status != PSA_SUCCESS) {
7538 return status;
7539 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007540
David Brown12ca5032021-02-11 11:02:00 -07007541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (type == PSA_KEY_TYPE_DES) {
7543 psa_des_set_key_parity(key_buffer, key_buffer_size);
7544 }
David Brown8107e312021-02-12 08:08:46 -07007545#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007547
Valerio Setti76df8c12023-07-11 14:11:28 +02007548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7550 return mbedtls_psa_rsa_generate_key(attributes,
7551 key_buffer,
7552 key_buffer_size,
7553 key_buffer_length);
7554 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007555#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007556
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7559 return mbedtls_psa_ecp_generate_key(attributes,
7560 key_buffer,
7561 key_buffer_size,
7562 key_buffer_length);
7563 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007564#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007565
Valerio Settia55f0422023-07-10 15:34:41 +02007566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007567 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7568 return mbedtls_psa_ffdh_generate_key(attributes,
7569 key_buffer,
7570 key_buffer_size,
7571 key_buffer_length);
7572 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007574 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 (void) key_buffer_length;
7576 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007577 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007580}
Darryl Green0c6575a2018-11-07 16:05:30 +00007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7583 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007584{
7585 psa_status_t status;
7586 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007587 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007588 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007589
Ronald Cron81709fc2020-11-14 12:10:32 +01007590 *key = MBEDTLS_SVC_KEY_ID_INIT;
7591
Gilles Peskine0f84d622019-09-12 19:03:13 +02007592 /* Reject any attempt to create a zero-length key so that we don't
7593 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 if (psa_get_key_bits(attributes) == 0) {
7595 return PSA_ERROR_INVALID_ARGUMENT;
7596 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007597
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007598 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7600 return PSA_ERROR_INVALID_ARGUMENT;
7601 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7604 &slot, &driver);
7605 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 }
Gilles Peskine11792082019-08-06 18:36:36 +02007608
Ronald Cron977c2472020-10-13 08:32:21 +02007609 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307610 * storage ( thus not in the case of generating a key in a secure element
7611 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7612 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (slot->key.data == NULL) {
7614 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7615 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007616 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 attributes->core.type, attributes->core.bits);
7618 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007621
7622 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 attributes->core.type,
7624 attributes->core.bits);
7625 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007626 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 attributes, &key_buffer_size);
7628 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 }
Ronald Cron977c2472020-10-13 08:32:21 +02007631 }
Ronald Cron977c2472020-10-13 08:32:21 +02007632
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7634 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 }
Ronald Cron977c2472020-10-13 08:32:21 +02007637 }
7638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 status = psa_driver_wrapper_generate_key(attributes,
7640 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (status != PSA_SUCCESS) {
7643 psa_remove_key_data_from_memory(slot);
7644 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007645
Gilles Peskine11792082019-08-06 18:36:36 +02007646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 if (status == PSA_SUCCESS) {
7648 status = psa_finish_key_creation(slot, driver, key);
7649 }
7650 if (status != PSA_SUCCESS) {
7651 psa_fail_key_creation(slot, driver);
7652 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007655}
7656
Gilles Peskinee59236f2018-01-27 23:32:46 +01007657/****************************************************************/
7658/* Module setup */
7659/****************************************************************/
7660
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007661#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007662psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 void (* entropy_init)(mbedtls_entropy_context *ctx),
7664 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007665{
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7667 return PSA_ERROR_BAD_STATE;
7668 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007669 global_data.rng.entropy_init = entropy_init;
7670 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007672}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007673#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007674
Gilles Peskine449bd832023-01-11 14:50:10 +01007675void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007676{
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 psa_wipe_all_key_slots();
Valerio Setti83e0de82023-11-24 12:13:05 +01007678 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7679 mbedtls_psa_random_free(&global_data.rng);
7680 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007681 /* Wipe all remaining data, including configuration.
7682 * In particular, this sets all state indicator to the value
7683 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007685
7686 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007688}
7689
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7691/** Recover a transaction that was interrupted by a power failure.
7692 *
7693 * This function is called during initialization, before psa_crypto_init()
7694 * returns. If this function returns a failure status, the initialization
7695 * fails.
7696 */
7697static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699{
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007701 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7702 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 /* TODO - fall through to the failure case until this
7704 * is implemented.
7705 * https://github.com/ARMmbed/mbed-crypto/issues/218
7706 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007707 default:
7708 /* We found an unsupported transaction in the storage.
7709 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007711 }
7712}
7713#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7714
Gilles Peskine449bd832023-01-11 14:50:10 +01007715psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007716{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007717 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007718
Gilles Peskinec6b69072018-11-20 21:42:52 +01007719 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (global_data.initialized != 0) {
7721 return PSA_SUCCESS;
7722 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007724 /* Init drivers */
7725 status = psa_driver_wrapper_init();
7726 if (status != PSA_SUCCESS) {
7727 goto exit;
7728 }
7729 global_data.drivers_initialized = 1;
7730
Valerio Setti402cfba2023-11-13 10:24:32 +01007731 status = psa_initialize_key_slots();
7732 if (status != PSA_SUCCESS) {
7733 goto exit;
7734 }
7735
Gilles Peskine30524eb2020-11-13 17:02:26 +01007736 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007738 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 status = mbedtls_psa_random_seed(&global_data.rng);
7740 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007741 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007743 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007744
Gilles Peskine4b734222019-07-24 15:56:31 +02007745#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 status = psa_crypto_load_transaction();
7747 if (status == PSA_SUCCESS) {
7748 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7749 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 }
7752 status = psa_crypto_stop_transaction();
7753 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007754 /* There's no transaction to complete. It's all good. */
7755 status = PSA_SUCCESS;
7756 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007757#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007758
Gilles Peskinec6b69072018-11-20 21:42:52 +01007759 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007760 global_data.initialized = 1;
7761
7762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 if (status != PSA_SUCCESS) {
7764 mbedtls_psa_crypto_free();
7765 }
7766 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767}
7768
Yanray Wangffc3c482023-07-11 12:01:04 +08007769#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007770psa_status_t psa_crypto_driver_pake_get_password_len(
7771 const psa_crypto_driver_pake_inputs_t *inputs,
7772 size_t *password_len)
7773{
7774 if (inputs->password_len == 0) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
7778 *password_len = inputs->password_len;
7779
7780 return PSA_SUCCESS;
7781}
7782
7783psa_status_t psa_crypto_driver_pake_get_password(
7784 const psa_crypto_driver_pake_inputs_t *inputs,
7785 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7786{
7787 if (inputs->password_len == 0) {
7788 return PSA_ERROR_BAD_STATE;
7789 }
7790
7791 if (buffer_size < inputs->password_len) {
7792 return PSA_ERROR_BUFFER_TOO_SMALL;
7793 }
7794
7795 memcpy(buffer, inputs->password, inputs->password_len);
7796 *buffer_length = inputs->password_len;
7797
7798 return PSA_SUCCESS;
7799}
7800
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007801psa_status_t psa_crypto_driver_pake_get_user_len(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
7803 size_t *user_len)
7804{
7805 if (inputs->user_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 *user_len = inputs->user_len;
7810
7811 return PSA_SUCCESS;
7812}
7813
7814psa_status_t psa_crypto_driver_pake_get_user(
7815 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007816 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007817{
7818 if (inputs->user_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821
Przemek Stekielc0e62502023-03-14 11:49:36 +01007822 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007823 return PSA_ERROR_BUFFER_TOO_SMALL;
7824 }
7825
Przemek Stekielc0e62502023-03-14 11:49:36 +01007826 memcpy(user_id, inputs->user, inputs->user_len);
7827 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007828
7829 return PSA_SUCCESS;
7830}
7831
7832psa_status_t psa_crypto_driver_pake_get_peer_len(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
7834 size_t *peer_len)
7835{
7836 if (inputs->peer_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
7840 *peer_len = inputs->peer_len;
7841
7842 return PSA_SUCCESS;
7843}
7844
7845psa_status_t psa_crypto_driver_pake_get_peer(
7846 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007848{
7849 if (inputs->peer_len == 0) {
7850 return PSA_ERROR_BAD_STATE;
7851 }
7852
Przemek Stekielc0e62502023-03-14 11:49:36 +01007853 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007854 return PSA_ERROR_BUFFER_TOO_SMALL;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 memcpy(peer_id, inputs->peer, inputs->peer_len);
7858 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007859
7860 return PSA_SUCCESS;
7861}
7862
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007863psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7864 const psa_crypto_driver_pake_inputs_t *inputs,
7865 psa_pake_cipher_suite_t *cipher_suite)
7866{
7867 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7868 return PSA_ERROR_BAD_STATE;
7869 }
7870
7871 *cipher_suite = inputs->cipher_suite;
7872
7873 return PSA_SUCCESS;
7874}
7875
Neil Armstronga7d08c32022-06-01 18:21:20 +02007876psa_status_t psa_pake_setup(
7877 psa_pake_operation_t *operation,
7878 const psa_pake_cipher_suite_t *cipher_suite)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 }
7886
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007887 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007889 status = PSA_ERROR_INVALID_ARGUMENT;
7890 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 }
7892
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7894
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007896 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7897 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 operation->data.inputs.cipher_suite = *cipher_suite;
7899
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007900#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007901 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007902 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007903 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007904
David Horstmann16f01512023-06-14 17:21:07 +01007905 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007906 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 {
7910 status = PSA_ERROR_NOT_SUPPORTED;
7911 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 }
7913
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007914 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7915
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918 psa_pake_abort(operation);
7919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_password_key(
7923 psa_pake_operation_t *operation,
7924 mbedtls_svc_key_id_t password)
7925{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7928 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007929 psa_key_attributes_t attributes;
7930 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007931
Przemek Stekiel51eac532022-12-07 11:04:51 +01007932 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 status = PSA_ERROR_BAD_STATE;
7934 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 }
7936
Przemek Stekiel6c764412022-11-22 14:05:12 +01007937 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7938 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007939 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007942 }
7943
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007944 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007945 .core = slot->attr
7946 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007947
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007948 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007949
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 if (type != PSA_KEY_TYPE_PASSWORD &&
7951 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7952 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007953 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 }
7955
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7957 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007958 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960 }
7961
7962 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7963 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007964 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007966 if (status != PSA_SUCCESS) {
7967 psa_pake_abort(operation);
7968 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007971}
7972
7973psa_status_t psa_pake_set_user(
7974 psa_pake_operation_t *operation,
7975 const uint8_t *user_id,
7976 size_t user_id_len)
7977{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007979
7980 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007983 }
7984
Przemek Stekiel51eac532022-12-07 11:04:51 +01007985 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 status = PSA_ERROR_INVALID_ARGUMENT;
7987 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007988 }
7989
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007990 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007991 status = PSA_ERROR_BAD_STATE;
7992 goto exit;
7993 }
7994
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007995 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7996 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007997 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7998 goto exit;
7999 }
8000
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008001 memcpy(operation->data.inputs.user, user_id, user_id_len);
8002 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008003
8004 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008005exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008006 psa_pake_abort(operation);
8007 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008008}
8009
8010psa_status_t psa_pake_set_peer(
8011 psa_pake_operation_t *operation,
8012 const uint8_t *peer_id,
8013 size_t peer_id_len)
8014{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008016
8017 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008018 status = PSA_ERROR_BAD_STATE;
8019 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008020 }
8021
Przemek Stekiel51eac532022-12-07 11:04:51 +01008022 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008023 status = PSA_ERROR_INVALID_ARGUMENT;
8024 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008025 }
8026
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008027 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008028 status = PSA_ERROR_BAD_STATE;
8029 goto exit;
8030 }
8031
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008032 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8033 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008034 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8035 goto exit;
8036 }
8037
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008038 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8039 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008040
8041 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008043 psa_pake_abort(operation);
8044 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008045}
8046
8047psa_status_t psa_pake_set_role(
8048 psa_pake_operation_t *operation,
8049 psa_pake_role_t role)
8050{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008054 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008056 }
8057
Przemek Stekiel09104b82023-03-06 14:11:51 +01008058 switch (operation->alg) {
8059#if defined(PSA_WANT_ALG_JPAKE)
8060 case PSA_ALG_JPAKE:
8061 if (role == PSA_PAKE_ROLE_NONE) {
8062 return PSA_SUCCESS;
8063 }
8064 status = PSA_ERROR_INVALID_ARGUMENT;
8065 break;
8066#endif
8067 default:
8068 (void) role;
8069 status = PSA_ERROR_NOT_SUPPORTED;
8070 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008071 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008073 psa_pake_abort(operation);
8074 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008075}
8076
David Horstmanne7f21e62023-05-12 18:17:21 +01008077/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008078#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008079static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008080 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008081{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008083 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008085
David Horstmann024e5c52023-06-14 15:48:21 +01008086 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 is_x1 = (stage->outputs < 1);
8088 } else {
8089 is_x1 = (stage->inputs < 1);
8090 }
8091
David Horstmann74a3d8c2023-06-14 18:28:19 +01008092 key_share_step = is_x1 ?
8093 PSA_JPAKE_X1_STEP_KEY_SHARE :
8094 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008095 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008096 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8097 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8098 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8099 } else {
8100 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008101 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008102 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008103}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008104#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008105
Przemek Stekiel2797d372022-12-22 11:19:22 +01008106static psa_status_t psa_pake_complete_inputs(
8107 psa_pake_operation_t *operation)
8108{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008110 /* Create copy of the inputs on stack as inputs share memory
8111 with the driver context which will be setup by the driver. */
8112 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113
Przemek Stekielfde11282023-03-13 16:06:09 +01008114 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008115 return PSA_ERROR_BAD_STATE;
8116 }
8117
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008118 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008119 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8120 return PSA_ERROR_BAD_STATE;
8121 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008122 }
8123
Przemek Stekiel18620a32023-01-17 16:34:52 +01008124 /* Clear driver context */
8125 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8126
8127 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128
8129 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008130 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008132 /* User and peer are translated to role. */
8133 mbedtls_free(inputs.user);
8134 mbedtls_free(inputs.peer);
8135
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008137#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008138 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008139 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008140 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008141#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008142 {
8143 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008145 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008146 return status;
8147}
8148
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008149#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008150static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008152 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008153 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8156 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8157 step != PSA_PAKE_STEP_ZK_PROOF) {
8158 return PSA_ERROR_INVALID_ARGUMENT;
8159 }
8160
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008161 psa_jpake_computation_stage_t *computation_stage =
8162 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163
David Horstmann5da95602023-06-08 15:37:12 +01008164 if (computation_stage->round != PSA_JPAKE_FIRST &&
8165 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008166 return PSA_ERROR_BAD_STATE;
8167 }
8168
David Horstmanne7f21e62023-05-12 18:17:21 +01008169 /* Check that the step we are given is the one we were expecting */
8170 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008171 return PSA_ERROR_BAD_STATE;
8172 }
8173
David Horstmanne7f21e62023-05-12 18:17:21 +01008174 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8175 computation_stage->inputs == 0 &&
8176 computation_stage->outputs == 0) {
8177 /* Start of the round, so function decides whether we are inputting
8178 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008179 computation_stage->io_mode = io_mode;
8180 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008181 /* Middle of the round so the mode we are in must match the function
8182 * called by the user */
8183 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 }
8185
8186 return PSA_SUCCESS;
8187}
8188
David Horstmanne7f21e62023-05-12 18:17:21 +01008189static psa_status_t psa_jpake_epilogue(
8190 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008191 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192{
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008194 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8197 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008198 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008200 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008201 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 }
8203 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008204 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008206 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008207 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 }
8209 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008210 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8211 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 /* End of a round, move to the next round */
8213 stage->inputs = 0;
8214 stage->outputs = 0;
8215 stage->round++;
8216 }
8217 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008218 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008219 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 return PSA_SUCCESS;
8222}
David Horstmanne7f21e62023-05-12 18:17:21 +01008223
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008224#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225
Neil Armstronga7d08c32022-06-01 18:21:20 +02008226psa_status_t psa_pake_output(
8227 psa_pake_operation_t *operation,
8228 psa_pake_step_t step,
8229 uint8_t *output,
8230 size_t output_size,
8231 size_t *output_length)
8232{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008234 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008235 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236
8237 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008238 status = psa_pake_complete_inputs(operation);
8239 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008241 }
8242 }
8243
8244 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 status = PSA_ERROR_BAD_STATE;
8246 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008247 }
8248
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008249 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008250 status = PSA_ERROR_INVALID_ARGUMENT;
8251 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008252 }
8253
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008255#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008257 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008261 driver_step = convert_jpake_computation_stage_to_driver_step(
8262 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008264#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008266 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_NOT_SUPPORTED;
8268 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 }
8270
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008271 status = psa_driver_wrapper_pake_output(operation, driver_step,
8272 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273
8274 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277
8278 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008279#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008281 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008283 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 }
8285 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008286#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288 status = PSA_ERROR_NOT_SUPPORTED;
8289 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 }
8291
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008292 return PSA_SUCCESS;
8293exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008294 psa_pake_abort(operation);
8295 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008296}
8297
8298psa_status_t psa_pake_input(
8299 psa_pake_operation_t *operation,
8300 psa_pake_step_t step,
8301 const uint8_t *input,
8302 size_t input_length)
8303{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008305 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008306 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8307 operation->primitive,
8308 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008309
8310 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008311 status = psa_pake_complete_inputs(operation);
8312 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008314 }
8315 }
8316
8317 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008318 status = PSA_ERROR_BAD_STATE;
8319 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008320 }
8321
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008322 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008323 status = PSA_ERROR_INVALID_ARGUMENT;
8324 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008325 }
8326
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008328#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008330 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008332 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008334 driver_step = convert_jpake_computation_stage_to_driver_step(
8335 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008337#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008339 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008340 status = PSA_ERROR_NOT_SUPPORTED;
8341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 }
8343
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008344 status = psa_driver_wrapper_pake_input(operation, driver_step,
8345 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346
8347 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350
8351 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008352#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008354 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008355 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008356 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 }
8358 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008359#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 status = PSA_ERROR_NOT_SUPPORTED;
8362 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008363 }
8364
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 return PSA_SUCCESS;
8366exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008367 psa_pake_abort(operation);
8368 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008369}
8370
8371psa_status_t psa_pake_get_implicit_key(
8372 psa_pake_operation_t *operation,
8373 psa_key_derivation_operation_t *output)
8374{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008376 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008377 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008378 size_t shared_key_len = 0;
8379
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008380 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008381 status = PSA_ERROR_BAD_STATE;
8382 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008383 }
8384
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008385#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008386 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008388 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008389 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390 status = PSA_ERROR_BAD_STATE;
8391 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008392 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008393 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008394#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008395 {
8396 status = PSA_ERROR_NOT_SUPPORTED;
8397 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008398 }
8399
Przemek Stekiel0c781802022-11-29 14:53:13 +01008400 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8401 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008402 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008403 &shared_key_len);
8404
8405 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008406 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008407 }
8408
8409 status = psa_key_derivation_input_bytes(output,
8410 PSA_KEY_DERIVATION_INPUT_SECRET,
8411 shared_key,
8412 shared_key_len);
8413
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008414 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008415exit:
8416 abort_status = psa_pake_abort(operation);
8417 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008418}
8419
8420psa_status_t psa_pake_abort(
8421 psa_pake_operation_t *operation)
8422{
Przemek Stekield69dca92023-01-31 19:59:20 +01008423 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008424
Przemek Stekield69dca92023-01-31 19:59:20 +01008425 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008427 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008428
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008429 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8430 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008431 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008432 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008433 }
8434 if (operation->data.inputs.user != NULL) {
8435 mbedtls_free(operation->data.inputs.user);
8436 }
8437 if (operation->data.inputs.peer != NULL) {
8438 mbedtls_free(operation->data.inputs.peer);
8439 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008440 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008441 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008442
Przemek Stekield69dca92023-01-31 19:59:20 +01008443 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008444}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008445#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008446
Gilles Peskinee59236f2018-01-27 23:32:46 +01008447#endif /* MBEDTLS_PSA_CRYPTO_C */