blob: ddc834f95ce44e1d4a3f6f917195b10cbd612625 [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
David Horstmann372b8bb2023-11-24 16:21:04 +000075#if defined(MBEDTLS_TEST_HOOKS)
76#include "test/memory.h"
77#endif
78
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
80 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
81 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020082#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020083#endif
84
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010085/****************************************************************/
86/* Global data, support functions and library management */
87/****************************************************************/
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020090{
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020092}
93
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094/* Values for psa_global_data_t::rng_state */
95#define RNG_NOT_INITIALIZED 0
96#define RNG_INITIALIZED 1
97#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100100 uint8_t initialized;
101 uint8_t rng_state;
102 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100103 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100104} psa_global_data_t;
105
106static psa_global_data_t global_data;
107
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100108#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
109mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
110 &global_data.rng.drbg;
111#endif
112
itayzafrir0adf0fc2018-09-06 16:24:41 +0300113#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (global_data.initialized == 0) \
115 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300116
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100117int psa_can_do_hash(psa_algorithm_t hash_alg)
118{
119 (void) hash_alg;
120 return global_data.drivers_initialized;
121}
Valerio Settia55f0422023-07-10 15:34:41 +0200122#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200123 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200124 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200125static int psa_is_dh_key_size_valid(size_t bits)
126{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200127 if (bits != 2048 && bits != 3072 && bits != 4096 &&
128 bits != 6144 && bits != 8192) {
129 return 0;
130 }
131
132 return 1;
133}
Valerio Settia55f0422023-07-10 15:34:41 +0200134#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200135 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200136 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100140 /* Mbed TLS error codes can combine a high-level error code and a
141 * low-level error code. The low-level error usually reflects the
142 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 int low_level_ret = -(-ret & 0x007f);
144 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100147
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100148#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100149 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
150 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100152 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
153 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100154#endif
155
156#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100167#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200168
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100169#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100175#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100180#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100182#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200183 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200186
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100187#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200190 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100192#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200193
Dave Rodgman509b5672023-08-16 19:26:23 +0100194#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100209#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
212 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100213 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
214 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
218 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100222#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100223
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100224#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Gilles Peskinee59236f2018-01-27 23:32:46 +0100229 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
230 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
231 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100234#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200237 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100241#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100242
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100245 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
246 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100247 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100249 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
250 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100252 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100254#endif
255
Dave Rodgmandea266f2023-08-31 11:52:43 +0100256#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100263#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100266#endif
267#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100268
Dave Rodgman509b5672023-08-16 19:26:23 +0100269#if defined(MBEDTLS_BIGNUM_C)
270#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100271 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100274 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
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_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
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_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100288#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289
Dave Rodgman509b5672023-08-16 19:26:23 +0100290#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
294 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100296#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
297 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100300#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
302 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
307 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_INVALID_ALG:
312 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
313 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200317 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100319#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100320
Gilles Peskineff2d2002019-05-06 15:26:23 +0200321 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200323 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325
Dave Rodgman509b5672023-08-16 19:26:23 +0100326#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100327 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
336 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100344#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200345
Dave Rodgman1dab4452023-09-01 09:59:51 +0100346#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300347 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300348 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300350 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300352 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300354 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
355 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100359 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100361
Dave Rodgman509b5672023-08-16 19:26:23 +0100362#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000363 case MBEDTLS_ERR_ECP_IN_PROGRESS:
364 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100365#endif
366#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000367
Janos Follatha13b9052019-11-22 12:48:59 +0000368 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300370
Gilles Peskinee59236f2018-01-27 23:32:46 +0100371 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100373 }
374}
375
Paul Elliottdc42ca82023-02-24 18:11:59 +0000376/**
377 * \brief For output buffers which contain "tags"
378 * (outputs that may be checked for validity like
379 * hashes, MACs and signatures), fill the unused
380 * part of the output buffer (the whole buffer on
381 * error, the trailing part on success) with
382 * something that isn't a valid tag (barring an
383 * attack on the tag and deliberately-crafted
384 * input), in case the caller doesn't check the
385 * return status properly.
386 *
387 * \param output_buffer Pointer to buffer to wipe. May not be NULL
388 * unless \p output_buffer_size is zero.
389 * \param status Status of function called to generate
390 * output_buffer originally
391 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
392 * could be NULL.
393 * \param output_buffer_length Length of data written to output_buffer, must be
394 * less than \p output_buffer_size
395 */
396static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000397 size_t output_buffer_size, size_t output_buffer_length)
398{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000399 size_t offset = 0;
400
401 if (output_buffer_size == 0) {
402 /* If output_buffer_size is 0 then we have nothing to do. We must not
403 call memset because output_buffer may be NULL in this case */
404 return;
405 }
406
407 if (status == PSA_SUCCESS) {
408 offset = output_buffer_length;
409 }
410
411 memset(output_buffer + offset, '!', output_buffer_size - offset);
412}
413
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200414
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200415
416
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417/****************************************************************/
418/* Key management */
419/****************************************************************/
420
Valerio Settia9aab1a2023-06-19 13:39:54 +0200421#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200422psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
423 size_t *bits)
424{
425 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200426#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200427 case MBEDTLS_ECP_DP_SECP192R1:
428 *bits = 192;
429 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100430#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200431#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200432 case MBEDTLS_ECP_DP_SECP224R1:
433 *bits = 224;
434 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100435#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200436#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200437 case MBEDTLS_ECP_DP_SECP256R1:
438 *bits = 256;
439 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100440#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200441#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200442 case MBEDTLS_ECP_DP_SECP384R1:
443 *bits = 384;
444 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100445#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200446#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200447 case MBEDTLS_ECP_DP_SECP521R1:
448 *bits = 521;
449 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100450#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200451#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200452 case MBEDTLS_ECP_DP_BP256R1:
453 *bits = 256;
454 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100455#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200456#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200457 case MBEDTLS_ECP_DP_BP384R1:
458 *bits = 384;
459 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100460#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200461#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200462 case MBEDTLS_ECP_DP_BP512R1:
463 *bits = 512;
464 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100465#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200466#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200467 case MBEDTLS_ECP_DP_CURVE25519:
468 *bits = 255;
469 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100470#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200471#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200472 case MBEDTLS_ECP_DP_SECP192K1:
473 *bits = 192;
474 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100475#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200476#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200477 case MBEDTLS_ECP_DP_SECP224K1:
478 *bits = 224;
479 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100480#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200481#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200482 case MBEDTLS_ECP_DP_SECP256K1:
483 *bits = 256;
484 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100485#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200486#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 case MBEDTLS_ECP_DP_CURVE448:
488 *bits = 448;
489 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100490#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200491 default:
492 *bits = 0;
493 return 0;
494 }
495}
496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
498 size_t bits,
499 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200500{
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100502 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100504#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
508#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100509 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#endif
512#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100519#endif
520#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits_is_sloppy) {
525 return MBEDTLS_ECP_DP_SECP521R1;
526 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100528#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100529 }
530 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100531
Paul Elliott8ff510a2020-06-02 17:19:28 +0100532 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100534#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100535 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100537#endif
538#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100539 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#endif
542#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100543 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100545#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100546 }
547 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100548
Paul Elliott8ff510a2020-06-02 17:19:28 +0100549 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100552 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100554 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (bits_is_sloppy) {
556 return MBEDTLS_ECP_DP_CURVE25519;
557 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100559#endif
560#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100561 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100563#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100564 }
565 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100566
Paul Elliott8ff510a2020-06-02 17:19:28 +0100567 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100569#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100570 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100572#endif
573#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100574 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#endif
577#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100578 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100580#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100581 }
582 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200583 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100584
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100585 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200587}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200588#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
591 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592{
593 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200595 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200597 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200598 case PSA_KEY_TYPE_PASSWORD:
599 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200600 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100601#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (bits != 128 && bits != 192 && bits != 256) {
604 return PSA_ERROR_INVALID_ARGUMENT;
605 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 break;
607#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200608#if defined(PSA_WANT_KEY_TYPE_ARIA)
609 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (bits != 128 && bits != 192 && bits != 256) {
611 return PSA_ERROR_INVALID_ARGUMENT;
612 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200613 break;
614#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100615#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (bits != 128 && bits != 192 && bits != 256) {
618 return PSA_ERROR_INVALID_ARGUMENT;
619 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200620 break;
621#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100622#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (bits != 64 && bits != 128 && bits != 192) {
625 return PSA_ERROR_INVALID_ARGUMENT;
626 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 break;
628#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100629#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200630 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (bits != 256) {
632 return PSA_ERROR_INVALID_ARGUMENT;
633 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200634 break;
635#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200636 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200638 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (bits % 8 != 0) {
640 return PSA_ERROR_INVALID_ARGUMENT;
641 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200644}
645
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100646/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100647 *
Steven Cooremancd640932021-03-08 12:00:27 +0100648 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
649 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100650 *
651 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100652 * \param[in] key_type The key type of the key to be used with the
653 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100654 *
655 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100656 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100658 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100659 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100660MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100661 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100663{
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (PSA_ALG_IS_HMAC(algorithm)) {
665 if (key_type == PSA_KEY_TYPE_HMAC) {
666 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100667 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
671 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
672 * key. */
673 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
674 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
675 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
676 * the block length (larger than 1) for block ciphers. */
677 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
678 return PSA_SUCCESS;
679 }
680 }
681 }
682
683 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100684}
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
687 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200688{
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (slot->key.data != NULL) {
690 return PSA_ERROR_ALREADY_EXISTS;
691 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 slot->key.data = mbedtls_calloc(1, buffer_length);
694 if (slot->key.data == NULL) {
695 return PSA_ERROR_INSUFFICIENT_MEMORY;
696 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200697
Ronald Cronea0f8a62020-11-25 17:52:23 +0100698 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200700}
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
703 const uint8_t *data,
704 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200705{
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 psa_status_t status = psa_allocate_buffer_to_slot(slot,
707 data_length);
708 if (status != PSA_SUCCESS) {
709 return status;
710 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 memcpy(slot->key.data, data, data_length);
713 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200714}
715
Ronald Crona0fe59f2020-11-29 11:14:53 +0100716psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100717 const psa_key_attributes_t *attributes,
718 const uint8_t *data, size_t data_length,
719 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100722 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
723 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200725 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (data_length == 0) {
727 return PSA_ERROR_NOT_SUPPORTED;
728 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (key_type_is_raw_bytes(type)) {
731 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
734 *bits);
735 if (status != PSA_SUCCESS) {
736 return status;
737 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200738
Ronald Crondd04d422020-11-28 15:14:42 +0100739 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100741 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return PSA_SUCCESS;
745 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200746#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200747 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100748 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200749 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100750 return PSA_ERROR_INVALID_ARGUMENT;
751 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200752 return mbedtls_psa_ffdh_import_key(attributes,
753 data, data_length,
754 key_buffer, key_buffer_size,
755 key_buffer_length,
756 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100757 }
Valerio Settia55f0422023-07-10 15:34:41 +0200758#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200759 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200760#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
762 if (PSA_KEY_TYPE_IS_ECC(type)) {
763 return mbedtls_psa_ecp_import_key(attributes,
764 data, data_length,
765 key_buffer, key_buffer_size,
766 key_buffer_length,
767 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100768 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200769#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800770 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200771#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
772 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
774 if (PSA_KEY_TYPE_IS_RSA(type)) {
775 return mbedtls_psa_rsa_import_key(attributes,
776 data, data_length,
777 key_buffer, key_buffer_size,
778 key_buffer_length,
779 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200780 }
Valerio Settife478902023-07-25 12:27:19 +0200781#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800783 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100784 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100787}
788
Gilles Peskinef603c712019-01-19 13:40:11 +0100789/** Calculate the intersection of two algorithm usage policies.
790 *
791 * Return 0 (which allows no operation) on incompatibility.
792 */
793static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100794 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100795 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100797{
Gilles Peskine549ea862019-05-22 11:45:59 +0200798 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (alg1 == alg2) {
800 return alg1;
801 }
Steven Cooreman03488022021-02-18 12:07:20 +0100802 /* If the policies are from the same hash-and-sign family, check
803 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
805 PSA_ALG_IS_SIGN_HASH(alg2) &&
806 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
807 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
808 return alg2;
809 }
810 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
811 return alg1;
812 }
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
814 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100815 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100816 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
818 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
819 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
820 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
821 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100822 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100824 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
826 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
827 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
828 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100829 }
830 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
832 (alg1_len <= alg2_len)) {
833 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100834 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
836 (alg2_len <= alg1_len)) {
837 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100838 }
839 }
840 /* If the policies are from the same MAC family, check whether one
841 * of them is a minimum-MAC-length policy. Calculate the most
842 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
844 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
845 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100846 /* Validate the combination of key type and algorithm. Since the base
847 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
849 return 0;
850 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100851
Steven Cooreman31a876d2021-03-03 20:47:40 +0100852 /* Get the (exact or at-least) output lengths for both sides of the
853 * requested intersection. None of the currently supported algorithms
854 * have an output length dependent on the actual key size, so setting it
855 * to a bogus value of 0 is currently OK.
856 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100857 * Note that for at-least-this-length wildcard algorithms, the output
858 * length is set to the shortest allowed length, which allows us to
859 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
861 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100862 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100863
Steven Cooremana1d83222021-02-25 10:20:29 +0100864 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
866 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
867 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100868 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100869
870 /* If only one is an at-least-this-length policy, the intersection would
871 * be the other (fixed-length) policy as long as said fixed length is
872 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
874 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100875 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
877 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100878 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879
Steven Cooremancd640932021-03-08 12:00:27 +0100880 /* If none of them are wildcards, check whether they define the same tag
881 * length. This is still possible here when one is default-length and
882 * the other specific-length. Ensure to always return the
883 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (alg1_len == alg2_len) {
885 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
886 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 }
888 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100890}
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892static int psa_key_algorithm_permits(psa_key_type_t key_type,
893 psa_algorithm_t policy_alg,
894 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200895{
Gilles Peskine549ea862019-05-22 11:45:59 +0200896 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (requested_alg == policy_alg) {
898 return 1;
899 }
Steven Cooreman03488022021-02-18 12:07:20 +0100900 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
901 * and requested_alg is the same hash-and-sign family with any hash,
902 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
904 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
905 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
906 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100907 }
908 /* If policy_alg is a wildcard AEAD algorithm of the same base as
909 * the requested algorithm, check the requested tag length to be
910 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (PSA_ALG_IS_AEAD(policy_alg) &&
912 PSA_ALG_IS_AEAD(requested_alg) &&
913 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
914 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
915 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
916 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
917 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100918 }
Steven Cooremancd640932021-03-08 12:00:27 +0100919 /* If policy_alg is a MAC algorithm of the same base as the requested
920 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (PSA_ALG_IS_MAC(policy_alg) &&
922 PSA_ALG_IS_MAC(requested_alg) &&
923 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
924 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100925 /* Validate the combination of key type and algorithm. Since the policy
926 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
928 return 0;
929 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100930
Steven Cooreman31a876d2021-03-03 20:47:40 +0100931 /* Get both the requested output length for the algorithm which is to be
932 * verified, and the default output length for the base algorithm.
933 * Note that none of the currently supported algorithms have an output
934 * length dependent on actual key size, so setting it to a bogus value
935 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100936 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100938 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 key_type, 0,
940 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100941
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100942 /* If the policy is default-length, only allow an algorithm with
943 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
945 return requested_output_length == default_output_length;
946 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100947
948 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100949 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
951 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
952 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100953 }
954
Steven Cooremancd640932021-03-08 12:00:27 +0100955 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
956 * check for the requested MAC length to be equal to or longer than the
957 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
959 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
960 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100961 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200962 }
Steven Cooremance48e852020-10-05 16:02:45 +0200963 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200964 * a key derivation with that key agreement should also be allowed. This
965 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
967 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
968 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
969 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200970 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100971 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200973}
974
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100975/** Test whether a policy permits an algorithm.
976 *
977 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100978 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100979 * \note This function requires providing the key type for which the policy is
980 * being validated, since some algorithm policy definitions (e.g. MAC)
981 * have different properties depending on what kind of cipher it is
982 * combined with.
983 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100984 * \retval PSA_SUCCESS When \p alg is a specific algorithm
985 * allowed by the \p policy.
986 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
987 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
988 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
991 psa_key_type_t key_type,
992 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100993{
Steven Cooremand788fab2021-02-25 11:29:17 +0100994 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (alg == 0) {
996 return PSA_ERROR_INVALID_ARGUMENT;
997 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100998
Steven Cooreman7e39f052021-02-23 14:18:32 +0100999 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (PSA_ALG_IS_WILDCARD(alg)) {
1001 return PSA_ERROR_INVALID_ARGUMENT;
1002 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1005 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1006 return PSA_SUCCESS;
1007 } else {
1008 return PSA_ERROR_NOT_PERMITTED;
1009 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001010}
1011
Gilles Peskinef603c712019-01-19 13:40:11 +01001012/** Restrict a key policy based on a constraint.
1013 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001014 * \note This function requires providing the key type for which the policy is
1015 * being restricted, since some algorithm policy definitions (e.g. MAC)
1016 * have different properties depending on what kind of cipher it is
1017 * combined with.
1018 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001019 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001020 * \param[in,out] policy The policy to restrict.
1021 * \param[in] constraint The policy constraint to apply.
1022 *
1023 * \retval #PSA_SUCCESS
1024 * \c *policy contains the intersection of the original value of
1025 * \c *policy and \c *constraint.
1026 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001027 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001028 * \c *policy is unchanged.
1029 */
1030static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001031 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001032 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001034{
1035 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1037 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001038 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1040 constraint->alg2);
1041 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1042 return PSA_ERROR_INVALID_ARGUMENT;
1043 }
1044 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1045 return PSA_ERROR_INVALID_ARGUMENT;
1046 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001047 policy->usage &= constraint->usage;
1048 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001049 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001051}
1052
Przemek Stekiel061f6942022-11-30 10:51:35 +01001053/** Get the description of a key given its identifier and policy constraints
1054 * and lock it.
1055 *
1056 * The key must have allow all the usage flags set in \p usage. If \p alg is
1057 * nonzero, the key must allow operations with this algorithm. If \p alg is
1058 * zero, the algorithm is not checked.
1059 *
1060 * In case of a persistent key, the function loads the description of the key
1061 * into a key slot if not already done.
1062 *
1063 * On success, the returned key slot is locked. It is the responsibility of
1064 * the caller to unlock the key slot when it does not access it anymore.
1065 */
1066static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001067 mbedtls_svc_key_id_t key,
1068 psa_key_slot_t **p_slot,
1069 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001071{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001073 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 status = psa_get_and_lock_key_slot(key, p_slot);
1076 if (status != PSA_SUCCESS) {
1077 return status;
1078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001080
1081 /* Enforce that usage policy for the key slot contains all the flags
1082 * required by the usage parameter. There is one exception: public
1083 * keys can always be exported, so we treat public key objects as
1084 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001086 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001090 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001091 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001092 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001093
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001094 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (alg != 0) {
1096 status = psa_key_policy_permits(&slot->attr.policy,
1097 slot->attr.type,
1098 alg);
1099 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001100 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001102 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001105
1106error:
1107 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001111}
Darryl Green940d72c2018-07-13 13:18:51 +01001112
Ronald Cron5c522922020-11-14 16:35:34 +01001113/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001114 *
1115 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001116 * available, as opposed to a key in a secure element and/or to be used
1117 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001118 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001119 * This is a temporary function that may be used instead of
1120 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1121 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001122 *
Ronald Cron5c522922020-11-14 16:35:34 +01001123 * On success, the returned key slot is locked. It is the responsibility of the
1124 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001125 */
Ronald Cron5c522922020-11-14 16:35:34 +01001126static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1127 mbedtls_svc_key_id_t key,
1128 psa_key_slot_t **p_slot,
1129 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001131{
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1133 usage, alg);
1134 if (status != PSA_SUCCESS) {
1135 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1139 psa_unlock_key_slot(*p_slot);
1140 *p_slot = NULL;
1141 return PSA_ERROR_NOT_SUPPORTED;
1142 }
1143
1144 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001145}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001148{
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001150 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001152
Ronald Cronea0f8a62020-11-25 17:52:23 +01001153 slot->key.data = NULL;
1154 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001157}
1158
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001159/** Completely wipe a slot in memory, including its policy.
1160 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001162{
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 /*
1166 * As the return error code may not be handled in case of multiple errors,
1167 * do our best to report an unexpected lock counter. Assert with
1168 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1169 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1170 * function is called as part of the execution of a test suite, the
1171 * execution of the test suite is stopped in error if the assertion fails.
1172 */
1173 if (slot->lock_count != 1) {
1174 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001175 status = PSA_ERROR_CORRUPTION_DETECTED;
1176 }
1177
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001178 /* Multipart operations may still be using the key. This is safe
1179 * because all multipart operation objects are independent from
1180 * the key slot: if they need to access the key after the setup
1181 * phase, they have a copy of the key. Note that this means that
1182 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001183 /* At this point, key material and other type-specific content has
1184 * been wiped. Clear remaining metadata. We can call memset and not
1185 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 memset(slot, 0, sizeof(*slot));
1187 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001188}
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001192 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001193 psa_status_t status; /* status of the last operation */
1194 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1196 psa_se_drv_table_entry_t *driver;
1197#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (mbedtls_svc_key_id_is_null(key)) {
1200 return PSA_SUCCESS;
1201 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001202
Ronald Cronf2911112020-10-29 17:51:10 +01001203 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001204 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001205 * key, this will load the key description from persistent memory if not
1206 * done yet. We cannot avoid this loading as without it we don't know if
1207 * the key is operated by an SE or not and this information is needed by
1208 * the current implementation.
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 status = psa_get_and_lock_key_slot(key, &slot);
1211 if (status != PSA_SUCCESS) {
1212 return status;
1213 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001214
Ronald Cronf2911112020-10-29 17:51:10 +01001215 /*
1216 * If the key slot containing the key description is under access by the
1217 * library (apart from the present access), the key cannot be destroyed
1218 * yet. For the time being, just return in error. Eventually (to be
1219 * implemented), the key should be destroyed when all accesses have
1220 * stopped.
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (slot->lock_count > 1) {
1223 psa_unlock_key_slot(slot);
1224 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001228 /* Refuse the destruction of a read-only key (which may or may not work
1229 * if we attempt it, depending on whether the key is merely read-only
1230 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001231 * Just do the best we can, which is to wipe the copy in memory
1232 * (done in this function's cleanup code). */
1233 overall_status = PSA_ERROR_NOT_PERMITTED;
1234 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001235 }
1236
Gilles Peskine354f7672019-07-12 23:46:38 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1239 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001240 /* For a key in a secure element, we need to do three things:
1241 * remove the key file in internal storage, destroy the
1242 * key inside the secure element, and update the driver's
1243 * persistent data. Start a transaction that will encompass these
1244 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001246 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001248 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 status = psa_crypto_save_transaction();
1250 if (status != PSA_SUCCESS) {
1251 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001252 /* We should still try to destroy the key in the secure
1253 * element and the key metadata in storage. This is especially
1254 * important if the error is that the storage is full.
1255 * But how to do it exactly without risking an inconsistent
1256 * state after a reset?
1257 * https://github.com/ARMmbed/mbed-crypto/issues/215
1258 */
1259 overall_status = status;
1260 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001261 }
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 status = psa_destroy_se_key(driver,
1264 psa_key_slot_get_slot_number(slot));
1265 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001266 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001268 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1270
Darryl Greend49a4992018-06-18 17:27:26 +01001271#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1273 status = psa_destroy_persistent_key(slot->attr.id);
1274 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001275 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001277
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001278 /* TODO: other slots may have a copy of the same key. We should
1279 * invalidate them.
1280 * https://github.com/ARMmbed/mbed-crypto/issues/214
1281 */
Darryl Greend49a4992018-06-18 17:27:26 +01001282 }
1283#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001284
Gilles Peskinefc762652019-07-22 19:30:34 +02001285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (driver != NULL) {
1287 status = psa_save_se_persistent_data(driver);
1288 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001289 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 }
1291 status = psa_crypto_stop_transaction();
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 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001295 }
1296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1297
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001300 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001302 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 }
1304 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305}
1306
Valerio Settib2bcedb2023-07-10 11:24:00 +02001307#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001308 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001309static psa_status_t psa_get_rsa_public_exponent(
1310 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001312{
1313 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001315 uint8_t *buffer = NULL;
1316 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1320 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001321 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 }
1323 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001324 /* It's the default value, which is reported as an empty string,
1325 * so there's nothing to do. */
1326 goto exit;
1327 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 buflen = mbedtls_mpi_size(&mpi);
1330 buffer = mbedtls_calloc(1, buflen);
1331 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1333 goto exit;
1334 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1336 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001337 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001339 attributes->domain_parameters = buffer;
1340 attributes->domain_parameters_size = buflen;
1341
1342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 mbedtls_mpi_free(&mpi);
1344 if (ret != 0) {
1345 mbedtls_free(buffer);
1346 }
1347 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001348}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001349#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001350 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001351
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001352/** Retrieve all the publicly-accessible attributes of a key.
1353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1355 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001358 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001359 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1364 if (status != PSA_SUCCESS) {
1365 return status;
1366 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001367
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001368 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1370 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001371
1372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1374 psa_set_key_slot_number(attributes,
1375 psa_key_slot_get_slot_number(slot));
1376 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001380#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1381 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001382 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001383 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001384 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001385 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001386 * is not yet implemented.
1387 * https://github.com/ARMmbed/mbed-crypto/issues/216
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001390 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001391
Ronald Cron90857082020-11-25 14:58:33 +01001392 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 slot->attr.type,
1394 slot->key.data,
1395 slot->key.bytes,
1396 &rsa);
1397 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001398 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 status = psa_get_rsa_public_exponent(rsa,
1402 attributes);
1403 mbedtls_rsa_free(rsa);
1404 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001405 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001406 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001407#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1408 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001409 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001410 default:
1411 /* Nothing else to do. */
1412 break;
1413 }
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (status != PSA_SUCCESS) {
1416 psa_reset_key_attributes(attributes);
1417 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422}
1423
Gilles Peskinec8000c02019-08-02 20:15:51 +02001424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1425psa_status_t psa_get_key_slot_number(
1426 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001428{
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001430 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return PSA_SUCCESS;
1432 } else {
1433 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001435}
1436#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1439 size_t key_buffer_size,
1440 uint8_t *data,
1441 size_t data_size,
1442 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001443{
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (key_buffer_size > data_size) {
1445 return PSA_ERROR_BUFFER_TOO_SMALL;
1446 }
1447 memcpy(data, key_buffer, key_buffer_size);
1448 memset(data + key_buffer_size, 0,
1449 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001450 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001452}
1453
Ronald Cron7285cda2020-11-26 14:46:37 +01001454psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 const psa_key_attributes_t *attributes,
1456 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001458{
Ronald Crond18b5f82020-11-25 16:46:05 +01001459 psa_key_type_t type = attributes->core.type;
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 if (key_type_is_raw_bytes(type) ||
1462 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001463 PSA_KEY_TYPE_IS_ECC(type) ||
1464 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 return psa_export_key_buffer_internal(
1466 key_buffer, key_buffer_size,
1467 data, data_size, data_length);
1468 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001469 /* This shouldn't happen in the reference implementation, but
1470 it is valid for a special-purpose implementation to omit
1471 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001473 }
1474}
1475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1477 uint8_t *data,
1478 size_t data_size,
1479 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480{
1481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1482 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1483 psa_key_slot_t *slot;
1484
Ronald Crone907e552021-01-18 13:22:38 +01001485 /* Reject a zero-length output buffer now, since this can never be a
1486 * valid key representation. This way we know that data must be a valid
1487 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (data_size == 0) {
1489 return PSA_ERROR_BUFFER_TOO_SMALL;
1490 }
Ronald Crone907e552021-01-18 13:22:38 +01001491
Ronald Cron9486f9d2020-11-26 13:36:23 +01001492 /* Set the key to empty now, so that even when there are errors, we always
1493 * set data_length to a value between 0 and data_size. On error, setting
1494 * the key to empty is a good choice because an empty key representation is
1495 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001496 *data_length = 0;
1497
Ronald Cron9486f9d2020-11-26 13:36:23 +01001498 /* Export requires the EXPORT flag. There is an exception for public keys,
1499 * which don't require any flag, but
1500 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1503 PSA_KEY_USAGE_EXPORT, 0);
1504 if (status != PSA_SUCCESS) {
1505 return status;
1506 }
mohammad160306e79202018-03-28 13:17:44 +03001507
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 psa_key_attributes_t attributes = {
1509 .core = slot->attr
1510 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 status = psa_driver_wrapper_export_key(&attributes,
1512 slot->key.data, slot->key.bytes,
1513 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001518}
1519
Ronald Cron7285cda2020-11-26 14:46:37 +01001520psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001521 const psa_key_attributes_t *attributes,
1522 const uint8_t *key_buffer,
1523 size_t key_buffer_size,
1524 uint8_t *data,
1525 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527{
Ronald Crond18b5f82020-11-25 16:46:05 +01001528 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001529
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001530 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001531 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1532 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001533 /* Exporting public -> public */
1534 return psa_export_key_buffer_internal(
1535 key_buffer, key_buffer_size,
1536 data, data_size, data_length);
1537 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001538#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1540 return mbedtls_psa_rsa_export_public_key(attributes,
1541 key_buffer,
1542 key_buffer_size,
1543 data,
1544 data_size,
1545 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001546#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001547 /* We don't know how to convert a private RSA key to public. */
1548 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001549#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001550 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001551 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001553 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1554 return mbedtls_psa_ecp_export_public_key(attributes,
1555 key_buffer,
1556 key_buffer_size,
1557 data,
1558 data_size,
1559 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001560#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001561 /* We don't know how to convert a private ECC key to public */
1562 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001563#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001564 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001565 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001567 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001568 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001569 key_buffer,
1570 key_buffer_size,
1571 data, data_size,
1572 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001573#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001574 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001575#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001576 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001578 (void) key_buffer;
1579 (void) key_buffer_size;
1580 (void) data;
1581 (void) data_size;
1582 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001584 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585}
Ronald Crond18b5f82020-11-25 16:46:05 +01001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1588 uint8_t *data,
1589 size_t data_size,
1590 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001591{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001593 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001594 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001595 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001596
Ronald Crone907e552021-01-18 13:22:38 +01001597 /* Reject a zero-length output buffer now, since this can never be a
1598 * valid key representation. This way we know that data must be a valid
1599 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (data_size == 0) {
1601 return PSA_ERROR_BUFFER_TOO_SMALL;
1602 }
Ronald Crone907e552021-01-18 13:22:38 +01001603
Darryl Greendd8fb772018-11-07 16:00:44 +00001604 /* Set the key to empty now, so that even when there are errors, we always
1605 * set data_length to a value between 0 and data_size. On error, setting
1606 * the key to empty is a good choice because an empty key representation is
1607 * unlikely to be accepted anywhere. */
1608 *data_length = 0;
1609
1610 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1612 if (status != PSA_SUCCESS) {
1613 return status;
1614 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1617 status = PSA_ERROR_INVALID_ARGUMENT;
1618 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001619 }
1620
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001621 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001622 .core = slot->attr
1623 };
Ronald Cron67227982020-11-26 15:16:05 +01001624 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001625 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001627
1628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001632}
1633
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001634MBEDTLS_STATIC_ASSERT(
1635 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1636 "One or more key attribute flag is listed as both external-only and dual-use")
1637MBEDTLS_STATIC_ASSERT(
1638 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1639 "One or more key attribute flag is listed as both internal-only and dual-use")
1640MBEDTLS_STATIC_ASSERT(
1641 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1642 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001643
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001644/** Validate that a key policy is internally well-formed.
1645 *
1646 * This function only rejects invalid policies. It does not validate the
1647 * consistency of the policy with respect to other attributes of the key
1648 * such as the key type.
1649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001651{
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1653 PSA_KEY_USAGE_COPY |
1654 PSA_KEY_USAGE_ENCRYPT |
1655 PSA_KEY_USAGE_DECRYPT |
1656 PSA_KEY_USAGE_SIGN_MESSAGE |
1657 PSA_KEY_USAGE_VERIFY_MESSAGE |
1658 PSA_KEY_USAGE_SIGN_HASH |
1659 PSA_KEY_USAGE_VERIFY_HASH |
1660 PSA_KEY_USAGE_VERIFY_DERIVATION |
1661 PSA_KEY_USAGE_DERIVE)) != 0) {
1662 return PSA_ERROR_INVALID_ARGUMENT;
1663 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001666}
1667
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001668/** Validate the internal consistency of key attributes.
1669 *
1670 * This function only rejects invalid attribute values. If does not
1671 * validate the consistency of the attributes with any key data that may
1672 * be involved in the creation of the key.
1673 *
1674 * Call this function early in the key creation process.
1675 *
1676 * \param[in] attributes Key attributes for the new key.
1677 * \param[out] p_drv On any return, the driver for the key, if any.
1678 * NULL for a transparent key.
1679 *
1680 */
1681static psa_status_t psa_validate_key_attributes(
1682 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001684{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001685 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1687 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 status = psa_validate_key_location(lifetime, p_drv);
1690 if (status != PSA_SUCCESS) {
1691 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001692 }
1693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 status = psa_validate_key_persistence(lifetime);
1695 if (status != PSA_SUCCESS) {
1696 return status;
1697 }
1698
1699 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1700 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1701 return PSA_ERROR_INVALID_ARGUMENT;
1702 }
1703 } else {
1704 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1705 return PSA_ERROR_INVALID_ARGUMENT;
1706 }
1707 }
1708
1709 status = psa_validate_key_policy(&attributes->core.policy);
1710 if (status != PSA_SUCCESS) {
1711 return status;
1712 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001713
1714 /* Refuse to create overly large keys.
1715 * Note that this doesn't trigger on import if the attributes don't
1716 * explicitly specify a size (so psa_get_key_bits returns 0), so
1717 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1719 return PSA_ERROR_NOT_SUPPORTED;
1720 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001721
Gilles Peskine91e8c332019-08-02 19:19:39 +02001722 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1724 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1725 return PSA_ERROR_INVALID_ARGUMENT;
1726 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001729}
1730
Gilles Peskine4747d192019-04-17 15:05:45 +02001731/** Prepare a key slot to receive key material.
1732 *
1733 * This function allocates a key slot and sets its metadata.
1734 *
1735 * If this function fails, call psa_fail_key_creation().
1736 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001737 * This function is intended to be used as follows:
1738 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001739 * it with the specified attributes, and in case of a volatile key assign it
1740 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * -# Populate the slot with the key material.
1742 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1743 * In case of failure at any step, stop the sequence and call
1744 * psa_fail_key_creation().
1745 *
Ronald Cron5c522922020-11-14 16:35:34 +01001746 * On success, the key slot is locked. It is the responsibility of the caller
1747 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001748 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001749 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001750 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 * \param[out] p_slot On success, a pointer to the prepared slot.
1752 * \param[out] p_drv On any return, the driver for the key, if any.
1753 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001754 *
1755 * \retval #PSA_SUCCESS
1756 * The key slot is ready to receive key material.
1757 * \return If this function fails, the key slot is an invalid state.
1758 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 */
1760static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001761 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001762 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001765{
1766 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001767 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 psa_key_slot_t *slot;
1769
Gilles Peskinedf179142019-07-15 22:02:14 +02001770 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001771 *p_drv = NULL;
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 status = psa_validate_key_attributes(attributes, p_drv);
1774 if (status != PSA_SUCCESS) {
1775 return status;
1776 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1779 if (status != PSA_SUCCESS) {
1780 return status;
1781 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 slot = *p_slot;
1783
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001784 /* We're storing the declared bit-size of the key. It's up to each
1785 * creation mechanism to verify that this information is correct.
1786 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001787 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001788 * is optional (import, copy). In case of a volatile key, assign it the
1789 * volatile key identifier associated to the slot returned to contain its
1790 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001791
1792 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001794#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1795 slot->attr.id = volatile_key_id;
1796#else
1797 slot->attr.id.key_id = volatile_key_id;
1798#endif
1799 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001800
Gilles Peskine91e8c332019-08-02 19:19:39 +02001801 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001802 * external-only flags, query `attributes`. Thanks to the check
1803 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001804 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001805 * may have set. */
1806 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001807
Gilles Peskinecbaff462019-07-12 23:46:04 +02001808#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001809 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001810 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001811 * create the key file in internal storage, create the
1812 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001813 * persistent data. This is done by starting a transaction that will
1814 * encompass these three actions.
1815 * For registering a volatile key, we just need to find an appropriate
1816 * slot number inside the SE. Since the key is designated volatile, creating
1817 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001818 /* The first thing to do is to find a slot number for the new key.
1819 * We save the slot number in persistent storage as part of the
1820 * transaction data. It will be needed to recover if the power
1821 * fails during the key creation process, to clean up on the secure
1822 * element side after restarting. Obtaining a slot number from the
1823 * secure element driver updates its persistent state, but we do not yet
1824 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001825 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001827 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1829 &slot_number);
1830 if (status != PSA_SUCCESS) {
1831 return status;
1832 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1835 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001836 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001837 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001838 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 status = psa_crypto_save_transaction();
1840 if (status != PSA_SUCCESS) {
1841 (void) psa_crypto_stop_transaction();
1842 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001844 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001845
1846 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001848 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001851 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001853 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1855
Ryan Everett975d4112023-11-16 13:37:51 +00001856 slot->status = PSA_SLOT_OCCUPIED;
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001859}
Gilles Peskine4747d192019-04-17 15:05:45 +02001860
1861/** Finalize the creation of a key once its key material has been set.
1862 *
1863 * This entails writing the key to persistent storage.
1864 *
1865 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001866 * See the documentation of psa_start_key_creation() for the intended use
1867 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001868 *
Ronald Cron5c522922020-11-14 16:35:34 +01001869 * If the finalization succeeds, the function unlocks the key slot (it was
1870 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1871 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001872 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001873 * \param[in,out] slot Pointer to the slot with key material.
1874 * \param[in] driver The secure element driver for the key,
1875 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001876 * \param[out] key On success, identifier of the key. Note that the
1877 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001878 *
1879 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001880 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001881 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1882 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1883 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1884 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1885 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1886 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001887 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001888 * \return If this function fails, the key slot is an invalid state.
1889 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001891static psa_status_t psa_finish_key_creation(
1892 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001893 psa_se_drv_table_entry_t *driver,
1894 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001895{
1896 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001897 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001898 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001899
1900#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001904 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001905 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001907
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001908 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1909 sizeof(data.slot_number),
1910 "Slot number size does not match psa_se_key_data_storage_t");
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1913 status = psa_save_persistent_key(&slot->attr,
1914 (uint8_t *) &data,
1915 sizeof(data));
1916 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001917#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1918 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001919 /* Key material is saved in export representation in the slot, so
1920 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 status = psa_save_persistent_key(&slot->attr,
1922 slot->key.data,
1923 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001924 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001925 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001926#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1927
Gilles Peskinecbaff462019-07-12 23:46:04 +02001928#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001929 /* Finish the transaction for a key creation. This does not
1930 * happen when registering an existing key. Detect this case
1931 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001932 * creation of a persistent key in a secure element requires a transaction,
1933 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (driver != NULL &&
1935 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1936 status = psa_save_se_persistent_data(driver);
1937 if (status != PSA_SUCCESS) {
1938 psa_destroy_persistent_key(slot->attr.id);
1939 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001942 }
1943#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001946 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 status = psa_unlock_key_slot(slot);
1948 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001949 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001951 }
Ronald Cron50972942020-11-14 11:28:25 +01001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001954}
1955
1956/** Abort the creation of a key.
1957 *
1958 * You may call this function after calling psa_start_key_creation(),
1959 * or after psa_finish_key_creation() fails. In other circumstances, this
1960 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001961 * See the documentation of psa_start_key_creation() for the intended use
1962 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001963 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001964 * \param[in,out] slot Pointer to the slot with key material.
1965 * \param[in] driver The secure element driver for the key,
1966 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001967 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968static void psa_fail_key_creation(psa_key_slot_t *slot,
1969 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001970{
Gilles Peskine011e4282019-06-26 18:34:38 +02001971 (void) driver;
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001976
1977#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001978 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001979 * element, and the failure happened later (when saving metadata
1980 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001981 * element.
1982 * https://github.com/ARMmbed/mbed-crypto/issues/217
1983 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001984
Gilles Peskined7729582019-08-05 15:55:54 +02001985 /* Abort the ongoing transaction if any (there may not be one if
1986 * the creation process failed before starting one, or if the
1987 * key creation is a registration of a key in a secure element).
1988 * Earlier functions must already have done what it takes to undo any
1989 * partial creation. All that's left is to update the transaction data
1990 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001992#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001995}
1996
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001997/** Validate optional attributes during key creation.
1998 *
1999 * Some key attributes are optional during key creation. If they are
2000 * specified in the attributes structure, check that they are consistent
2001 * with the data in the slot.
2002 *
2003 * This function should be called near the end of key creation, after
2004 * the slot in memory is fully populated but before saving persistent data.
2005 */
2006static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009{
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (attributes->core.type != 0) {
2011 if (attributes->core.type != slot->attr.type) {
2012 return PSA_ERROR_INVALID_ARGUMENT;
2013 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 }
2015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002017#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2018 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2020 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002021 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002022 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002024
Ronald Cron90857082020-11-25 14:58:33 +01002025 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 slot->attr.type,
2027 slot->key.data,
2028 slot->key.bytes,
2029 &rsa);
2030 if (status != PSA_SUCCESS) {
2031 return status;
2032 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 mbedtls_mpi_init(&actual);
2035 mbedtls_mpi_init(&required);
2036 ret = mbedtls_rsa_export(rsa,
2037 NULL, NULL, NULL, NULL, &actual);
2038 mbedtls_rsa_free(rsa);
2039 mbedtls_free(rsa);
2040 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
2043 ret = mbedtls_mpi_read_binary(&required,
2044 attributes->domain_parameters,
2045 attributes->domain_parameters_size);
2046 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 }
2049 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 }
2052rsa_exit:
2053 mbedtls_mpi_free(&actual);
2054 mbedtls_mpi_free(&required);
2055 if (ret != 0) {
2056 return mbedtls_to_psa_error(ret);
2057 }
2058 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002059#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2060 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002061 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002062 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 }
2065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (attributes->core.bits != 0) {
2068 if (attributes->core.bits != slot->attr.bits) {
2069 return PSA_ERROR_INVALID_ARGUMENT;
2070 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 }
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074}
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2077 const uint8_t *data,
2078 size_t data_length,
2079 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002080{
2081 psa_status_t status;
2082 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002083 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002084 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302085 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002086
Ronald Cron81709fc2020-11-14 12:10:32 +01002087 *key = MBEDTLS_SVC_KEY_ID_INIT;
2088
Gilles Peskine0f84d622019-09-12 19:03:13 +02002089 /* Reject zero-length symmetric keys (including raw data key objects).
2090 * This also rejects any key which might be encoded as an empty string,
2091 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (data_length == 0) {
2093 return PSA_ERROR_INVALID_ARGUMENT;
2094 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002095
Archana449608b2021-09-08 15:36:05 +05302096 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (data_length > SIZE_MAX / 8) {
2098 return PSA_ERROR_NOT_SUPPORTED;
2099 }
Archana6ed4bda2021-08-04 10:47:15 +05302100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2102 &slot, &driver);
2103 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002107 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302108 * storage ( thus not in the case of importing a key in a secure element
2109 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2110 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (slot->key.data == NULL) {
2112 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302113 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 attributes, data, data_length, &storage_size);
2115 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 }
Archanad8a83dc2021-06-14 10:04:16 +05302118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 status = psa_allocate_buffer_to_slot(slot, storage_size);
2120 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002123 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002124
2125 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_driver_wrapper_import_key(attributes,
2127 data, data_length,
2128 slot->key.data,
2129 slot->key.bytes,
2130 &slot->key.bytes, &bits);
2131 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002136 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002138 status = PSA_ERROR_INVALID_ARGUMENT;
2139 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002140 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002141
Archana6ed4bda2021-08-04 10:47:15 +05302142 /* Enforce a size limit, and in particular ensure that the bit
2143 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302145 status = PSA_ERROR_NOT_SUPPORTED;
2146 goto exit;
2147 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 status = psa_validate_optional_attributes(slot, attributes);
2149 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (status != PSA_SUCCESS) {
2156 psa_fail_key_creation(slot, driver);
2157 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002160}
2161
Gilles Peskined7729582019-08-05 15:55:54 +02002162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2163psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002165{
2166 psa_status_t status;
2167 psa_key_slot_t *slot = NULL;
2168 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002170
2171 /* Leaving attributes unspecified is not currently supported.
2172 * It could make sense to query the key type and size from the
2173 * secure element, but not all secure elements support this
2174 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2176 return PSA_ERROR_NOT_SUPPORTED;
2177 }
2178 if (psa_get_key_bits(attributes) == 0) {
2179 return PSA_ERROR_NOT_SUPPORTED;
2180 }
Gilles Peskined7729582019-08-05 15:55:54 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2183 &slot, &driver);
2184 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 }
Gilles Peskined7729582019-08-05 15:55:54 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002189
2190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (status != PSA_SUCCESS) {
2192 psa_fail_key_creation(slot, driver);
2193 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002194
Gilles Peskined7729582019-08-05 15:55:54 +02002195 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 psa_close_key(key);
2197 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002198}
2199#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2202 const psa_key_attributes_t *specified_attributes,
2203 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002204{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002205 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002206 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002207 psa_key_slot_t *source_slot = NULL;
2208 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002210 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302211 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002212
Ronald Cron81709fc2020-11-14 12:10:32 +01002213 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2214
Archana8a180362021-07-05 02:18:48 +05302215 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2217 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 status = psa_validate_optional_attributes(source_slot,
2222 specified_attributes);
2223 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226
Archana9d17bf42021-09-10 06:22:44 +05302227 /* The target key type and number of bits have been validated by
2228 * psa_validate_optional_attributes() to be either equal to zero or
2229 * equal to the ones of the source key. So it is safe to inherit
2230 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302231 * */
Archana9d17bf42021-09-10 06:22:44 +05302232 actual_attributes.core.bits = source_slot->attr.bits;
2233 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302234
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 status = psa_restrict_key_policy(source_slot->attr.type,
2237 &actual_attributes.core.policy,
2238 &source_slot->attr.policy);
2239 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2244 &target_slot, &driver);
2245 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 }
2248 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2249 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302250 /*
Archana9d17bf42021-09-10 06:22:44 +05302251 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302252 * the source key would need to be exported as plaintext and re-imported
2253 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302254 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302255 * appropriate API invocations from the application, if needed.
2256 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002257 status = PSA_ERROR_NOT_SUPPORTED;
2258 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002259 }
Archana8a180362021-07-05 02:18:48 +05302260 /*
2261 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302262 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302263 * - For opaque keys this translates to an invocation of the drivers'
2264 * copy_key entry point through the dispatch layer.
2265 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2267 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2268 &storage_size);
2269 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302270 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 }
Archana374fe5b2021-09-08 15:50:28 +05302272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2274 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 }
Archana374fe5b2021-09-08 15:50:28 +05302277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_driver_wrapper_copy_key(&actual_attributes,
2279 source_slot->key.data,
2280 source_slot->key.bytes,
2281 target_slot->key.data,
2282 target_slot->key.bytes,
2283 &target_slot->key.bytes);
2284 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 }
2287 } else {
2288 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302289 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 source_slot->key.bytes);
2291 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302292 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 }
Archana8a180362021-07-05 02:18:48 +05302294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (status != PSA_SUCCESS) {
2298 psa_fail_key_creation(target_slot, driver);
2299 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002304}
2305
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002306
2307
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002308/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002309/* Message digests */
2310/****************************************************************/
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002314 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (operation->id == 0) {
2316 return PSA_SUCCESS;
2317 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002320 operation->id = 0;
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2326 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002330 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002332 status = PSA_ERROR_BAD_STATE;
2333 goto exit;
2334 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002337 status = PSA_ERROR_INVALID_ARGUMENT;
2338 goto exit;
2339 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002340
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002341 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2342 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002346
2347exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (status != PSA_SUCCESS) {
2349 psa_hash_abort(operation);
2350 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002351
2352 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353}
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2356 const uint8_t *input,
2357 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002362 status = PSA_ERROR_BAD_STATE;
2363 goto exit;
2364 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002365
Steven Cooremanc8288352021-03-04 14:02:19 +01002366 /* Don't require hash implementations to behave correctly on a
2367 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (input_length == 0) {
2369 return PSA_SUCCESS;
2370 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002373
2374exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (status != PSA_SUCCESS) {
2376 psa_hash_abort(operation);
2377 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380}
2381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2383 uint8_t *hash,
2384 size_t hash_size,
2385 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002387 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (operation->id == 0) {
2389 return PSA_ERROR_BAD_STATE;
2390 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391
Steven Cooreman1e582352021-02-18 17:24:37 +01002392 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 operation, hash, hash_size, hash_length);
2394 psa_hash_abort(operation);
2395 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396}
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2399 const uint8_t *hash,
2400 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401{
Ronald Cron69a63422021-10-18 09:47:58 +02002402 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002404 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 operation,
2406 actual_hash, sizeof(actual_hash),
2407 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002410 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002414 status = PSA_ERROR_INVALID_SIGNATURE;
2415 goto exit;
2416 }
2417
Dave Rodgman78701152023-08-29 14:20:18 +01002418 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421
2422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2424 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002425 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429}
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431psa_status_t psa_hash_compute(psa_algorithm_t alg,
2432 const uint8_t *input, size_t input_length,
2433 uint8_t *hash, size_t hash_size,
2434 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002435{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002436 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (!PSA_ALG_IS_HASH(alg)) {
2438 return PSA_ERROR_INVALID_ARGUMENT;
2439 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2442 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002443}
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445psa_status_t psa_hash_compare(psa_algorithm_t alg,
2446 const uint8_t *input, size_t input_length,
2447 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002448{
Ronald Cron69a63422021-10-18 09:47:58 +02002449 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002450 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (!PSA_ALG_IS_HASH(alg)) {
2453 return PSA_ERROR_INVALID_ARGUMENT;
2454 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002455
Steven Cooreman1e582352021-02-18 17:24:37 +01002456 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 alg, input, input_length,
2458 actual_hash, sizeof(actual_hash),
2459 &actual_hash_length);
2460 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002461 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 }
2463 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002464 status = PSA_ERROR_INVALID_SIGNATURE;
2465 goto exit;
2466 }
Dave Rodgman78701152023-08-29 14:20:18 +01002467 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002468 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002470
2471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2473 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002474}
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2477 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002478{
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (source_operation->id == 0 ||
2480 target_operation->id != 0) {
2481 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002482 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2485 target_operation);
2486 if (status != PSA_SUCCESS) {
2487 psa_hash_abort(target_operation);
2488 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002491}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492
2493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495/* MAC */
2496/****************************************************************/
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002499{
Steven Cooremane6804192021-03-19 18:28:56 +01002500 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (operation->id == 0) {
2502 return PSA_SUCCESS;
2503 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002506 operation->mac_size = 0;
2507 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002508 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002511}
2512
Ronald Cron2dff3b22021-06-17 16:33:22 +02002513static psa_status_t psa_mac_finalize_alg_and_key_validation(
2514 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002515 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002517{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 psa_key_type_t key_type = psa_get_key_type(attributes);
2520 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (!PSA_ALG_IS_MAC(alg)) {
2523 return PSA_ERROR_INVALID_ARGUMENT;
2524 }
Steven Cooremane6804192021-03-19 18:28:56 +01002525
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002526 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 status = psa_mac_key_can_do(alg, key_type);
2528 if (status != PSA_SUCCESS) {
2529 return status;
2530 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002531
Steven Cooremand1a68f12021-05-10 11:13:41 +02002532 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002536 /* A very short MAC is too short for security since it can be
2537 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2538 * so we make this our minimum, even though 32 bits is still
2539 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002541 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2544 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002545 /* It's impossible to "truncate" to a larger length than the full length
2546 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002548 }
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002551 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2552 * that is disabled in the compile-time configuration. The result can
2553 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2554 * configuration into account. In this case, force a return of
2555 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2556 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2557 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2558 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2559 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002561 }
2562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002564}
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2567 mbedtls_svc_key_id_t key,
2568 psa_algorithm_t alg,
2569 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002570{
2571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2572 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002573 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002574 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575
2576 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002578 status = PSA_ERROR_BAD_STATE;
2579 goto exit;
2580 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581
2582 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 key,
2584 &slot,
2585 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2586 alg);
2587 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002588 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002591 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592 .core = slot->attr
2593 };
2594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2596 &operation->mac_size);
2597 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600
2601 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002602 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (is_sign) {
2604 status = psa_driver_wrapper_mac_sign_setup(operation,
2605 &attributes,
2606 slot->key.data,
2607 slot->key.bytes,
2608 alg);
2609 } else {
2610 status = psa_driver_wrapper_mac_verify_setup(operation,
2611 &attributes,
2612 slot->key.data,
2613 slot->key.bytes,
2614 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002616
Gilles Peskinefbfac682018-07-08 20:51:54 +02002617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (status != PSA_SUCCESS) {
2619 psa_mac_abort(operation);
2620 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625}
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2628 mbedtls_svc_key_id_t key,
2629 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002630{
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002632}
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2635 mbedtls_svc_key_id_t key,
2636 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637{
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002639}
2640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2642 const uint8_t *input,
2643 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644{
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (operation->id == 0) {
2646 return PSA_ERROR_BAD_STATE;
2647 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002649 /* Don't require hash implementations to behave correctly on a
2650 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (input_length == 0) {
2652 return PSA_SUCCESS;
2653 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2656 input, input_length);
2657 if (status != PSA_SUCCESS) {
2658 psa_mac_abort(operation);
2659 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662}
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2665 uint8_t *mac,
2666 size_t mac_size,
2667 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002668{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2670 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002673 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002674 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002678 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002679 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002680 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002681
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002682 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2683 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002685 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002686 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002687 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002690 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002691 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002692 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 status = psa_driver_wrapper_mac_sign_finish(operation,
2695 mac, operation->mac_size,
2696 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002697
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002698exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002699 /* In case of success, set the potential excess room in the output buffer
2700 * to an invalid value, to avoid potentially leaking a longer MAC.
2701 * In case of error, set the output length and content to a safe default,
2702 * such that in case the caller misses an error check, the output would be
2703 * an unachievable MAC.
2704 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002706 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002708 }
mohammad16036df908f2018-04-02 08:34:15 -07002709
Paul Elliottdc42ca82023-02-24 18:11:59 +00002710 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002715}
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2718 const uint8_t *mac,
2719 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002721 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2722 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002725 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002726 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002727 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002730 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002731 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002732 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002735 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002736 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002737 }
2738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 status = psa_driver_wrapper_mac_verify_finish(operation,
2740 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002742exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746}
2747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2749 psa_algorithm_t alg,
2750 const uint8_t *input,
2751 size_t input_length,
2752 uint8_t *mac,
2753 size_t mac_size,
2754 size_t *mac_length,
2755 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002756{
2757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002758 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2759 psa_key_slot_t *slot;
2760 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002761 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762
Ronald Cron51131b52021-06-17 17:17:20 +02002763 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 key,
2765 &slot,
2766 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2767 alg);
2768 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002771
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002772 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002773 .core = slot->attr
2774 };
2775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2777 &operation_mac_size);
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
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002783 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002784 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002785 }
2786
2787 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 &attributes,
2789 slot->key.data, slot->key.bytes,
2790 alg,
2791 input, input_length,
2792 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002793
2794exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002795 /* In case of success, set the potential excess room in the output buffer
2796 * to an invalid value, to avoid potentially leaking a longer MAC.
2797 * In case of error, set the output length and content to a safe default,
2798 * such that in case the caller misses an error check, the output would be
2799 * an unachievable MAC.
2800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002802 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002803 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002804 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002805
2806 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002811}
2812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 uint8_t *mac,
2818 size_t mac_size,
2819 size_t *mac_length)
2820{
2821 return psa_mac_compute_internal(key, alg,
2822 input, input_length,
2823 mac, mac_size, mac_length, 1);
2824}
2825
2826psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2827 psa_algorithm_t alg,
2828 const uint8_t *input,
2829 size_t input_length,
2830 const uint8_t *mac,
2831 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002832{
2833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002834 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2835 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 status = psa_mac_compute_internal(key, alg,
2838 input, input_length,
2839 actual_mac, sizeof(actual_mac),
2840 &actual_mac_length, 0);
2841 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002846 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002848 }
Dave Rodgman78701152023-08-29 14:20:18 +01002849 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002850 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002851 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002852 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853
2854exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002859
Gilles Peskinee59236f2018-01-27 23:32:46 +01002860/****************************************************************/
2861/* Asymmetric cryptography */
2862/****************************************************************/
2863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2865 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002866{
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (input_is_message) {
2868 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2869 return PSA_ERROR_INVALID_ARGUMENT;
2870 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2873 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2874 return PSA_ERROR_INVALID_ARGUMENT;
2875 }
2876 }
2877 } else {
2878 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2879 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002880 }
2881 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002884}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2887 int input_is_message,
2888 psa_algorithm_t alg,
2889 const uint8_t *input,
2890 size_t input_length,
2891 uint8_t *signature,
2892 size_t signature_size,
2893 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002894{
2895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2896 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2897 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002898 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899
2900 *signature_length = 0;
2901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 status = psa_sign_verify_check_alg(input_is_message, alg);
2903 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002904 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002906
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002907 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002908 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909 * buffer can in principle be empty since it doesn't actually have
2910 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (signature_size == 0) {
2912 return PSA_ERROR_BUFFER_TOO_SMALL;
2913 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002915 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 key, &slot,
2917 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2918 PSA_KEY_USAGE_SIGN_HASH,
2919 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002926 status = PSA_ERROR_INVALID_ARGUMENT;
2927 goto exit;
2928 }
2929
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002930 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932 };
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002935 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002936 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002937 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 signature, signature_size, signature_length);
2939 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002940
2941 status = psa_driver_wrapper_sign_hash(
2942 &attributes, slot->key.data, slot->key.bytes,
2943 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002945 }
2946
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002947
2948exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002949 psa_wipe_tag_output_buffer(signature, status, signature_size,
2950 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002955}
2956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2958 int input_is_message,
2959 psa_algorithm_t alg,
2960 const uint8_t *input,
2961 size_t input_length,
2962 const uint8_t *signature,
2963 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002964{
2965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2966 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2967 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 status = psa_sign_verify_check_alg(input_is_message, alg);
2970 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002971 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002973
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002974 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 key, &slot,
2976 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2977 PSA_KEY_USAGE_VERIFY_HASH,
2978 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (status != PSA_SUCCESS) {
2981 return status;
2982 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002983
2984 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002986 };
2987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002989 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002990 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002991 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 signature, signature_length);
2993 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002994 status = psa_driver_wrapper_verify_hash(
2995 &attributes, slot->key.data, slot->key.bytes,
2996 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002998 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003003
3004}
3005
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003006psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003007 const psa_key_attributes_t *attributes,
3008 const uint8_t *key_buffer,
3009 size_t key_buffer_size,
3010 psa_algorithm_t alg,
3011 const uint8_t *input,
3012 size_t input_length,
3013 uint8_t *signature,
3014 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003016{
3017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003020 size_t hash_length;
3021 uint8_t hash[PSA_HASH_MAX_SIZE];
3022
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003023 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 PSA_ALG_SIGN_GET_HASH(alg),
3025 input, input_length,
3026 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003029 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003031
3032 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 attributes, key_buffer, key_buffer_size,
3034 alg, hash, hash_length,
3035 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003036 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003039}
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3042 psa_algorithm_t alg,
3043 const uint8_t *input,
3044 size_t input_length,
3045 uint8_t *signature,
3046 size_t signature_size,
3047 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003048{
3049 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003050 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003052}
3053
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003054psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003055 const psa_key_attributes_t *attributes,
3056 const uint8_t *key_buffer,
3057 size_t key_buffer_size,
3058 psa_algorithm_t alg,
3059 const uint8_t *input,
3060 size_t input_length,
3061 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003063{
3064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003067 size_t hash_length;
3068 uint8_t hash[PSA_HASH_MAX_SIZE];
3069
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003070 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 PSA_ALG_SIGN_GET_HASH(alg),
3072 input, input_length,
3073 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003076 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003078
3079 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 attributes, key_buffer, key_buffer_size,
3081 alg, hash, hash_length,
3082 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003083 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003086}
3087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3089 psa_algorithm_t alg,
3090 const uint8_t *input,
3091 size_t input_length,
3092 const uint8_t *signature,
3093 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003094{
3095 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003096 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003098}
3099
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003100psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003101 const psa_key_attributes_t *attributes,
3102 const uint8_t *key_buffer, size_t key_buffer_size,
3103 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003105{
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3107 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3108 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003109#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3111 return mbedtls_psa_rsa_sign_hash(
3112 attributes,
3113 key_buffer, key_buffer_size,
3114 alg, hash, hash_length,
3115 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003116#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3117 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 } else {
3119 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3122 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003123#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3125 return mbedtls_psa_ecdsa_sign_hash(
3126 attributes,
3127 key_buffer, key_buffer_size,
3128 alg, hash, hash_length,
3129 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003130#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3131 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 } else {
3133 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003134 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003135 }
Ronald Cron4501c982021-03-19 20:09:32 +01003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 (void) key_buffer;
3138 (void) key_buffer_size;
3139 (void) hash;
3140 (void) hash_length;
3141 (void) signature;
3142 (void) signature_size;
3143 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003146}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3149 psa_algorithm_t alg,
3150 const uint8_t *hash,
3151 size_t hash_length,
3152 uint8_t *signature,
3153 size_t signature_size,
3154 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003155{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003156 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003157 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003159}
3160
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003161psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003162 const psa_key_attributes_t *attributes,
3163 const uint8_t *key_buffer, size_t key_buffer_size,
3164 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003166{
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3168 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3169 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003170#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3172 return mbedtls_psa_rsa_verify_hash(
3173 attributes,
3174 key_buffer, key_buffer_size,
3175 alg, hash, hash_length,
3176 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003177#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3178 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 } else {
3180 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3183 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003184#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3186 return mbedtls_psa_ecdsa_verify_hash(
3187 attributes,
3188 key_buffer, key_buffer_size,
3189 alg, hash, hash_length,
3190 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003191#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3192 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 } else {
3194 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003195 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003196 }
Ronald Cron4501c982021-03-19 20:09:32 +01003197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 (void) key_buffer;
3199 (void) key_buffer_size;
3200 (void) hash;
3201 (void) hash_length;
3202 (void) signature;
3203 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003206}
3207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3209 psa_algorithm_t alg,
3210 const uint8_t *hash,
3211 size_t hash_length,
3212 const uint8_t *signature,
3213 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003214{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003215 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003216 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003218}
3219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3221 psa_algorithm_t alg,
3222 const uint8_t *input,
3223 size_t input_length,
3224 const uint8_t *salt,
3225 size_t salt_length,
3226 uint8_t *output,
3227 size_t output_size,
3228 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003231 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003232 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003233 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003234
Darryl Green5cc689a2018-07-24 15:34:10 +01003235 (void) input;
3236 (void) input_length;
3237 (void) salt;
3238 (void) output;
3239 (void) output_size;
3240
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003241 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3244 return PSA_ERROR_INVALID_ARGUMENT;
3245 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003246
Ronald Cron5c522922020-11-14 16:35:34 +01003247 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3249 if (status != PSA_SUCCESS) {
3250 return status;
3251 }
3252 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3253 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003254 status = PSA_ERROR_INVALID_ARGUMENT;
3255 goto exit;
3256 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003258 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003260 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003261
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003262 status = psa_driver_wrapper_asymmetric_encrypt(
3263 &attributes, slot->key.data, slot->key.bytes,
3264 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003270}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3273 psa_algorithm_t alg,
3274 const uint8_t *input,
3275 size_t input_length,
3276 const uint8_t *salt,
3277 size_t salt_length,
3278 uint8_t *output,
3279 size_t output_size,
3280 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003283 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003284 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003285 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003286
Darryl Green5cc689a2018-07-24 15:34:10 +01003287 (void) input;
3288 (void) input_length;
3289 (void) salt;
3290 (void) output;
3291 (void) output_size;
3292
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003293 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3296 return PSA_ERROR_INVALID_ARGUMENT;
3297 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003298
Ronald Cron5c522922020-11-14 16:35:34 +01003299 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3301 if (status != PSA_SUCCESS) {
3302 return status;
3303 }
3304 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003305 status = PSA_ERROR_INVALID_ARGUMENT;
3306 goto exit;
3307 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003309 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003311 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003313 status = psa_driver_wrapper_asymmetric_decrypt(
3314 &attributes, slot->key.data, slot->key.bytes,
3315 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003317
3318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003322}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003323
Paul Elliott9fe12f62022-11-30 19:16:02 +00003324/****************************************************************/
3325/* Asymmetric interruptible cryptography */
3326/****************************************************************/
3327
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003328static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3329
Paul Elliott9fe12f62022-11-30 19:16:02 +00003330void psa_interruptible_set_max_ops(uint32_t max_ops)
3331{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003332 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333}
3334
3335uint32_t psa_interruptible_get_max_ops(void)
3336{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003337 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338}
3339
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340uint32_t psa_sign_hash_get_num_ops(
3341 const psa_sign_hash_interruptible_operation_t *operation)
3342{
Paul Elliott296ede92022-12-15 17:00:30 +00003343 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003344}
3345
3346uint32_t psa_verify_hash_get_num_ops(
3347 const psa_verify_hash_interruptible_operation_t *operation)
3348{
Paul Elliott296ede92022-12-15 17:00:30 +00003349 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003350}
3351
Paul Elliott59ad9452022-12-18 15:09:02 +00003352static psa_status_t psa_sign_hash_abort_internal(
3353 psa_sign_hash_interruptible_operation_t *operation)
3354{
3355 if (operation->id == 0) {
3356 /* The object has (apparently) been initialized but it is not (yet)
3357 * in use. It's ok to call abort on such an object, and there's
3358 * nothing to do. */
3359 return PSA_SUCCESS;
3360 }
3361
3362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3363
3364 status = psa_driver_wrapper_sign_hash_abort(operation);
3365
3366 operation->id = 0;
3367
Paul Elliotte6145dc2023-02-07 12:51:21 +00003368 /* Do not clear either the error_occurred or num_ops elements here as they
3369 * only want to be cleared by the application calling abort, not by abort
3370 * being called at completion of an operation. */
3371
Paul Elliott59ad9452022-12-18 15:09:02 +00003372 return status;
3373}
3374
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375psa_status_t psa_sign_hash_start(
3376 psa_sign_hash_interruptible_operation_t *operation,
3377 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3378 const uint8_t *hash, size_t hash_length)
3379{
3380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3381 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3382 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003383 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384
Paul Elliottc9774412023-02-06 15:14:07 +00003385 /* Check that start has not been previously called, or operation has not
3386 * previously errored. */
3387 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003388 return PSA_ERROR_BAD_STATE;
3389 }
3390
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391 status = psa_sign_verify_check_alg(0, alg);
3392 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003393 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 return status;
3395 }
3396
3397 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3398 PSA_KEY_USAGE_SIGN_HASH,
3399 alg);
3400
3401 if (status != PSA_SUCCESS) {
3402 goto exit;
3403 }
3404
3405 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3406 status = PSA_ERROR_INVALID_ARGUMENT;
3407 goto exit;
3408 }
3409
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003410 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003411 .core = slot->attr
3412 };
3413
Paul Elliott296ede92022-12-15 17:00:30 +00003414 /* Ensure ops count gets reset, in case of operation re-use. */
3415 operation->num_ops = 0;
3416
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3418 slot->key.data,
3419 slot->key.bytes, alg,
3420 hash, hash_length);
3421exit:
3422
3423 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003424 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003425 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 }
3427
3428 unlock_status = psa_unlock_key_slot(slot);
3429
Paul Elliottc9774412023-02-06 15:14:07 +00003430 if (unlock_status != PSA_SUCCESS) {
3431 operation->error_occurred = 1;
3432 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433
Paul Elliottc9774412023-02-06 15:14:07 +00003434 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003435}
3436
3437
3438psa_status_t psa_sign_hash_complete(
3439 psa_sign_hash_interruptible_operation_t *operation,
3440 uint8_t *signature, size_t signature_size,
3441 size_t *signature_length)
3442{
3443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3444
3445 *signature_length = 0;
3446
Paul Elliottc9774412023-02-06 15:14:07 +00003447 /* Check that start has been called first, and that operation has not
3448 * previously errored. */
3449 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003450 status = PSA_ERROR_BAD_STATE;
3451 goto exit;
3452 }
3453
Paul Elliott096abc42023-02-03 18:33:23 +00003454 /* Immediately reject a zero-length signature buffer. This guarantees that
3455 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003456 if (signature_size == 0) {
3457 status = PSA_ERROR_BUFFER_TOO_SMALL;
3458 goto exit;
3459 }
3460
3461 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3462 signature_size,
3463 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464
Paul Elliott296ede92022-12-15 17:00:30 +00003465 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003466 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003467
Paul Elliottebe225c2023-02-10 14:32:53 +00003468exit:
3469
Paul Elliott59200a22023-02-21 15:07:40 +00003470 psa_wipe_tag_output_buffer(signature, status, signature_size,
3471 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliott53bb3122023-02-10 14:22:22 +00003473 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003474 if (status != PSA_SUCCESS) {
3475 operation->error_occurred = 1;
3476 }
3477
Paul Elliott59ad9452022-12-18 15:09:02 +00003478 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003479 }
3480
3481 return status;
3482}
3483
3484psa_status_t psa_sign_hash_abort(
3485 psa_sign_hash_interruptible_operation_t *operation)
3486{
Paul Elliott59ad9452022-12-18 15:09:02 +00003487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3488
3489 status = psa_sign_hash_abort_internal(operation);
3490
3491 /* We clear the number of ops done here, so that it is not cleared when
3492 * the operation fails or succeeds, only on manual abort. */
3493 operation->num_ops = 0;
3494
Paul Elliottc9774412023-02-06 15:14:07 +00003495 /* Likewise, failure state. */
3496 operation->error_occurred = 0;
3497
Paul Elliott59ad9452022-12-18 15:09:02 +00003498 return status;
3499}
3500
3501static psa_status_t psa_verify_hash_abort_internal(
3502 psa_verify_hash_interruptible_operation_t *operation)
3503{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504 if (operation->id == 0) {
3505 /* The object has (apparently) been initialized but it is not (yet)
3506 * in use. It's ok to call abort on such an object, and there's
3507 * nothing to do. */
3508 return PSA_SUCCESS;
3509 }
3510
Paul Elliott59ad9452022-12-18 15:09:02 +00003511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3512
3513 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514
3515 operation->id = 0;
3516
Paul Elliotte6145dc2023-02-07 12:51:21 +00003517 /* Do not clear either the error_occurred or num_ops elements here as they
3518 * only want to be cleared by the application calling abort, not by abort
3519 * being called at completion of an operation. */
3520
Paul Elliott59ad9452022-12-18 15:09:02 +00003521 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522}
3523
3524psa_status_t psa_verify_hash_start(
3525 psa_verify_hash_interruptible_operation_t *operation,
3526 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3527 const uint8_t *hash, size_t hash_length,
3528 const uint8_t *signature, size_t signature_length)
3529{
3530 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3531 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3532 psa_key_slot_t *slot;
3533
Paul Elliottc9774412023-02-06 15:14:07 +00003534 /* Check that start has not been previously called, or operation has not
3535 * previously errored. */
3536 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003537 return PSA_ERROR_BAD_STATE;
3538 }
3539
3540 status = psa_sign_verify_check_alg(0, alg);
3541 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003542 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003543 return status;
3544 }
3545
3546 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3547 PSA_KEY_USAGE_VERIFY_HASH,
3548 alg);
3549
3550 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003551 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003552 return status;
3553 }
3554
3555 psa_key_attributes_t attributes = {
3556 .core = slot->attr
3557 };
3558
Paul Elliott296ede92022-12-15 17:00:30 +00003559 /* Ensure ops count gets reset, in case of operation re-use. */
3560 operation->num_ops = 0;
3561
Paul Elliott9fe12f62022-11-30 19:16:02 +00003562 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3563 slot->key.data,
3564 slot->key.bytes,
3565 alg, hash, hash_length,
3566 signature, signature_length);
3567
3568 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003569 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003570 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003571 }
3572
3573 unlock_status = psa_unlock_key_slot(slot);
3574
Paul Elliottc9774412023-02-06 15:14:07 +00003575 if (unlock_status != PSA_SUCCESS) {
3576 operation->error_occurred = 1;
3577 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003578
Paul Elliottc9774412023-02-06 15:14:07 +00003579 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003580}
3581
3582psa_status_t psa_verify_hash_complete(
3583 psa_verify_hash_interruptible_operation_t *operation)
3584{
3585 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3586
Paul Elliottc9774412023-02-06 15:14:07 +00003587 /* Check that start has been called first, and that operation has not
3588 * previously errored. */
3589 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003590 status = PSA_ERROR_BAD_STATE;
3591 goto exit;
3592 }
3593
3594 status = psa_driver_wrapper_verify_hash_complete(operation);
3595
Paul Elliott296ede92022-12-15 17:00:30 +00003596 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003597 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003598 operation);
3599
Paul Elliottebe225c2023-02-10 14:32:53 +00003600exit:
3601
Paul Elliott9fe12f62022-11-30 19:16:02 +00003602 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003603 if (status != PSA_SUCCESS) {
3604 operation->error_occurred = 1;
3605 }
3606
Paul Elliott59ad9452022-12-18 15:09:02 +00003607 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003608 }
3609
3610 return status;
3611}
3612
3613psa_status_t psa_verify_hash_abort(
3614 psa_verify_hash_interruptible_operation_t *operation)
3615{
Paul Elliott59ad9452022-12-18 15:09:02 +00003616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003617
Paul Elliott59ad9452022-12-18 15:09:02 +00003618 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003619
Paul Elliott59ad9452022-12-18 15:09:02 +00003620 /* We clear the number of ops done here, so that it is not cleared when
3621 * the operation fails or succeeds, only on manual abort. */
3622 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003623
Paul Elliottc9774412023-02-06 15:14:07 +00003624 /* Likewise, failure state. */
3625 operation->error_occurred = 0;
3626
Paul Elliott59ad9452022-12-18 15:09:02 +00003627 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003628}
3629
Paul Elliott588f8ed2022-12-02 18:10:26 +00003630/****************************************************************/
3631/* Asymmetric interruptible cryptography internal */
3632/* implementations */
3633/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003634
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3636{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003637
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3639 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3640 defined(MBEDTLS_ECP_RESTARTABLE)
3641
3642 /* Internal implementation uses zero to indicate infinite number max ops,
3643 * therefore avoid this value, and set to minimum possible. */
3644 if (max_ops == 0) {
3645 max_ops = 1;
3646 }
3647
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003649#else
3650 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003651#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3652 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3653 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3654}
3655
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003657 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658{
3659#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3660 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3661 defined(MBEDTLS_ECP_RESTARTABLE)
3662
Paul Elliott93d9ca82023-02-15 18:14:21 +00003663 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664#else
3665 (void) operation;
3666 return 0;
3667#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3668 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3669 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3670}
3671
3672uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003673 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674{
3675 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3676 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3677 defined(MBEDTLS_ECP_RESTARTABLE)
3678
Paul Elliott93d9ca82023-02-15 18:14:21 +00003679 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680#else
3681 (void) operation;
3682 return 0;
3683#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3684 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3685 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3686}
3687
3688psa_status_t mbedtls_psa_sign_hash_start(
3689 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3690 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3691 size_t key_buffer_size, psa_algorithm_t alg,
3692 const uint8_t *hash, size_t hash_length)
3693{
3694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003695 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003696
Paul Elliott068fe072023-01-16 13:59:15 +00003697 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3698 return PSA_ERROR_NOT_SUPPORTED;
3699 }
3700
3701 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003702 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003703 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704
3705#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003706 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3707 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003708
Paul Elliotta1c94092023-02-15 16:38:04 +00003709 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3710
Paul Elliott93d9ca82023-02-15 18:14:21 +00003711 /* Ensure num_ops is zero'ed in case of context re-use. */
3712 operation->num_ops = 0;
3713
Paul Elliott068fe072023-01-16 13:59:15 +00003714 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3715 attributes->core.bits,
3716 key_buffer,
3717 key_buffer_size,
3718 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003719
Paul Elliott068fe072023-01-16 13:59:15 +00003720 if (status != PSA_SUCCESS) {
3721 return status;
3722 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003723
Paul Elliott1bc59df2023-02-05 13:41:57 +00003724 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003725 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
Paul Elliott068fe072023-01-16 13:59:15 +00003727 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003728 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003729 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730
Paul Elliott0290a762023-02-09 14:30:24 +00003731 /* We only need to store the same length of hash as the private key size
3732 * here, it would be truncated by the internal implementation anyway. */
3733 required_hash_length = (hash_length < operation->coordinate_bytes ?
3734 hash_length : operation->coordinate_bytes);
3735
Paul Elliottba70ad42023-02-15 18:23:53 +00003736 if (required_hash_length > sizeof(operation->hash)) {
3737 /* Shouldn't happen, but better safe than sorry. */
3738 return PSA_ERROR_CORRUPTION_DETECTED;
3739 }
3740
Paul Elliott0290a762023-02-09 14:30:24 +00003741 memcpy(operation->hash, hash, required_hash_length);
3742 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003743
3744 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003745
3746#else
Paul Elliott068fe072023-01-16 13:59:15 +00003747 (void) operation;
3748 (void) key_buffer;
3749 (void) key_buffer_size;
3750 (void) alg;
3751 (void) hash;
3752 (void) hash_length;
3753 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003754 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
Paul Elliott068fe072023-01-16 13:59:15 +00003756 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003757#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3758 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3759 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760}
3761
3762psa_status_t mbedtls_psa_sign_hash_complete(
3763 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3764 uint8_t *signature, size_t signature_size,
3765 size_t *signature_length)
3766{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3768 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3769 defined(MBEDTLS_ECP_RESTARTABLE)
3770
Paul Elliotta1c94092023-02-15 16:38:04 +00003771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003772 mbedtls_mpi r;
3773 mbedtls_mpi s;
3774
Paul Elliott46845252023-02-03 14:59:11 +00003775 mbedtls_mpi_init(&r);
3776 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003778 /* Ensure max_ops is set to the current value (or default). */
3779 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3780
Paul Elliotta1c94092023-02-15 16:38:04 +00003781 if (signature_size < 2 * operation->coordinate_bytes) {
3782 status = PSA_ERROR_BUFFER_TOO_SMALL;
3783 goto exit;
3784 }
3785
Paul Elliott588f8ed2022-12-02 18:10:26 +00003786 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003787
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3789 status = mbedtls_to_psa_error(
3790 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003791 &r,
3792 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793 &operation->ctx->d,
3794 operation->hash,
3795 operation->hash_length,
3796 operation->md_alg,
3797 mbedtls_psa_get_random,
3798 MBEDTLS_PSA_RANDOM_STATE,
3799 &operation->restart_ctx));
3800#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003801 status = PSA_ERROR_NOT_SUPPORTED;
3802 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3804 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003805 status = mbedtls_to_psa_error(
3806 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003807 &r,
3808 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809 &operation->ctx->d,
3810 operation->hash,
3811 operation->hash_length,
3812 mbedtls_psa_get_random,
3813 MBEDTLS_PSA_RANDOM_STATE,
3814 mbedtls_psa_get_random,
3815 MBEDTLS_PSA_RANDOM_STATE,
3816 &operation->restart_ctx));
3817 }
3818
Paul Elliottf8e5b562023-02-19 18:43:45 +00003819 /* Hide the fact that the restart context only holds a delta of number of
3820 * ops done during the last operation, not an absolute value. */
3821 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3822
Paul Elliott724bd252023-02-08 12:35:08 +00003823 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003824 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003825 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003827 operation->coordinate_bytes)
3828 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829
3830 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003831 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 }
3833
3834 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003835 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003837 operation->coordinate_bytes,
3838 operation->coordinate_bytes)
3839 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840
3841 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003842 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843 }
3844
Paul Elliott1bc59df2023-02-05 13:41:57 +00003845 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846
Paul Elliott724bd252023-02-08 12:35:08 +00003847 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848 }
Paul Elliott724bd252023-02-08 12:35:08 +00003849
3850exit:
3851
3852 mbedtls_mpi_free(&r);
3853 mbedtls_mpi_free(&s);
3854 return status;
3855
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 #else
3857
3858 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 (void) signature;
3860 (void) signature_size;
3861 (void) signature_length;
3862
3863 return PSA_ERROR_NOT_SUPPORTED;
3864
3865#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3866 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3867 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3868}
3869
3870psa_status_t mbedtls_psa_sign_hash_abort(
3871 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3872{
3873
3874#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3875 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3876 defined(MBEDTLS_ECP_RESTARTABLE)
3877
3878 if (operation->ctx) {
3879 mbedtls_ecdsa_free(operation->ctx);
3880 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003881 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882 }
3883
3884 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3885
Paul Elliott93d9ca82023-02-15 18:14:21 +00003886 operation->num_ops = 0;
3887
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888 return PSA_SUCCESS;
3889
3890#else
3891
3892 (void) operation;
3893
3894 return PSA_ERROR_NOT_SUPPORTED;
3895
3896#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3897 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3898 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3899}
3900
3901psa_status_t mbedtls_psa_verify_hash_start(
3902 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3903 const psa_key_attributes_t *attributes,
3904 const uint8_t *key_buffer, size_t key_buffer_size,
3905 psa_algorithm_t alg,
3906 const uint8_t *hash, size_t hash_length,
3907 const uint8_t *signature, size_t signature_length)
3908{
3909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003910 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003911 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912
Paul Elliott068fe072023-01-16 13:59:15 +00003913 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3914 return PSA_ERROR_NOT_SUPPORTED;
3915 }
3916
3917 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003918 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003919 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003920
3921#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003922 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3923 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003924
Paul Elliotta1c94092023-02-15 16:38:04 +00003925 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3926 mbedtls_mpi_init(&operation->r);
3927 mbedtls_mpi_init(&operation->s);
3928
Paul Elliott93d9ca82023-02-15 18:14:21 +00003929 /* Ensure num_ops is zero'ed in case of context re-use. */
3930 operation->num_ops = 0;
3931
Paul Elliott068fe072023-01-16 13:59:15 +00003932 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3933 attributes->core.bits,
3934 key_buffer,
3935 key_buffer_size,
3936 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003937
Paul Elliott068fe072023-01-16 13:59:15 +00003938 if (status != PSA_SUCCESS) {
3939 return status;
3940 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003941
Paul Elliottc569fc22023-02-10 13:02:54 +00003942 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliott1bc59df2023-02-05 13:41:57 +00003944 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003945 return PSA_ERROR_INVALID_SIGNATURE;
3946 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliott068fe072023-01-16 13:59:15 +00003948 status = mbedtls_to_psa_error(
3949 mbedtls_mpi_read_binary(&operation->r,
3950 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003951 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952
Paul Elliott068fe072023-01-16 13:59:15 +00003953 if (status != PSA_SUCCESS) {
3954 return status;
3955 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003956
Paul Elliott068fe072023-01-16 13:59:15 +00003957 status = mbedtls_to_psa_error(
3958 mbedtls_mpi_read_binary(&operation->s,
3959 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003960 coordinate_bytes,
3961 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 Elliott2c9843f2023-02-15 17:32:42 +00003967 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968
Paul Elliott2c9843f2023-02-15 17:32:42 +00003969 if (status != PSA_SUCCESS) {
3970 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003971 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Paul Elliott0290a762023-02-09 14:30:24 +00003973 /* We only need to store the same length of hash as the private key size
3974 * here, it would be truncated by the internal implementation anyway. */
3975 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3976 coordinate_bytes);
3977
Paul Elliottba70ad42023-02-15 18:23:53 +00003978 if (required_hash_length > sizeof(operation->hash)) {
3979 /* Shouldn't happen, but better safe than sorry. */
3980 return PSA_ERROR_CORRUPTION_DETECTED;
3981 }
3982
Paul Elliott0290a762023-02-09 14:30:24 +00003983 memcpy(operation->hash, hash, required_hash_length);
3984 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003985
3986 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003987#else
Paul Elliott068fe072023-01-16 13:59:15 +00003988 (void) operation;
3989 (void) key_buffer;
3990 (void) key_buffer_size;
3991 (void) alg;
3992 (void) hash;
3993 (void) hash_length;
3994 (void) signature;
3995 (void) signature_length;
3996 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003997 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003998 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999
Paul Elliott068fe072023-01-16 13:59:15 +00004000 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4002 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4003 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004004}
4005
4006psa_status_t mbedtls_psa_verify_hash_complete(
4007 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4008{
4009
4010#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4011 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4012 defined(MBEDTLS_ECP_RESTARTABLE)
4013
Paul Elliottf8e5b562023-02-19 18:43:45 +00004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4015
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004016 /* Ensure max_ops is set to the current value (or default). */
4017 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4018
Paul Elliottf8e5b562023-02-19 18:43:45 +00004019 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004020 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4021 operation->hash,
4022 operation->hash_length,
4023 &operation->ctx->Q,
4024 &operation->r,
4025 &operation->s,
4026 &operation->restart_ctx));
4027
Paul Elliottf8e5b562023-02-19 18:43:45 +00004028 /* Hide the fact that the restart context only holds a delta of number of
4029 * ops done during the last operation, not an absolute value. */
4030 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4031
4032 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004033#else
4034 (void) operation;
4035
4036 return PSA_ERROR_NOT_SUPPORTED;
4037
4038#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4039 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4040 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4041}
4042
4043psa_status_t mbedtls_psa_verify_hash_abort(
4044 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4045{
4046
4047#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4048 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4049 defined(MBEDTLS_ECP_RESTARTABLE)
4050
4051 if (operation->ctx) {
4052 mbedtls_ecdsa_free(operation->ctx);
4053 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004054 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055 }
4056
4057 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4058
Paul Elliott93d9ca82023-02-15 18:14:21 +00004059 operation->num_ops = 0;
4060
Paul Elliott588f8ed2022-12-02 18:10:26 +00004061 mbedtls_mpi_free(&operation->r);
4062 mbedtls_mpi_free(&operation->s);
4063
4064 return PSA_SUCCESS;
4065
4066#else
4067 (void) operation;
4068
4069 return PSA_ERROR_NOT_SUPPORTED;
4070
4071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4072 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4073 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4074}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004075
mohammad1603503973b2018-03-12 15:59:30 +02004076/****************************************************************/
4077/* Symmetric cryptography */
4078/****************************************************************/
4079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4081 mbedtls_svc_key_id_t key,
4082 psa_algorithm_t alg,
4083 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004084{
4085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4086 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004087 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4089 PSA_KEY_USAGE_ENCRYPT :
4090 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004091 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004092
4093 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004095 status = PSA_ERROR_BAD_STATE;
4096 goto exit;
4097 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004100 status = PSA_ERROR_INVALID_ARGUMENT;
4101 goto exit;
4102 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4105 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004108
Ronald Cron49fafa92021-03-10 08:34:23 +01004109 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004110 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004111 * so we only set it (in the driver wrapper) after resources have been
4112 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004113 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004115 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004117 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 }
4119 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004120
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004121 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004123 };
4124
Ronald Cronab99ac22020-10-01 16:38:28 +02004125 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 if (cipher_operation == MBEDTLS_ENCRYPT) {
4127 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4128 &attributes,
4129 slot->key.data,
4130 slot->key.bytes,
4131 alg);
4132 } else {
4133 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4134 &attributes,
4135 slot->key.data,
4136 slot->key.bytes,
4137 alg);
4138 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004139
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (status != PSA_SUCCESS) {
4142 psa_cipher_abort(operation);
4143 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004148}
4149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4151 mbedtls_svc_key_id_t key,
4152 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004153{
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004155}
4156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4158 mbedtls_svc_key_id_t key,
4159 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004160{
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004162}
4163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4165 uint8_t *iv,
4166 size_t iv_size,
4167 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004168{
Ronald Cron6d051732020-10-01 14:10:20 +02004169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004170 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004171 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004174 status = PSA_ERROR_BAD_STATE;
4175 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004176 }
4177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004179 status = PSA_ERROR_BAD_STATE;
4180 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004181 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004182
Ronald Cron2fb90522021-07-05 12:31:44 +02004183 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004185 status = PSA_ERROR_BUFFER_TOO_SMALL;
4186 goto exit;
4187 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004190 status = PSA_ERROR_GENERIC_ERROR;
4191 goto exit;
4192 }
4193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 status = psa_generate_random(local_iv, default_iv_length);
4195 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 }
Ronald Cron5618a392021-03-26 09:52:26 +01004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 status = psa_driver_wrapper_cipher_set_iv(operation,
4200 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004201
4202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (status == PSA_SUCCESS) {
4204 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004205 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004206 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004208 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004210 }
Ronald Cron6d051732020-10-01 14:10:20 +02004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004213}
4214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4216 const uint8_t *iv,
4217 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004218{
Ronald Cron6d051732020-10-01 14:10:20 +02004219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004222 status = PSA_ERROR_BAD_STATE;
4223 goto exit;
4224 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004227 status = PSA_ERROR_BAD_STATE;
4228 goto exit;
4229 }
Ronald Crona0d68172021-03-26 10:15:08 +01004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004232 status = PSA_ERROR_INVALID_ARGUMENT;
4233 goto exit;
4234 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 status = psa_driver_wrapper_cipher_set_iv(operation,
4237 iv,
4238 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004239
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004242 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 } else {
4244 psa_cipher_abort(operation);
4245 }
4246 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004247}
4248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4250 const uint8_t *input,
4251 size_t input_length,
4252 uint8_t *output,
4253 size_t output_size,
4254 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004255{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004259 status = PSA_ERROR_BAD_STATE;
4260 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004261 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264 status = PSA_ERROR_BAD_STATE;
4265 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004266 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 status = psa_driver_wrapper_cipher_update(operation,
4269 input,
4270 input_length,
4271 output,
4272 output_size,
4273 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004274
4275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status != PSA_SUCCESS) {
4277 psa_cipher_abort(operation);
4278 }
Ronald Cron6d051732020-10-01 14:10:20 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004281}
4282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4284 uint8_t *output,
4285 size_t output_size,
4286 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004287{
David Saadab4ecc272019-02-14 13:48:10 +02004288 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004291 status = PSA_ERROR_BAD_STATE;
4292 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004293 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004296 status = PSA_ERROR_BAD_STATE;
4297 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004298 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 status = psa_driver_wrapper_cipher_finish(operation,
4301 output,
4302 output_size,
4303 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304
4305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 if (status == PSA_SUCCESS) {
4307 return psa_cipher_abort(operation);
4308 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004309 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004311
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004313 }
mohammad1603503973b2018-03-12 15:59:30 +02004314}
4315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004317{
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004319 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004320 * in use. It's ok to call abort on such an object, and there's
4321 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004323 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004326
Ronald Cron49fafa92021-03-10 08:34:23 +01004327 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004328 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004329 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004332}
4333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4335 psa_algorithm_t alg,
4336 const uint8_t *input,
4337 size_t input_length,
4338 uint8_t *output,
4339 size_t output_size,
4340 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004341{
4342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004343 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004344 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004345 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4346 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004347 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004350 status = PSA_ERROR_INVALID_ARGUMENT;
4351 goto exit;
4352 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4355 PSA_KEY_USAGE_ENCRYPT,
4356 alg);
4357 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004360
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004361 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363 };
4364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4366 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004367 status = PSA_ERROR_GENERIC_ERROR;
4368 goto exit;
4369 }
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (default_iv_length > 0) {
4372 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004373 status = PSA_ERROR_BUFFER_TOO_SMALL;
4374 goto exit;
4375 }
4376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 status = psa_generate_random(local_iv, default_iv_length);
4378 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004379 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004381 }
4382
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004383 status = psa_driver_wrapper_cipher_encrypt(
4384 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004385 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004386 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004388
4389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 unlock_status = psa_unlock_key_slot(slot);
4391 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004392 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004393 }
Ronald Cron23919522021-07-08 19:00:07 +02004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (status == PSA_SUCCESS) {
4396 if (default_iv_length > 0) {
4397 memcpy(output, local_iv, default_iv_length);
4398 }
4399 *output_length += default_iv_length;
4400 } else {
4401 *output_length = 0;
4402 }
4403
4404 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004405}
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4408 psa_algorithm_t alg,
4409 const uint8_t *input,
4410 size_t input_length,
4411 uint8_t *output,
4412 size_t output_size,
4413 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004414{
4415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004417 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004418 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004419
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004421 status = PSA_ERROR_INVALID_ARGUMENT;
4422 goto exit;
4423 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4426 PSA_KEY_USAGE_DECRYPT,
4427 alg);
4428 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004431
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004432 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004434 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004435
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4437 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004438 status = PSA_ERROR_INVALID_ARGUMENT;
4439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004441 status = PSA_ERROR_INVALID_ARGUMENT;
4442 goto exit;
4443 }
4444
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004445 status = psa_driver_wrapper_cipher_decrypt(
4446 &attributes, slot->key.data, slot->key.bytes,
4447 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004449
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 unlock_status = psa_unlock_key_slot(slot);
4452 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004453 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004457 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 }
Ronald Cron23919522021-07-08 19:00:07 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004461}
4462
4463
mohammad16035955c982018-04-26 00:53:03 +03004464/****************************************************************/
4465/* AEAD */
4466/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004467
Paul Elliottbaff51c2021-09-28 17:44:45 +01004468/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004470{
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004472}
4473
4474/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4476 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004477{
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004481#if defined(PSA_WANT_ALG_GCM)
4482 case PSA_ALG_GCM:
4483 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 * arbitrarily large nonces. Please note that we do not generally
4485 * recommend the usage of nonces of greater length than
4486 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4487 * size, which can then lead to collisions if you encrypt a very
4488 * large number of messages.*/
4489 if (nonce_length != 0) {
4490 return PSA_SUCCESS;
4491 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004492 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004493#endif /* PSA_WANT_ALG_GCM */
4494#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004495 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (nonce_length >= 7 && nonce_length <= 13) {
4497 return PSA_SUCCESS;
4498 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004499 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004500#endif /* PSA_WANT_ALG_CCM */
4501#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004502 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (nonce_length == 12) {
4504 return PSA_SUCCESS;
4505 } else if (nonce_length == 8) {
4506 return PSA_ERROR_NOT_SUPPORTED;
4507 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004508 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004509#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004511 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004513 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004516}
4517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004519{
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4521 return PSA_ERROR_INVALID_ARGUMENT;
4522 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004525}
4526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4528 psa_algorithm_t alg,
4529 const uint8_t *nonce,
4530 size_t nonce_length,
4531 const uint8_t *additional_data,
4532 size_t additional_data_length,
4533 const uint8_t *plaintext,
4534 size_t plaintext_length,
4535 uint8_t *ciphertext,
4536 size_t ciphertext_size,
4537 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004538{
Ronald Cron215633c2021-03-16 17:15:37 +01004539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4540 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004541
mohammad1603f08a5502018-06-03 15:05:47 +03004542 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 status = psa_aead_check_algorithm(alg);
4545 if (status != PSA_SUCCESS) {
4546 return status;
4547 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004548
Ronald Cron9a986162021-03-26 12:40:07 +01004549 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4551 if (status != PSA_SUCCESS) {
4552 return status;
4553 }
mohammad16035955c982018-04-26 00:53:03 +03004554
Ronald Cron215633c2021-03-16 17:15:37 +01004555 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004557 };
mohammad16035955c982018-04-26 00:53:03 +03004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 status = psa_aead_check_nonce_length(alg, nonce_length);
4560 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004561 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004563
Ronald Cronde822812021-03-17 16:08:20 +01004564 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004565 &attributes, slot->key.data, slot->key.bytes,
4566 alg,
4567 nonce, nonce_length,
4568 additional_data, additional_data_length,
4569 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4573 memset(ciphertext, 0, ciphertext_size);
4574 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004575
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004580}
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4583 psa_algorithm_t alg,
4584 const uint8_t *nonce,
4585 size_t nonce_length,
4586 const uint8_t *additional_data,
4587 size_t additional_data_length,
4588 const uint8_t *ciphertext,
4589 size_t ciphertext_length,
4590 uint8_t *plaintext,
4591 size_t plaintext_size,
4592 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004593{
Ronald Cron215633c2021-03-16 17:15:37 +01004594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4595 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004596
Gilles Peskineee652a32018-06-01 19:23:52 +02004597 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 status = psa_aead_check_algorithm(alg);
4600 if (status != PSA_SUCCESS) {
4601 return status;
4602 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004603
Ronald Cron9a986162021-03-26 12:40:07 +01004604 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4606 if (status != PSA_SUCCESS) {
4607 return status;
4608 }
mohammad16035955c982018-04-26 00:53:03 +03004609
Ronald Cron215633c2021-03-16 17:15:37 +01004610 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004612 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 status = psa_aead_check_nonce_length(alg, nonce_length);
4615 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004618
Ronald Cronde822812021-03-17 16:08:20 +01004619 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004620 &attributes, slot->key.data, slot->key.bytes,
4621 alg,
4622 nonce, nonce_length,
4623 additional_data, additional_data_length,
4624 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (status != PSA_SUCCESS && plaintext_size != 0) {
4628 memset(plaintext, 0, plaintext_size);
4629 }
mohammad160360a64d02018-06-03 17:20:42 +03004630
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 return status;
mohammad16035955c982018-04-26 00:53:03 +03004635}
4636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4638{
4639 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004642#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004644 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4646 return PSA_ERROR_INVALID_ARGUMENT;
4647 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004649#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650
Przemek Stekiel86679c72022-10-06 17:06:56 +02004651#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4655 return PSA_ERROR_INVALID_ARGUMENT;
4656 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004658#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004659
Przemek Stekiel86679c72022-10-06 17:06:56 +02004660#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (tag_len != 16) {
4664 return PSA_ERROR_INVALID_ARGUMENT;
4665 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004667#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004668
4669 default:
4670 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674}
4675
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004676/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4678 int is_encrypt,
4679 mbedtls_svc_key_id_t key,
4680 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004681{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4683 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4684 psa_key_slot_t *slot = NULL;
4685 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004686 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 status = psa_aead_check_algorithm(alg);
4689 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004694 status = PSA_ERROR_BAD_STATE;
4695 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004696 }
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (operation->nonce_set || operation->lengths_set ||
4699 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700 status = PSA_ERROR_BAD_STATE;
4701 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004702 }
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004705 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4711 alg);
4712 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004716 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717 .core = slot->attr
4718 };
4719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if (is_encrypt) {
4725 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4726 &attributes,
4727 slot->key.data,
4728 slot->key.bytes,
4729 alg);
4730 } else {
4731 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4732 &attributes,
4733 slot->key.data,
4734 slot->key.bytes,
4735 alg);
4736 }
4737 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742
4743exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004749 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 } else {
4751 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004755}
4756
Paul Elliott302ff6b2021-04-20 18:10:30 +01004757/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4759 mbedtls_svc_key_id_t key,
4760 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761{
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004763}
4764
4765/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4767 mbedtls_svc_key_id_t key,
4768 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004769{
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
4772
4773/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4775 uint8_t *nonce,
4776 size_t nonce_size,
4777 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778{
4779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004780 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004781 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004782
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004783 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004786 status = PSA_ERROR_BAD_STATE;
4787 goto exit;
4788 }
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004791 status = PSA_ERROR_BAD_STATE;
4792 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004793 }
4794
Paul Elliotte193ea82021-10-01 13:00:16 +01004795 /* For CCM, this size may not be correct according to the PSA
4796 * specification. The PSA Crypto 1.0.1 specification states:
4797 *
4798 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4799 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4800 *
4801 * However this restriction that L has to be the smallest integer is not
4802 * applied in practice, and it is not implementable here since the
4803 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4805 operation->alg);
4806 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004807 status = PSA_ERROR_BUFFER_TOO_SMALL;
4808 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809 }
4810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 status = psa_generate_random(local_nonce, required_nonce_size);
4812 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817
Paul Elliott39dc6b82021-05-11 19:16:09 +01004818exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (status == PSA_SUCCESS) {
4820 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 } else {
4823 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004824 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827}
4828
4829/* Set the nonce for a multipart authenticated encryption or decryption
4830 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004831psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4832 const uint8_t *nonce,
4833 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004835 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004838 status = PSA_ERROR_BAD_STATE;
4839 goto exit;
4840 }
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004845 }
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4848 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004849 status = PSA_ERROR_INVALID_ARGUMENT;
4850 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004851 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4854 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004855
Paul Elliott39dc6b82021-05-11 19:16:09 +01004856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004858 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 } else {
4860 psa_aead_abort(operation);
4861 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004864}
4865
4866/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004867psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4868 size_t ad_length,
4869 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004870{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004874 status = PSA_ERROR_BAD_STATE;
4875 goto exit;
4876 }
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->lengths_set || operation->ad_started ||
4879 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004880 status = PSA_ERROR_BAD_STATE;
4881 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004882 }
4883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004885#if defined(PSA_WANT_ALG_GCM)
4886 case PSA_ALG_GCM:
4887 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 * bits. Without the guard this code will generate warnings on 32bit
4889 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004890#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (((uint64_t) ad_length) >> 61 != 0 ||
4892 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004893 status = PSA_ERROR_INVALID_ARGUMENT;
4894 goto exit;
4895 }
Paul Elliott325d3742021-09-27 17:56:28 +01004896#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004897 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004898#endif /* PSA_WANT_ALG_GCM */
4899#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004900 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004902 status = PSA_ERROR_INVALID_ARGUMENT;
4903 goto exit;
4904 }
4905 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004906#endif /* PSA_WANT_ALG_CCM */
4907#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 case PSA_ALG_CHACHA20_POLY1305:
4909 /* No length restrictions for ChaChaPoly. */
4910 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004911#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004912 default:
4913 break;
4914 }
Paul Elliott325d3742021-09-27 17:56:28 +01004915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4917 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004918
Paul Elliott39dc6b82021-05-11 19:16:09 +01004919exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004921 operation->ad_remaining = ad_length;
4922 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004923 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 } else {
4925 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004926 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004930
4931/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004932psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4933 const uint8_t *input,
4934 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004935{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004939 status = PSA_ERROR_BAD_STATE;
4940 goto exit;
4941 }
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004944 status = PSA_ERROR_BAD_STATE;
4945 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946 }
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (operation->lengths_set) {
4949 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004950 status = PSA_ERROR_INVALID_ARGUMENT;
4951 goto exit;
4952 }
4953
4954 operation->ad_remaining -= input_length;
4955 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004956#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004958 status = PSA_ERROR_BAD_STATE;
4959 goto exit;
4960 }
4961#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004962
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 status = psa_driver_wrapper_aead_update_ad(operation, input,
4964 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004965
Paul Elliott39dc6b82021-05-11 19:16:09 +01004966exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004968 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 } else {
4970 psa_aead_abort(operation);
4971 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004974}
4975
4976/* Encrypt or decrypt a message fragment in an active multipart AEAD
4977 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004978psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4979 const uint8_t *input,
4980 size_t input_length,
4981 uint8_t *output,
4982 size_t output_size,
4983 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004986
4987 *output_length = 0;
4988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004990 status = PSA_ERROR_BAD_STATE;
4991 goto exit;
4992 }
4993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004995 status = PSA_ERROR_BAD_STATE;
4996 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005000 /* Additional data length was supplied, but not all the additional
5001 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005003 status = PSA_ERROR_INVALID_ARGUMENT;
5004 goto exit;
5005 }
5006
5007 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005009 status = PSA_ERROR_INVALID_ARGUMENT;
5010 goto exit;
5011 }
5012
5013 operation->body_remaining -= input_length;
5014 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005015#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005017 status = PSA_ERROR_BAD_STATE;
5018 goto exit;
5019 }
5020#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005021
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5023 output, output_size,
5024 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005025
Paul Elliott39dc6b82021-05-11 19:16:09 +01005026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005028 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 } else {
5030 psa_aead_abort(operation);
5031 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005034}
5035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005037{
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 if (operation->id == 0 || !operation->nonce_set) {
5039 return PSA_ERROR_BAD_STATE;
5040 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5043 operation->body_remaining != 0)) {
5044 return PSA_ERROR_INVALID_ARGUMENT;
5045 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005048}
5049
Paul Elliott302ff6b2021-04-20 18:10:30 +01005050/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005051psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5052 uint8_t *ciphertext,
5053 size_t ciphertext_size,
5054 size_t *ciphertext_length,
5055 uint8_t *tag,
5056 size_t tag_size,
5057 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5060
Paul Elliott302ff6b2021-04-20 18:10:30 +01005061 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005062 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 status = psa_aead_final_checks(operation);
5065 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005070 status = PSA_ERROR_BAD_STATE;
5071 goto exit;
5072 }
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5075 ciphertext_size,
5076 ciphertext_length,
5077 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005078
Paul Elliott39dc6b82021-05-11 19:16:09 +01005079exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005080
5081
Paul Elliottf47b0952021-05-21 18:02:33 +01005082 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005083 * the zero tag size, make sure the tag is set to something implausible.
5084 * Even if the operation succeeds, make sure we clear the rest of the
5085 * buffer to prevent potential leakage of anything previously placed in
5086 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005087 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005088
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005092}
5093
5094/* Finish authenticating and decrypting a message in a multipart AEAD
5095 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005096psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5097 uint8_t *plaintext,
5098 size_t plaintext_size,
5099 size_t *plaintext_length,
5100 const uint8_t *tag,
5101 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5104
Paul Elliott302ff6b2021-04-20 18:10:30 +01005105 *plaintext_length = 0;
5106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 status = psa_aead_final_checks(operation);
5108 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005113 status = PSA_ERROR_BAD_STATE;
5114 goto exit;
5115 }
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5118 plaintext_size,
5119 plaintext_length,
5120 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005121
5122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005126}
5127
5128/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005130{
5131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005134 /* The object has (apparently) been initialized but it is not (yet)
5135 * in use. It's ok to call abort on such an object, and there's
5136 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005138 }
5139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145}
5146
Gilles Peskinee59236f2018-01-27 23:32:46 +01005147/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148/* Generators */
5149/****************************************************************/
5150
Przemek Stekiel69c46792022-06-10 12:59:51 +02005151#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005152 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005153 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305154 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305155 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005156#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005157#endif /* At least one builtin KDF */
5158
Przemek Stekiel69c46792022-06-10 12:59:51 +02005159#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5162static psa_status_t psa_key_derivation_start_hmac(
5163 psa_mac_operation_t *operation,
5164 psa_algorithm_t hash_alg,
5165 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005167{
5168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5171 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5172 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005173
Steven Cooreman72f736a2021-05-07 14:14:37 +02005174 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 status = psa_driver_wrapper_mac_sign_setup(operation,
5178 &attributes,
5179 hmac_key, hmac_key_length,
5180 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 psa_reset_key_attributes(&attributes);
5183 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005184}
5185#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005186
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187#define HKDF_STATE_INIT 0 /* no input yet */
5188#define HKDF_STATE_STARTED 1 /* got salt */
5189#define HKDF_STATE_KEYED 2 /* got key */
5190#define HKDF_STATE_OUTPUT 3 /* output started */
5191
Gilles Peskinecbe66502019-05-16 16:59:18 +02005192static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005194{
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5196 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5197 } else {
5198 return operation->alg;
5199 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005200}
5201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005203{
5204 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5206 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005207 /* The object has (apparently) been initialized but it is not
5208 * in use. It's ok to call abort on such an object, and there's
5209 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005211#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5213 mbedtls_free(operation->ctx.hkdf.info);
5214 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5215 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005216#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005217#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5218 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5220 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5221 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5222 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005223 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005225 }
5226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005228 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005230 }
5231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005233 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005235 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005236#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005238 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005240 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005241#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005242 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005243
5244 /* We leave the fields Ai and output_block to be erased safely by the
5245 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005247#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5248 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005249#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5251 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5252 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5253 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005254#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305255#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305256 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305257 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005258 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305259 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305260 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305261
5262 status = PSA_SUCCESS;
5263 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305264#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265 {
5266 status = PSA_ERROR_BAD_STATE;
5267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 mbedtls_platform_zeroize(operation, sizeof(*operation));
5269 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270}
5271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005274{
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005278 }
5279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282}
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5285 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286{
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 if (operation->alg == 0) {
5288 return PSA_ERROR_BAD_STATE;
5289 }
5290 if (capacity > operation->capacity) {
5291 return PSA_ERROR_INVALID_ARGUMENT;
5292 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005295}
5296
Przemek Stekiel69c46792022-06-10 12:59:51 +02005297#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005298/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005299static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5300 psa_algorithm_t kdf_alg,
5301 uint8_t *output,
5302 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005303{
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5305 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005306 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005307 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005308#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005310#else
5311 const uint8_t last_block = 0xff;
5312#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 if (hkdf->state < HKDF_STATE_KEYED ||
5315 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 )) {
5320 return PSA_ERROR_BAD_STATE;
5321 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005322 hkdf->state = HKDF_STATE_OUTPUT;
5323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005325 /* Copy what remains of the current block */
5326 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005328 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 }
5330 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005331 output += n;
5332 output_length -= n;
5333 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005337 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005338 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005339 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005340 * object was corrupted or if this function is called directly
5341 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (hkdf->block_number == last_block) {
5343 return PSA_ERROR_BAD_STATE;
5344 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345
5346 /* We need a new block */
5347 ++hkdf->block_number;
5348 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5351 hash_alg,
5352 hkdf->prk,
5353 hash_length);
5354 if (status != PSA_SUCCESS) {
5355 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005356 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005357
5358 if (hkdf->block_number != 1) {
5359 status = psa_mac_update(&hkdf->hmac,
5360 hkdf->output_block,
5361 hash_length);
5362 if (status != PSA_SUCCESS) {
5363 return status;
5364 }
5365 }
5366 status = psa_mac_update(&hkdf->hmac,
5367 hkdf->info,
5368 hkdf->info_length);
5369 if (status != PSA_SUCCESS) {
5370 return status;
5371 }
5372 status = psa_mac_update(&hkdf->hmac,
5373 &hkdf->block_number, 1);
5374 if (status != PSA_SUCCESS) {
5375 return status;
5376 }
5377 status = psa_mac_sign_finish(&hkdf->hmac,
5378 hkdf->output_block,
5379 sizeof(hkdf->output_block),
5380 &hmac_output_length);
5381 if (status != PSA_SUCCESS) {
5382 return status;
5383 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005384 }
5385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005387}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005388#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005389
John Durkop07cc04a2020-11-16 22:08:34 -08005390#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5391 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005392static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5393 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005395{
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5397 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005398 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5399 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005400 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005401
Janos Follath7742fee2019-06-17 12:58:10 +01005402 /* We can't be wanting more output after block 0xff, otherwise
5403 * the capacity check in psa_key_derivation_output_bytes() would have
5404 * prevented this call. It could happen only if the operation
5405 * object was corrupted or if this function is called directly
5406 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (tls12_prf->block_number == 0xff) {
5408 return PSA_ERROR_CORRUPTION_DETECTED;
5409 }
Janos Follath7742fee2019-06-17 12:58:10 +01005410
5411 /* We need a new block */
5412 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005413 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005414
5415 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5416 *
5417 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5418 *
5419 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5420 * HMAC_hash(secret, A(2) + seed) +
5421 * HMAC_hash(secret, A(3) + seed) + ...
5422 *
5423 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005424 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005425 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005426 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005427 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005428 * is currently extracted as `output_block` and where i is
5429 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005430 */
5431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 status = psa_key_derivation_start_hmac(&hmac,
5433 hash_alg,
5434 tls12_prf->secret,
5435 tls12_prf->secret_length);
5436 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005437 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005439
5440 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005442 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5443 * the variable seed and in this instance means it in the context of the
5444 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 status = psa_mac_update(&hmac,
5446 tls12_prf->label,
5447 tls12_prf->label_length);
5448 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005449 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 }
5451 status = psa_mac_update(&hmac,
5452 tls12_prf->seed,
5453 tls12_prf->seed_length);
5454 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 }
5457 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005458 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5460 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005461 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 }
5464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 status = psa_mac_sign_finish(&hmac,
5466 tls12_prf->Ai, hash_length,
5467 &hmac_output_length);
5468 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005469 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 }
5471 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005472 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005474
5475 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 status = psa_key_derivation_start_hmac(&hmac,
5477 hash_alg,
5478 tls12_prf->secret,
5479 tls12_prf->secret_length);
5480 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005481 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 }
5483 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005485 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
5487 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5488 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
5491 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5492 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005493 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 }
5495 status = psa_mac_sign_finish(&hmac,
5496 tls12_prf->output_block, hash_length,
5497 &hmac_output_length);
5498 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005501
Janos Follath7742fee2019-06-17 12:58:10 +01005502
5503cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 cleanup_status = psa_mac_abort(&hmac);
5505 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005510}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005511
Janos Follath844eb0e2019-06-19 12:10:49 +01005512static psa_status_t psa_key_derivation_tls12_prf_read(
5513 psa_tls12_prf_key_derivation_t *tls12_prf,
5514 psa_algorithm_t alg,
5515 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005517{
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5519 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005520 psa_status_t status;
5521 uint8_t offset, length;
5522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005524 case PSA_TLS12_PRF_STATE_LABEL_SET:
5525 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5526 break;
5527 case PSA_TLS12_PRF_STATE_OUTPUT:
5528 break;
5529 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005531 }
5532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005534 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 if (tls12_prf->left_in_block == 0) {
5536 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5537 alg);
5538 if (status != PSA_SUCCESS) {
5539 return status;
5540 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005541
5542 continue;
5543 }
5544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005546 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005548 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005550
5551 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005553 output += length;
5554 output_length -= length;
5555 tls12_prf->left_in_block -= length;
5556 }
5557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005559}
John Durkop07cc04a2020-11-16 22:08:34 -08005560#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5561 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005562
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005563#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5564static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5565 psa_tls12_ecjpake_to_pms_t *ecjpake,
5566 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005568{
5569 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005570 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (output_length != 32) {
5573 return PSA_ERROR_INVALID_ARGUMENT;
5574 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5577 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5578 &output_size);
5579 if (status != PSA_SUCCESS) {
5580 return status;
5581 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (output_size != output_length) {
5584 return PSA_ERROR_GENERIC_ERROR;
5585 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005588}
5589#endif
5590
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305591#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305592static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5593 psa_pbkdf2_key_derivation_t *pbkdf2,
5594 psa_algorithm_t prf_alg,
5595 uint8_t prf_output_length,
5596 psa_key_attributes_t *attributes)
5597{
5598 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305599 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305600 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305601 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305602 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305603 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305604 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305606 mac_operation.is_sign = 1;
5607 mac_operation.mac_size = prf_output_length;
5608 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305609
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305610 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5611 attributes,
5612 pbkdf2->password,
5613 pbkdf2->password_length,
5614 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305615 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305616 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5619 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305620 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305621 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305622 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305623 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305624 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305625 }
5626 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5627 &mac_output_length);
5628 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305629 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305630 }
5631
5632 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305633 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305634 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 }
5636
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305637 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305638
5639 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305640 /* We are passing prf_output_length as mac_size because the driver
5641 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305642 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305643 status = psa_driver_wrapper_mac_compute(attributes,
5644 pbkdf2->password,
5645 pbkdf2->password_length,
5646 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305647 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648 &mac_output_length);
5649 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305650 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305651 }
5652
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305653 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305654 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305655
5656cleanup:
5657 /* Zeroise buffers to clear sensitive data from memory. */
5658 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5659 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305660}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305661
5662static psa_status_t psa_key_derivation_pbkdf2_read(
5663 psa_pbkdf2_key_derivation_t *pbkdf2,
5664 psa_algorithm_t kdf_alg,
5665 uint8_t *output,
5666 size_t output_length)
5667{
5668 psa_status_t status;
5669 psa_algorithm_t prf_alg;
5670 uint8_t prf_output_length;
5671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5672 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5673 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5674
5675 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5676 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5677 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5678 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305679 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5680 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305681 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305682 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305683 } else {
5684 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305685 }
5686
5687 switch (pbkdf2->state) {
5688 case PSA_PBKDF2_STATE_PASSWORD_SET:
5689 /* Initially we need a new block so bytes_used is equal to block size*/
5690 pbkdf2->bytes_used = prf_output_length;
5691 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5692 break;
5693 case PSA_PBKDF2_STATE_OUTPUT:
5694 break;
5695 default:
5696 return PSA_ERROR_BAD_STATE;
5697 }
5698
5699 while (output_length != 0) {
5700 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5701 if (n > output_length) {
5702 n = (uint8_t) output_length;
5703 }
5704 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5705 output += n;
5706 output_length -= n;
5707 pbkdf2->bytes_used += n;
5708
5709 if (output_length == 0) {
5710 break;
5711 }
5712
5713 /* We need a new block */
5714 pbkdf2->bytes_used = 0;
5715 pbkdf2->block_number++;
5716
5717 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5718 prf_output_length,
5719 &attributes);
5720 if (status != PSA_SUCCESS) {
5721 return status;
5722 }
5723 }
5724
5725 return PSA_SUCCESS;
5726}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305727#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305728
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005729psa_status_t psa_key_derivation_output_bytes(
5730 psa_key_derivation_operation_t *operation,
5731 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005733{
5734 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005740 }
5741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005744 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005746 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747 goto exit;
5748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005751 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005752 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5753 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005754 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759
Przemek Stekiel69c46792022-06-10 12:59:51 +02005760#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5762 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5763 output, output_length);
5764 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005765#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005766#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5767 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5769 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5770 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5771 kdf_alg, output,
5772 output_length);
5773 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005774#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5775 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005776#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005778 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5780 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005781#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305782#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305783 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305784 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5785 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305786 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305787#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005788
Gilles Peskineeab56e42018-07-12 17:12:33 +02005789 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005790 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005792 }
5793
5794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005796 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005797 * This allows us to differentiate between exhausted operations and
5798 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5799 * operations. */
5800 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005804 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806}
5807
David Brown7807bf72021-02-09 16:28:23 -07005808#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005809static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005810{
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 if (data_size >= 8) {
5812 mbedtls_des_key_set_parity(data);
5813 }
5814 if (data_size >= 16) {
5815 mbedtls_des_key_set_parity(data + 8);
5816 }
5817 if (data_size >= 24) {
5818 mbedtls_des_key_set_parity(data + 16);
5819 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005820}
David Brown7807bf72021-02-09 16:28:23 -07005821#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005822
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005823/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 * ECC keys on a Weierstrass elliptic curve require the generation
5825 * of a private key which is an integer
5826 * in the range [1, N - 1], where N is the boundary of the private key domain:
5827 * N is the prime p for Diffie-Hellman, or the order of the
5828 * curve’s base point for ECC.
5829 *
5830 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5831 * This function generates the private key using the following process:
5832 *
5833 * 1. Draw a byte string of length ceiling(m/8) bytes.
5834 * 2. If m is not a multiple of 8, set the most significant
5835 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5836 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5837 * 4. If k > N - 2, discard the result and return to step 1.
5838 * 5. Output k + 1 as the private key.
5839 *
5840 * This method allows compliance to NIST standards, specifically the methods titled
5841 * Key-Pair Generation by Testing Candidates in the following publications:
5842 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5843 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5844 * Diffie-Hellman keys.
5845 *
5846 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5847 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5848 *
5849 * Note: Function allocates memory for *data buffer, so given *data should be
5850 * always NULL.
5851 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005852#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5853#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005854static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005855 psa_key_slot_t *slot,
5856 size_t bits,
5857 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005858 uint8_t **data
5859 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005860{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005861 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862 mbedtls_mpi k;
5863 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005866 size_t m;
5867 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 mbedtls_mpi_init(&k);
5870 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005871
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005878 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005879 goto cleanup;
5880 }
5881
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005887 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005889 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005891 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005892
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005893 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005895
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005896 /* Note: This function is always called with *data == NULL and it
5897 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 *data = mbedtls_calloc(1, m_bytes);
5899 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005900 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901 goto cleanup;
5902 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005905 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005907 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909
5910 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005912 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005913 * (8 * ceiling(m/8) - m) bits of the first byte in
5914 * the string to zero.
5915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005917 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005918 }
5919
5920 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 * big-endian byte string.
5922 */
5923 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005924
5925 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 * Result of comparison is returned. When it indicates error
5927 * then this function is called again.
5928 */
5929 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005930 }
5931
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005932 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5934 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005935cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (ret != 0) {
5937 status = mbedtls_to_psa_error(ret);
5938 }
5939 if (status != PSA_SUCCESS) {
5940 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005941 *data = NULL;
5942 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 mbedtls_mpi_free(&k);
5944 mbedtls_mpi_free(&diff_N_2);
5945 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005946}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005947
5948/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5949 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5950 *
5951 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5952 * draw a 32-byte string and process it as specified in
5953 * Elliptic Curves for Security [RFC7748] §5.
5954 *
5955 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5956 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5957 *
5958 * Note: Function allocates memory for *data buffer, so given *data should be
5959 * always NULL.
5960 */
5961
5962static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5963 size_t bits,
5964 psa_key_derivation_operation_t *operation,
5965 uint8_t **data
5966 )
5967{
5968 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005972 case 255:
5973 output_length = 32;
5974 break;
5975 case 448:
5976 output_length = 56;
5977 break;
5978 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005980 break;
5981 }
5982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (*data == NULL) {
5986 return PSA_ERROR_INSUFFICIENT_MEMORY;
5987 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005992 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005996 case 255:
5997 (*data)[0] &= 248;
5998 (*data)[31] &= 127;
5999 (*data)[31] |= 64;
6000 break;
6001 case 448:
6002 (*data)[0] &= 252;
6003 (*data)[55] |= 128;
6004 break;
6005 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006007 break;
6008 }
6009
6010 return status;
6011}
Valerio Setti86587ab2023-06-27 13:09:30 +02006012#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6013static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6014 psa_key_slot_t *slot, size_t bits,
6015 psa_key_derivation_operation_t *operation, uint8_t **data)
6016{
6017 (void) slot;
6018 (void) bits;
6019 (void) operation;
6020 (void) data;
6021 return PSA_ERROR_NOT_SUPPORTED;
6022}
6023
6024static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6025 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6026{
6027 (void) bits;
6028 (void) operation;
6029 (void) data;
6030 return PSA_ERROR_NOT_SUPPORTED;
6031}
6032#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6033#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006034
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006035static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006036 psa_key_slot_t *slot,
6037 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006039{
6040 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306042 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006043 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006044 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6047 return PSA_ERROR_INVALID_ARGUMENT;
6048 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006050#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006051 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6053 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6054 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006055 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6057 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006058 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 }
6060 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006061 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6063 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006064 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006066 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006067 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006068#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006069 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (key_type_is_raw_bytes(slot->attr.type)) {
6071 if (bits % 8 != 0) {
6072 return PSA_ERROR_INVALID_ARGUMENT;
6073 }
6074 data = mbedtls_calloc(1, bytes);
6075 if (data == NULL) {
6076 return PSA_ERROR_INSUFFICIENT_MEMORY;
6077 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 status = psa_key_derivation_output_bytes(operation, data, bytes);
6080 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 }
David Brown12ca5032021-02-11 11:02:00 -07006083#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6085 psa_des_set_key_parity(data, bytes);
6086 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006087#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 } else {
6089 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006090 }
Ronald Crondd04d422020-11-28 15:14:42 +01006091
Ronald Cronbf33c932020-11-28 18:06:53 +01006092 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006093 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006095 };
6096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6098 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6099 &storage_size);
6100 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 }
Archanad8a83dc2021-06-14 10:04:16 +05306103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 status = psa_allocate_buffer_to_slot(slot, storage_size);
6105 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 }
Archanad8a83dc2021-06-14 10:04:16 +05306108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 status = psa_driver_wrapper_import_key(&attributes,
6110 data, bytes,
6111 slot->key.data,
6112 slot->key.bytes,
6113 &slot->key.bytes, &bits);
6114 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006115 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006117
6118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 mbedtls_free(data);
6120 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006121}
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6124 psa_key_derivation_operation_t *operation,
6125 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006126{
6127 psa_status_t status;
6128 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006129 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006130
Ronald Cron81709fc2020-11-14 12:10:32 +01006131 *key = MBEDTLS_SVC_KEY_ID_INIT;
6132
Gilles Peskine0f84d622019-09-12 19:03:13 +02006133 /* Reject any attempt to create a zero-length key so that we don't
6134 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (psa_get_key_bits(attributes) == 0) {
6136 return PSA_ERROR_INVALID_ARGUMENT;
6137 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (operation->alg == PSA_ALG_NONE) {
6140 return PSA_ERROR_BAD_STATE;
6141 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (!operation->can_output_key) {
6144 return PSA_ERROR_NOT_PERMITTED;
6145 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6148 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006149#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006151 /* Deriving a key in a secure element is not implemented yet. */
6152 status = PSA_ERROR_NOT_SUPPORTED;
6153 }
6154#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if (status == PSA_SUCCESS) {
6156 status = psa_generate_derived_key_internal(slot,
6157 attributes->core.bits,
6158 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006159 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (status == PSA_SUCCESS) {
6161 status = psa_finish_key_creation(slot, driver, key);
6162 }
6163 if (status != PSA_SUCCESS) {
6164 psa_fail_key_creation(slot, driver);
6165 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006168}
6169
Gilles Peskineeab56e42018-07-12 17:12:33 +02006170
6171
6172/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006173/* Key derivation */
6174/****************************************************************/
6175
Steven Cooreman932ffb72021-02-15 12:14:32 +01006176#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006177static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006178{
6179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6181 return 1;
6182 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006183#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006184#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6186 return 1;
6187 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006188#endif
6189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6191 return 1;
6192 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006193#endif
6194#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6196 return 1;
6197 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006198#endif
6199#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6201 return 1;
6202 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006203#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006204#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6206 return 1;
6207 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006208#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306209#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6210 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6211 return 1;
6212 }
6213#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306214#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6215 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6216 return 1;
6217 }
6218#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006220}
6221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006223{
6224 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 psa_status_t status = psa_hash_setup(&operation, alg);
6226 psa_hash_abort(&operation);
6227 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006228}
6229
Gilles Peskine969c5d62019-01-16 15:53:06 +01006230static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006231 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006233{
Janos Follath5fe19732019-06-20 15:09:30 +01006234 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6235 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006237
Gilles Peskine969c5d62019-01-16 15:53:06 +01006238 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 if (!is_kdf_alg_supported(kdf_alg)) {
6240 return PSA_ERROR_NOT_SUPPORTED;
6241 }
John Durkop07cc04a2020-11-16 22:08:34 -08006242
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006243 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306244 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6246 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306247 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6248 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6249 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306250 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306251 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (hash_size == 0) {
6253 return PSA_ERROR_NOT_SUPPORTED;
6254 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006255
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006256 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6257 * we might fail later, which is somewhat unfriendly and potentially
6258 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 psa_status_t status = psa_hash_try_support(hash_alg);
6260 if (status != PSA_SUCCESS) {
6261 return status;
6262 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006263 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6266 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6267 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6268 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006269 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006270#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006271 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6273 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006274 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006276#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6277 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 operation->capacity = 255 * hash_size;
6279 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006280}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006283{
6284#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (alg == PSA_ALG_ECDH) {
6286 return PSA_SUCCESS;
6287 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006288#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006289#if defined(PSA_WANT_ALG_FFDH)
6290 if (alg == PSA_ALG_FFDH) {
6291 return PSA_SUCCESS;
6292 }
6293#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006294 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006296}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006297
6298static int psa_key_derivation_allows_free_form_secret_input(
6299 psa_algorithm_t kdf_alg)
6300{
6301#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6302 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6303 return 0;
6304 }
6305#endif
6306 (void) kdf_alg;
6307 return 1;
6308}
John Durkop07cc04a2020-11-16 22:08:34 -08006309#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6312 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006313{
6314 psa_status_t status;
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 if (operation->alg != 0) {
6317 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006318 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6321 return PSA_ERROR_INVALID_ARGUMENT;
6322 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6323#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6324 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6325 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6326 status = psa_key_agreement_try_support(ka_alg);
6327 if (status != PSA_SUCCESS) {
6328 return status;
6329 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006330 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6331 return PSA_ERROR_INVALID_ARGUMENT;
6332 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6334#else
6335 return PSA_ERROR_NOT_SUPPORTED;
6336#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6337 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6338#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6339 status = psa_key_derivation_setup_kdf(operation, alg);
6340#else
6341 return PSA_ERROR_NOT_SUPPORTED;
6342#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6343 } else {
6344 return PSA_ERROR_INVALID_ARGUMENT;
6345 }
6346
6347 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006348 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 }
6350 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006351}
6352
Przemek Stekiel69c46792022-06-10 12:59:51 +02006353#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006354static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6355 psa_algorithm_t kdf_alg,
6356 psa_key_derivation_step_t step,
6357 const uint8_t *data,
6358 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006359{
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006363 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006364#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6366 return PSA_ERROR_INVALID_ARGUMENT;
6367 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006368#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 if (hkdf->state != HKDF_STATE_INIT) {
6370 return PSA_ERROR_BAD_STATE;
6371 } else {
6372 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6373 hash_alg,
6374 data, data_length);
6375 if (status != PSA_SUCCESS) {
6376 return status;
6377 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006378 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006380 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006381 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006382#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006384 /* We shouldn't be in different state as HKDF_EXPAND only allows
6385 * two inputs: SECRET (this case) and INFO which does not modify
6386 * the state. It could happen only if the hkdf
6387 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 if (hkdf->state != HKDF_STATE_INIT) {
6389 return PSA_ERROR_BAD_STATE;
6390 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006391
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006392 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6394 return PSA_ERROR_INVALID_ARGUMENT;
6395 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 memcpy(hkdf->prk, data, data_length);
6398 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006399#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006400 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006401 /* HKDF: If no salt was provided, use an empty salt.
6402 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006404#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6406 return PSA_ERROR_BAD_STATE;
6407 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006408#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6410 hash_alg,
6411 NULL, 0);
6412 if (status != PSA_SUCCESS) {
6413 return status;
6414 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006415 hkdf->state = HKDF_STATE_STARTED;
6416 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (hkdf->state != HKDF_STATE_STARTED) {
6418 return PSA_ERROR_BAD_STATE;
6419 }
6420 status = psa_mac_update(&hkdf->hmac,
6421 data, data_length);
6422 if (status != PSA_SUCCESS) {
6423 return status;
6424 }
6425 status = psa_mac_sign_finish(&hkdf->hmac,
6426 hkdf->prk,
6427 sizeof(hkdf->prk),
6428 &data_length);
6429 if (status != PSA_SUCCESS) {
6430 return status;
6431 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006432 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006433
Gilles Peskine2b522db2019-04-12 15:11:49 +02006434 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006435 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006436#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006438 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006440 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006442#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006443 {
6444 /* Block 0 is empty, and the next block will be
6445 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006447 }
6448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006450 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006451#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6453 return PSA_ERROR_INVALID_ARGUMENT;
6454 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006456#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6458 hkdf->state == HKDF_STATE_INIT) {
6459 return PSA_ERROR_BAD_STATE;
6460 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006461#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 if (hkdf->state == HKDF_STATE_OUTPUT) {
6463 return PSA_ERROR_BAD_STATE;
6464 }
6465 if (hkdf->info_set) {
6466 return PSA_ERROR_BAD_STATE;
6467 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006468 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (data_length != 0) {
6470 hkdf->info = mbedtls_calloc(1, data_length);
6471 if (hkdf->info == NULL) {
6472 return PSA_ERROR_INSUFFICIENT_MEMORY;
6473 }
6474 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475 }
6476 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006480 }
6481}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006482#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006483
John Durkop07cc04a2020-11-16 22:08:34 -08006484#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6485 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006486static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6487 const uint8_t *data,
6488 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006489{
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6491 return PSA_ERROR_BAD_STATE;
6492 }
Janos Follathf08e2652019-06-13 09:05:41 +01006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (data_length != 0) {
6495 prf->seed = mbedtls_calloc(1, data_length);
6496 if (prf->seed == NULL) {
6497 return PSA_ERROR_INSUFFICIENT_MEMORY;
6498 }
Janos Follathf08e2652019-06-13 09:05:41 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006501 prf->seed_length = data_length;
6502 }
Janos Follathf08e2652019-06-13 09:05:41 +01006503
Gilles Peskine43f958b2020-12-13 14:55:14 +01006504 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006507}
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6510 const uint8_t *data,
6511 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006512{
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6514 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6515 return PSA_ERROR_BAD_STATE;
6516 }
Janos Follath81550542019-06-13 14:26:34 +01006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 if (data_length != 0) {
6519 prf->secret = mbedtls_calloc(1, data_length);
6520 if (prf->secret == NULL) {
6521 return PSA_ERROR_INSUFFICIENT_MEMORY;
6522 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006525 prf->secret_length = data_length;
6526 }
Janos Follath81550542019-06-13 14:26:34 +01006527
Gilles Peskine43f958b2020-12-13 14:55:14 +01006528 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006529
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006531}
6532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6534 const uint8_t *data,
6535 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006536{
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6538 return PSA_ERROR_BAD_STATE;
6539 }
Janos Follath63028dd2019-06-13 09:15:47 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 if (data_length != 0) {
6542 prf->label = mbedtls_calloc(1, data_length);
6543 if (prf->label == NULL) {
6544 return PSA_ERROR_INSUFFICIENT_MEMORY;
6545 }
Janos Follath63028dd2019-06-13 09:15:47 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006548 prf->label_length = data_length;
6549 }
Janos Follath63028dd2019-06-13 09:15:47 +01006550
Gilles Peskine43f958b2020-12-13 14:55:14 +01006551 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006554}
6555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6557 psa_key_derivation_step_t step,
6558 const uint8_t *data,
6559 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006560{
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006562 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006564 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006566 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006568 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006570 }
6571}
John Durkop07cc04a2020-11-16 22:08:34 -08006572#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6573 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6574
6575#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6576static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6577 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006578 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006580{
6581 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6583 4 + data_length + prf->other_secret_length :
6584 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6587 return PSA_ERROR_INVALID_ARGUMENT;
6588 }
John Durkop07cc04a2020-11-16 22:08:34 -08006589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 uint8_t *pms = mbedtls_calloc(1, pms_len);
6591 if (pms == NULL) {
6592 return PSA_ERROR_INSUFFICIENT_MEMORY;
6593 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006594 uint8_t *cur = pms;
6595
6596 /* pure-PSK:
6597 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006598 *
6599 * The premaster secret is formed as follows: if the PSK is N octets
6600 * long, concatenate a uint16 with the value N, N zero octets, a second
6601 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006602 *
6603 * mixed-PSK:
6604 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6605 * follows: concatenate a uint16 with the length of the other secret,
6606 * the other secret itself, uint16 with the length of PSK, and the
6607 * PSK itself.
6608 * For details please check:
6609 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6610 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6611 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006612 */
6613
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6615 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6616 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6617 if (prf->other_secret_length != 0) {
6618 memcpy(cur, prf->other_secret, prf->other_secret_length);
6619 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006620 cur += prf->other_secret_length;
6621 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 } else {
6623 *cur++ = MBEDTLS_BYTE_1(data_length);
6624 *cur++ = MBEDTLS_BYTE_0(data_length);
6625 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006626 cur += data_length;
6627 }
6628
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 *cur++ = MBEDTLS_BYTE_1(data_length);
6630 *cur++ = MBEDTLS_BYTE_0(data_length);
6631 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006632 cur += data_length;
6633
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006634 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006635
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006636 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006638}
Janos Follath6660f0e2019-06-17 08:44:03 +01006639
Przemek Stekiele47201b2022-04-19 13:53:28 +02006640static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6641 psa_tls12_prf_key_derivation_t *prf,
6642 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006644{
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6646 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006648
6649 if (data_length != 0) {
6650 prf->other_secret = mbedtls_calloc(1, data_length);
6651 if (prf->other_secret == NULL) {
6652 return PSA_ERROR_INSUFFICIENT_MEMORY;
6653 }
6654
6655 memcpy(prf->other_secret, data, data_length);
6656 prf->other_secret_length = data_length;
6657 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006658 prf->other_secret_length = 0;
6659 }
6660
6661 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006664}
6665
Janos Follath6660f0e2019-06-17 08:44:03 +01006666static psa_status_t psa_tls12_prf_psk_to_ms_input(
6667 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006668 psa_key_derivation_step_t step,
6669 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006671{
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006673 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 return psa_tls12_prf_psk_to_ms_set_key(prf,
6675 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006676 break;
6677 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6679 data,
6680 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681 break;
6682 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006684 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006685
Przemek Stekiele47201b2022-04-19 13:53:28 +02006686 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006687}
John Durkop07cc04a2020-11-16 22:08:34 -08006688#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006689
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006690#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6691static psa_status_t psa_tls12_ecjpake_to_pms_input(
6692 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006693 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006694 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006696{
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6698 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6699 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006700 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701
6702 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 if (data[0] != 0x04) {
6704 return PSA_ERROR_INVALID_ARGUMENT;
6705 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006706
6707 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006711}
6712#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306713
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306714#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306715static psa_status_t psa_pbkdf2_set_input_cost(
6716 psa_pbkdf2_key_derivation_t *pbkdf2,
6717 psa_key_derivation_step_t step,
6718 uint64_t data)
6719{
6720 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6721 return PSA_ERROR_INVALID_ARGUMENT;
6722 }
6723
6724 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6725 return PSA_ERROR_BAD_STATE;
6726 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306727
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306728 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306729 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306730 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306731
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306732 if (data == 0) {
6733 return PSA_ERROR_INVALID_ARGUMENT;
6734 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306735
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306736 pbkdf2->input_cost = data;
6737 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6738
6739 return PSA_SUCCESS;
6740}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306741
6742static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6743 const uint8_t *data,
6744 size_t data_length)
6745{
Gilles Peskineead17662023-08-20 22:02:51 +02006746 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6747 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6748 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6749 /* Appending to existing salt. No state change. */
6750 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306751 return PSA_ERROR_BAD_STATE;
6752 }
6753
Gilles Peskineca57d782023-07-20 19:08:06 +02006754 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006755 /* Appending an empty string, nothing to do. */
6756 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306757 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306758
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306759 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6760 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306761 return PSA_ERROR_INSUFFICIENT_MEMORY;
6762 }
6763
Gilles Peskineead17662023-08-20 22:02:51 +02006764 if (pbkdf2->salt_length != 0) {
6765 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6766 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306767 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306768 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306769 mbedtls_free(pbkdf2->salt);
6770 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306771 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306772 return PSA_SUCCESS;
6773}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306774
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306775#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306776static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306777 const uint8_t *input,
6778 size_t input_len,
6779 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306780 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306781{
6782 psa_status_t status = PSA_SUCCESS;
6783 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6784 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306785 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786 } else {
6787 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306788 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306789 }
6790 return status;
6791}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306792#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306793
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306794#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306795static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6796 size_t input_len,
6797 uint8_t *output,
6798 size_t *output_len)
6799{
6800 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306801 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306803 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306804 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6805 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6806 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306807 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306808 * mac_size as the driver function sets mac_output_length = mac_size
6809 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306810 status = psa_driver_wrapper_mac_compute(&attributes,
6811 zeros, sizeof(zeros),
6812 PSA_ALG_CMAC, input, input_len,
6813 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306814 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6815 128U,
6816 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306817 output_len);
6818 } else {
6819 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306820 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306821 }
6822 return status;
6823}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306824#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306825
6826static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306827 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306828 const uint8_t *data,
6829 size_t data_length)
6830{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306831 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306832 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6833 return PSA_ERROR_BAD_STATE;
6834 }
6835
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306836#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306837 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6838 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6839 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6840 pbkdf2->password,
6841 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306842 } else
6843#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6844#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6845 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306846 status = psa_pbkdf2_cmac_set_password(data, data_length,
6847 pbkdf2->password,
6848 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306849 } else
6850#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6851 {
6852 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306853 }
6854
6855 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6856
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306857 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306858}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306859
6860static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306861 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306862 psa_key_derivation_step_t step,
6863 const uint8_t *data,
6864 size_t data_length)
6865{
6866 switch (step) {
6867 case PSA_KEY_DERIVATION_INPUT_SALT:
6868 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6869 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306870 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306871 default:
6872 return PSA_ERROR_INVALID_ARGUMENT;
6873 }
6874}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306875#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306876
Gilles Peskineb8965192019-09-24 16:21:10 +02006877/** Check whether the given key type is acceptable for the given
6878 * input step of a key derivation.
6879 *
6880 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6881 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6882 * Both secret and non-secret inputs can alternatively have the type
6883 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6884 * that the input was passed as a buffer rather than via a key object.
6885 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006886static int psa_key_derivation_check_input_type(
6887 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006889{
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006891 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 if (key_type == PSA_KEY_TYPE_DERIVE) {
6893 return PSA_SUCCESS;
6894 }
6895 if (key_type == PSA_KEY_TYPE_NONE) {
6896 return PSA_SUCCESS;
6897 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006898 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006899 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (key_type == PSA_KEY_TYPE_DERIVE) {
6901 return PSA_SUCCESS;
6902 }
6903 if (key_type == PSA_KEY_TYPE_NONE) {
6904 return PSA_SUCCESS;
6905 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006906 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006907 case PSA_KEY_DERIVATION_INPUT_LABEL:
6908 case PSA_KEY_DERIVATION_INPUT_SALT:
6909 case PSA_KEY_DERIVATION_INPUT_INFO:
6910 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6912 return PSA_SUCCESS;
6913 }
6914 if (key_type == PSA_KEY_TYPE_NONE) {
6915 return PSA_SUCCESS;
6916 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006917 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306918 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6919 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6920 return PSA_SUCCESS;
6921 }
6922 if (key_type == PSA_KEY_TYPE_DERIVE) {
6923 return PSA_SUCCESS;
6924 }
6925 if (key_type == PSA_KEY_TYPE_NONE) {
6926 return PSA_SUCCESS;
6927 }
6928 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006929 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006931}
6932
Janos Follathb80a94e2019-06-12 15:54:46 +01006933static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006934 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006935 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006936 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006937 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006939{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006940 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 status = psa_key_derivation_check_input_type(step, key_type);
6944 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006947
Przemek Stekiel69c46792022-06-10 12:59:51 +02006948#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6950 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6951 step, data, data_length);
6952 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006953#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006954#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6956 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6957 step, data, data_length);
6958 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006959#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6960#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6962 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6963 step, data, data_length);
6964 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006965#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006966#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006968 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6970 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006971#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306972#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306973 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306974 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6975 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306976 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306977#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006978 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006979 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006980 (void) data;
6981 (void) data_length;
6982 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006984 }
6985
Gilles Peskine224b0d62019-09-23 18:13:17 +02006986exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 if (status != PSA_SUCCESS) {
6988 psa_key_derivation_abort(operation);
6989 }
6990 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006991}
6992
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306993static psa_status_t psa_key_derivation_input_integer_internal(
6994 psa_key_derivation_operation_t *operation,
6995 psa_key_derivation_step_t step,
6996 uint64_t value)
6997{
6998 psa_status_t status;
6999 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7000
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307001#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307002 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307003 status = psa_pbkdf2_set_input_cost(
7004 &operation->ctx.pbkdf2, step, value);
7005 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307006#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307007 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307008 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307009 (void) value;
7010 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307011 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307012 }
7013
7014 if (status != PSA_SUCCESS) {
7015 psa_key_derivation_abort(operation);
7016 }
7017 return status;
7018}
7019
Janos Follath51f4a0f2019-06-14 11:35:55 +01007020psa_status_t psa_key_derivation_input_bytes(
7021 psa_key_derivation_operation_t *operation,
7022 psa_key_derivation_step_t step,
7023 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007025{
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 return psa_key_derivation_input_internal(operation, step,
7027 PSA_KEY_TYPE_NONE,
7028 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007029}
7030
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307031psa_status_t psa_key_derivation_input_integer(
7032 psa_key_derivation_operation_t *operation,
7033 psa_key_derivation_step_t step,
7034 uint64_t value)
7035{
7036 return psa_key_derivation_input_integer_internal(operation, step, value);
7037}
7038
Janos Follath51f4a0f2019-06-14 11:35:55 +01007039psa_status_t psa_key_derivation_input_key(
7040 psa_key_derivation_operation_t *operation,
7041 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007043{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007045 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007046 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007047
Ronald Cron5c522922020-11-14 16:35:34 +01007048 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7050 if (status != PSA_SUCCESS) {
7051 psa_key_derivation_abort(operation);
7052 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007053 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007054
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307055 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7056 * permission to output to a key object. */
7057 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7058 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007059 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 status = psa_key_derivation_input_internal(operation,
7063 step, slot->attr.type,
7064 slot->key.data,
7065 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007066
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007070}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007071
7072
7073
7074/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007075/* Key agreement */
7076/****************************************************************/
7077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7079 const uint8_t *key_buffer,
7080 size_t key_buffer_size,
7081 psa_algorithm_t alg,
7082 const uint8_t *peer_key,
7083 size_t peer_key_length,
7084 uint8_t *shared_secret,
7085 size_t shared_secret_size,
7086 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007087{
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007089#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007090 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7092 key_buffer_size, alg,
7093 peer_key, peer_key_length,
7094 shared_secret,
7095 shared_secret_size,
7096 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007097#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007098
7099#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7100 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007101 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007102 peer_key,
7103 peer_key_length,
7104 key_buffer,
7105 key_buffer_size,
7106 shared_secret,
7107 shared_secret_size,
7108 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007109#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7110
Gilles Peskine01d718c2018-09-18 12:01:02 +02007111 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007112 (void) attributes;
7113 (void) key_buffer;
7114 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007115 (void) peer_key;
7116 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007117 (void) shared_secret;
7118 (void) shared_secret_size;
7119 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007121 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007122}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007123
Aditya Deshpande17845b82022-10-13 17:21:01 +01007124/** Internal function for raw key agreement
7125 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007126 * to the driver's implementation if a driver is present.
7127 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007128 * (psa_key_agreement_raw_builtin).
7129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7131 psa_key_slot_t *private_key,
7132 const uint8_t *peer_key,
7133 size_t peer_key_length,
7134 uint8_t *shared_secret,
7135 size_t shared_secret_size,
7136 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007137{
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7139 return PSA_ERROR_NOT_SUPPORTED;
7140 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007141
7142 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007144 };
7145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 return psa_driver_wrapper_key_agreement(&attributes,
7147 private_key->key.data,
7148 private_key->key.bytes, alg,
7149 peer_key, peer_key_length,
7150 shared_secret,
7151 shared_secret_size,
7152 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007153}
7154
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007155/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007156 * to potentially free embedded data structures and wipe confidential data.
7157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007158static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7159 psa_key_derivation_step_t step,
7160 psa_key_slot_t *private_key,
7161 const uint8_t *peer_key,
7162 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007163{
7164 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007165 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007166 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007168
7169 /* Step 1: run the secret agreement algorithm to generate the shared
7170 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 status = psa_key_agreement_raw_internal(ka_alg,
7172 private_key,
7173 peer_key, peer_key_length,
7174 shared_secret,
7175 sizeof(shared_secret),
7176 &shared_secret_length);
7177 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007180
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007181 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007182 * the shared secret. A shared secret is permitted wherever a key
7183 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 status = psa_key_derivation_input_internal(operation, step,
7185 PSA_KEY_TYPE_DERIVE,
7186 shared_secret,
7187 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7190 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007191}
7192
Gilles Peskine449bd832023-01-11 14:50:10 +01007193psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7194 psa_key_derivation_step_t step,
7195 mbedtls_svc_key_id_t private_key,
7196 const uint8_t *peer_key,
7197 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007200 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007201 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7204 return PSA_ERROR_INVALID_ARGUMENT;
7205 }
Ronald Cron5c522922020-11-14 16:35:34 +01007206 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7208 if (status != PSA_SUCCESS) {
7209 return status;
7210 }
7211 status = psa_key_agreement_internal(operation, step,
7212 slot,
7213 peer_key, peer_key_length);
7214 if (status != PSA_SUCCESS) {
7215 psa_key_derivation_abort(operation);
7216 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007217 /* If a private key has been added as SECRET, we allow the derived
7218 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007220 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007222 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007227}
7228
Gilles Peskine449bd832023-01-11 14:50:10 +01007229psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7230 mbedtls_svc_key_id_t private_key,
7231 const uint8_t *peer_key,
7232 size_t peer_key_length,
7233 uint8_t *output,
7234 size_t output_size,
7235 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007236{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007238 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007239 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007240 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007243 status = PSA_ERROR_INVALID_ARGUMENT;
7244 goto exit;
7245 }
Ronald Cron5c522922020-11-14 16:35:34 +01007246 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7248 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007249 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251
Gilles Peskine3e561302022-04-14 00:17:15 +02007252 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7253 * for the output size. The PSA specification only guarantees that this
7254 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7255 * but it might be nice to allow smaller buffers if the output fits.
7256 * At the time of writing this comment, with only ECDH implemented,
7257 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7258 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7259 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007260 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7262 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007263 status = PSA_ERROR_BUFFER_TOO_SMALL;
7264 goto exit;
7265 }
7266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 status = psa_key_agreement_raw_internal(alg, slot,
7268 peer_key, peer_key_length,
7269 output, output_size,
7270 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007271
7272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007274 /* If an error happens and is not handled properly, the output
7275 * may be used as a key to protect sensitive data. Arrange for such
7276 * a key to be random, which is likely to result in decryption or
7277 * verification errors. This is better than filling the buffer with
7278 * some constant data such as zeros, which would result in the data
7279 * being protected with a reproducible, easily knowable key.
7280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007282 *output_length = output_size;
7283 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007284
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007288}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007289
7290
Gilles Peskine30524eb2020-11-13 17:02:26 +01007291
Gilles Peskine86a440b2018-11-12 18:39:40 +01007292/****************************************************************/
7293/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007294/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007295
Gilles Peskine672a7712023-04-28 21:00:28 +02007296#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7297#include "entropy_poll.h"
7298#endif
7299
Gilles Peskine30524eb2020-11-13 17:02:26 +01007300/** Initialize the PSA random generator.
7301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007302static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007303{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007304#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007306#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7307
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308 /* Set default configuration if
7309 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 }
7313 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007314 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007316
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7319 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7320 /* The PSA entropy injection feature depends on using NV seed as an entropy
7321 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 mbedtls_entropy_add_source(&rng->entropy,
7323 mbedtls_nv_seed_poll, NULL,
7324 MBEDTLS_ENTROPY_BLOCK_SIZE,
7325 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326#endif
7327
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007329#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007330}
7331
7332/** Deinitialize the PSA random generator.
7333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007334static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007335{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007336#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007338#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7340 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007341#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007342}
7343
7344/** Seed the PSA random generator.
7345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007346static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007347{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7349 /* Do nothing: the external RNG seeds itself. */
7350 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007352#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007353 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7355 drbg_seed, sizeof(drbg_seed) - 1);
7356 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007357#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007358}
7359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360psa_status_t psa_generate_random(uint8_t *output,
7361 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007362{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007363 GUARD_MODULE_INITIALIZED;
7364
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007365#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7366
7367 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7369 output, output_size,
7370 &output_length);
7371 if (status != PSA_SUCCESS) {
7372 return status;
7373 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007374 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007375 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 if (output_length != output_size) {
7377 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7378 }
7379 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007380
7381#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7382
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007384 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7386 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7387 output_size);
7388 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7389 output, request_size);
7390 if (ret != 0) {
7391 return mbedtls_to_psa_error(ret);
7392 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007393 output_size -= request_size;
7394 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007397#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007398}
7399
Gilles Peskine8814fc42020-12-14 15:33:44 +01007400/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007401 *
7402 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7403 * `psa_generate_random(...)`. The state parameter is ignored since the
7404 * PSA API doesn't support passing an explicit state.
7405 *
7406 * In the non-external case, psa_generate_random() calls an
7407 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7408 * and semantics as mbedtls_psa_get_random(). As an optimization,
7409 * instead of doing this back-and-forth between the PSA API and the
7410 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7411 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007413#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7414int mbedtls_psa_get_random(void *p_rng,
7415 unsigned char *output,
7416 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007417{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007418 /* This function takes a pointer to the RNG state because that's what
7419 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7420 * its own state internally and doesn't let the caller access that state.
7421 * So we just ignore the state parameter, and in practice we'll pass
7422 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007423 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 psa_status_t status = psa_generate_random(output, output_size);
7425 if (status == PSA_SUCCESS) {
7426 return 0;
7427 } else {
7428 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7429 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007430}
7431#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7432
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007433#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007434psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7435 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007436{
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (global_data.initialized) {
7438 return PSA_ERROR_NOT_PERMITTED;
7439 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7442 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7443 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7444 return PSA_ERROR_INVALID_ARGUMENT;
7445 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007446
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007448}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007449#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007450
Ronald Cron3772afe2021-02-08 16:10:05 +01007451/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007452 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007453 * \param type The key type
7454 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007455 *
7456 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007457 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007458 * \retval #PSA_ERROR_INVALID_ARGUMENT
7459 * The size in bits of the key is not valid.
7460 * \retval #PSA_ERROR_NOT_SUPPORTED
7461 * The type and/or the size in bits of the key or the combination of
7462 * the two is not supported.
7463 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007464static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007466{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 if (key_type_is_raw_bytes(type)) {
7470 status = psa_validate_unstructured_key_bit_size(type, bits);
7471 if (status != PSA_SUCCESS) {
7472 return status;
7473 }
7474 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007475#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7477 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7478 return PSA_ERROR_NOT_SUPPORTED;
7479 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007480 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007481 return PSA_ERROR_NOT_SUPPORTED;
7482 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007483
Ronald Cron01b2aba2020-10-05 09:42:02 +02007484 /* Accept only byte-aligned keys, for the same reasons as
7485 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (bits % 8 != 0) {
7487 return PSA_ERROR_NOT_SUPPORTED;
7488 }
7489 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007490#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007491
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007492#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007494 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 return PSA_SUCCESS;
7496 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007497#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007498
Valerio Settia55f0422023-07-10 15:34:41 +02007499#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007500 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007501 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007502 return PSA_ERROR_NOT_SUPPORTED;
7503 }
7504 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007505#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007506 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007508 }
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007511}
7512
Ronald Cron55ed0592020-10-05 10:30:40 +02007513psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007514 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516{
7517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007518 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 if ((attributes->domain_parameters == NULL) &&
7521 (attributes->domain_parameters_size != 0)) {
7522 return PSA_ERROR_INVALID_ARGUMENT;
7523 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 if (key_type_is_raw_bytes(type)) {
7526 status = psa_generate_random(key_buffer, key_buffer_size);
7527 if (status != PSA_SUCCESS) {
7528 return status;
7529 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007530
David Brown12ca5032021-02-11 11:02:00 -07007531#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (type == PSA_KEY_TYPE_DES) {
7533 psa_des_set_key_parity(key_buffer, key_buffer_size);
7534 }
David Brown8107e312021-02-12 08:08:46 -07007535#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007537
Valerio Setti76df8c12023-07-11 14:11:28 +02007538#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7540 return mbedtls_psa_rsa_generate_key(attributes,
7541 key_buffer,
7542 key_buffer_size,
7543 key_buffer_length);
7544 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007545#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007546
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007547#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7549 return mbedtls_psa_ecp_generate_key(attributes,
7550 key_buffer,
7551 key_buffer_size,
7552 key_buffer_length);
7553 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007554#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007555
Valerio Settia55f0422023-07-10 15:34:41 +02007556#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007557 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7558 return mbedtls_psa_ffdh_generate_key(attributes,
7559 key_buffer,
7560 key_buffer_size,
7561 key_buffer_length);
7562 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007563#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007564 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 (void) key_buffer_length;
7566 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007567 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007570}
Darryl Green0c6575a2018-11-07 16:05:30 +00007571
Gilles Peskine449bd832023-01-11 14:50:10 +01007572psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7573 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007574{
7575 psa_status_t status;
7576 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007577 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007578 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007579
Ronald Cron81709fc2020-11-14 12:10:32 +01007580 *key = MBEDTLS_SVC_KEY_ID_INIT;
7581
Gilles Peskine0f84d622019-09-12 19:03:13 +02007582 /* Reject any attempt to create a zero-length key so that we don't
7583 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 if (psa_get_key_bits(attributes) == 0) {
7585 return PSA_ERROR_INVALID_ARGUMENT;
7586 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007587
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007588 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7590 return PSA_ERROR_INVALID_ARGUMENT;
7591 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7594 &slot, &driver);
7595 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 }
Gilles Peskine11792082019-08-06 18:36:36 +02007598
Ronald Cron977c2472020-10-13 08:32:21 +02007599 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307600 * storage ( thus not in the case of generating a key in a secure element
7601 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7602 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (slot->key.data == NULL) {
7604 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7605 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007606 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 attributes->core.type, attributes->core.bits);
7608 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007611
7612 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 attributes->core.type,
7614 attributes->core.bits);
7615 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007616 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 attributes, &key_buffer_size);
7618 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Ronald Cron977c2472020-10-13 08:32:21 +02007621 }
Ronald Cron977c2472020-10-13 08:32:21 +02007622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7624 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 }
Ronald Cron977c2472020-10-13 08:32:21 +02007627 }
7628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 status = psa_driver_wrapper_generate_key(attributes,
7630 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007631
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 if (status != PSA_SUCCESS) {
7633 psa_remove_key_data_from_memory(slot);
7634 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007635
Gilles Peskine11792082019-08-06 18:36:36 +02007636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 if (status == PSA_SUCCESS) {
7638 status = psa_finish_key_creation(slot, driver, key);
7639 }
7640 if (status != PSA_SUCCESS) {
7641 psa_fail_key_creation(slot, driver);
7642 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007643
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007645}
7646
Gilles Peskinee59236f2018-01-27 23:32:46 +01007647/****************************************************************/
7648/* Module setup */
7649/****************************************************************/
7650
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007651#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007652psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 void (* entropy_init)(mbedtls_entropy_context *ctx),
7654 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007655{
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7657 return PSA_ERROR_BAD_STATE;
7658 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007659 global_data.rng.entropy_init = entropy_init;
7660 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007662}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007663#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007666{
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 psa_wipe_all_key_slots();
7668 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7669 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007670 }
7671 /* Wipe all remaining data, including configuration.
7672 * In particular, this sets all state indicator to the value
7673 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007675
7676 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007678}
7679
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007680#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7681/** Recover a transaction that was interrupted by a power failure.
7682 *
7683 * This function is called during initialization, before psa_crypto_init()
7684 * returns. If this function returns a failure status, the initialization
7685 * fails.
7686 */
7687static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007689{
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007691 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7692 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 /* TODO - fall through to the failure case until this
7694 * is implemented.
7695 * https://github.com/ARMmbed/mbed-crypto/issues/218
7696 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007697 default:
7698 /* We found an unsupported transaction in the storage.
7699 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007701 }
7702}
7703#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7704
Gilles Peskine449bd832023-01-11 14:50:10 +01007705psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007706{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007707 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007708
Gilles Peskinec6b69072018-11-20 21:42:52 +01007709 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 if (global_data.initialized != 0) {
7711 return PSA_SUCCESS;
7712 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007713
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007714 /* Init drivers */
7715 status = psa_driver_wrapper_init();
7716 if (status != PSA_SUCCESS) {
7717 goto exit;
7718 }
7719 global_data.drivers_initialized = 1;
7720
Gilles Peskine30524eb2020-11-13 17:02:26 +01007721 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007723 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 status = mbedtls_psa_random_seed(&global_data.rng);
7725 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007728 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 status = psa_initialize_key_slots();
7731 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007732 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007734
Gilles Peskine4b734222019-07-24 15:56:31 +02007735#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 status = psa_crypto_load_transaction();
7737 if (status == PSA_SUCCESS) {
7738 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7739 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 }
7742 status = psa_crypto_stop_transaction();
7743 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007744 /* There's no transaction to complete. It's all good. */
7745 status = PSA_SUCCESS;
7746 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007747#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007748
Gilles Peskinec6b69072018-11-20 21:42:52 +01007749 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007750 global_data.initialized = 1;
7751
7752exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 if (status != PSA_SUCCESS) {
7754 mbedtls_psa_crypto_free();
7755 }
7756 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007757}
7758
Yanray Wangffc3c482023-07-11 12:01:04 +08007759#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007760psa_status_t psa_crypto_driver_pake_get_password_len(
7761 const psa_crypto_driver_pake_inputs_t *inputs,
7762 size_t *password_len)
7763{
7764 if (inputs->password_len == 0) {
7765 return PSA_ERROR_BAD_STATE;
7766 }
7767
7768 *password_len = inputs->password_len;
7769
7770 return PSA_SUCCESS;
7771}
7772
7773psa_status_t psa_crypto_driver_pake_get_password(
7774 const psa_crypto_driver_pake_inputs_t *inputs,
7775 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7776{
7777 if (inputs->password_len == 0) {
7778 return PSA_ERROR_BAD_STATE;
7779 }
7780
7781 if (buffer_size < inputs->password_len) {
7782 return PSA_ERROR_BUFFER_TOO_SMALL;
7783 }
7784
7785 memcpy(buffer, inputs->password, inputs->password_len);
7786 *buffer_length = inputs->password_len;
7787
7788 return PSA_SUCCESS;
7789}
7790
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007791psa_status_t psa_crypto_driver_pake_get_user_len(
7792 const psa_crypto_driver_pake_inputs_t *inputs,
7793 size_t *user_len)
7794{
7795 if (inputs->user_len == 0) {
7796 return PSA_ERROR_BAD_STATE;
7797 }
7798
7799 *user_len = inputs->user_len;
7800
7801 return PSA_SUCCESS;
7802}
7803
7804psa_status_t psa_crypto_driver_pake_get_user(
7805 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007806 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007807{
7808 if (inputs->user_len == 0) {
7809 return PSA_ERROR_BAD_STATE;
7810 }
7811
Przemek Stekielc0e62502023-03-14 11:49:36 +01007812 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007813 return PSA_ERROR_BUFFER_TOO_SMALL;
7814 }
7815
Przemek Stekielc0e62502023-03-14 11:49:36 +01007816 memcpy(user_id, inputs->user, inputs->user_len);
7817 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007818
7819 return PSA_SUCCESS;
7820}
7821
7822psa_status_t psa_crypto_driver_pake_get_peer_len(
7823 const psa_crypto_driver_pake_inputs_t *inputs,
7824 size_t *peer_len)
7825{
7826 if (inputs->peer_len == 0) {
7827 return PSA_ERROR_BAD_STATE;
7828 }
7829
7830 *peer_len = inputs->peer_len;
7831
7832 return PSA_SUCCESS;
7833}
7834
7835psa_status_t psa_crypto_driver_pake_get_peer(
7836 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007837 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007838{
7839 if (inputs->peer_len == 0) {
7840 return PSA_ERROR_BAD_STATE;
7841 }
7842
Przemek Stekielc0e62502023-03-14 11:49:36 +01007843 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007844 return PSA_ERROR_BUFFER_TOO_SMALL;
7845 }
7846
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 memcpy(peer_id, inputs->peer, inputs->peer_len);
7848 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007849
7850 return PSA_SUCCESS;
7851}
7852
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007853psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7854 const psa_crypto_driver_pake_inputs_t *inputs,
7855 psa_pake_cipher_suite_t *cipher_suite)
7856{
7857 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7858 return PSA_ERROR_BAD_STATE;
7859 }
7860
7861 *cipher_suite = inputs->cipher_suite;
7862
7863 return PSA_SUCCESS;
7864}
7865
Neil Armstronga7d08c32022-06-01 18:21:20 +02007866psa_status_t psa_pake_setup(
7867 psa_pake_operation_t *operation,
7868 const psa_pake_cipher_suite_t *cipher_suite)
7869{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007871
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007872 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873 status = PSA_ERROR_BAD_STATE;
7874 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007875 }
7876
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007877 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007878 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007879 status = PSA_ERROR_INVALID_ARGUMENT;
7880 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007881 }
7882
Przemek Stekiel51eac532022-12-07 11:04:51 +01007883 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7884
Przemek Stekiele12ed362022-12-21 12:54:46 +01007885 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007886 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7887 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 operation->data.inputs.cipher_suite = *cipher_suite;
7889
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007890#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007891 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007892 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007893 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007894
David Horstmann16f01512023-06-14 17:21:07 +01007895 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007896 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007897 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007898#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007899 {
7900 status = PSA_ERROR_NOT_SUPPORTED;
7901 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 }
7903
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007904 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7905
Przemek Stekiel51eac532022-12-07 11:04:51 +01007906 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007907exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908 psa_pake_abort(operation);
7909 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007910}
7911
7912psa_status_t psa_pake_set_password_key(
7913 psa_pake_operation_t *operation,
7914 mbedtls_svc_key_id_t password)
7915{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007917 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7918 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007919 psa_key_attributes_t attributes;
7920 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007921
Przemek Stekiel51eac532022-12-07 11:04:51 +01007922 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007923 status = PSA_ERROR_BAD_STATE;
7924 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007925 }
7926
Przemek Stekiel6c764412022-11-22 14:05:12 +01007927 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7928 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007929 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007930 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007931 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932 }
7933
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007934 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007935 .core = slot->attr
7936 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007937
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007938 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007939
Przemek Stekiel51eac532022-12-07 11:04:51 +01007940 if (type != PSA_KEY_TYPE_PASSWORD &&
7941 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7942 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007943 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007944 }
7945
Przemek Stekiel51eac532022-12-07 11:04:51 +01007946 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7947 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007948 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 }
7951
7952 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7953 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007954 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956 if (status != PSA_SUCCESS) {
7957 psa_pake_abort(operation);
7958 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007960 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007961}
7962
7963psa_status_t psa_pake_set_user(
7964 psa_pake_operation_t *operation,
7965 const uint8_t *user_id,
7966 size_t user_id_len)
7967{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007968 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007969
7970 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007971 status = PSA_ERROR_BAD_STATE;
7972 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007973 }
7974
Przemek Stekiel51eac532022-12-07 11:04:51 +01007975 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 status = PSA_ERROR_INVALID_ARGUMENT;
7977 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007978 }
7979
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007980 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
7983 }
7984
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007985 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7986 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007987 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7988 goto exit;
7989 }
7990
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007991 memcpy(operation->data.inputs.user, user_id, user_id_len);
7992 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007993
7994 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007996 psa_pake_abort(operation);
7997 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007998}
7999
8000psa_status_t psa_pake_set_peer(
8001 psa_pake_operation_t *operation,
8002 const uint8_t *peer_id,
8003 size_t peer_id_len)
8004{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008006
8007 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008008 status = PSA_ERROR_BAD_STATE;
8009 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008010 }
8011
Przemek Stekiel51eac532022-12-07 11:04:51 +01008012 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008013 status = PSA_ERROR_INVALID_ARGUMENT;
8014 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008015 }
8016
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008017 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008018 status = PSA_ERROR_BAD_STATE;
8019 goto exit;
8020 }
8021
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008022 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8023 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008024 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8025 goto exit;
8026 }
8027
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008028 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8029 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008030
8031 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008032exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008033 psa_pake_abort(operation);
8034 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008035}
8036
8037psa_status_t psa_pake_set_role(
8038 psa_pake_operation_t *operation,
8039 psa_pake_role_t role)
8040{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042
Przemek Stekiel51eac532022-12-07 11:04:51 +01008043 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008044 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008045 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008046 }
8047
Przemek Stekiel09104b82023-03-06 14:11:51 +01008048 switch (operation->alg) {
8049#if defined(PSA_WANT_ALG_JPAKE)
8050 case PSA_ALG_JPAKE:
8051 if (role == PSA_PAKE_ROLE_NONE) {
8052 return PSA_SUCCESS;
8053 }
8054 status = PSA_ERROR_INVALID_ARGUMENT;
8055 break;
8056#endif
8057 default:
8058 (void) role;
8059 status = PSA_ERROR_NOT_SUPPORTED;
8060 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008061 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008062exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008063 psa_pake_abort(operation);
8064 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008065}
8066
David Horstmanne7f21e62023-05-12 18:17:21 +01008067/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008068#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008069static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008070 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008071{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008072 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008073 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008074 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008075
David Horstmann024e5c52023-06-14 15:48:21 +01008076 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008077 is_x1 = (stage->outputs < 1);
8078 } else {
8079 is_x1 = (stage->inputs < 1);
8080 }
8081
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082 key_share_step = is_x1 ?
8083 PSA_JPAKE_X1_STEP_KEY_SHARE :
8084 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008085 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008086 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8087 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8088 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8089 } else {
8090 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008091 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008092 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008093}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008094#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008095
Przemek Stekiel2797d372022-12-22 11:19:22 +01008096static psa_status_t psa_pake_complete_inputs(
8097 psa_pake_operation_t *operation)
8098{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008100 /* Create copy of the inputs on stack as inputs share memory
8101 with the driver context which will be setup by the driver. */
8102 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008103
Przemek Stekielfde11282023-03-13 16:06:09 +01008104 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008105 return PSA_ERROR_BAD_STATE;
8106 }
8107
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008108 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008109 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8110 return PSA_ERROR_BAD_STATE;
8111 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008112 }
8113
Przemek Stekiel18620a32023-01-17 16:34:52 +01008114 /* Clear driver context */
8115 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8116
8117 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008118
8119 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008120 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008121
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008122 /* User and peer are translated to role. */
8123 mbedtls_free(inputs.user);
8124 mbedtls_free(inputs.peer);
8125
Przemek Stekiel2797d372022-12-22 11:19:22 +01008126 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008127#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008129 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008130 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008131#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008132 {
8133 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008134 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 return status;
8137}
8138
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008139#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008140static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008141 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008142 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008143 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008145 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8146 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8147 step != PSA_PAKE_STEP_ZK_PROOF) {
8148 return PSA_ERROR_INVALID_ARGUMENT;
8149 }
8150
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008151 psa_jpake_computation_stage_t *computation_stage =
8152 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153
David Horstmann5da95602023-06-08 15:37:12 +01008154 if (computation_stage->round != PSA_JPAKE_FIRST &&
8155 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008156 return PSA_ERROR_BAD_STATE;
8157 }
8158
David Horstmanne7f21e62023-05-12 18:17:21 +01008159 /* Check that the step we are given is the one we were expecting */
8160 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008161 return PSA_ERROR_BAD_STATE;
8162 }
8163
David Horstmanne7f21e62023-05-12 18:17:21 +01008164 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8165 computation_stage->inputs == 0 &&
8166 computation_stage->outputs == 0) {
8167 /* Start of the round, so function decides whether we are inputting
8168 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008169 computation_stage->io_mode = io_mode;
8170 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008171 /* Middle of the round so the mode we are in must match the function
8172 * called by the user */
8173 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008174 }
8175
8176 return PSA_SUCCESS;
8177}
8178
David Horstmanne7f21e62023-05-12 18:17:21 +01008179static psa_status_t psa_jpake_epilogue(
8180 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008181 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182{
David Horstmanne7f21e62023-05-12 18:17:21 +01008183 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008184 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185
David Horstmanne7f21e62023-05-12 18:17:21 +01008186 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8187 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008188 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008189 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008190 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008191 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008192 }
8193 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008194 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008195 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008196 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008197 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008198 }
8199 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008200 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8201 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 /* End of a round, move to the next round */
8203 stage->inputs = 0;
8204 stage->outputs = 0;
8205 stage->round++;
8206 }
8207 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008208 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008210 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 return PSA_SUCCESS;
8212}
David Horstmanne7f21e62023-05-12 18:17:21 +01008213
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008214#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215
Neil Armstronga7d08c32022-06-01 18:21:20 +02008216psa_status_t psa_pake_output(
8217 psa_pake_operation_t *operation,
8218 psa_pake_step_t step,
8219 uint8_t *output,
8220 size_t output_size,
8221 size_t *output_length)
8222{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008224 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008225 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008226
8227 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008228 status = psa_pake_complete_inputs(operation);
8229 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008230 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008231 }
8232 }
8233
8234 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008235 status = PSA_ERROR_BAD_STATE;
8236 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008237 }
8238
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008239 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 status = PSA_ERROR_INVALID_ARGUMENT;
8241 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008242 }
8243
Przemek Stekiele12ed362022-12-21 12:54:46 +01008244 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008245#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008246 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008247 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008248 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008250 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008251 driver_step = convert_jpake_computation_stage_to_driver_step(
8252 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008254#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008256 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008257 status = PSA_ERROR_NOT_SUPPORTED;
8258 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 }
8260
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008261 status = psa_driver_wrapper_pake_output(operation, driver_step,
8262 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263
8264 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 }
8267
8268 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008269#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008271 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008273 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 }
8275 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008276#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 status = PSA_ERROR_NOT_SUPPORTED;
8279 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 }
8281
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008282 return PSA_SUCCESS;
8283exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008284 psa_pake_abort(operation);
8285 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008286}
8287
8288psa_status_t psa_pake_input(
8289 psa_pake_operation_t *operation,
8290 psa_pake_step_t step,
8291 const uint8_t *input,
8292 size_t input_length)
8293{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008295 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008296 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8297 operation->primitive,
8298 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008299
8300 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008301 status = psa_pake_complete_inputs(operation);
8302 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008303 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008304 }
8305 }
8306
8307 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008308 status = PSA_ERROR_BAD_STATE;
8309 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008310 }
8311
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008312 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313 status = PSA_ERROR_INVALID_ARGUMENT;
8314 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008315 }
8316
Przemek Stekiele12ed362022-12-21 12:54:46 +01008317 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008318#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008319 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008320 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008322 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008323 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008324 driver_step = convert_jpake_computation_stage_to_driver_step(
8325 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008326 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008327#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008329 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008330 status = PSA_ERROR_NOT_SUPPORTED;
8331 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008332 }
8333
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008334 status = psa_driver_wrapper_pake_input(operation, driver_step,
8335 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336
8337 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008338 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 }
8340
8341 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008342#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008344 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008346 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 }
8348 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008349#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008351 status = PSA_ERROR_NOT_SUPPORTED;
8352 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 }
8354
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008355 return PSA_SUCCESS;
8356exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008357 psa_pake_abort(operation);
8358 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008359}
8360
8361psa_status_t psa_pake_get_implicit_key(
8362 psa_pake_operation_t *operation,
8363 psa_key_derivation_operation_t *output)
8364{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008366 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008367 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008368 size_t shared_key_len = 0;
8369
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008370 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008371 status = PSA_ERROR_BAD_STATE;
8372 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008373 }
8374
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008375#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008376 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008377 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008378 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008379 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008380 status = PSA_ERROR_BAD_STATE;
8381 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008382 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008383 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008384#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008385 {
8386 status = PSA_ERROR_NOT_SUPPORTED;
8387 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008388 }
8389
Przemek Stekiel0c781802022-11-29 14:53:13 +01008390 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8391 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008392 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008393 &shared_key_len);
8394
8395 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008396 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008397 }
8398
8399 status = psa_key_derivation_input_bytes(output,
8400 PSA_KEY_DERIVATION_INPUT_SECRET,
8401 shared_key,
8402 shared_key_len);
8403
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008404 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008405exit:
8406 abort_status = psa_pake_abort(operation);
8407 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008408}
8409
8410psa_status_t psa_pake_abort(
8411 psa_pake_operation_t *operation)
8412{
Przemek Stekield69dca92023-01-31 19:59:20 +01008413 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008414
Przemek Stekield69dca92023-01-31 19:59:20 +01008415 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008416 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008417 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008418
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008419 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8420 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008421 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008422 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008423 }
8424 if (operation->data.inputs.user != NULL) {
8425 mbedtls_free(operation->data.inputs.user);
8426 }
8427 if (operation->data.inputs.peer != NULL) {
8428 mbedtls_free(operation->data.inputs.peer);
8429 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008430 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008431 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008432
Przemek Stekield69dca92023-01-31 19:59:20 +01008433 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008434}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008435#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008436
David Horstmannfde97392023-10-30 20:29:43 +00008437
David Horstmann1b7279a2023-11-15 15:18:30 +00008438/** Copy from an input buffer to a local copy.
8439 *
8440 * \param[in] input Pointer to input buffer.
8441 * \param[in] input_len Length of the input buffer.
8442 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8443 * \param[out] input_copy_len Length of the local copy buffer.
8444 * \return #PSA_SUCCESS, if the buffer was successfully
8445 * copied.
8446 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8447 * copy is too small to hold contents of the
8448 * input buffer.
8449 */
8450MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008451psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8452 uint8_t *input_copy, size_t input_copy_len)
8453{
8454 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008455 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008456 }
8457
David Horstmann372b8bb2023-11-24 16:21:04 +00008458#if defined(MBEDTLS_TEST_HOOKS)
8459 MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len);
8460#endif
8461
David Horstmann777e7412023-11-15 17:33:47 +00008462 if (input_len > 0) {
8463 memcpy(input_copy, input, input_len);
8464 }
David Horstmannfde97392023-10-30 20:29:43 +00008465
David Horstmann372b8bb2023-11-24 16:21:04 +00008466#if defined(MBEDTLS_TEST_HOOKS)
8467 MBEDTLS_TEST_MEMORY_POISON(input, input_len);
8468#endif
8469
David Horstmannfde97392023-10-30 20:29:43 +00008470 return PSA_SUCCESS;
8471}
8472
David Horstmann1b7279a2023-11-15 15:18:30 +00008473/** Copy from a local output buffer into a user-supplied one.
8474 *
8475 * \param[in] output_copy Pointer to a local buffer containing the output.
8476 * \param[in] output_copy_len Length of the local buffer.
8477 * \param[out] output Pointer to user-supplied output buffer.
8478 * \param[out] output_len Length of the user-supplied output buffer.
8479 * \return #PSA_SUCCESS, if the buffer was successfully
8480 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008481 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008482 * user-supplied output buffer is too small to
8483 * hold the contents of the local buffer.
8484 */
8485MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008486psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8487 uint8_t *output, size_t output_len)
8488{
8489 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008490 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008491 }
David Horstmann777e7412023-11-15 17:33:47 +00008492
David Horstmann372b8bb2023-11-24 16:21:04 +00008493#if defined(MBEDTLS_TEST_HOOKS)
8494 MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len);
8495#endif
8496
David Horstmann777e7412023-11-15 17:33:47 +00008497 if (output_copy_len > 0) {
8498 memcpy(output, output_copy, output_copy_len);
8499 }
8500
David Horstmann372b8bb2023-11-24 16:21:04 +00008501#if defined(MBEDTLS_TEST_HOOKS)
8502 MBEDTLS_TEST_MEMORY_POISON(output, output_len);
8503#endif
8504
David Horstmann8978f5c2023-10-30 20:37:12 +00008505 return PSA_SUCCESS;
8506}
8507
David Horstmannf1734052023-11-20 17:15:32 +00008508psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8509 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008510{
8511 psa_status_t status;
8512
David Horstmann9db14482023-11-23 15:50:37 +00008513 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008514
David Horstmannc5cc1c32023-11-15 18:11:26 +00008515 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008516 return PSA_SUCCESS;
8517 }
8518
David Horstmannf1734052023-11-20 17:15:32 +00008519 local_input->buffer = mbedtls_calloc(input_len, 1);
8520 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008521 /* Since we dealt with the zero-length case above, we know that
8522 * a NULL return value means a failure of allocation. */
8523 return PSA_ERROR_INSUFFICIENT_MEMORY;
8524 }
David Horstmannf1734052023-11-20 17:15:32 +00008525 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008526
David Horstmannf1734052023-11-20 17:15:32 +00008527 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008528
8529 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008530 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008531 if (status != PSA_SUCCESS) {
8532 goto error;
8533 }
8534
8535 return PSA_SUCCESS;
8536
8537error:
David Horstmannf1734052023-11-20 17:15:32 +00008538 mbedtls_free(local_input->buffer);
8539 local_input->buffer = NULL;
8540 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008541 return status;
8542}
8543
David Horstmannf1734052023-11-20 17:15:32 +00008544void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008545{
David Horstmannf1734052023-11-20 17:15:32 +00008546 mbedtls_free(local_input->buffer);
8547 local_input->buffer = NULL;
8548 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008549}
8550
David Horstmann89875a42023-11-20 12:54:09 +00008551psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8552 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008553{
David Horstmann9db14482023-11-23 15:50:37 +00008554 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008555
David Horstmannc5cc1c32023-11-15 18:11:26 +00008556 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008557 return PSA_SUCCESS;
8558 }
David Horstmann89875a42023-11-20 12:54:09 +00008559 local_output->buffer = mbedtls_calloc(output_len, 1);
8560 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008561 /* Since we dealt with the zero-length case above, we know that
8562 * a NULL return value means a failure of allocation. */
8563 return PSA_ERROR_INSUFFICIENT_MEMORY;
8564 }
David Horstmann89875a42023-11-20 12:54:09 +00008565 local_output->length = output_len;
8566 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008567
8568 return PSA_SUCCESS;
8569}
8570
David Horstmann89875a42023-11-20 12:54:09 +00008571psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008572{
David Horstmannc335a4e2023-11-15 15:57:32 +00008573 psa_status_t status;
8574
David Horstmann89875a42023-11-20 12:54:09 +00008575 if (local_output->buffer == NULL) {
8576 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008577 return PSA_SUCCESS;
8578 }
David Horstmann89875a42023-11-20 12:54:09 +00008579 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008580 /* We have an internal copy but nothing to copy back to. */
8581 return PSA_ERROR_CORRUPTION_DETECTED;
8582 }
8583
David Horstmann89875a42023-11-20 12:54:09 +00008584 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8585 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008586 if (status != PSA_SUCCESS) {
8587 return status;
8588 }
David Horstmann9467ea32023-11-08 17:44:18 +00008589
David Horstmann89875a42023-11-20 12:54:09 +00008590 mbedtls_free(local_output->buffer);
8591 local_output->buffer = NULL;
8592 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008593
8594 return PSA_SUCCESS;
8595}
8596
Gilles Peskinee59236f2018-01-27 23:32:46 +01008597#endif /* MBEDTLS_PSA_CRYPTO_C */