blob: 36c5f7566557fd02c1cd0903db85ecb6132369f8 [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
David Horstmannc6977b42023-11-27 17:43:05 +00004349 psa_crypto_local_input_t local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
4350 status = psa_crypto_local_input_alloc(input, input_length, &local_input);
4351 if (status != PSA_SUCCESS) {
4352 goto exit;
4353 }
4354 input = local_input.buffer;
4355
4356 psa_crypto_local_output_t local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
4357 status = psa_crypto_local_output_alloc(output, output_size, &local_output);
4358 if (status != PSA_SUCCESS) {
4359 goto exit;
4360 }
4361 output = local_output.buffer;
4362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004364 status = PSA_ERROR_INVALID_ARGUMENT;
4365 goto exit;
4366 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4369 PSA_KEY_USAGE_ENCRYPT,
4370 alg);
4371 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004374
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004375 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004377 };
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4380 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004381 status = PSA_ERROR_GENERIC_ERROR;
4382 goto exit;
4383 }
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (default_iv_length > 0) {
4386 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004387 status = PSA_ERROR_BUFFER_TOO_SMALL;
4388 goto exit;
4389 }
4390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 status = psa_generate_random(local_iv, default_iv_length);
4392 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004395 }
4396
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004397 status = psa_driver_wrapper_cipher_encrypt(
4398 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004399 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004400 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004402
4403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 unlock_status = psa_unlock_key_slot(slot);
4405 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004406 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004407 }
Ronald Cron23919522021-07-08 19:00:07 +02004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (status == PSA_SUCCESS) {
4410 if (default_iv_length > 0) {
4411 memcpy(output, local_iv, default_iv_length);
4412 }
4413 *output_length += default_iv_length;
4414 } else {
4415 *output_length = 0;
4416 }
4417
David Horstmannc6977b42023-11-27 17:43:05 +00004418 psa_crypto_local_input_free(&local_input);
4419 psa_crypto_local_output_free(&local_output);
4420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004422}
4423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4425 psa_algorithm_t alg,
4426 const uint8_t *input,
4427 size_t input_length,
4428 uint8_t *output,
4429 size_t output_size,
4430 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004431{
4432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004433 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004434 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004435 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004438 status = PSA_ERROR_INVALID_ARGUMENT;
4439 goto exit;
4440 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4443 PSA_KEY_USAGE_DECRYPT,
4444 alg);
4445 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004448
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004449 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004451 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4454 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004455 status = PSA_ERROR_INVALID_ARGUMENT;
4456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004458 status = PSA_ERROR_INVALID_ARGUMENT;
4459 goto exit;
4460 }
4461
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004462 status = psa_driver_wrapper_cipher_decrypt(
4463 &attributes, slot->key.data, slot->key.bytes,
4464 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004466
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 unlock_status = psa_unlock_key_slot(slot);
4469 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004470 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004474 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 }
Ronald Cron23919522021-07-08 19:00:07 +02004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004478}
4479
4480
mohammad16035955c982018-04-26 00:53:03 +03004481/****************************************************************/
4482/* AEAD */
4483/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004484
Paul Elliottbaff51c2021-09-28 17:44:45 +01004485/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004486static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004487{
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004489}
4490
4491/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004492static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4493 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004494{
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004498#if defined(PSA_WANT_ALG_GCM)
4499 case PSA_ALG_GCM:
4500 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 * arbitrarily large nonces. Please note that we do not generally
4502 * recommend the usage of nonces of greater length than
4503 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4504 * size, which can then lead to collisions if you encrypt a very
4505 * large number of messages.*/
4506 if (nonce_length != 0) {
4507 return PSA_SUCCESS;
4508 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004510#endif /* PSA_WANT_ALG_GCM */
4511#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004512 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (nonce_length >= 7 && nonce_length <= 13) {
4514 return PSA_SUCCESS;
4515 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004516 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004517#endif /* PSA_WANT_ALG_CCM */
4518#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004519 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 if (nonce_length == 12) {
4521 return PSA_SUCCESS;
4522 } else if (nonce_length == 8) {
4523 return PSA_ERROR_NOT_SUPPORTED;
4524 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004525 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004526#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004527 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004528 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004530 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004533}
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004536{
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4538 return PSA_ERROR_INVALID_ARGUMENT;
4539 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004542}
4543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4545 psa_algorithm_t alg,
4546 const uint8_t *nonce,
4547 size_t nonce_length,
4548 const uint8_t *additional_data,
4549 size_t additional_data_length,
4550 const uint8_t *plaintext,
4551 size_t plaintext_length,
4552 uint8_t *ciphertext,
4553 size_t ciphertext_size,
4554 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004555{
Ronald Cron215633c2021-03-16 17:15:37 +01004556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4557 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004558
mohammad1603f08a5502018-06-03 15:05:47 +03004559 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 status = psa_aead_check_algorithm(alg);
4562 if (status != PSA_SUCCESS) {
4563 return status;
4564 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004565
Ronald Cron9a986162021-03-26 12:40:07 +01004566 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4568 if (status != PSA_SUCCESS) {
4569 return status;
4570 }
mohammad16035955c982018-04-26 00:53:03 +03004571
Ronald Cron215633c2021-03-16 17:15:37 +01004572 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004574 };
mohammad16035955c982018-04-26 00:53:03 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 status = psa_aead_check_nonce_length(alg, nonce_length);
4577 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004578 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004580
Ronald Cronde822812021-03-17 16:08:20 +01004581 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004582 &attributes, slot->key.data, slot->key.bytes,
4583 alg,
4584 nonce, nonce_length,
4585 additional_data, additional_data_length,
4586 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4590 memset(ciphertext, 0, ciphertext_size);
4591 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004592
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004597}
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4600 psa_algorithm_t alg,
4601 const uint8_t *nonce,
4602 size_t nonce_length,
4603 const uint8_t *additional_data,
4604 size_t additional_data_length,
4605 const uint8_t *ciphertext,
4606 size_t ciphertext_length,
4607 uint8_t *plaintext,
4608 size_t plaintext_size,
4609 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004610{
Ronald Cron215633c2021-03-16 17:15:37 +01004611 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4612 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004613
Gilles Peskineee652a32018-06-01 19:23:52 +02004614 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 status = psa_aead_check_algorithm(alg);
4617 if (status != PSA_SUCCESS) {
4618 return status;
4619 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004620
Ronald Cron9a986162021-03-26 12:40:07 +01004621 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4623 if (status != PSA_SUCCESS) {
4624 return status;
4625 }
mohammad16035955c982018-04-26 00:53:03 +03004626
Ronald Cron215633c2021-03-16 17:15:37 +01004627 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004629 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 status = psa_aead_check_nonce_length(alg, nonce_length);
4632 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004635
Ronald Cronde822812021-03-17 16:08:20 +01004636 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004637 &attributes, slot->key.data, slot->key.bytes,
4638 alg,
4639 nonce, nonce_length,
4640 additional_data, additional_data_length,
4641 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 if (status != PSA_SUCCESS && plaintext_size != 0) {
4645 memset(plaintext, 0, plaintext_size);
4646 }
mohammad160360a64d02018-06-03 17:20:42 +03004647
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 return status;
mohammad16035955c982018-04-26 00:53:03 +03004652}
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4655{
4656 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004659#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4663 return PSA_ERROR_INVALID_ARGUMENT;
4664 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004665 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004666#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667
Przemek Stekiel86679c72022-10-06 17:06:56 +02004668#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4672 return PSA_ERROR_INVALID_ARGUMENT;
4673 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004675#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676
Przemek Stekiel86679c72022-10-06 17:06:56 +02004677#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004679 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if (tag_len != 16) {
4681 return PSA_ERROR_INVALID_ARGUMENT;
4682 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004683 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004684#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004685
4686 default:
4687 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004691}
4692
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004693/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004694static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4695 int is_encrypt,
4696 mbedtls_svc_key_id_t key,
4697 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004698{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004699 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4700 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4701 psa_key_slot_t *slot = NULL;
4702 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004703 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 status = psa_aead_check_algorithm(alg);
4706 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004711 status = PSA_ERROR_BAD_STATE;
4712 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004713 }
4714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (operation->nonce_set || operation->lengths_set ||
4716 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717 status = PSA_ERROR_BAD_STATE;
4718 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004719 }
4720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004722 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004724 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4728 alg);
4729 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004730 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004732
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004733 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004734 .core = slot->attr
4735 };
4736
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (is_encrypt) {
4742 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4743 &attributes,
4744 slot->key.data,
4745 slot->key.bytes,
4746 alg);
4747 } else {
4748 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4749 &attributes,
4750 slot->key.data,
4751 slot->key.bytes,
4752 alg);
4753 }
4754 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759
4760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004764 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004766 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 } else {
4768 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004769 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004772}
4773
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004775psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4776 mbedtls_svc_key_id_t key,
4777 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778{
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004780}
4781
4782/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004783psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4784 mbedtls_svc_key_id_t key,
4785 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786{
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788}
4789
4790/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004791psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4792 uint8_t *nonce,
4793 size_t nonce_size,
4794 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004795{
4796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004797 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004798 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004799
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004800 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004803 status = PSA_ERROR_BAD_STATE;
4804 goto exit;
4805 }
4806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004808 status = PSA_ERROR_BAD_STATE;
4809 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810 }
4811
Paul Elliotte193ea82021-10-01 13:00:16 +01004812 /* For CCM, this size may not be correct according to the PSA
4813 * specification. The PSA Crypto 1.0.1 specification states:
4814 *
4815 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4816 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4817 *
4818 * However this restriction that L has to be the smallest integer is not
4819 * applied in practice, and it is not implementable here since the
4820 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4822 operation->alg);
4823 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004824 status = PSA_ERROR_BUFFER_TOO_SMALL;
4825 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826 }
4827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 status = psa_generate_random(local_nonce, required_nonce_size);
4829 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004830 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834
Paul Elliott39dc6b82021-05-11 19:16:09 +01004835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (status == PSA_SUCCESS) {
4837 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004838 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 } else {
4840 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004841 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004844}
4845
4846/* Set the nonce for a multipart authenticated encryption or decryption
4847 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004848psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4849 const uint8_t *nonce,
4850 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004851{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004852 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004855 status = PSA_ERROR_BAD_STATE;
4856 goto exit;
4857 }
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004860 status = PSA_ERROR_BAD_STATE;
4861 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004862 }
4863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4865 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004866 status = PSA_ERROR_INVALID_ARGUMENT;
4867 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004868 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4871 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004872
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004875 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 } else {
4877 psa_aead_abort(operation);
4878 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004881}
4882
4883/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004884psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4885 size_t ad_length,
4886 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004887{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004888 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004891 status = PSA_ERROR_BAD_STATE;
4892 goto exit;
4893 }
4894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (operation->lengths_set || operation->ad_started ||
4896 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897 status = PSA_ERROR_BAD_STATE;
4898 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004899 }
4900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004902#if defined(PSA_WANT_ALG_GCM)
4903 case PSA_ALG_GCM:
4904 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 * bits. Without the guard this code will generate warnings on 32bit
4906 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004907#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (((uint64_t) ad_length) >> 61 != 0 ||
4909 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 status = PSA_ERROR_INVALID_ARGUMENT;
4911 goto exit;
4912 }
Paul Elliott325d3742021-09-27 17:56:28 +01004913#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004914 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004915#endif /* PSA_WANT_ALG_GCM */
4916#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004917 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004919 status = PSA_ERROR_INVALID_ARGUMENT;
4920 goto exit;
4921 }
4922 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004923#endif /* PSA_WANT_ALG_CCM */
4924#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004925 case PSA_ALG_CHACHA20_POLY1305:
4926 /* No length restrictions for ChaChaPoly. */
4927 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004928#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004929 default:
4930 break;
4931 }
Paul Elliott325d3742021-09-27 17:56:28 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4934 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004935
Paul Elliott39dc6b82021-05-11 19:16:09 +01004936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004938 operation->ad_remaining = ad_length;
4939 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 } else {
4942 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004943 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004946}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004947
4948/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4950 const uint8_t *input,
4951 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004952{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004956 status = PSA_ERROR_BAD_STATE;
4957 goto exit;
4958 }
4959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004961 status = PSA_ERROR_BAD_STATE;
4962 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004963 }
4964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (operation->lengths_set) {
4966 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004967 status = PSA_ERROR_INVALID_ARGUMENT;
4968 goto exit;
4969 }
4970
4971 operation->ad_remaining -= input_length;
4972 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004973#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004975 status = PSA_ERROR_BAD_STATE;
4976 goto exit;
4977 }
4978#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 status = psa_driver_wrapper_aead_update_ad(operation, input,
4981 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004982
Paul Elliott39dc6b82021-05-11 19:16:09 +01004983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004985 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 } else {
4987 psa_aead_abort(operation);
4988 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991}
4992
4993/* Encrypt or decrypt a message fragment in an active multipart AEAD
4994 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004995psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4996 const uint8_t *input,
4997 size_t input_length,
4998 uint8_t *output,
4999 size_t output_size,
5000 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005001{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005003
5004 *output_length = 0;
5005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005007 status = PSA_ERROR_BAD_STATE;
5008 goto exit;
5009 }
5010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005012 status = PSA_ERROR_BAD_STATE;
5013 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005014 }
5015
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005017 /* Additional data length was supplied, but not all the additional
5018 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005020 status = PSA_ERROR_INVALID_ARGUMENT;
5021 goto exit;
5022 }
5023
5024 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005026 status = PSA_ERROR_INVALID_ARGUMENT;
5027 goto exit;
5028 }
5029
5030 operation->body_remaining -= input_length;
5031 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005032#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005034 status = PSA_ERROR_BAD_STATE;
5035 goto exit;
5036 }
5037#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5040 output, output_size,
5041 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005042
Paul Elliott39dc6b82021-05-11 19:16:09 +01005043exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005045 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 } else {
5047 psa_aead_abort(operation);
5048 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005051}
5052
Gilles Peskine449bd832023-01-11 14:50:10 +01005053static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054{
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (operation->id == 0 || !operation->nonce_set) {
5056 return PSA_ERROR_BAD_STATE;
5057 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5060 operation->body_remaining != 0)) {
5061 return PSA_ERROR_INVALID_ARGUMENT;
5062 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005065}
5066
Paul Elliott302ff6b2021-04-20 18:10:30 +01005067/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5069 uint8_t *ciphertext,
5070 size_t ciphertext_size,
5071 size_t *ciphertext_length,
5072 uint8_t *tag,
5073 size_t tag_size,
5074 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5077
Paul Elliott302ff6b2021-04-20 18:10:30 +01005078 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005079 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 status = psa_aead_final_checks(operation);
5082 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005087 status = PSA_ERROR_BAD_STATE;
5088 goto exit;
5089 }
5090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5092 ciphertext_size,
5093 ciphertext_length,
5094 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005095
Paul Elliott39dc6b82021-05-11 19:16:09 +01005096exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005097
5098
Paul Elliottf47b0952021-05-21 18:02:33 +01005099 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005100 * the zero tag size, make sure the tag is set to something implausible.
5101 * Even if the operation succeeds, make sure we clear the rest of the
5102 * buffer to prevent potential leakage of anything previously placed in
5103 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005104 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005109}
5110
5111/* Finish authenticating and decrypting a message in a multipart AEAD
5112 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005113psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5114 uint8_t *plaintext,
5115 size_t plaintext_size,
5116 size_t *plaintext_length,
5117 const uint8_t *tag,
5118 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005119{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5121
Paul Elliott302ff6b2021-04-20 18:10:30 +01005122 *plaintext_length = 0;
5123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 status = psa_aead_final_checks(operation);
5125 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005130 status = PSA_ERROR_BAD_STATE;
5131 goto exit;
5132 }
5133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5135 plaintext_size,
5136 plaintext_length,
5137 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005138
5139exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143}
5144
5145/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005147{
5148 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151 /* The object has (apparently) been initialized but it is not (yet)
5152 * in use. It's ok to call abort on such an object, and there's
5153 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005155 }
5156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005162}
5163
Gilles Peskinee59236f2018-01-27 23:32:46 +01005164/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005165/* Generators */
5166/****************************************************************/
5167
Przemek Stekiel69c46792022-06-10 12:59:51 +02005168#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005169 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005170 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305171 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305172 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005173#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005174#endif /* At least one builtin KDF */
5175
Przemek Stekiel69c46792022-06-10 12:59:51 +02005176#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005177 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5178 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5179static psa_status_t psa_key_derivation_start_hmac(
5180 psa_mac_operation_t *operation,
5181 psa_algorithm_t hash_alg,
5182 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005184{
5185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5188 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5189 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005190
Steven Cooreman72f736a2021-05-07 14:14:37 +02005191 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 status = psa_driver_wrapper_mac_sign_setup(operation,
5195 &attributes,
5196 hmac_key, hmac_key_length,
5197 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 psa_reset_key_attributes(&attributes);
5200 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005201}
5202#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005203
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204#define HKDF_STATE_INIT 0 /* no input yet */
5205#define HKDF_STATE_STARTED 1 /* got salt */
5206#define HKDF_STATE_KEYED 2 /* got key */
5207#define HKDF_STATE_OUTPUT 3 /* output started */
5208
Gilles Peskinecbe66502019-05-16 16:59:18 +02005209static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211{
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5213 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5214 } else {
5215 return operation->alg;
5216 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005217}
5218
Gilles Peskine449bd832023-01-11 14:50:10 +01005219psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220{
5221 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5223 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224 /* The object has (apparently) been initialized but it is not
5225 * in use. It's ok to call abort on such an object, and there's
5226 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005228#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5230 mbedtls_free(operation->ctx.hkdf.info);
5231 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5232 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005233#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005234#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5235 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5237 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5238 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5239 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005240 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005242 }
5243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005245 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005247 }
5248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005250 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005252 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005253#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005255 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005257 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005258#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005259 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005260
5261 /* We leave the fields Ai and output_block to be erased safely by the
5262 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005264#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5265 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005266#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5268 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5269 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5270 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005271#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305272#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305273 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305274 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005275 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305276 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305277 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305278
5279 status = PSA_SUCCESS;
5280 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305281#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282 {
5283 status = PSA_ERROR_BAD_STATE;
5284 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 mbedtls_platform_zeroize(operation, sizeof(*operation));
5286 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287}
5288
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005291{
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005295 }
5296
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005299}
5300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5302 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005303{
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (operation->alg == 0) {
5305 return PSA_ERROR_BAD_STATE;
5306 }
5307 if (capacity > operation->capacity) {
5308 return PSA_ERROR_INVALID_ARGUMENT;
5309 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005312}
5313
Przemek Stekiel69c46792022-06-10 12:59:51 +02005314#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005315/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5317 psa_algorithm_t kdf_alg,
5318 uint8_t *output,
5319 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320{
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5322 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005323 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005325#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005327#else
5328 const uint8_t last_block = 0xff;
5329#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 if (hkdf->state < HKDF_STATE_KEYED ||
5332 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005333#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005335#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 )) {
5337 return PSA_ERROR_BAD_STATE;
5338 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005339 hkdf->state = HKDF_STATE_OUTPUT;
5340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005342 /* Copy what remains of the current block */
5343 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 }
5347 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348 output += n;
5349 output_length -= n;
5350 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005352 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005354 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005355 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005356 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005357 * object was corrupted or if this function is called directly
5358 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 if (hkdf->block_number == last_block) {
5360 return PSA_ERROR_BAD_STATE;
5361 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005362
5363 /* We need a new block */
5364 ++hkdf->block_number;
5365 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5368 hash_alg,
5369 hkdf->prk,
5370 hash_length);
5371 if (status != PSA_SUCCESS) {
5372 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005373 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005374
5375 if (hkdf->block_number != 1) {
5376 status = psa_mac_update(&hkdf->hmac,
5377 hkdf->output_block,
5378 hash_length);
5379 if (status != PSA_SUCCESS) {
5380 return status;
5381 }
5382 }
5383 status = psa_mac_update(&hkdf->hmac,
5384 hkdf->info,
5385 hkdf->info_length);
5386 if (status != PSA_SUCCESS) {
5387 return status;
5388 }
5389 status = psa_mac_update(&hkdf->hmac,
5390 &hkdf->block_number, 1);
5391 if (status != PSA_SUCCESS) {
5392 return status;
5393 }
5394 status = psa_mac_sign_finish(&hkdf->hmac,
5395 hkdf->output_block,
5396 sizeof(hkdf->output_block),
5397 &hmac_output_length);
5398 if (status != PSA_SUCCESS) {
5399 return status;
5400 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005401 }
5402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005404}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005405#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005406
John Durkop07cc04a2020-11-16 22:08:34 -08005407#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5408 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005409static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5410 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005412{
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5414 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005415 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5416 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005417 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005418
Janos Follath7742fee2019-06-17 12:58:10 +01005419 /* We can't be wanting more output after block 0xff, otherwise
5420 * the capacity check in psa_key_derivation_output_bytes() would have
5421 * prevented this call. It could happen only if the operation
5422 * object was corrupted or if this function is called directly
5423 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (tls12_prf->block_number == 0xff) {
5425 return PSA_ERROR_CORRUPTION_DETECTED;
5426 }
Janos Follath7742fee2019-06-17 12:58:10 +01005427
5428 /* We need a new block */
5429 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005430 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005431
5432 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5433 *
5434 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5435 *
5436 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5437 * HMAC_hash(secret, A(2) + seed) +
5438 * HMAC_hash(secret, A(3) + seed) + ...
5439 *
5440 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005441 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005442 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005443 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005444 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005445 * is currently extracted as `output_block` and where i is
5446 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005447 */
5448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 status = psa_key_derivation_start_hmac(&hmac,
5450 hash_alg,
5451 tls12_prf->secret,
5452 tls12_prf->secret_length);
5453 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005454 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005456
5457 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5460 * the variable seed and in this instance means it in the context of the
5461 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 status = psa_mac_update(&hmac,
5463 tls12_prf->label,
5464 tls12_prf->label_length);
5465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
5468 status = psa_mac_update(&hmac,
5469 tls12_prf->seed,
5470 tls12_prf->seed_length);
5471 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005472 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 }
5474 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005475 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5477 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005478 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005480 }
5481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 status = psa_mac_sign_finish(&hmac,
5483 tls12_prf->Ai, hash_length,
5484 &hmac_output_length);
5485 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005486 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 }
5488 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005491
5492 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 status = psa_key_derivation_start_hmac(&hmac,
5494 hash_alg,
5495 tls12_prf->secret,
5496 tls12_prf->secret_length);
5497 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005498 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 }
5500 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5501 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005502 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 }
5504 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5505 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 }
5508 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5509 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005510 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 }
5512 status = psa_mac_sign_finish(&hmac,
5513 tls12_prf->output_block, hash_length,
5514 &hmac_output_length);
5515 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005516 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005518
Janos Follath7742fee2019-06-17 12:58:10 +01005519
5520cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 cleanup_status = psa_mac_abort(&hmac);
5522 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005523 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005527}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005528
Janos Follath844eb0e2019-06-19 12:10:49 +01005529static psa_status_t psa_key_derivation_tls12_prf_read(
5530 psa_tls12_prf_key_derivation_t *tls12_prf,
5531 psa_algorithm_t alg,
5532 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005534{
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5536 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005537 psa_status_t status;
5538 uint8_t offset, length;
5539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005541 case PSA_TLS12_PRF_STATE_LABEL_SET:
5542 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5543 break;
5544 case PSA_TLS12_PRF_STATE_OUTPUT:
5545 break;
5546 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005548 }
5549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005551 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 if (tls12_prf->left_in_block == 0) {
5553 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5554 alg);
5555 if (status != PSA_SUCCESS) {
5556 return status;
5557 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005558
5559 continue;
5560 }
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005563 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005565 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005567
5568 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005570 output += length;
5571 output_length -= length;
5572 tls12_prf->left_in_block -= length;
5573 }
5574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005576}
John Durkop07cc04a2020-11-16 22:08:34 -08005577#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5578 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005579
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005580#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5581static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5582 psa_tls12_ecjpake_to_pms_t *ecjpake,
5583 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005585{
5586 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005587 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 if (output_length != 32) {
5590 return PSA_ERROR_INVALID_ARGUMENT;
5591 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5594 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5595 &output_size);
5596 if (status != PSA_SUCCESS) {
5597 return status;
5598 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 if (output_size != output_length) {
5601 return PSA_ERROR_GENERIC_ERROR;
5602 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005605}
5606#endif
5607
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305608#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305609static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5610 psa_pbkdf2_key_derivation_t *pbkdf2,
5611 psa_algorithm_t prf_alg,
5612 uint8_t prf_output_length,
5613 psa_key_attributes_t *attributes)
5614{
5615 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305616 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305619 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305620 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305621 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305622
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305623 mac_operation.is_sign = 1;
5624 mac_operation.mac_size = prf_output_length;
5625 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305626
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305627 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5628 attributes,
5629 pbkdf2->password,
5630 pbkdf2->password_length,
5631 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305632 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305633 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305634 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5636 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305637 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305638 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305639 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305640 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305641 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305642 }
5643 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5644 &mac_output_length);
5645 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305646 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305647 }
5648
5649 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305650 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305651 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305652 }
5653
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305654 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305655
5656 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305657 /* We are passing prf_output_length as mac_size because the driver
5658 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305659 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305660 status = psa_driver_wrapper_mac_compute(attributes,
5661 pbkdf2->password,
5662 pbkdf2->password_length,
5663 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305664 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305665 &mac_output_length);
5666 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305667 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305668 }
5669
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305670 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305671 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305672
5673cleanup:
5674 /* Zeroise buffers to clear sensitive data from memory. */
5675 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5676 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305677}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305678
5679static psa_status_t psa_key_derivation_pbkdf2_read(
5680 psa_pbkdf2_key_derivation_t *pbkdf2,
5681 psa_algorithm_t kdf_alg,
5682 uint8_t *output,
5683 size_t output_length)
5684{
5685 psa_status_t status;
5686 psa_algorithm_t prf_alg;
5687 uint8_t prf_output_length;
5688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5689 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5690 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5691
5692 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5693 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5694 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5695 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305696 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5697 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305698 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305699 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305700 } else {
5701 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305702 }
5703
5704 switch (pbkdf2->state) {
5705 case PSA_PBKDF2_STATE_PASSWORD_SET:
5706 /* Initially we need a new block so bytes_used is equal to block size*/
5707 pbkdf2->bytes_used = prf_output_length;
5708 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5709 break;
5710 case PSA_PBKDF2_STATE_OUTPUT:
5711 break;
5712 default:
5713 return PSA_ERROR_BAD_STATE;
5714 }
5715
5716 while (output_length != 0) {
5717 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5718 if (n > output_length) {
5719 n = (uint8_t) output_length;
5720 }
5721 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5722 output += n;
5723 output_length -= n;
5724 pbkdf2->bytes_used += n;
5725
5726 if (output_length == 0) {
5727 break;
5728 }
5729
5730 /* We need a new block */
5731 pbkdf2->bytes_used = 0;
5732 pbkdf2->block_number++;
5733
5734 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5735 prf_output_length,
5736 &attributes);
5737 if (status != PSA_SUCCESS) {
5738 return status;
5739 }
5740 }
5741
5742 return PSA_SUCCESS;
5743}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305744#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305745
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005746psa_status_t psa_key_derivation_output_bytes(
5747 psa_key_derivation_operation_t *operation,
5748 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750{
5751 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005757 }
5758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005761 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005763 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764 goto exit;
5765 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005767 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005768 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005769 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5770 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005771 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005772 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005774 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005775 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005776
Przemek Stekiel69c46792022-06-10 12:59:51 +02005777#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5779 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5780 output, output_length);
5781 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005782#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005783#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5784 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5786 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5787 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5788 kdf_alg, output,
5789 output_length);
5790 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005791#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5792 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005793#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005795 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5797 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005798#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305799#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305800 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305801 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5802 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305803 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305804#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005805
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005807 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005809 }
5810
5811exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005813 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005814 * This allows us to differentiate between exhausted operations and
5815 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5816 * operations. */
5817 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005819 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005823}
5824
David Brown7807bf72021-02-09 16:28:23 -07005825#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005826static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005827{
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (data_size >= 8) {
5829 mbedtls_des_key_set_parity(data);
5830 }
5831 if (data_size >= 16) {
5832 mbedtls_des_key_set_parity(data + 8);
5833 }
5834 if (data_size >= 24) {
5835 mbedtls_des_key_set_parity(data + 16);
5836 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005837}
David Brown7807bf72021-02-09 16:28:23 -07005838#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005839
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005840/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 * ECC keys on a Weierstrass elliptic curve require the generation
5842 * of a private key which is an integer
5843 * in the range [1, N - 1], where N is the boundary of the private key domain:
5844 * N is the prime p for Diffie-Hellman, or the order of the
5845 * curve’s base point for ECC.
5846 *
5847 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5848 * This function generates the private key using the following process:
5849 *
5850 * 1. Draw a byte string of length ceiling(m/8) bytes.
5851 * 2. If m is not a multiple of 8, set the most significant
5852 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5853 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5854 * 4. If k > N - 2, discard the result and return to step 1.
5855 * 5. Output k + 1 as the private key.
5856 *
5857 * This method allows compliance to NIST standards, specifically the methods titled
5858 * Key-Pair Generation by Testing Candidates in the following publications:
5859 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5860 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5861 * Diffie-Hellman keys.
5862 *
5863 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5864 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5865 *
5866 * Note: Function allocates memory for *data buffer, so given *data should be
5867 * always NULL.
5868 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005869#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5870#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005871static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 psa_key_slot_t *slot,
5873 size_t bits,
5874 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005875 uint8_t **data
5876 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005877{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005878 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879 mbedtls_mpi k;
5880 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005883 size_t m;
5884 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 mbedtls_mpi_init(&k);
5887 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005888
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005889 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005891 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005895 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005896 goto cleanup;
5897 }
5898
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005899 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005904 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005905 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005906 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005907
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005908 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005913 /* Note: This function is always called with *data == NULL and it
5914 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 *data = mbedtls_calloc(1, m_bytes);
5916 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005917 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005918 goto cleanup;
5919 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005922 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005924 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005926
5927 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005929 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005930 * (8 * ceiling(m/8) - m) bits of the first byte in
5931 * the string to zero.
5932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005934 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005935 }
5936
5937 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 * big-endian byte string.
5939 */
5940 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005941
5942 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 * Result of comparison is returned. When it indicates error
5944 * then this function is called again.
5945 */
5946 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005947 }
5948
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005949 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5951 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005952cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (ret != 0) {
5954 status = mbedtls_to_psa_error(ret);
5955 }
5956 if (status != PSA_SUCCESS) {
5957 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005958 *data = NULL;
5959 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 mbedtls_mpi_free(&k);
5961 mbedtls_mpi_free(&diff_N_2);
5962 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005963}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005964
5965/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5966 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5967 *
5968 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5969 * draw a 32-byte string and process it as specified in
5970 * Elliptic Curves for Security [RFC7748] §5.
5971 *
5972 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5973 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5974 *
5975 * Note: Function allocates memory for *data buffer, so given *data should be
5976 * always NULL.
5977 */
5978
5979static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5980 size_t bits,
5981 psa_key_derivation_operation_t *operation,
5982 uint8_t **data
5983 )
5984{
5985 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005989 case 255:
5990 output_length = 32;
5991 break;
5992 case 448:
5993 output_length = 56;
5994 break;
5995 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005997 break;
5998 }
5999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 if (*data == NULL) {
6003 return PSA_ERROR_INSUFFICIENT_MEMORY;
6004 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006009 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006013 case 255:
6014 (*data)[0] &= 248;
6015 (*data)[31] &= 127;
6016 (*data)[31] |= 64;
6017 break;
6018 case 448:
6019 (*data)[0] &= 252;
6020 (*data)[55] |= 128;
6021 break;
6022 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006024 break;
6025 }
6026
6027 return status;
6028}
Valerio Setti86587ab2023-06-27 13:09:30 +02006029#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6030static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6031 psa_key_slot_t *slot, size_t bits,
6032 psa_key_derivation_operation_t *operation, uint8_t **data)
6033{
6034 (void) slot;
6035 (void) bits;
6036 (void) operation;
6037 (void) data;
6038 return PSA_ERROR_NOT_SUPPORTED;
6039}
6040
6041static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6042 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6043{
6044 (void) bits;
6045 (void) operation;
6046 (void) data;
6047 return PSA_ERROR_NOT_SUPPORTED;
6048}
6049#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6050#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006051
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006052static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006053 psa_key_slot_t *slot,
6054 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006056{
6057 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306059 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006061 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6064 return PSA_ERROR_INVALID_ARGUMENT;
6065 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006066
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006067#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006068 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6070 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6071 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006072 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6074 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 }
6077 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006078 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6080 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006083 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006084 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006085#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006086 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (key_type_is_raw_bytes(slot->attr.type)) {
6088 if (bits % 8 != 0) {
6089 return PSA_ERROR_INVALID_ARGUMENT;
6090 }
6091 data = mbedtls_calloc(1, bytes);
6092 if (data == NULL) {
6093 return PSA_ERROR_INSUFFICIENT_MEMORY;
6094 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 status = psa_key_derivation_output_bytes(operation, data, bytes);
6097 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 }
David Brown12ca5032021-02-11 11:02:00 -07006100#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6102 psa_des_set_key_parity(data, bytes);
6103 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006104#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 } else {
6106 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006107 }
Ronald Crondd04d422020-11-28 15:14:42 +01006108
Ronald Cronbf33c932020-11-28 18:06:53 +01006109 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006110 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006112 };
6113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6115 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6116 &storage_size);
6117 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 }
Archanad8a83dc2021-06-14 10:04:16 +05306120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 status = psa_allocate_buffer_to_slot(slot, storage_size);
6122 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 }
Archanad8a83dc2021-06-14 10:04:16 +05306125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 status = psa_driver_wrapper_import_key(&attributes,
6127 data, bytes,
6128 slot->key.data,
6129 slot->key.bytes,
6130 &slot->key.bytes, &bits);
6131 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006132 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006134
6135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 mbedtls_free(data);
6137 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006138}
6139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6141 psa_key_derivation_operation_t *operation,
6142 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006143{
6144 psa_status_t status;
6145 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006146 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006147
Ronald Cron81709fc2020-11-14 12:10:32 +01006148 *key = MBEDTLS_SVC_KEY_ID_INIT;
6149
Gilles Peskine0f84d622019-09-12 19:03:13 +02006150 /* Reject any attempt to create a zero-length key so that we don't
6151 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 if (psa_get_key_bits(attributes) == 0) {
6153 return PSA_ERROR_INVALID_ARGUMENT;
6154 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (operation->alg == PSA_ALG_NONE) {
6157 return PSA_ERROR_BAD_STATE;
6158 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (!operation->can_output_key) {
6161 return PSA_ERROR_NOT_PERMITTED;
6162 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6165 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006168 /* Deriving a key in a secure element is not implemented yet. */
6169 status = PSA_ERROR_NOT_SUPPORTED;
6170 }
6171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if (status == PSA_SUCCESS) {
6173 status = psa_generate_derived_key_internal(slot,
6174 attributes->core.bits,
6175 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006176 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 if (status == PSA_SUCCESS) {
6178 status = psa_finish_key_creation(slot, driver, key);
6179 }
6180 if (status != PSA_SUCCESS) {
6181 psa_fail_key_creation(slot, driver);
6182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006185}
6186
Gilles Peskineeab56e42018-07-12 17:12:33 +02006187
6188
6189/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006190/* Key derivation */
6191/****************************************************************/
6192
Steven Cooreman932ffb72021-02-15 12:14:32 +01006193#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006194static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006195{
6196#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6198 return 1;
6199 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006200#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006201#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6203 return 1;
6204 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006205#endif
6206#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6208 return 1;
6209 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006210#endif
6211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6213 return 1;
6214 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006215#endif
6216#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6218 return 1;
6219 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006220#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006221#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6223 return 1;
6224 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006225#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306226#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6227 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6228 return 1;
6229 }
6230#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306231#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6232 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6233 return 1;
6234 }
6235#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006237}
6238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006240{
6241 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 psa_status_t status = psa_hash_setup(&operation, alg);
6243 psa_hash_abort(&operation);
6244 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006245}
6246
Gilles Peskine969c5d62019-01-16 15:53:06 +01006247static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006248 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006250{
Janos Follath5fe19732019-06-20 15:09:30 +01006251 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6252 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006254
Gilles Peskine969c5d62019-01-16 15:53:06 +01006255 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (!is_kdf_alg_supported(kdf_alg)) {
6257 return PSA_ERROR_NOT_SUPPORTED;
6258 }
John Durkop07cc04a2020-11-16 22:08:34 -08006259
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006260 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306261 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6263 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306264 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6265 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6266 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306267 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306268 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (hash_size == 0) {
6270 return PSA_ERROR_NOT_SUPPORTED;
6271 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006272
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006273 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6274 * we might fail later, which is somewhat unfriendly and potentially
6275 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 psa_status_t status = psa_hash_try_support(hash_alg);
6277 if (status != PSA_SUCCESS) {
6278 return status;
6279 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006280 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6283 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6284 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6285 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006286 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006287#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006288 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6290 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006291 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6294 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 operation->capacity = 255 * hash_size;
6296 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006297}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006300{
6301#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (alg == PSA_ALG_ECDH) {
6303 return PSA_SUCCESS;
6304 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006305#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006306#if defined(PSA_WANT_ALG_FFDH)
6307 if (alg == PSA_ALG_FFDH) {
6308 return PSA_SUCCESS;
6309 }
6310#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006311 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006313}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006314
6315static int psa_key_derivation_allows_free_form_secret_input(
6316 psa_algorithm_t kdf_alg)
6317{
6318#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6319 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6320 return 0;
6321 }
6322#endif
6323 (void) kdf_alg;
6324 return 1;
6325}
John Durkop07cc04a2020-11-16 22:08:34 -08006326#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6329 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006330{
6331 psa_status_t status;
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 if (operation->alg != 0) {
6334 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006335 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6338 return PSA_ERROR_INVALID_ARGUMENT;
6339 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6340#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6341 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6342 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6343 status = psa_key_agreement_try_support(ka_alg);
6344 if (status != PSA_SUCCESS) {
6345 return status;
6346 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006347 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6348 return PSA_ERROR_INVALID_ARGUMENT;
6349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6351#else
6352 return PSA_ERROR_NOT_SUPPORTED;
6353#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6354 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6355#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6356 status = psa_key_derivation_setup_kdf(operation, alg);
6357#else
6358 return PSA_ERROR_NOT_SUPPORTED;
6359#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6360 } else {
6361 return PSA_ERROR_INVALID_ARGUMENT;
6362 }
6363
6364 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006365 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 }
6367 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006368}
6369
Przemek Stekiel69c46792022-06-10 12:59:51 +02006370#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006371static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6372 psa_algorithm_t kdf_alg,
6373 psa_key_derivation_step_t step,
6374 const uint8_t *data,
6375 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006376{
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006378 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006380 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006381#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6383 return PSA_ERROR_INVALID_ARGUMENT;
6384 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006385#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 if (hkdf->state != HKDF_STATE_INIT) {
6387 return PSA_ERROR_BAD_STATE;
6388 } else {
6389 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6390 hash_alg,
6391 data, data_length);
6392 if (status != PSA_SUCCESS) {
6393 return status;
6394 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006395 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006397 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006398 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006399#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006401 /* We shouldn't be in different state as HKDF_EXPAND only allows
6402 * two inputs: SECRET (this case) and INFO which does not modify
6403 * the state. It could happen only if the hkdf
6404 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (hkdf->state != HKDF_STATE_INIT) {
6406 return PSA_ERROR_BAD_STATE;
6407 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006408
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006409 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6411 return PSA_ERROR_INVALID_ARGUMENT;
6412 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 memcpy(hkdf->prk, data, data_length);
6415 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006416#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006417 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006418 /* HKDF: If no salt was provided, use an empty salt.
6419 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006421#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6423 return PSA_ERROR_BAD_STATE;
6424 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006425#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6427 hash_alg,
6428 NULL, 0);
6429 if (status != PSA_SUCCESS) {
6430 return status;
6431 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006432 hkdf->state = HKDF_STATE_STARTED;
6433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 if (hkdf->state != HKDF_STATE_STARTED) {
6435 return PSA_ERROR_BAD_STATE;
6436 }
6437 status = psa_mac_update(&hkdf->hmac,
6438 data, data_length);
6439 if (status != PSA_SUCCESS) {
6440 return status;
6441 }
6442 status = psa_mac_sign_finish(&hkdf->hmac,
6443 hkdf->prk,
6444 sizeof(hkdf->prk),
6445 &data_length);
6446 if (status != PSA_SUCCESS) {
6447 return status;
6448 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006449 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006450
Gilles Peskine2b522db2019-04-12 15:11:49 +02006451 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006452 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006453#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006455 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006457 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006459#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006460 {
6461 /* Block 0 is empty, and the next block will be
6462 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006464 }
6465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006467 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006468#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6470 return PSA_ERROR_INVALID_ARGUMENT;
6471 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006472#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006473#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6475 hkdf->state == HKDF_STATE_INIT) {
6476 return PSA_ERROR_BAD_STATE;
6477 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006478#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 if (hkdf->state == HKDF_STATE_OUTPUT) {
6480 return PSA_ERROR_BAD_STATE;
6481 }
6482 if (hkdf->info_set) {
6483 return PSA_ERROR_BAD_STATE;
6484 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006485 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (data_length != 0) {
6487 hkdf->info = mbedtls_calloc(1, data_length);
6488 if (hkdf->info == NULL) {
6489 return PSA_ERROR_INSUFFICIENT_MEMORY;
6490 }
6491 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006492 }
6493 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006495 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006497 }
6498}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006499#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006500
John Durkop07cc04a2020-11-16 22:08:34 -08006501#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6502 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006503static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6504 const uint8_t *data,
6505 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006506{
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6508 return PSA_ERROR_BAD_STATE;
6509 }
Janos Follathf08e2652019-06-13 09:05:41 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 if (data_length != 0) {
6512 prf->seed = mbedtls_calloc(1, data_length);
6513 if (prf->seed == NULL) {
6514 return PSA_ERROR_INSUFFICIENT_MEMORY;
6515 }
Janos Follathf08e2652019-06-13 09:05:41 +01006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006518 prf->seed_length = data_length;
6519 }
Janos Follathf08e2652019-06-13 09:05:41 +01006520
Gilles Peskine43f958b2020-12-13 14:55:14 +01006521 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006524}
6525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6527 const uint8_t *data,
6528 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006529{
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6531 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6532 return PSA_ERROR_BAD_STATE;
6533 }
Janos Follath81550542019-06-13 14:26:34 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (data_length != 0) {
6536 prf->secret = mbedtls_calloc(1, data_length);
6537 if (prf->secret == NULL) {
6538 return PSA_ERROR_INSUFFICIENT_MEMORY;
6539 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006542 prf->secret_length = data_length;
6543 }
Janos Follath81550542019-06-13 14:26:34 +01006544
Gilles Peskine43f958b2020-12-13 14:55:14 +01006545 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006548}
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6551 const uint8_t *data,
6552 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006553{
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6555 return PSA_ERROR_BAD_STATE;
6556 }
Janos Follath63028dd2019-06-13 09:15:47 +01006557
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 if (data_length != 0) {
6559 prf->label = mbedtls_calloc(1, data_length);
6560 if (prf->label == NULL) {
6561 return PSA_ERROR_INSUFFICIENT_MEMORY;
6562 }
Janos Follath63028dd2019-06-13 09:15:47 +01006563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006565 prf->label_length = data_length;
6566 }
Janos Follath63028dd2019-06-13 09:15:47 +01006567
Gilles Peskine43f958b2020-12-13 14:55:14 +01006568 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006571}
6572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6574 psa_key_derivation_step_t step,
6575 const uint8_t *data,
6576 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006577{
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006579 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006581 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006583 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006585 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006587 }
6588}
John Durkop07cc04a2020-11-16 22:08:34 -08006589#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6590 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6591
6592#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6593static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6594 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006595 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006597{
6598 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6600 4 + data_length + prf->other_secret_length :
6601 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6604 return PSA_ERROR_INVALID_ARGUMENT;
6605 }
John Durkop07cc04a2020-11-16 22:08:34 -08006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 uint8_t *pms = mbedtls_calloc(1, pms_len);
6608 if (pms == NULL) {
6609 return PSA_ERROR_INSUFFICIENT_MEMORY;
6610 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006611 uint8_t *cur = pms;
6612
6613 /* pure-PSK:
6614 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006615 *
6616 * The premaster secret is formed as follows: if the PSK is N octets
6617 * long, concatenate a uint16 with the value N, N zero octets, a second
6618 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006619 *
6620 * mixed-PSK:
6621 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6622 * follows: concatenate a uint16 with the length of the other secret,
6623 * the other secret itself, uint16 with the length of PSK, and the
6624 * PSK itself.
6625 * For details please check:
6626 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6627 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6628 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006629 */
6630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6632 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6633 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6634 if (prf->other_secret_length != 0) {
6635 memcpy(cur, prf->other_secret, prf->other_secret_length);
6636 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006637 cur += prf->other_secret_length;
6638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 } else {
6640 *cur++ = MBEDTLS_BYTE_1(data_length);
6641 *cur++ = MBEDTLS_BYTE_0(data_length);
6642 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006643 cur += data_length;
6644 }
6645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 *cur++ = MBEDTLS_BYTE_1(data_length);
6647 *cur++ = MBEDTLS_BYTE_0(data_length);
6648 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006649 cur += data_length;
6650
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006651 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006652
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006653 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006655}
Janos Follath6660f0e2019-06-17 08:44:03 +01006656
Przemek Stekiele47201b2022-04-19 13:53:28 +02006657static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6658 psa_tls12_prf_key_derivation_t *prf,
6659 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006661{
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6663 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006664 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006665
6666 if (data_length != 0) {
6667 prf->other_secret = mbedtls_calloc(1, data_length);
6668 if (prf->other_secret == NULL) {
6669 return PSA_ERROR_INSUFFICIENT_MEMORY;
6670 }
6671
6672 memcpy(prf->other_secret, data, data_length);
6673 prf->other_secret_length = data_length;
6674 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675 prf->other_secret_length = 0;
6676 }
6677
6678 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6679
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681}
6682
Janos Follath6660f0e2019-06-17 08:44:03 +01006683static psa_status_t psa_tls12_prf_psk_to_ms_input(
6684 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006685 psa_key_derivation_step_t step,
6686 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006688{
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006690 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 return psa_tls12_prf_psk_to_ms_set_key(prf,
6692 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006693 break;
6694 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6696 data,
6697 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006698 break;
6699 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006701 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006702
Przemek Stekiele47201b2022-04-19 13:53:28 +02006703 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006704}
John Durkop07cc04a2020-11-16 22:08:34 -08006705#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006706
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006707#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6708static psa_status_t psa_tls12_ecjpake_to_pms_input(
6709 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006710 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006711 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006713{
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6715 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6716 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006717 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006718
6719 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 if (data[0] != 0x04) {
6721 return PSA_ERROR_INVALID_ARGUMENT;
6722 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006723
6724 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006726
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006728}
6729#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306730
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306731#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306732static psa_status_t psa_pbkdf2_set_input_cost(
6733 psa_pbkdf2_key_derivation_t *pbkdf2,
6734 psa_key_derivation_step_t step,
6735 uint64_t data)
6736{
6737 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6738 return PSA_ERROR_INVALID_ARGUMENT;
6739 }
6740
6741 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6742 return PSA_ERROR_BAD_STATE;
6743 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306744
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306745 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306746 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306747 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306748
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306749 if (data == 0) {
6750 return PSA_ERROR_INVALID_ARGUMENT;
6751 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306752
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306753 pbkdf2->input_cost = data;
6754 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6755
6756 return PSA_SUCCESS;
6757}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306758
6759static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6760 const uint8_t *data,
6761 size_t data_length)
6762{
Gilles Peskineead17662023-08-20 22:02:51 +02006763 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6764 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6765 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6766 /* Appending to existing salt. No state change. */
6767 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306768 return PSA_ERROR_BAD_STATE;
6769 }
6770
Gilles Peskineca57d782023-07-20 19:08:06 +02006771 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006772 /* Appending an empty string, nothing to do. */
6773 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306774 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306775
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306776 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6777 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306778 return PSA_ERROR_INSUFFICIENT_MEMORY;
6779 }
6780
Gilles Peskineead17662023-08-20 22:02:51 +02006781 if (pbkdf2->salt_length != 0) {
6782 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6783 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306784 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306785 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306786 mbedtls_free(pbkdf2->salt);
6787 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306788 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306789 return PSA_SUCCESS;
6790}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306791
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306792#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306793static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306794 const uint8_t *input,
6795 size_t input_len,
6796 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306797 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306798{
6799 psa_status_t status = PSA_SUCCESS;
6800 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6801 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306802 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306803 } else {
6804 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306805 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306806 }
6807 return status;
6808}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306809#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306810
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306811#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6813 size_t input_len,
6814 uint8_t *output,
6815 size_t *output_len)
6816{
6817 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306818 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306820 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306821 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6822 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6823 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306824 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306825 * mac_size as the driver function sets mac_output_length = mac_size
6826 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306827 status = psa_driver_wrapper_mac_compute(&attributes,
6828 zeros, sizeof(zeros),
6829 PSA_ALG_CMAC, input, input_len,
6830 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306831 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6832 128U,
6833 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306834 output_len);
6835 } else {
6836 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306837 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306838 }
6839 return status;
6840}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306841#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306842
6843static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306844 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306845 const uint8_t *data,
6846 size_t data_length)
6847{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306848 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306849 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6850 return PSA_ERROR_BAD_STATE;
6851 }
6852
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306853#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306854 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6855 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6856 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6857 pbkdf2->password,
6858 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306859 } else
6860#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6861#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6862 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306863 status = psa_pbkdf2_cmac_set_password(data, data_length,
6864 pbkdf2->password,
6865 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306866 } else
6867#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6868 {
6869 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306870 }
6871
6872 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6873
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306874 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306875}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306876
6877static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306878 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306879 psa_key_derivation_step_t step,
6880 const uint8_t *data,
6881 size_t data_length)
6882{
6883 switch (step) {
6884 case PSA_KEY_DERIVATION_INPUT_SALT:
6885 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6886 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306887 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306888 default:
6889 return PSA_ERROR_INVALID_ARGUMENT;
6890 }
6891}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306892#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306893
Gilles Peskineb8965192019-09-24 16:21:10 +02006894/** Check whether the given key type is acceptable for the given
6895 * input step of a key derivation.
6896 *
6897 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6898 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6899 * Both secret and non-secret inputs can alternatively have the type
6900 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6901 * that the input was passed as a buffer rather than via a key object.
6902 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006903static int psa_key_derivation_check_input_type(
6904 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006906{
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006908 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (key_type == PSA_KEY_TYPE_DERIVE) {
6910 return PSA_SUCCESS;
6911 }
6912 if (key_type == PSA_KEY_TYPE_NONE) {
6913 return PSA_SUCCESS;
6914 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006915 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006916 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 if (key_type == PSA_KEY_TYPE_DERIVE) {
6918 return PSA_SUCCESS;
6919 }
6920 if (key_type == PSA_KEY_TYPE_NONE) {
6921 return PSA_SUCCESS;
6922 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006923 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006924 case PSA_KEY_DERIVATION_INPUT_LABEL:
6925 case PSA_KEY_DERIVATION_INPUT_SALT:
6926 case PSA_KEY_DERIVATION_INPUT_INFO:
6927 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6929 return PSA_SUCCESS;
6930 }
6931 if (key_type == PSA_KEY_TYPE_NONE) {
6932 return PSA_SUCCESS;
6933 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006934 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306935 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6936 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6937 return PSA_SUCCESS;
6938 }
6939 if (key_type == PSA_KEY_TYPE_DERIVE) {
6940 return PSA_SUCCESS;
6941 }
6942 if (key_type == PSA_KEY_TYPE_NONE) {
6943 return PSA_SUCCESS;
6944 }
6945 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006946 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006948}
6949
Janos Follathb80a94e2019-06-12 15:54:46 +01006950static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006951 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006952 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006953 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006954 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006956{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006957 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006959
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 status = psa_key_derivation_check_input_type(step, key_type);
6961 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006962 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006964
Przemek Stekiel69c46792022-06-10 12:59:51 +02006965#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6967 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6968 step, data, data_length);
6969 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006970#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006971#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6973 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6974 step, data, data_length);
6975 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006976#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6977#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6979 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6980 step, data, data_length);
6981 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006982#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006983#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006985 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6987 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006988#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306989#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306990 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306991 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6992 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306993 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306994#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006995 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006996 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006997 (void) data;
6998 (void) data_length;
6999 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007001 }
7002
Gilles Peskine224b0d62019-09-23 18:13:17 +02007003exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 if (status != PSA_SUCCESS) {
7005 psa_key_derivation_abort(operation);
7006 }
7007 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007008}
7009
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307010static psa_status_t psa_key_derivation_input_integer_internal(
7011 psa_key_derivation_operation_t *operation,
7012 psa_key_derivation_step_t step,
7013 uint64_t value)
7014{
7015 psa_status_t status;
7016 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7017
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307018#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307019 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307020 status = psa_pbkdf2_set_input_cost(
7021 &operation->ctx.pbkdf2, step, value);
7022 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307023#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307024 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307025 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307026 (void) value;
7027 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307028 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307029 }
7030
7031 if (status != PSA_SUCCESS) {
7032 psa_key_derivation_abort(operation);
7033 }
7034 return status;
7035}
7036
Janos Follath51f4a0f2019-06-14 11:35:55 +01007037psa_status_t psa_key_derivation_input_bytes(
7038 psa_key_derivation_operation_t *operation,
7039 psa_key_derivation_step_t step,
7040 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007042{
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 return psa_key_derivation_input_internal(operation, step,
7044 PSA_KEY_TYPE_NONE,
7045 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007046}
7047
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307048psa_status_t psa_key_derivation_input_integer(
7049 psa_key_derivation_operation_t *operation,
7050 psa_key_derivation_step_t step,
7051 uint64_t value)
7052{
7053 return psa_key_derivation_input_integer_internal(operation, step, value);
7054}
7055
Janos Follath51f4a0f2019-06-14 11:35:55 +01007056psa_status_t psa_key_derivation_input_key(
7057 psa_key_derivation_operation_t *operation,
7058 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007060{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007062 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007063 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007064
Ronald Cron5c522922020-11-14 16:35:34 +01007065 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7067 if (status != PSA_SUCCESS) {
7068 psa_key_derivation_abort(operation);
7069 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007070 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007071
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307072 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7073 * permission to output to a key object. */
7074 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7075 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007076 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 status = psa_key_derivation_input_internal(operation,
7080 step, slot->attr.type,
7081 slot->key.data,
7082 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007087}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007088
7089
7090
7091/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007092/* Key agreement */
7093/****************************************************************/
7094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7096 const uint8_t *key_buffer,
7097 size_t key_buffer_size,
7098 psa_algorithm_t alg,
7099 const uint8_t *peer_key,
7100 size_t peer_key_length,
7101 uint8_t *shared_secret,
7102 size_t shared_secret_size,
7103 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007104{
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007106#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007107 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7109 key_buffer_size, alg,
7110 peer_key, peer_key_length,
7111 shared_secret,
7112 shared_secret_size,
7113 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007114#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007115
7116#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7117 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007118 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007119 peer_key,
7120 peer_key_length,
7121 key_buffer,
7122 key_buffer_size,
7123 shared_secret,
7124 shared_secret_size,
7125 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007126#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7127
Gilles Peskine01d718c2018-09-18 12:01:02 +02007128 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007129 (void) attributes;
7130 (void) key_buffer;
7131 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007132 (void) peer_key;
7133 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007134 (void) shared_secret;
7135 (void) shared_secret_size;
7136 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007138 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007139}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007140
Aditya Deshpande17845b82022-10-13 17:21:01 +01007141/** Internal function for raw key agreement
7142 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007143 * to the driver's implementation if a driver is present.
7144 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007145 * (psa_key_agreement_raw_builtin).
7146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7148 psa_key_slot_t *private_key,
7149 const uint8_t *peer_key,
7150 size_t peer_key_length,
7151 uint8_t *shared_secret,
7152 size_t shared_secret_size,
7153 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007154{
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7156 return PSA_ERROR_NOT_SUPPORTED;
7157 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007158
7159 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007161 };
7162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 return psa_driver_wrapper_key_agreement(&attributes,
7164 private_key->key.data,
7165 private_key->key.bytes, alg,
7166 peer_key, peer_key_length,
7167 shared_secret,
7168 shared_secret_size,
7169 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007170}
7171
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007172/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173 * to potentially free embedded data structures and wipe confidential data.
7174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007175static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7176 psa_key_derivation_step_t step,
7177 psa_key_slot_t *private_key,
7178 const uint8_t *peer_key,
7179 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007180{
7181 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007182 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007183 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007185
7186 /* Step 1: run the secret agreement algorithm to generate the shared
7187 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 status = psa_key_agreement_raw_internal(ka_alg,
7189 private_key,
7190 peer_key, peer_key_length,
7191 shared_secret,
7192 sizeof(shared_secret),
7193 &shared_secret_length);
7194 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007195 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007197
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007198 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007199 * the shared secret. A shared secret is permitted wherever a key
7200 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 status = psa_key_derivation_input_internal(operation, step,
7202 PSA_KEY_TYPE_DERIVE,
7203 shared_secret,
7204 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7207 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007208}
7209
Gilles Peskine449bd832023-01-11 14:50:10 +01007210psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7211 psa_key_derivation_step_t step,
7212 mbedtls_svc_key_id_t private_key,
7213 const uint8_t *peer_key,
7214 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007215{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007217 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007218 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7221 return PSA_ERROR_INVALID_ARGUMENT;
7222 }
Ronald Cron5c522922020-11-14 16:35:34 +01007223 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7225 if (status != PSA_SUCCESS) {
7226 return status;
7227 }
7228 status = psa_key_agreement_internal(operation, step,
7229 slot,
7230 peer_key, peer_key_length);
7231 if (status != PSA_SUCCESS) {
7232 psa_key_derivation_abort(operation);
7233 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007234 /* If a private key has been added as SECRET, we allow the derived
7235 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007237 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007239 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007244}
7245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7247 mbedtls_svc_key_id_t private_key,
7248 const uint8_t *peer_key,
7249 size_t peer_key_length,
7250 uint8_t *output,
7251 size_t output_size,
7252 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007253{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007255 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007256 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007257 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007260 status = PSA_ERROR_INVALID_ARGUMENT;
7261 goto exit;
7262 }
Ronald Cron5c522922020-11-14 16:35:34 +01007263 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7265 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007268
Gilles Peskine3e561302022-04-14 00:17:15 +02007269 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7270 * for the output size. The PSA specification only guarantees that this
7271 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7272 * but it might be nice to allow smaller buffers if the output fits.
7273 * At the time of writing this comment, with only ECDH implemented,
7274 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7275 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7276 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007277 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7279 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007280 status = PSA_ERROR_BUFFER_TOO_SMALL;
7281 goto exit;
7282 }
7283
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 status = psa_key_agreement_raw_internal(alg, slot,
7285 peer_key, peer_key_length,
7286 output, output_size,
7287 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007288
7289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007291 /* If an error happens and is not handled properly, the output
7292 * may be used as a key to protect sensitive data. Arrange for such
7293 * a key to be random, which is likely to result in decryption or
7294 * verification errors. This is better than filling the buffer with
7295 * some constant data such as zeros, which would result in the data
7296 * being protected with a reproducible, easily knowable key.
7297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007299 *output_length = output_size;
7300 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007301
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007303
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007305}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007306
7307
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308
Gilles Peskine86a440b2018-11-12 18:39:40 +01007309/****************************************************************/
7310/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007311/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007312
Gilles Peskine672a7712023-04-28 21:00:28 +02007313#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7314#include "entropy_poll.h"
7315#endif
7316
Gilles Peskine30524eb2020-11-13 17:02:26 +01007317/** Initialize the PSA random generator.
7318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007319static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007320{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007321#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007323#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7324
Gilles Peskine30524eb2020-11-13 17:02:26 +01007325 /* Set default configuration if
7326 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 }
7330 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007331 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007335#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7336 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7337 /* The PSA entropy injection feature depends on using NV seed as an entropy
7338 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 mbedtls_entropy_add_source(&rng->entropy,
7340 mbedtls_nv_seed_poll, NULL,
7341 MBEDTLS_ENTROPY_BLOCK_SIZE,
7342 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007343#endif
7344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007347}
7348
7349/** Deinitialize the PSA random generator.
7350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007351static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007353#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007355#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7357 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007358#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007359}
7360
7361/** Seed the PSA random generator.
7362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007363static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007364{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007365#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7366 /* Do nothing: the external RNG seeds itself. */
7367 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007369#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007370 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7372 drbg_seed, sizeof(drbg_seed) - 1);
7373 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007374#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007375}
7376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377psa_status_t psa_generate_random(uint8_t *output,
7378 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007379{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007380 GUARD_MODULE_INITIALIZED;
7381
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007382#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7383
7384 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7386 output, output_size,
7387 &output_length);
7388 if (status != PSA_SUCCESS) {
7389 return status;
7390 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007391 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007392 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 if (output_length != output_size) {
7394 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7395 }
7396 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007397
7398#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7399
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007401 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7403 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7404 output_size);
7405 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7406 output, request_size);
7407 if (ret != 0) {
7408 return mbedtls_to_psa_error(ret);
7409 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007410 output_size -= request_size;
7411 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007414#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007415}
7416
Gilles Peskine8814fc42020-12-14 15:33:44 +01007417/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007418 *
7419 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7420 * `psa_generate_random(...)`. The state parameter is ignored since the
7421 * PSA API doesn't support passing an explicit state.
7422 *
7423 * In the non-external case, psa_generate_random() calls an
7424 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7425 * and semantics as mbedtls_psa_get_random(). As an optimization,
7426 * instead of doing this back-and-forth between the PSA API and the
7427 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7428 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007430#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7431int mbedtls_psa_get_random(void *p_rng,
7432 unsigned char *output,
7433 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007434{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007435 /* This function takes a pointer to the RNG state because that's what
7436 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7437 * its own state internally and doesn't let the caller access that state.
7438 * So we just ignore the state parameter, and in practice we'll pass
7439 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007440 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 psa_status_t status = psa_generate_random(output, output_size);
7442 if (status == PSA_SUCCESS) {
7443 return 0;
7444 } else {
7445 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7446 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007447}
7448#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7449
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007450#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007451psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7452 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007453{
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 if (global_data.initialized) {
7455 return PSA_ERROR_NOT_PERMITTED;
7456 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007457
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7459 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7460 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7461 return PSA_ERROR_INVALID_ARGUMENT;
7462 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007465}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007466#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007467
Ronald Cron3772afe2021-02-08 16:10:05 +01007468/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007469 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007470 * \param type The key type
7471 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007472 *
7473 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007474 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007475 * \retval #PSA_ERROR_INVALID_ARGUMENT
7476 * The size in bits of the key is not valid.
7477 * \retval #PSA_ERROR_NOT_SUPPORTED
7478 * The type and/or the size in bits of the key or the combination of
7479 * the two is not supported.
7480 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007481static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007483{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007485
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (key_type_is_raw_bytes(type)) {
7487 status = psa_validate_unstructured_key_bit_size(type, bits);
7488 if (status != PSA_SUCCESS) {
7489 return status;
7490 }
7491 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007492#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7494 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7495 return PSA_ERROR_NOT_SUPPORTED;
7496 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007497 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007498 return PSA_ERROR_NOT_SUPPORTED;
7499 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007500
Ronald Cron01b2aba2020-10-05 09:42:02 +02007501 /* Accept only byte-aligned keys, for the same reasons as
7502 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (bits % 8 != 0) {
7504 return PSA_ERROR_NOT_SUPPORTED;
7505 }
7506 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007507#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007508
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007509#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007511 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 return PSA_SUCCESS;
7513 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007514#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007515
Valerio Settia55f0422023-07-10 15:34:41 +02007516#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007517 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007518 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007519 return PSA_ERROR_NOT_SUPPORTED;
7520 }
7521 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007522#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007523 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007525 }
7526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007528}
7529
Ronald Cron55ed0592020-10-05 10:30:40 +02007530psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007531 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007533{
7534 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007535 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007536
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 if ((attributes->domain_parameters == NULL) &&
7538 (attributes->domain_parameters_size != 0)) {
7539 return PSA_ERROR_INVALID_ARGUMENT;
7540 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (key_type_is_raw_bytes(type)) {
7543 status = psa_generate_random(key_buffer, key_buffer_size);
7544 if (status != PSA_SUCCESS) {
7545 return status;
7546 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007547
David Brown12ca5032021-02-11 11:02:00 -07007548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (type == PSA_KEY_TYPE_DES) {
7550 psa_des_set_key_parity(key_buffer, key_buffer_size);
7551 }
David Brown8107e312021-02-12 08:08:46 -07007552#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007554
Valerio Setti76df8c12023-07-11 14:11:28 +02007555#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7557 return mbedtls_psa_rsa_generate_key(attributes,
7558 key_buffer,
7559 key_buffer_size,
7560 key_buffer_length);
7561 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007562#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007563
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007564#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7566 return mbedtls_psa_ecp_generate_key(attributes,
7567 key_buffer,
7568 key_buffer_size,
7569 key_buffer_length);
7570 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007572
Valerio Settia55f0422023-07-10 15:34:41 +02007573#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007574 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7575 return mbedtls_psa_ffdh_generate_key(attributes,
7576 key_buffer,
7577 key_buffer_size,
7578 key_buffer_length);
7579 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007580#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007581 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 (void) key_buffer_length;
7583 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007584 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007587}
Darryl Green0c6575a2018-11-07 16:05:30 +00007588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7590 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007591{
7592 psa_status_t status;
7593 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007594 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007595 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007596
Ronald Cron81709fc2020-11-14 12:10:32 +01007597 *key = MBEDTLS_SVC_KEY_ID_INIT;
7598
Gilles Peskine0f84d622019-09-12 19:03:13 +02007599 /* Reject any attempt to create a zero-length key so that we don't
7600 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 if (psa_get_key_bits(attributes) == 0) {
7602 return PSA_ERROR_INVALID_ARGUMENT;
7603 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007604
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007605 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7607 return PSA_ERROR_INVALID_ARGUMENT;
7608 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7611 &slot, &driver);
7612 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007613 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 }
Gilles Peskine11792082019-08-06 18:36:36 +02007615
Ronald Cron977c2472020-10-13 08:32:21 +02007616 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307617 * storage ( thus not in the case of generating a key in a secure element
7618 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7619 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 if (slot->key.data == NULL) {
7621 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7622 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007623 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 attributes->core.type, attributes->core.bits);
7625 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007628
7629 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 attributes->core.type,
7631 attributes->core.bits);
7632 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007633 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 attributes, &key_buffer_size);
7635 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007636 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 }
Ronald Cron977c2472020-10-13 08:32:21 +02007638 }
Ronald Cron977c2472020-10-13 08:32:21 +02007639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7641 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 }
Ronald Cron977c2472020-10-13 08:32:21 +02007644 }
7645
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 status = psa_driver_wrapper_generate_key(attributes,
7647 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 if (status != PSA_SUCCESS) {
7650 psa_remove_key_data_from_memory(slot);
7651 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007652
Gilles Peskine11792082019-08-06 18:36:36 +02007653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 if (status == PSA_SUCCESS) {
7655 status = psa_finish_key_creation(slot, driver, key);
7656 }
7657 if (status != PSA_SUCCESS) {
7658 psa_fail_key_creation(slot, driver);
7659 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007662}
7663
Gilles Peskinee59236f2018-01-27 23:32:46 +01007664/****************************************************************/
7665/* Module setup */
7666/****************************************************************/
7667
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007668#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007669psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 void (* entropy_init)(mbedtls_entropy_context *ctx),
7671 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007672{
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7674 return PSA_ERROR_BAD_STATE;
7675 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007676 global_data.rng.entropy_init = entropy_init;
7677 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007679}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007680#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007683{
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 psa_wipe_all_key_slots();
7685 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7686 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007687 }
7688 /* Wipe all remaining data, including configuration.
7689 * In particular, this sets all state indicator to the value
7690 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007692
7693 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007695}
7696
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007697#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7698/** Recover a transaction that was interrupted by a power failure.
7699 *
7700 * This function is called during initialization, before psa_crypto_init()
7701 * returns. If this function returns a failure status, the initialization
7702 * fails.
7703 */
7704static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007706{
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007708 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7709 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 /* TODO - fall through to the failure case until this
7711 * is implemented.
7712 * https://github.com/ARMmbed/mbed-crypto/issues/218
7713 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007714 default:
7715 /* We found an unsupported transaction in the storage.
7716 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007718 }
7719}
7720#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7721
Gilles Peskine449bd832023-01-11 14:50:10 +01007722psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007724 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007725
Gilles Peskinec6b69072018-11-20 21:42:52 +01007726 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 if (global_data.initialized != 0) {
7728 return PSA_SUCCESS;
7729 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007730
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007731 /* Init drivers */
7732 status = psa_driver_wrapper_init();
7733 if (status != PSA_SUCCESS) {
7734 goto exit;
7735 }
7736 global_data.drivers_initialized = 1;
7737
Gilles Peskine30524eb2020-11-13 17:02:26 +01007738 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007740 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 status = mbedtls_psa_random_seed(&global_data.rng);
7742 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007745 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007746
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 status = psa_initialize_key_slots();
7748 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007749 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007751
Gilles Peskine4b734222019-07-24 15:56:31 +02007752#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 status = psa_crypto_load_transaction();
7754 if (status == PSA_SUCCESS) {
7755 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7756 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007757 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 }
7759 status = psa_crypto_stop_transaction();
7760 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007761 /* There's no transaction to complete. It's all good. */
7762 status = PSA_SUCCESS;
7763 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007764#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007765
Gilles Peskinec6b69072018-11-20 21:42:52 +01007766 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767 global_data.initialized = 1;
7768
7769exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (status != PSA_SUCCESS) {
7771 mbedtls_psa_crypto_free();
7772 }
7773 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007774}
7775
Yanray Wangffc3c482023-07-11 12:01:04 +08007776#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007777psa_status_t psa_crypto_driver_pake_get_password_len(
7778 const psa_crypto_driver_pake_inputs_t *inputs,
7779 size_t *password_len)
7780{
7781 if (inputs->password_len == 0) {
7782 return PSA_ERROR_BAD_STATE;
7783 }
7784
7785 *password_len = inputs->password_len;
7786
7787 return PSA_SUCCESS;
7788}
7789
7790psa_status_t psa_crypto_driver_pake_get_password(
7791 const psa_crypto_driver_pake_inputs_t *inputs,
7792 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7793{
7794 if (inputs->password_len == 0) {
7795 return PSA_ERROR_BAD_STATE;
7796 }
7797
7798 if (buffer_size < inputs->password_len) {
7799 return PSA_ERROR_BUFFER_TOO_SMALL;
7800 }
7801
7802 memcpy(buffer, inputs->password, inputs->password_len);
7803 *buffer_length = inputs->password_len;
7804
7805 return PSA_SUCCESS;
7806}
7807
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007808psa_status_t psa_crypto_driver_pake_get_user_len(
7809 const psa_crypto_driver_pake_inputs_t *inputs,
7810 size_t *user_len)
7811{
7812 if (inputs->user_len == 0) {
7813 return PSA_ERROR_BAD_STATE;
7814 }
7815
7816 *user_len = inputs->user_len;
7817
7818 return PSA_SUCCESS;
7819}
7820
7821psa_status_t psa_crypto_driver_pake_get_user(
7822 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007823 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007824{
7825 if (inputs->user_len == 0) {
7826 return PSA_ERROR_BAD_STATE;
7827 }
7828
Przemek Stekielc0e62502023-03-14 11:49:36 +01007829 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007830 return PSA_ERROR_BUFFER_TOO_SMALL;
7831 }
7832
Przemek Stekielc0e62502023-03-14 11:49:36 +01007833 memcpy(user_id, inputs->user, inputs->user_len);
7834 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007835
7836 return PSA_SUCCESS;
7837}
7838
7839psa_status_t psa_crypto_driver_pake_get_peer_len(
7840 const psa_crypto_driver_pake_inputs_t *inputs,
7841 size_t *peer_len)
7842{
7843 if (inputs->peer_len == 0) {
7844 return PSA_ERROR_BAD_STATE;
7845 }
7846
7847 *peer_len = inputs->peer_len;
7848
7849 return PSA_SUCCESS;
7850}
7851
7852psa_status_t psa_crypto_driver_pake_get_peer(
7853 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007854 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007855{
7856 if (inputs->peer_len == 0) {
7857 return PSA_ERROR_BAD_STATE;
7858 }
7859
Przemek Stekielc0e62502023-03-14 11:49:36 +01007860 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007861 return PSA_ERROR_BUFFER_TOO_SMALL;
7862 }
7863
Przemek Stekielc0e62502023-03-14 11:49:36 +01007864 memcpy(peer_id, inputs->peer, inputs->peer_len);
7865 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007866
7867 return PSA_SUCCESS;
7868}
7869
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007870psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7871 const psa_crypto_driver_pake_inputs_t *inputs,
7872 psa_pake_cipher_suite_t *cipher_suite)
7873{
7874 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7875 return PSA_ERROR_BAD_STATE;
7876 }
7877
7878 *cipher_suite = inputs->cipher_suite;
7879
7880 return PSA_SUCCESS;
7881}
7882
Neil Armstronga7d08c32022-06-01 18:21:20 +02007883psa_status_t psa_pake_setup(
7884 psa_pake_operation_t *operation,
7885 const psa_pake_cipher_suite_t *cipher_suite)
7886{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007887 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007889 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007890 status = PSA_ERROR_BAD_STATE;
7891 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007892 }
7893
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007894 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007896 status = PSA_ERROR_INVALID_ARGUMENT;
7897 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 }
7899
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7901
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007903 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7904 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007905 operation->data.inputs.cipher_suite = *cipher_suite;
7906
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007907#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007908 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007909 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007910 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007911
David Horstmann16f01512023-06-14 17:21:07 +01007912 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007913 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007914 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007915#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007916 {
7917 status = PSA_ERROR_NOT_SUPPORTED;
7918 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007919 }
7920
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007921 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7922
Przemek Stekiel51eac532022-12-07 11:04:51 +01007923 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007924exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007925 psa_pake_abort(operation);
7926 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007927}
7928
7929psa_status_t psa_pake_set_password_key(
7930 psa_pake_operation_t *operation,
7931 mbedtls_svc_key_id_t password)
7932{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007933 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007934 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7935 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007936 psa_key_attributes_t attributes;
7937 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007938
Przemek Stekiel51eac532022-12-07 11:04:51 +01007939 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007940 status = PSA_ERROR_BAD_STATE;
7941 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007942 }
7943
Przemek Stekiel6c764412022-11-22 14:05:12 +01007944 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7945 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007946 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007947 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007948 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007949 }
7950
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007951 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007952 .core = slot->attr
7953 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007954
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007955 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007956
Przemek Stekiel51eac532022-12-07 11:04:51 +01007957 if (type != PSA_KEY_TYPE_PASSWORD &&
7958 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7959 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007960 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007961 }
7962
Przemek Stekiel51eac532022-12-07 11:04:51 +01007963 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7964 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007965 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007967 }
7968
7969 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7970 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007971 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007973 if (status != PSA_SUCCESS) {
7974 psa_pake_abort(operation);
7975 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007977 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007978}
7979
7980psa_status_t psa_pake_set_user(
7981 psa_pake_operation_t *operation,
7982 const uint8_t *user_id,
7983 size_t user_id_len)
7984{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007986
7987 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007988 status = PSA_ERROR_BAD_STATE;
7989 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007990 }
7991
Przemek Stekiel51eac532022-12-07 11:04:51 +01007992 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993 status = PSA_ERROR_INVALID_ARGUMENT;
7994 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007995 }
7996
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007997 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007998 status = PSA_ERROR_BAD_STATE;
7999 goto exit;
8000 }
8001
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008002 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8003 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008004 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8005 goto exit;
8006 }
8007
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008008 memcpy(operation->data.inputs.user, user_id, user_id_len);
8009 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008010
8011 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008013 psa_pake_abort(operation);
8014 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008015}
8016
8017psa_status_t psa_pake_set_peer(
8018 psa_pake_operation_t *operation,
8019 const uint8_t *peer_id,
8020 size_t peer_id_len)
8021{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008023
8024 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008025 status = PSA_ERROR_BAD_STATE;
8026 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008027 }
8028
Przemek Stekiel51eac532022-12-07 11:04:51 +01008029 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008030 status = PSA_ERROR_INVALID_ARGUMENT;
8031 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008032 }
8033
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008034 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008035 status = PSA_ERROR_BAD_STATE;
8036 goto exit;
8037 }
8038
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008039 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8040 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008041 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8042 goto exit;
8043 }
8044
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008045 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8046 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008047
8048 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008049exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008050 psa_pake_abort(operation);
8051 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008052}
8053
8054psa_status_t psa_pake_set_role(
8055 psa_pake_operation_t *operation,
8056 psa_pake_role_t role)
8057{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059
Przemek Stekiel51eac532022-12-07 11:04:51 +01008060 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008061 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008062 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008063 }
8064
Przemek Stekiel09104b82023-03-06 14:11:51 +01008065 switch (operation->alg) {
8066#if defined(PSA_WANT_ALG_JPAKE)
8067 case PSA_ALG_JPAKE:
8068 if (role == PSA_PAKE_ROLE_NONE) {
8069 return PSA_SUCCESS;
8070 }
8071 status = PSA_ERROR_INVALID_ARGUMENT;
8072 break;
8073#endif
8074 default:
8075 (void) role;
8076 status = PSA_ERROR_NOT_SUPPORTED;
8077 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008078 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008079exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008080 psa_pake_abort(operation);
8081 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008082}
8083
David Horstmanne7f21e62023-05-12 18:17:21 +01008084/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008085#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008086static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008087 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008088{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008089 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008090 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008091 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008092
David Horstmann024e5c52023-06-14 15:48:21 +01008093 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008094 is_x1 = (stage->outputs < 1);
8095 } else {
8096 is_x1 = (stage->inputs < 1);
8097 }
8098
David Horstmann74a3d8c2023-06-14 18:28:19 +01008099 key_share_step = is_x1 ?
8100 PSA_JPAKE_X1_STEP_KEY_SHARE :
8101 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008102 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008103 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8104 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8105 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8106 } else {
8107 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008108 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008109 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008110}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008111#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008112
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113static psa_status_t psa_pake_complete_inputs(
8114 psa_pake_operation_t *operation)
8115{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008117 /* Create copy of the inputs on stack as inputs share memory
8118 with the driver context which will be setup by the driver. */
8119 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008120
Przemek Stekielfde11282023-03-13 16:06:09 +01008121 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008122 return PSA_ERROR_BAD_STATE;
8123 }
8124
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008125 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008126 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8127 return PSA_ERROR_BAD_STATE;
8128 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008129 }
8130
Przemek Stekiel18620a32023-01-17 16:34:52 +01008131 /* Clear driver context */
8132 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8133
8134 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135
8136 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008137 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008138
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008139 /* User and peer are translated to role. */
8140 mbedtls_free(inputs.user);
8141 mbedtls_free(inputs.peer);
8142
Przemek Stekiel2797d372022-12-22 11:19:22 +01008143 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008144#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008145 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008146 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008147 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008148#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008149 {
8150 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008151 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008152 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008153 return status;
8154}
8155
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008156#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008157static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008158 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008159 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008160 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8163 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8164 step != PSA_PAKE_STEP_ZK_PROOF) {
8165 return PSA_ERROR_INVALID_ARGUMENT;
8166 }
8167
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008168 psa_jpake_computation_stage_t *computation_stage =
8169 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170
David Horstmann5da95602023-06-08 15:37:12 +01008171 if (computation_stage->round != PSA_JPAKE_FIRST &&
8172 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008173 return PSA_ERROR_BAD_STATE;
8174 }
8175
David Horstmanne7f21e62023-05-12 18:17:21 +01008176 /* Check that the step we are given is the one we were expecting */
8177 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008178 return PSA_ERROR_BAD_STATE;
8179 }
8180
David Horstmanne7f21e62023-05-12 18:17:21 +01008181 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8182 computation_stage->inputs == 0 &&
8183 computation_stage->outputs == 0) {
8184 /* Start of the round, so function decides whether we are inputting
8185 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008186 computation_stage->io_mode = io_mode;
8187 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008188 /* Middle of the round so the mode we are in must match the function
8189 * called by the user */
8190 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008191 }
8192
8193 return PSA_SUCCESS;
8194}
8195
David Horstmanne7f21e62023-05-12 18:17:21 +01008196static psa_status_t psa_jpake_epilogue(
8197 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008198 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008199{
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008201 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202
David Horstmanne7f21e62023-05-12 18:17:21 +01008203 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8204 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008205 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008206 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008207 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008208 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 }
8210 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008211 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008213 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008214 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008215 }
8216 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008217 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8218 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008219 /* End of a round, move to the next round */
8220 stage->inputs = 0;
8221 stage->outputs = 0;
8222 stage->round++;
8223 }
8224 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008225 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008226 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008227 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 return PSA_SUCCESS;
8229}
David Horstmanne7f21e62023-05-12 18:17:21 +01008230
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008231#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008232
Neil Armstronga7d08c32022-06-01 18:21:20 +02008233psa_status_t psa_pake_output(
8234 psa_pake_operation_t *operation,
8235 psa_pake_step_t step,
8236 uint8_t *output,
8237 size_t output_size,
8238 size_t *output_length)
8239{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008241 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008242 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008243
8244 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008245 status = psa_pake_complete_inputs(operation);
8246 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008248 }
8249 }
8250
8251 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008252 status = PSA_ERROR_BAD_STATE;
8253 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008254 }
8255
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008256 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008257 status = PSA_ERROR_INVALID_ARGUMENT;
8258 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008259 }
8260
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008262#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008264 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008266 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008268 driver_step = convert_jpake_computation_stage_to_driver_step(
8269 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008271#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008273 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008274 status = PSA_ERROR_NOT_SUPPORTED;
8275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008278 status = psa_driver_wrapper_pake_output(operation, driver_step,
8279 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280
8281 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008282 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 }
8284
8285 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008286#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008288 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008289 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008290 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008291 }
8292 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008293#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008295 status = PSA_ERROR_NOT_SUPPORTED;
8296 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008297 }
8298
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008299 return PSA_SUCCESS;
8300exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008301 psa_pake_abort(operation);
8302 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008303}
8304
8305psa_status_t psa_pake_input(
8306 psa_pake_operation_t *operation,
8307 psa_pake_step_t step,
8308 const uint8_t *input,
8309 size_t input_length)
8310{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008312 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008313 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8314 operation->primitive,
8315 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008316
8317 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008318 status = psa_pake_complete_inputs(operation);
8319 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008320 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008321 }
8322 }
8323
8324 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008325 status = PSA_ERROR_BAD_STATE;
8326 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008327 }
8328
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008329 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008330 status = PSA_ERROR_INVALID_ARGUMENT;
8331 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008332 }
8333
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008335#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008337 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008339 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008340 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008341 driver_step = convert_jpake_computation_stage_to_driver_step(
8342 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008344#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008345 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008346 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008347 status = PSA_ERROR_NOT_SUPPORTED;
8348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008351 status = psa_driver_wrapper_pake_input(operation, driver_step,
8352 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353
8354 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008355 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008356 }
8357
8358 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008359#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008361 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008362 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008363 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008364 }
8365 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008366#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008367 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008368 status = PSA_ERROR_NOT_SUPPORTED;
8369 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008370 }
8371
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008372 return PSA_SUCCESS;
8373exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008374 psa_pake_abort(operation);
8375 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008376}
8377
8378psa_status_t psa_pake_get_implicit_key(
8379 psa_pake_operation_t *operation,
8380 psa_key_derivation_operation_t *output)
8381{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008383 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008384 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008385 size_t shared_key_len = 0;
8386
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008387 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008388 status = PSA_ERROR_BAD_STATE;
8389 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008390 }
8391
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008392#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008393 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008394 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008395 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008396 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008397 status = PSA_ERROR_BAD_STATE;
8398 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008399 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008400 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008401#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008402 {
8403 status = PSA_ERROR_NOT_SUPPORTED;
8404 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008405 }
8406
Przemek Stekiel0c781802022-11-29 14:53:13 +01008407 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8408 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008409 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008410 &shared_key_len);
8411
8412 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008413 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008414 }
8415
8416 status = psa_key_derivation_input_bytes(output,
8417 PSA_KEY_DERIVATION_INPUT_SECRET,
8418 shared_key,
8419 shared_key_len);
8420
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008421 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008422exit:
8423 abort_status = psa_pake_abort(operation);
8424 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008425}
8426
8427psa_status_t psa_pake_abort(
8428 psa_pake_operation_t *operation)
8429{
Przemek Stekield69dca92023-01-31 19:59:20 +01008430 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008431
Przemek Stekield69dca92023-01-31 19:59:20 +01008432 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008433 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008434 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008435
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008436 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8437 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008438 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008439 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008440 }
8441 if (operation->data.inputs.user != NULL) {
8442 mbedtls_free(operation->data.inputs.user);
8443 }
8444 if (operation->data.inputs.peer != NULL) {
8445 mbedtls_free(operation->data.inputs.peer);
8446 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008447 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008448 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008449
Przemek Stekield69dca92023-01-31 19:59:20 +01008450 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008451}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008452#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008453
David Horstmannfde97392023-10-30 20:29:43 +00008454
David Horstmann1b7279a2023-11-15 15:18:30 +00008455/** Copy from an input buffer to a local copy.
8456 *
8457 * \param[in] input Pointer to input buffer.
8458 * \param[in] input_len Length of the input buffer.
8459 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8460 * \param[out] input_copy_len Length of the local copy buffer.
8461 * \return #PSA_SUCCESS, if the buffer was successfully
8462 * copied.
8463 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8464 * copy is too small to hold contents of the
8465 * input buffer.
8466 */
8467MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008468psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8469 uint8_t *input_copy, size_t input_copy_len)
8470{
8471 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008472 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008473 }
8474
David Horstmann372b8bb2023-11-24 16:21:04 +00008475#if defined(MBEDTLS_TEST_HOOKS)
8476 MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len);
8477#endif
8478
David Horstmann777e7412023-11-15 17:33:47 +00008479 if (input_len > 0) {
8480 memcpy(input_copy, input, input_len);
8481 }
David Horstmannfde97392023-10-30 20:29:43 +00008482
David Horstmann372b8bb2023-11-24 16:21:04 +00008483#if defined(MBEDTLS_TEST_HOOKS)
8484 MBEDTLS_TEST_MEMORY_POISON(input, input_len);
8485#endif
8486
David Horstmannfde97392023-10-30 20:29:43 +00008487 return PSA_SUCCESS;
8488}
8489
David Horstmann1b7279a2023-11-15 15:18:30 +00008490/** Copy from a local output buffer into a user-supplied one.
8491 *
8492 * \param[in] output_copy Pointer to a local buffer containing the output.
8493 * \param[in] output_copy_len Length of the local buffer.
8494 * \param[out] output Pointer to user-supplied output buffer.
8495 * \param[out] output_len Length of the user-supplied output buffer.
8496 * \return #PSA_SUCCESS, if the buffer was successfully
8497 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008498 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008499 * user-supplied output buffer is too small to
8500 * hold the contents of the local buffer.
8501 */
8502MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008503psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8504 uint8_t *output, size_t output_len)
8505{
8506 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008507 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008508 }
David Horstmann777e7412023-11-15 17:33:47 +00008509
David Horstmann372b8bb2023-11-24 16:21:04 +00008510#if defined(MBEDTLS_TEST_HOOKS)
8511 MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len);
8512#endif
8513
David Horstmann777e7412023-11-15 17:33:47 +00008514 if (output_copy_len > 0) {
8515 memcpy(output, output_copy, output_copy_len);
8516 }
8517
David Horstmann372b8bb2023-11-24 16:21:04 +00008518#if defined(MBEDTLS_TEST_HOOKS)
8519 MBEDTLS_TEST_MEMORY_POISON(output, output_len);
8520#endif
8521
David Horstmann8978f5c2023-10-30 20:37:12 +00008522 return PSA_SUCCESS;
8523}
8524
David Horstmannf1734052023-11-20 17:15:32 +00008525psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8526 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008527{
8528 psa_status_t status;
8529
David Horstmann9db14482023-11-23 15:50:37 +00008530 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008531
David Horstmannc5cc1c32023-11-15 18:11:26 +00008532 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008533 return PSA_SUCCESS;
8534 }
8535
David Horstmannf1734052023-11-20 17:15:32 +00008536 local_input->buffer = mbedtls_calloc(input_len, 1);
8537 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008538 /* Since we dealt with the zero-length case above, we know that
8539 * a NULL return value means a failure of allocation. */
8540 return PSA_ERROR_INSUFFICIENT_MEMORY;
8541 }
David Horstmannf1734052023-11-20 17:15:32 +00008542 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008543
David Horstmannf1734052023-11-20 17:15:32 +00008544 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008545
8546 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008547 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008548 if (status != PSA_SUCCESS) {
8549 goto error;
8550 }
8551
8552 return PSA_SUCCESS;
8553
8554error:
David Horstmannf1734052023-11-20 17:15:32 +00008555 mbedtls_free(local_input->buffer);
8556 local_input->buffer = NULL;
8557 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008558 return status;
8559}
8560
David Horstmannf1734052023-11-20 17:15:32 +00008561void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008562{
David Horstmannf1734052023-11-20 17:15:32 +00008563 mbedtls_free(local_input->buffer);
8564 local_input->buffer = NULL;
8565 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008566}
8567
David Horstmann89875a42023-11-20 12:54:09 +00008568psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8569 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008570{
David Horstmann9db14482023-11-23 15:50:37 +00008571 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008572
David Horstmannc5cc1c32023-11-15 18:11:26 +00008573 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008574 return PSA_SUCCESS;
8575 }
David Horstmann89875a42023-11-20 12:54:09 +00008576 local_output->buffer = mbedtls_calloc(output_len, 1);
8577 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008578 /* Since we dealt with the zero-length case above, we know that
8579 * a NULL return value means a failure of allocation. */
8580 return PSA_ERROR_INSUFFICIENT_MEMORY;
8581 }
David Horstmann89875a42023-11-20 12:54:09 +00008582 local_output->length = output_len;
8583 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008584
8585 return PSA_SUCCESS;
8586}
8587
David Horstmann89875a42023-11-20 12:54:09 +00008588psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008589{
David Horstmannc335a4e2023-11-15 15:57:32 +00008590 psa_status_t status;
8591
David Horstmann89875a42023-11-20 12:54:09 +00008592 if (local_output->buffer == NULL) {
8593 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008594 return PSA_SUCCESS;
8595 }
David Horstmann89875a42023-11-20 12:54:09 +00008596 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008597 /* We have an internal copy but nothing to copy back to. */
8598 return PSA_ERROR_CORRUPTION_DETECTED;
8599 }
8600
David Horstmann89875a42023-11-20 12:54:09 +00008601 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8602 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008603 if (status != PSA_SUCCESS) {
8604 return status;
8605 }
David Horstmann9467ea32023-11-08 17:44:18 +00008606
David Horstmann89875a42023-11-20 12:54:09 +00008607 mbedtls_free(local_output->buffer);
8608 local_output->buffer = NULL;
8609 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008610
8611 return PSA_SUCCESS;
8612}
8613
Gilles Peskinee59236f2018-01-27 23:32:46 +01008614#endif /* MBEDTLS_PSA_CRYPTO_C */