blob: 070484cf9d33b3ff33ccd1f236973a5fb0bde416 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinee59236f2018-01-27 23:32:46 +01007 */
8
Gilles Peskinedb09ef62020-06-03 01:43:33 +02009#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010010#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010011
12#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030013
John Durkop07cc04a2020-11-16 22:08:34 -080014#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
15#include "check_crypto_config.h"
16#endif
17
Gilles Peskinee59236f2018-01-27 23:32:46 +010018#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010019#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010020
Ronald Crond6d28882020-12-14 14:56:02 +010021#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010022#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010023#include "psa_crypto_invasive.h"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000024#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000025#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010027#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010028#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010029#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010031#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb2b64d32020-12-14 16:43:58 +010040#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010041
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010042#include <stdlib.h>
43#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010046#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020047#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000048#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020049#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020051#include "mbedtls/chacha20.h"
52#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/cipher.h"
54#include "mbedtls/ccm.h"
55#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010056#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020058#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010059#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/error.h"
62#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/md5.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010064#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000065#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010066#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000067#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/sha1.h"
71#include "mbedtls/sha256.h"
72#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020073#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
76 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
77 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020078#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020079#endif
80
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081/****************************************************************/
82/* Global data, support functions and library management */
83/****************************************************************/
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020088}
89
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010090/* Values for psa_global_data_t::rng_state */
91#define RNG_NOT_INITIALIZED 0
92#define RNG_INITIALIZED 1
93#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +010096 uint8_t initialized;
97 uint8_t rng_state;
98 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
David Horstmanne9a88ab2023-11-29 17:07:13 +0000113/* Substitute an input buffer for a local copy of itself.
114 * Assumptions:
115 * - psa_status_t status exists
116 * - An exit label is declared
117 * - The name <buffer>_copy is not used for the given value of <buffer>
118 */
119#define SWAP_FOR_LOCAL_INPUT(input, length) \
120 psa_crypto_local_input_t input ## _copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
121 status = psa_crypto_local_input_alloc(input, length, &input ## _copy); \
122 if (status != PSA_SUCCESS) { \
123 goto exit; \
124 } \
125 input = input ## _copy.buffer;
126
127#define FREE_LOCAL_INPUT(input) \
128 psa_crypto_local_input_free(&input ## _copy);
129
130/* Substitute an output buffer for a local copy of itself.
131 * Assumptions:
132 * - psa_status_t status exists
133 * - An exit label is declared
134 * - The name <buffer>_copy is not used for the given value of <buffer>
135 */
136#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
137 psa_crypto_local_output_t output ## _copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
138 status = psa_crypto_local_output_alloc(output, length, &output ## _copy); \
139 if (status != PSA_SUCCESS) { \
140 goto exit; \
141 } \
142 output = output ## _copy.buffer;
143
144#define FREE_LOCAL_OUTPUT(output) \
145 psa_status_t local_output_free_status; \
146 local_output_free_status = psa_crypto_local_output_free(&output ## _copy); \
147 if (local_output_free_status != PSA_SUCCESS) { \
148 status = local_output_free_status; \
149 }
150
151
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100152int psa_can_do_hash(psa_algorithm_t hash_alg)
153{
154 (void) hash_alg;
155 return global_data.drivers_initialized;
156}
Valerio Settia55f0422023-07-10 15:34:41 +0200157#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200158 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200159 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200160static int psa_is_dh_key_size_valid(size_t bits)
161{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200162 if (bits != 2048 && bits != 3072 && bits != 4096 &&
163 bits != 6144 && bits != 8192) {
164 return 0;
165 }
166
167 return 1;
168}
Valerio Settia55f0422023-07-10 15:34:41 +0200169#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200170 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200171 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100174{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100175 /* Mbed TLS error codes can combine a high-level error code and a
176 * low-level error code. The low-level error usually reflects the
177 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 int low_level_ret = -(-ret & 0x007f);
179 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100180 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100183#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100184 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
185 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100187 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
188 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100189#endif
190
191#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200192 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
193 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
194 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
195 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
196 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200198 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200200 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100202#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200203
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100204#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000205 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100208#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100210#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100217#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200218 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100220#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200221
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100222#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200223 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200225 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100227#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200228
Dave Rodgman509b5672023-08-16 19:26:23 +0100229#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100238 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100244#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
247 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100248 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
249 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
253 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100259#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100262#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100263
Gilles Peskinee59236f2018-01-27 23:32:46 +0100264 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
265 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
266 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100269#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200272 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100274 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100276#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Gilles Peskine82e57d12020-11-13 21:31:17 +0100278#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100280 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
281 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100282 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100284 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
285 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100287 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100289#endif
290
Dave Rodgmandea266f2023-08-31 11:52:43 +0100291#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100298#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100301#endif
302#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100303
Dave Rodgman509b5672023-08-16 19:26:23 +0100304#if defined(MBEDTLS_BIGNUM_C)
305#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100306 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100308#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100309 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100311 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100313 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100315 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100317 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100319 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100321 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100323#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100324
Dave Rodgman509b5672023-08-16 19:26:23 +0100325#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100326 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100328 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
329 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100331#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
332 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100333 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100335#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100336 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
337 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100339 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100341 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
342 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346 case MBEDTLS_ERR_PK_INVALID_ALG:
347 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
348 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200352 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100354#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100355
Gilles Peskineff2d2002019-05-06 15:26:23 +0200356 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200358 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200360
Dave Rodgman509b5672023-08-16 19:26:23 +0100361#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100362 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100366 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100368 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100370 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
371 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100373 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100379#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200380
Dave Rodgman1dab4452023-09-01 09:59:51 +0100381#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300382 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300383 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300385 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300387 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300389 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
390 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300392 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100394 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100396
Dave Rodgman509b5672023-08-16 19:26:23 +0100397#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000398 case MBEDTLS_ERR_ECP_IN_PROGRESS:
399 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100400#endif
401#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000402
Janos Follatha13b9052019-11-22 12:48:59 +0000403 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300405
Gilles Peskinee59236f2018-01-27 23:32:46 +0100406 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100408 }
409}
410
Paul Elliottdc42ca82023-02-24 18:11:59 +0000411/**
412 * \brief For output buffers which contain "tags"
413 * (outputs that may be checked for validity like
414 * hashes, MACs and signatures), fill the unused
415 * part of the output buffer (the whole buffer on
416 * error, the trailing part on success) with
417 * something that isn't a valid tag (barring an
418 * attack on the tag and deliberately-crafted
419 * input), in case the caller doesn't check the
420 * return status properly.
421 *
422 * \param output_buffer Pointer to buffer to wipe. May not be NULL
423 * unless \p output_buffer_size is zero.
424 * \param status Status of function called to generate
425 * output_buffer originally
426 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
427 * could be NULL.
428 * \param output_buffer_length Length of data written to output_buffer, must be
429 * less than \p output_buffer_size
430 */
431static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000432 size_t output_buffer_size, size_t output_buffer_length)
433{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000434 size_t offset = 0;
435
436 if (output_buffer_size == 0) {
437 /* If output_buffer_size is 0 then we have nothing to do. We must not
438 call memset because output_buffer may be NULL in this case */
439 return;
440 }
441
442 if (status == PSA_SUCCESS) {
443 offset = output_buffer_length;
444 }
445
446 memset(output_buffer + offset, '!', output_buffer_size - offset);
447}
448
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200449
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200450
451
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452/****************************************************************/
453/* Key management */
454/****************************************************************/
455
Valerio Settia9aab1a2023-06-19 13:39:54 +0200456#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200457psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
458 size_t *bits)
459{
460 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200461#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200462 case MBEDTLS_ECP_DP_SECP192R1:
463 *bits = 192;
464 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100465#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200466#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200467 case MBEDTLS_ECP_DP_SECP224R1:
468 *bits = 224;
469 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100470#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200471#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200472 case MBEDTLS_ECP_DP_SECP256R1:
473 *bits = 256;
474 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100475#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200476#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200477 case MBEDTLS_ECP_DP_SECP384R1:
478 *bits = 384;
479 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100480#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200481#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200482 case MBEDTLS_ECP_DP_SECP521R1:
483 *bits = 521;
484 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100485#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200486#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 case MBEDTLS_ECP_DP_BP256R1:
488 *bits = 256;
489 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100490#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200491#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200492 case MBEDTLS_ECP_DP_BP384R1:
493 *bits = 384;
494 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100495#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200496#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200497 case MBEDTLS_ECP_DP_BP512R1:
498 *bits = 512;
499 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100500#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200501#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200502 case MBEDTLS_ECP_DP_CURVE25519:
503 *bits = 255;
504 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100505#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200506#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200507 case MBEDTLS_ECP_DP_SECP192K1:
508 *bits = 192;
509 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100510#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200511#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200512 case MBEDTLS_ECP_DP_SECP224K1:
513 *bits = 224;
514 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100515#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200516#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200517 case MBEDTLS_ECP_DP_SECP256K1:
518 *bits = 256;
519 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100520#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200521#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200522 case MBEDTLS_ECP_DP_CURVE448:
523 *bits = 448;
524 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100525#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200526 default:
527 *bits = 0;
528 return 0;
529 }
530}
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
533 size_t bits,
534 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100537 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100539#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100540 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100542#endif
543#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100544 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#endif
547#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100550#endif
551#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100552 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100554#endif
555#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100556 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100558 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (bits_is_sloppy) {
560 return MBEDTLS_ECP_DP_SECP521R1;
561 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 break;
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_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100569#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100570 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100572#endif
573#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100574 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#endif
577#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100578 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100580#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100581 }
582 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100583
Paul Elliott8ff510a2020-06-02 17:19:28 +0100584 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100586#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100587 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100589 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (bits_is_sloppy) {
591 return MBEDTLS_ECP_DP_CURVE25519;
592 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100593 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100594#endif
595#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100596 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100598#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100599 }
600 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100601
Paul Elliott8ff510a2020-06-02 17:19:28 +0100602 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100604#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100605 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100607#endif
608#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100609 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100611#endif
612#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100613 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100615#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100616 }
617 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200618 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100619
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100620 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200622}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200623#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
626 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627{
628 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200631 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200632 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200633 case PSA_KEY_TYPE_PASSWORD:
634 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200635 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100636#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (bits != 128 && bits != 192 && bits != 256) {
639 return PSA_ERROR_INVALID_ARGUMENT;
640 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200641 break;
642#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200643#if defined(PSA_WANT_KEY_TYPE_ARIA)
644 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (bits != 128 && bits != 192 && bits != 256) {
646 return PSA_ERROR_INVALID_ARGUMENT;
647 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200648 break;
649#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100650#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200651 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (bits != 128 && bits != 192 && bits != 256) {
653 return PSA_ERROR_INVALID_ARGUMENT;
654 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200655 break;
656#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100657#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200658 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (bits != 64 && bits != 128 && bits != 192) {
660 return PSA_ERROR_INVALID_ARGUMENT;
661 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200662 break;
663#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100664#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200665 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (bits != 256) {
667 return PSA_ERROR_INVALID_ARGUMENT;
668 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200669 break;
670#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200671 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200673 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (bits % 8 != 0) {
675 return PSA_ERROR_INVALID_ARGUMENT;
676 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200679}
680
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100681/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100682 *
Steven Cooremancd640932021-03-08 12:00:27 +0100683 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
684 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100685 *
686 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100687 * \param[in] key_type The key type of the key to be used with the
688 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100689 *
690 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100691 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100692 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100693 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100694 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100695MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100696 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100698{
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (PSA_ALG_IS_HMAC(algorithm)) {
700 if (key_type == PSA_KEY_TYPE_HMAC) {
701 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100702 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100703 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
706 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
707 * key. */
708 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
709 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
710 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
711 * the block length (larger than 1) for block ciphers. */
712 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
713 return PSA_SUCCESS;
714 }
715 }
716 }
717
718 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100719}
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
722 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200723{
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (slot->key.data != NULL) {
725 return PSA_ERROR_ALREADY_EXISTS;
726 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 slot->key.data = mbedtls_calloc(1, buffer_length);
729 if (slot->key.data == NULL) {
730 return PSA_ERROR_INSUFFICIENT_MEMORY;
731 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200732
Ronald Cronea0f8a62020-11-25 17:52:23 +0100733 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200735}
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
738 const uint8_t *data,
739 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200740{
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 psa_status_t status = psa_allocate_buffer_to_slot(slot,
742 data_length);
743 if (status != PSA_SUCCESS) {
744 return status;
745 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 memcpy(slot->key.data, data, data_length);
748 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200749}
750
Ronald Crona0fe59f2020-11-29 11:14:53 +0100751psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100752 const psa_key_attributes_t *attributes,
753 const uint8_t *data, size_t data_length,
754 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100757 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
758 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100759
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200760 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (data_length == 0) {
762 return PSA_ERROR_NOT_SUPPORTED;
763 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (key_type_is_raw_bytes(type)) {
766 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
769 *bits);
770 if (status != PSA_SUCCESS) {
771 return status;
772 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200773
Ronald Crondd04d422020-11-28 15:14:42 +0100774 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100776 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 return PSA_SUCCESS;
780 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200781#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100783 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200784 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100785 return PSA_ERROR_INVALID_ARGUMENT;
786 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200787 return mbedtls_psa_ffdh_import_key(attributes,
788 data, data_length,
789 key_buffer, key_buffer_size,
790 key_buffer_length,
791 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100792 }
Valerio Settia55f0422023-07-10 15:34:41 +0200793#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200794 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200795#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
797 if (PSA_KEY_TYPE_IS_ECC(type)) {
798 return mbedtls_psa_ecp_import_key(attributes,
799 data, data_length,
800 key_buffer, key_buffer_size,
801 key_buffer_length,
802 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100803 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200804#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800805 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200806#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
807 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
809 if (PSA_KEY_TYPE_IS_RSA(type)) {
810 return mbedtls_psa_rsa_import_key(attributes,
811 data, data_length,
812 key_buffer, key_buffer_size,
813 key_buffer_length,
814 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200815 }
Valerio Settife478902023-07-25 12:27:19 +0200816#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
817 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800818 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100819 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100822}
823
Gilles Peskinef603c712019-01-19 13:40:11 +0100824/** Calculate the intersection of two algorithm usage policies.
825 *
826 * Return 0 (which allows no operation) on incompatibility.
827 */
828static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100829 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100830 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100832{
Gilles Peskine549ea862019-05-22 11:45:59 +0200833 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (alg1 == alg2) {
835 return alg1;
836 }
Steven Cooreman03488022021-02-18 12:07:20 +0100837 /* If the policies are from the same hash-and-sign family, check
838 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
840 PSA_ALG_IS_SIGN_HASH(alg2) &&
841 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
842 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
843 return alg2;
844 }
845 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
846 return alg1;
847 }
Steven Cooreman03488022021-02-18 12:07:20 +0100848 }
849 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100850 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100851 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
853 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
854 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
855 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
856 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100857 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100858
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100859 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
861 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
862 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
863 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100864 }
865 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
867 (alg1_len <= alg2_len)) {
868 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100869 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
871 (alg2_len <= alg1_len)) {
872 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100873 }
874 }
875 /* If the policies are from the same MAC family, check whether one
876 * of them is a minimum-MAC-length policy. Calculate the most
877 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
879 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
880 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100881 /* Validate the combination of key type and algorithm. Since the base
882 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
884 return 0;
885 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100886
Steven Cooreman31a876d2021-03-03 20:47:40 +0100887 /* Get the (exact or at-least) output lengths for both sides of the
888 * requested intersection. None of the currently supported algorithms
889 * have an output length dependent on the actual key size, so setting it
890 * to a bogus value of 0 is currently OK.
891 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100892 * Note that for at-least-this-length wildcard algorithms, the output
893 * length is set to the shortest allowed length, which allows us to
894 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
896 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100897 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100898
Steven Cooremana1d83222021-02-25 10:20:29 +0100899 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
901 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
902 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100903 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100904
905 /* If only one is an at-least-this-length policy, the intersection would
906 * be the other (fixed-length) policy as long as said fixed length is
907 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
909 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100910 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
912 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100913 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100914
Steven Cooremancd640932021-03-08 12:00:27 +0100915 /* If none of them are wildcards, check whether they define the same tag
916 * length. This is still possible here when one is default-length and
917 * the other specific-length. Ensure to always return the
918 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (alg1_len == alg2_len) {
920 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
921 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100922 }
923 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100925}
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927static int psa_key_algorithm_permits(psa_key_type_t key_type,
928 psa_algorithm_t policy_alg,
929 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930{
Gilles Peskine549ea862019-05-22 11:45:59 +0200931 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (requested_alg == policy_alg) {
933 return 1;
934 }
Steven Cooreman03488022021-02-18 12:07:20 +0100935 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
936 * and requested_alg is the same hash-and-sign family with any hash,
937 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
939 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
940 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
941 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100942 }
943 /* If policy_alg is a wildcard AEAD algorithm of the same base as
944 * the requested algorithm, check the requested tag length to be
945 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (PSA_ALG_IS_AEAD(policy_alg) &&
947 PSA_ALG_IS_AEAD(requested_alg) &&
948 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
949 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
950 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
951 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
952 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100953 }
Steven Cooremancd640932021-03-08 12:00:27 +0100954 /* If policy_alg is a MAC algorithm of the same base as the requested
955 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (PSA_ALG_IS_MAC(policy_alg) &&
957 PSA_ALG_IS_MAC(requested_alg) &&
958 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
959 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100960 /* Validate the combination of key type and algorithm. Since the policy
961 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
963 return 0;
964 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100965
Steven Cooreman31a876d2021-03-03 20:47:40 +0100966 /* Get both the requested output length for the algorithm which is to be
967 * verified, and the default output length for the base algorithm.
968 * Note that none of the currently supported algorithms have an output
969 * length dependent on actual key size, so setting it to a bogus value
970 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100971 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100973 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 key_type, 0,
975 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100976
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100977 /* If the policy is default-length, only allow an algorithm with
978 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
980 return requested_output_length == default_output_length;
981 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100982
983 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100984 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
986 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
987 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100988 }
989
Steven Cooremancd640932021-03-08 12:00:27 +0100990 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
991 * check for the requested MAC length to be equal to or longer than the
992 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
994 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
995 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100996 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200997 }
Steven Cooremance48e852020-10-05 16:02:45 +0200998 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200999 * a key derivation with that key agreement should also be allowed. This
1000 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
1002 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
1003 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
1004 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +02001005 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001006 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001008}
1009
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001010/** Test whether a policy permits an algorithm.
1011 *
1012 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +01001013 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001014 * \note This function requires providing the key type for which the policy is
1015 * being validated, 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 Cooreman7e39f052021-02-23 14:18:32 +01001019 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1020 * allowed by the \p policy.
1021 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1022 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1023 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001024 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1026 psa_key_type_t key_type,
1027 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001028{
Steven Cooremand788fab2021-02-25 11:29:17 +01001029 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (alg == 0) {
1031 return PSA_ERROR_INVALID_ARGUMENT;
1032 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001033
Steven Cooreman7e39f052021-02-23 14:18:32 +01001034 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (PSA_ALG_IS_WILDCARD(alg)) {
1036 return PSA_ERROR_INVALID_ARGUMENT;
1037 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1040 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1041 return PSA_SUCCESS;
1042 } else {
1043 return PSA_ERROR_NOT_PERMITTED;
1044 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001045}
1046
Gilles Peskinef603c712019-01-19 13:40:11 +01001047/** Restrict a key policy based on a constraint.
1048 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001049 * \note This function requires providing the key type for which the policy is
1050 * being restricted, since some algorithm policy definitions (e.g. MAC)
1051 * have different properties depending on what kind of cipher it is
1052 * combined with.
1053 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001054 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 * \param[in,out] policy The policy to restrict.
1056 * \param[in] constraint The policy constraint to apply.
1057 *
1058 * \retval #PSA_SUCCESS
1059 * \c *policy contains the intersection of the original value of
1060 * \c *policy and \c *constraint.
1061 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001062 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001063 * \c *policy is unchanged.
1064 */
1065static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001066 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001067 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001069{
1070 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1072 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001073 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1075 constraint->alg2);
1076 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1077 return PSA_ERROR_INVALID_ARGUMENT;
1078 }
1079 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1080 return PSA_ERROR_INVALID_ARGUMENT;
1081 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001082 policy->usage &= constraint->usage;
1083 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001084 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001086}
1087
Przemek Stekiel061f6942022-11-30 10:51:35 +01001088/** Get the description of a key given its identifier and policy constraints
1089 * and lock it.
1090 *
1091 * The key must have allow all the usage flags set in \p usage. If \p alg is
1092 * nonzero, the key must allow operations with this algorithm. If \p alg is
1093 * zero, the algorithm is not checked.
1094 *
1095 * In case of a persistent key, the function loads the description of the key
1096 * into a key slot if not already done.
1097 *
1098 * On success, the returned key slot is locked. It is the responsibility of
1099 * the caller to unlock the key slot when it does not access it anymore.
1100 */
1101static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001102 mbedtls_svc_key_id_t key,
1103 psa_key_slot_t **p_slot,
1104 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001106{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001108 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 status = psa_get_and_lock_key_slot(key, p_slot);
1111 if (status != PSA_SUCCESS) {
1112 return status;
1113 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001114 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001115
1116 /* Enforce that usage policy for the key slot contains all the flags
1117 * required by the usage parameter. There is one exception: public
1118 * keys can always be exported, so we treat public key objects as
1119 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001121 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001125 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001126 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001127 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001128
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001129 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (alg != 0) {
1131 status = psa_key_policy_permits(&slot->attr.policy,
1132 slot->attr.type,
1133 alg);
1134 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001135 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001137 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001140
1141error:
1142 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001146}
Darryl Green940d72c2018-07-13 13:18:51 +01001147
Ronald Cron5c522922020-11-14 16:35:34 +01001148/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001149 *
1150 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001151 * available, as opposed to a key in a secure element and/or to be used
1152 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001153 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001154 * This is a temporary function that may be used instead of
1155 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1156 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001157 *
Ronald Cron5c522922020-11-14 16:35:34 +01001158 * On success, the returned key slot is locked. It is the responsibility of the
1159 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001160 */
Ronald Cron5c522922020-11-14 16:35:34 +01001161static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1162 mbedtls_svc_key_id_t key,
1163 psa_key_slot_t **p_slot,
1164 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001166{
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1168 usage, alg);
1169 if (status != PSA_SUCCESS) {
1170 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001171 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1174 psa_unlock_key_slot(*p_slot);
1175 *p_slot = NULL;
1176 return PSA_ERROR_NOT_SUPPORTED;
1177 }
1178
1179 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001180}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001183{
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001185 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001187
Ronald Cronea0f8a62020-11-25 17:52:23 +01001188 slot->key.data = NULL;
1189 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001192}
1193
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001194/** Completely wipe a slot in memory, including its policy.
1195 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001197{
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 /*
1201 * As the return error code may not be handled in case of multiple errors,
1202 * do our best to report an unexpected lock counter. Assert with
1203 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1204 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1205 * function is called as part of the execution of a test suite, the
1206 * execution of the test suite is stopped in error if the assertion fails.
1207 */
1208 if (slot->lock_count != 1) {
1209 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001210 status = PSA_ERROR_CORRUPTION_DETECTED;
1211 }
1212
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001213 /* Multipart operations may still be using the key. This is safe
1214 * because all multipart operation objects are independent from
1215 * the key slot: if they need to access the key after the setup
1216 * phase, they have a copy of the key. Note that this means that
1217 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001218 /* At this point, key material and other type-specific content has
1219 * been wiped. Clear remaining metadata. We can call memset and not
1220 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 memset(slot, 0, sizeof(*slot));
1222 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001223}
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001227 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001228 psa_status_t status; /* status of the last operation */
1229 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001230#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1231 psa_se_drv_table_entry_t *driver;
1232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (mbedtls_svc_key_id_is_null(key)) {
1235 return PSA_SUCCESS;
1236 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001237
Ronald Cronf2911112020-10-29 17:51:10 +01001238 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001239 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001240 * key, this will load the key description from persistent memory if not
1241 * done yet. We cannot avoid this loading as without it we don't know if
1242 * the key is operated by an SE or not and this information is needed by
1243 * the current implementation.
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 status = psa_get_and_lock_key_slot(key, &slot);
1246 if (status != PSA_SUCCESS) {
1247 return status;
1248 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001249
Ronald Cronf2911112020-10-29 17:51:10 +01001250 /*
1251 * If the key slot containing the key description is under access by the
1252 * library (apart from the present access), the key cannot be destroyed
1253 * yet. For the time being, just return in error. Eventually (to be
1254 * implemented), the key should be destroyed when all accesses have
1255 * stopped.
1256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (slot->lock_count > 1) {
1258 psa_unlock_key_slot(slot);
1259 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001263 /* Refuse the destruction of a read-only key (which may or may not work
1264 * if we attempt it, depending on whether the key is merely read-only
1265 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001266 * Just do the best we can, which is to wipe the copy in memory
1267 * (done in this function's cleanup code). */
1268 overall_status = PSA_ERROR_NOT_PERMITTED;
1269 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001270 }
1271
Gilles Peskine354f7672019-07-12 23:46:38 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1274 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001275 /* For a key in a secure element, we need to do three things:
1276 * remove the key file in internal storage, destroy the
1277 * key inside the secure element, and update the driver's
1278 * persistent data. Start a transaction that will encompass these
1279 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001283 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 status = psa_crypto_save_transaction();
1285 if (status != PSA_SUCCESS) {
1286 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287 /* We should still try to destroy the key in the secure
1288 * element and the key metadata in storage. This is especially
1289 * important if the error is that the storage is full.
1290 * But how to do it exactly without risking an inconsistent
1291 * state after a reset?
1292 * https://github.com/ARMmbed/mbed-crypto/issues/215
1293 */
1294 overall_status = status;
1295 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001296 }
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 status = psa_destroy_se_key(driver,
1299 psa_key_slot_get_slot_number(slot));
1300 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001301 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001303 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001304#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1305
Darryl Greend49a4992018-06-18 17:27:26 +01001306#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1308 status = psa_destroy_persistent_key(slot->attr.id);
1309 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001310 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001313 /* TODO: other slots may have a copy of the same key. We should
1314 * invalidate them.
1315 * https://github.com/ARMmbed/mbed-crypto/issues/214
1316 */
Darryl Greend49a4992018-06-18 17:27:26 +01001317 }
1318#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001319
Gilles Peskinefc762652019-07-22 19:30:34 +02001320#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (driver != NULL) {
1322 status = psa_save_se_persistent_data(driver);
1323 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001324 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 }
1326 status = psa_crypto_stop_transaction();
1327 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001328 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001330 }
1331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1332
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001335 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001337 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 }
1339 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340}
1341
Valerio Settib2bcedb2023-07-10 11:24:00 +02001342#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001343 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001344static psa_status_t psa_get_rsa_public_exponent(
1345 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001347{
1348 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001350 uint8_t *buffer = NULL;
1351 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1355 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001356 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 }
1358 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001359 /* It's the default value, which is reported as an empty string,
1360 * so there's nothing to do. */
1361 goto exit;
1362 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 buflen = mbedtls_mpi_size(&mpi);
1365 buffer = mbedtls_calloc(1, buflen);
1366 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001367 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1368 goto exit;
1369 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1371 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001374 attributes->domain_parameters = buffer;
1375 attributes->domain_parameters_size = buflen;
1376
1377exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 mbedtls_mpi_free(&mpi);
1379 if (ret != 0) {
1380 mbedtls_free(buffer);
1381 }
1382 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001383}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001384#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001385 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001386
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001387/** Retrieve all the publicly-accessible attributes of a key.
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1390 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001393 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001394 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1399 if (status != PSA_SUCCESS) {
1400 return status;
1401 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001402
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001403 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1405 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001406
1407#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1409 psa_set_key_slot_number(attributes,
1410 psa_key_slot_get_slot_number(slot));
1411 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001412#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001415#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1416 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001417 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001418 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001419 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001420 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001421 * is not yet implemented.
1422 * https://github.com/ARMmbed/mbed-crypto/issues/216
1423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001425 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001426
Ronald Cron90857082020-11-25 14:58:33 +01001427 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 slot->attr.type,
1429 slot->key.data,
1430 slot->key.bytes,
1431 &rsa);
1432 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001433 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 status = psa_get_rsa_public_exponent(rsa,
1437 attributes);
1438 mbedtls_rsa_free(rsa);
1439 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001440 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001441 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001442#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1443 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001444 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001445 default:
1446 /* Nothing else to do. */
1447 break;
1448 }
1449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (status != PSA_SUCCESS) {
1451 psa_reset_key_attributes(attributes);
1452 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457}
1458
Gilles Peskinec8000c02019-08-02 20:15:51 +02001459#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1460psa_status_t psa_get_key_slot_number(
1461 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001463{
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001465 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return PSA_SUCCESS;
1467 } else {
1468 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001469 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001470}
1471#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1474 size_t key_buffer_size,
1475 uint8_t *data,
1476 size_t data_size,
1477 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001478{
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (key_buffer_size > data_size) {
1480 return PSA_ERROR_BUFFER_TOO_SMALL;
1481 }
1482 memcpy(data, key_buffer, key_buffer_size);
1483 memset(data + key_buffer_size, 0,
1484 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001485 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001487}
1488
Ronald Cron7285cda2020-11-26 14:46:37 +01001489psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001490 const psa_key_attributes_t *attributes,
1491 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001493{
Ronald Crond18b5f82020-11-25 16:46:05 +01001494 psa_key_type_t type = attributes->core.type;
1495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (key_type_is_raw_bytes(type) ||
1497 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001498 PSA_KEY_TYPE_IS_ECC(type) ||
1499 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return psa_export_key_buffer_internal(
1501 key_buffer, key_buffer_size,
1502 data, data_size, data_length);
1503 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001504 /* This shouldn't happen in the reference implementation, but
1505 it is valid for a special-purpose implementation to omit
1506 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 }
1509}
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1512 uint8_t *data,
1513 size_t data_size,
1514 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001515{
1516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1517 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1518 psa_key_slot_t *slot;
1519
Ronald Crone907e552021-01-18 13:22:38 +01001520 /* Reject a zero-length output buffer now, since this can never be a
1521 * valid key representation. This way we know that data must be a valid
1522 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (data_size == 0) {
1524 return PSA_ERROR_BUFFER_TOO_SMALL;
1525 }
Ronald Crone907e552021-01-18 13:22:38 +01001526
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527 /* Set the key to empty now, so that even when there are errors, we always
1528 * set data_length to a value between 0 and data_size. On error, setting
1529 * the key to empty is a good choice because an empty key representation is
1530 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001531 *data_length = 0;
1532
Ronald Cron9486f9d2020-11-26 13:36:23 +01001533 /* Export requires the EXPORT flag. There is an exception for public keys,
1534 * which don't require any flag, but
1535 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1538 PSA_KEY_USAGE_EXPORT, 0);
1539 if (status != PSA_SUCCESS) {
1540 return status;
1541 }
mohammad160306e79202018-03-28 13:17:44 +03001542
Ronald Crond18b5f82020-11-25 16:46:05 +01001543 psa_key_attributes_t attributes = {
1544 .core = slot->attr
1545 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 status = psa_driver_wrapper_export_key(&attributes,
1547 slot->key.data, slot->key.bytes,
1548 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001553}
1554
Ronald Cron7285cda2020-11-26 14:46:37 +01001555psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001556 const psa_key_attributes_t *attributes,
1557 const uint8_t *key_buffer,
1558 size_t key_buffer_size,
1559 uint8_t *data,
1560 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001562{
Ronald Crond18b5f82020-11-25 16:46:05 +01001563 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001564
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001565 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001566 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1567 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001568 /* Exporting public -> public */
1569 return psa_export_key_buffer_internal(
1570 key_buffer, key_buffer_size,
1571 data, data_size, data_length);
1572 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001573#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001574 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1575 return mbedtls_psa_rsa_export_public_key(attributes,
1576 key_buffer,
1577 key_buffer_size,
1578 data,
1579 data_size,
1580 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001581#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001582 /* We don't know how to convert a private RSA key to public. */
1583 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001584#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001585 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001586 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001587#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001588 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1589 return mbedtls_psa_ecp_export_public_key(attributes,
1590 key_buffer,
1591 key_buffer_size,
1592 data,
1593 data_size,
1594 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001595#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001596 /* We don't know how to convert a private ECC key to public */
1597 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001598#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001599 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001600 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001601#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001602 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001603 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001604 key_buffer,
1605 key_buffer_size,
1606 data, data_size,
1607 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001608#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001609 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001610#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001611 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001613 (void) key_buffer;
1614 (void) key_buffer_size;
1615 (void) data;
1616 (void) data_size;
1617 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001619 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620}
Ronald Crond18b5f82020-11-25 16:46:05 +01001621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1623 uint8_t *data,
1624 size_t data_size,
1625 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001626{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001628 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001629 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001630 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001631
Ronald Crone907e552021-01-18 13:22:38 +01001632 /* Reject a zero-length output buffer now, since this can never be a
1633 * valid key representation. This way we know that data must be a valid
1634 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (data_size == 0) {
1636 return PSA_ERROR_BUFFER_TOO_SMALL;
1637 }
Ronald Crone907e552021-01-18 13:22:38 +01001638
Darryl Greendd8fb772018-11-07 16:00:44 +00001639 /* Set the key to empty now, so that even when there are errors, we always
1640 * set data_length to a value between 0 and data_size. On error, setting
1641 * the key to empty is a good choice because an empty key representation is
1642 * unlikely to be accepted anywhere. */
1643 *data_length = 0;
1644
1645 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1647 if (status != PSA_SUCCESS) {
1648 return status;
1649 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1652 status = PSA_ERROR_INVALID_ARGUMENT;
1653 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001654 }
1655
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001656 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001657 .core = slot->attr
1658 };
Ronald Cron67227982020-11-26 15:16:05 +01001659 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001660 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001662
1663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001667}
1668
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001669MBEDTLS_STATIC_ASSERT(
1670 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1671 "One or more key attribute flag is listed as both external-only and dual-use")
1672MBEDTLS_STATIC_ASSERT(
1673 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1674 "One or more key attribute flag is listed as both internal-only and dual-use")
1675MBEDTLS_STATIC_ASSERT(
1676 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1677 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001678
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001679/** Validate that a key policy is internally well-formed.
1680 *
1681 * This function only rejects invalid policies. It does not validate the
1682 * consistency of the policy with respect to other attributes of the key
1683 * such as the key type.
1684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001686{
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1688 PSA_KEY_USAGE_COPY |
1689 PSA_KEY_USAGE_ENCRYPT |
1690 PSA_KEY_USAGE_DECRYPT |
1691 PSA_KEY_USAGE_SIGN_MESSAGE |
1692 PSA_KEY_USAGE_VERIFY_MESSAGE |
1693 PSA_KEY_USAGE_SIGN_HASH |
1694 PSA_KEY_USAGE_VERIFY_HASH |
1695 PSA_KEY_USAGE_VERIFY_DERIVATION |
1696 PSA_KEY_USAGE_DERIVE)) != 0) {
1697 return PSA_ERROR_INVALID_ARGUMENT;
1698 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001701}
1702
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001703/** Validate the internal consistency of key attributes.
1704 *
1705 * This function only rejects invalid attribute values. If does not
1706 * validate the consistency of the attributes with any key data that may
1707 * be involved in the creation of the key.
1708 *
1709 * Call this function early in the key creation process.
1710 *
1711 * \param[in] attributes Key attributes for the new key.
1712 * \param[out] p_drv On any return, the driver for the key, if any.
1713 * NULL for a transparent key.
1714 *
1715 */
1716static psa_status_t psa_validate_key_attributes(
1717 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001719{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001720 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1722 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 status = psa_validate_key_location(lifetime, p_drv);
1725 if (status != PSA_SUCCESS) {
1726 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001727 }
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 status = psa_validate_key_persistence(lifetime);
1730 if (status != PSA_SUCCESS) {
1731 return status;
1732 }
1733
1734 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1735 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1736 return PSA_ERROR_INVALID_ARGUMENT;
1737 }
1738 } else {
1739 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1740 return PSA_ERROR_INVALID_ARGUMENT;
1741 }
1742 }
1743
1744 status = psa_validate_key_policy(&attributes->core.policy);
1745 if (status != PSA_SUCCESS) {
1746 return status;
1747 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001748
1749 /* Refuse to create overly large keys.
1750 * Note that this doesn't trigger on import if the attributes don't
1751 * explicitly specify a size (so psa_get_key_bits returns 0), so
1752 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1754 return PSA_ERROR_NOT_SUPPORTED;
1755 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001756
Gilles Peskine91e8c332019-08-02 19:19:39 +02001757 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1759 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1760 return PSA_ERROR_INVALID_ARGUMENT;
1761 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001764}
1765
Gilles Peskine4747d192019-04-17 15:05:45 +02001766/** Prepare a key slot to receive key material.
1767 *
1768 * This function allocates a key slot and sets its metadata.
1769 *
1770 * If this function fails, call psa_fail_key_creation().
1771 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001772 * This function is intended to be used as follows:
1773 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 * it with the specified attributes, and in case of a volatile key assign it
1775 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001776 * -# Populate the slot with the key material.
1777 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1778 * In case of failure at any step, stop the sequence and call
1779 * psa_fail_key_creation().
1780 *
Ronald Cron5c522922020-11-14 16:35:34 +01001781 * On success, the key slot is locked. It is the responsibility of the caller
1782 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001783 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001784 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001785 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001786 * \param[out] p_slot On success, a pointer to the prepared slot.
1787 * \param[out] p_drv On any return, the driver for the key, if any.
1788 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001789 *
1790 * \retval #PSA_SUCCESS
1791 * The key slot is ready to receive key material.
1792 * \return If this function fails, the key slot is an invalid state.
1793 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001794 */
1795static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001796 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001797 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001800{
1801 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001802 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 psa_key_slot_t *slot;
1804
Gilles Peskinedf179142019-07-15 22:02:14 +02001805 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001806 *p_drv = NULL;
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 status = psa_validate_key_attributes(attributes, p_drv);
1809 if (status != PSA_SUCCESS) {
1810 return status;
1811 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1814 if (status != PSA_SUCCESS) {
1815 return status;
1816 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001817 slot = *p_slot;
1818
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001819 /* We're storing the declared bit-size of the key. It's up to each
1820 * creation mechanism to verify that this information is correct.
1821 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001823 * is optional (import, copy). In case of a volatile key, assign it the
1824 * volatile key identifier associated to the slot returned to contain its
1825 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001826
1827 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001829#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1830 slot->attr.id = volatile_key_id;
1831#else
1832 slot->attr.id.key_id = volatile_key_id;
1833#endif
1834 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001835
Gilles Peskine91e8c332019-08-02 19:19:39 +02001836 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001837 * external-only flags, query `attributes`. Thanks to the check
1838 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001839 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001840 * may have set. */
1841 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001842
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001844 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001846 * create the key file in internal storage, create the
1847 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848 * persistent data. This is done by starting a transaction that will
1849 * encompass these three actions.
1850 * For registering a volatile key, we just need to find an appropriate
1851 * slot number inside the SE. Since the key is designated volatile, creating
1852 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001853 /* The first thing to do is to find a slot number for the new key.
1854 * We save the slot number in persistent storage as part of the
1855 * transaction data. It will be needed to recover if the power
1856 * fails during the key creation process, to clean up on the secure
1857 * element side after restarting. Obtaining a slot number from the
1858 * secure element driver updates its persistent state, but we do not yet
1859 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001860 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001862 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1864 &slot_number);
1865 if (status != PSA_SUCCESS) {
1866 return status;
1867 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1870 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001871 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001872 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001873 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 status = psa_crypto_save_transaction();
1875 if (status != PSA_SUCCESS) {
1876 (void) psa_crypto_stop_transaction();
1877 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001878 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001879 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001880
1881 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001883 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001886 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001888 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001889#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1890
Ryan Everett975d4112023-11-16 13:37:51 +00001891 slot->status = PSA_SLOT_OCCUPIED;
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001894}
Gilles Peskine4747d192019-04-17 15:05:45 +02001895
1896/** Finalize the creation of a key once its key material has been set.
1897 *
1898 * This entails writing the key to persistent storage.
1899 *
1900 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001901 * See the documentation of psa_start_key_creation() for the intended use
1902 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 *
Ronald Cron5c522922020-11-14 16:35:34 +01001904 * If the finalization succeeds, the function unlocks the key slot (it was
1905 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1906 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001907 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 * \param[in,out] slot Pointer to the slot with key material.
1909 * \param[in] driver The secure element driver for the key,
1910 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001911 * \param[out] key On success, identifier of the key. Note that the
1912 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001913 *
1914 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001915 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1917 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1918 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1919 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1920 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1921 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001922 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001923 * \return If this function fails, the key slot is an invalid state.
1924 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001925 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001926static psa_status_t psa_finish_key_creation(
1927 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001928 psa_se_drv_table_entry_t *driver,
1929 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001930{
1931 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001932 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001933 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001934
1935#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001937#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001939 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001940 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001942
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001943 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1944 sizeof(data.slot_number),
1945 "Slot number size does not match psa_se_key_data_storage_t");
1946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1948 status = psa_save_persistent_key(&slot->attr,
1949 (uint8_t *) &data,
1950 sizeof(data));
1951 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001952#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1953 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001954 /* Key material is saved in export representation in the slot, so
1955 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 status = psa_save_persistent_key(&slot->attr,
1957 slot->key.data,
1958 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001959 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001961#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1962
Gilles Peskinecbaff462019-07-12 23:46:04 +02001963#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001964 /* Finish the transaction for a key creation. This does not
1965 * happen when registering an existing key. Detect this case
1966 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001967 * creation of a persistent key in a secure element requires a transaction,
1968 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (driver != NULL &&
1970 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1971 status = psa_save_se_persistent_data(driver);
1972 if (status != PSA_SUCCESS) {
1973 psa_destroy_persistent_key(slot->attr.id);
1974 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001975 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001977 }
1978#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001981 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 status = psa_unlock_key_slot(slot);
1983 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001984 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001986 }
Ronald Cron50972942020-11-14 11:28:25 +01001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001989}
1990
1991/** Abort the creation of a key.
1992 *
1993 * You may call this function after calling psa_start_key_creation(),
1994 * or after psa_finish_key_creation() fails. In other circumstances, this
1995 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001996 * See the documentation of psa_start_key_creation() for the intended use
1997 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001998 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 * \param[in,out] slot Pointer to the slot with key material.
2000 * \param[in] driver The secure element driver for the key,
2001 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002003static void psa_fail_key_creation(psa_key_slot_t *slot,
2004 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002005{
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 (void) driver;
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002009 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002011
2012#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002013 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002014 * element, and the failure happened later (when saving metadata
2015 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002016 * element.
2017 * https://github.com/ARMmbed/mbed-crypto/issues/217
2018 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002019
Gilles Peskined7729582019-08-05 15:55:54 +02002020 /* Abort the ongoing transaction if any (there may not be one if
2021 * the creation process failed before starting one, or if the
2022 * key creation is a registration of a key in a secure element).
2023 * Earlier functions must already have done what it takes to undo any
2024 * partial creation. All that's left is to update the transaction data
2025 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002030}
2031
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002032/** Validate optional attributes during key creation.
2033 *
2034 * Some key attributes are optional during key creation. If they are
2035 * specified in the attributes structure, check that they are consistent
2036 * with the data in the slot.
2037 *
2038 * This function should be called near the end of key creation, after
2039 * the slot in memory is fully populated but before saving persistent data.
2040 */
2041static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002042 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044{
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 if (attributes->core.type != 0) {
2046 if (attributes->core.type != slot->attr.type) {
2047 return PSA_ERROR_INVALID_ARGUMENT;
2048 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 }
2050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002052#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2053 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2055 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002056 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002057 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002059
Ronald Cron90857082020-11-25 14:58:33 +01002060 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 slot->attr.type,
2062 slot->key.data,
2063 slot->key.bytes,
2064 &rsa);
2065 if (status != PSA_SUCCESS) {
2066 return status;
2067 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 mbedtls_mpi_init(&actual);
2070 mbedtls_mpi_init(&required);
2071 ret = mbedtls_rsa_export(rsa,
2072 NULL, NULL, NULL, NULL, &actual);
2073 mbedtls_rsa_free(rsa);
2074 mbedtls_free(rsa);
2075 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
2078 ret = mbedtls_mpi_read_binary(&required,
2079 attributes->domain_parameters,
2080 attributes->domain_parameters_size);
2081 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
2084 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 }
2087rsa_exit:
2088 mbedtls_mpi_free(&actual);
2089 mbedtls_mpi_free(&required);
2090 if (ret != 0) {
2091 return mbedtls_to_psa_error(ret);
2092 }
2093 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002094#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2095 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002096 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002097 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002099 }
2100 }
2101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (attributes->core.bits != 0) {
2103 if (attributes->core.bits != slot->attr.bits) {
2104 return PSA_ERROR_INVALID_ARGUMENT;
2105 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106 }
2107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109}
2110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2112 const uint8_t *data,
2113 size_t data_length,
2114 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002115{
2116 psa_status_t status;
2117 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002118 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002119 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302120 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Ronald Cron81709fc2020-11-14 12:10:32 +01002122 *key = MBEDTLS_SVC_KEY_ID_INIT;
2123
Gilles Peskine0f84d622019-09-12 19:03:13 +02002124 /* Reject zero-length symmetric keys (including raw data key objects).
2125 * This also rejects any key which might be encoded as an empty string,
2126 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (data_length == 0) {
2128 return PSA_ERROR_INVALID_ARGUMENT;
2129 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002130
Archana449608b2021-09-08 15:36:05 +05302131 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (data_length > SIZE_MAX / 8) {
2133 return PSA_ERROR_NOT_SUPPORTED;
2134 }
Archana6ed4bda2021-08-04 10:47:15 +05302135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2137 &slot, &driver);
2138 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002141
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002142 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302143 * storage ( thus not in the case of importing a key in a secure element
2144 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2145 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (slot->key.data == NULL) {
2147 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302148 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 attributes, data, data_length, &storage_size);
2150 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
Archanad8a83dc2021-06-14 10:04:16 +05302153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 status = psa_allocate_buffer_to_slot(slot, storage_size);
2155 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002158 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002159
2160 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_driver_wrapper_import_key(attributes,
2162 data, data_length,
2163 slot->key.data,
2164 slot->key.bytes,
2165 &slot->key.bytes, &bits);
2166 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002171 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002173 status = PSA_ERROR_INVALID_ARGUMENT;
2174 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002175 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002176
Archana6ed4bda2021-08-04 10:47:15 +05302177 /* Enforce a size limit, and in particular ensure that the bit
2178 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302180 status = PSA_ERROR_NOT_SUPPORTED;
2181 goto exit;
2182 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 status = psa_validate_optional_attributes(slot, attributes);
2184 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002189exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (status != PSA_SUCCESS) {
2191 psa_fail_key_creation(slot, driver);
2192 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002195}
2196
Gilles Peskined7729582019-08-05 15:55:54 +02002197#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2198psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002200{
2201 psa_status_t status;
2202 psa_key_slot_t *slot = NULL;
2203 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002205
2206 /* Leaving attributes unspecified is not currently supported.
2207 * It could make sense to query the key type and size from the
2208 * secure element, but not all secure elements support this
2209 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2211 return PSA_ERROR_NOT_SUPPORTED;
2212 }
2213 if (psa_get_key_bits(attributes) == 0) {
2214 return PSA_ERROR_NOT_SUPPORTED;
2215 }
Gilles Peskined7729582019-08-05 15:55:54 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2218 &slot, &driver);
2219 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Gilles Peskined7729582019-08-05 15:55:54 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002224
2225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (status != PSA_SUCCESS) {
2227 psa_fail_key_creation(slot, driver);
2228 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002229
Gilles Peskined7729582019-08-05 15:55:54 +02002230 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 psa_close_key(key);
2232 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002233}
2234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2237 const psa_key_attributes_t *specified_attributes,
2238 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002239{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002241 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002242 psa_key_slot_t *source_slot = NULL;
2243 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002244 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002245 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302246 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002247
Ronald Cron81709fc2020-11-14 12:10:32 +01002248 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2249
Archana8a180362021-07-05 02:18:48 +05302250 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2252 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 status = psa_validate_optional_attributes(source_slot,
2257 specified_attributes);
2258 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002261
Archana9d17bf42021-09-10 06:22:44 +05302262 /* The target key type and number of bits have been validated by
2263 * psa_validate_optional_attributes() to be either equal to zero or
2264 * equal to the ones of the source key. So it is safe to inherit
2265 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302266 * */
Archana9d17bf42021-09-10 06:22:44 +05302267 actual_attributes.core.bits = source_slot->attr.bits;
2268 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302269
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 status = psa_restrict_key_policy(source_slot->attr.type,
2272 &actual_attributes.core.policy,
2273 &source_slot->attr.policy);
2274 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2279 &target_slot, &driver);
2280 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 }
2283 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2284 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302285 /*
Archana9d17bf42021-09-10 06:22:44 +05302286 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302287 * the source key would need to be exported as plaintext and re-imported
2288 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302289 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302290 * appropriate API invocations from the application, if needed.
2291 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002292 status = PSA_ERROR_NOT_SUPPORTED;
2293 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002294 }
Archana8a180362021-07-05 02:18:48 +05302295 /*
2296 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302297 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302298 * - For opaque keys this translates to an invocation of the drivers'
2299 * copy_key entry point through the dispatch layer.
2300 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2302 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2303 &storage_size);
2304 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302305 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 }
Archana374fe5b2021-09-08 15:50:28 +05302307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2309 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302310 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 }
Archana374fe5b2021-09-08 15:50:28 +05302312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 status = psa_driver_wrapper_copy_key(&actual_attributes,
2314 source_slot->key.data,
2315 source_slot->key.bytes,
2316 target_slot->key.data,
2317 target_slot->key.bytes,
2318 &target_slot->key.bytes);
2319 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302320 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 }
2322 } else {
2323 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302324 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 source_slot->key.bytes);
2326 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302327 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 }
Archana8a180362021-07-05 02:18:48 +05302329 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002331exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (status != PSA_SUCCESS) {
2333 psa_fail_key_creation(target_slot, driver);
2334 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002339}
2340
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002341
2342
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002343/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002344/* Message digests */
2345/****************************************************************/
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002349 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if (operation->id == 0) {
2351 return PSA_SUCCESS;
2352 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002355 operation->id = 0;
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358}
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2361 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002363 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002364
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002365 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367 status = PSA_ERROR_BAD_STATE;
2368 goto exit;
2369 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002372 status = PSA_ERROR_INVALID_ARGUMENT;
2373 goto exit;
2374 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002375
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002376 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2377 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002381
2382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (status != PSA_SUCCESS) {
2384 psa_hash_abort(operation);
2385 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002386
2387 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388}
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2391 const uint8_t *input,
2392 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002397 status = PSA_ERROR_BAD_STATE;
2398 goto exit;
2399 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002400
Steven Cooremanc8288352021-03-04 14:02:19 +01002401 /* Don't require hash implementations to behave correctly on a
2402 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (input_length == 0) {
2404 return PSA_SUCCESS;
2405 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002408
2409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (status != PSA_SUCCESS) {
2411 psa_hash_abort(operation);
2412 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002415}
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2418 uint8_t *hash,
2419 size_t hash_size,
2420 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002421{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002422 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (operation->id == 0) {
2424 return PSA_ERROR_BAD_STATE;
2425 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426
Steven Cooreman1e582352021-02-18 17:24:37 +01002427 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 operation, hash, hash_size, hash_length);
2429 psa_hash_abort(operation);
2430 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431}
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2434 const uint8_t *hash,
2435 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436{
Ronald Cron69a63422021-10-18 09:47:58 +02002437 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002439 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 operation,
2441 actual_hash, sizeof(actual_hash),
2442 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002449 status = PSA_ERROR_INVALID_SIGNATURE;
2450 goto exit;
2451 }
2452
Dave Rodgman78701152023-08-29 14:20:18 +01002453 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002454 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002456
2457exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2459 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002460 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464}
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466psa_status_t psa_hash_compute(psa_algorithm_t alg,
2467 const uint8_t *input, size_t input_length,
2468 uint8_t *hash, size_t hash_size,
2469 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002470{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002471 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (!PSA_ALG_IS_HASH(alg)) {
2473 return PSA_ERROR_INVALID_ARGUMENT;
2474 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2477 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002478}
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480psa_status_t psa_hash_compare(psa_algorithm_t alg,
2481 const uint8_t *input, size_t input_length,
2482 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002483{
Ronald Cron69a63422021-10-18 09:47:58 +02002484 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002485 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (!PSA_ALG_IS_HASH(alg)) {
2488 return PSA_ERROR_INVALID_ARGUMENT;
2489 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002490
Steven Cooreman1e582352021-02-18 17:24:37 +01002491 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 alg, input, input_length,
2493 actual_hash, sizeof(actual_hash),
2494 &actual_hash_length);
2495 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 }
2498 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002499 status = PSA_ERROR_INVALID_SIGNATURE;
2500 goto exit;
2501 }
Dave Rodgman78701152023-08-29 14:20:18 +01002502 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002503 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002505
2506exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2508 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002509}
2510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2512 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002513{
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (source_operation->id == 0 ||
2515 target_operation->id != 0) {
2516 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002517 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2520 target_operation);
2521 if (status != PSA_SUCCESS) {
2522 psa_hash_abort(target_operation);
2523 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002526}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002527
2528
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002529/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530/* MAC */
2531/****************************************************************/
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002534{
Steven Cooremane6804192021-03-19 18:28:56 +01002535 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (operation->id == 0) {
2537 return PSA_SUCCESS;
2538 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002541 operation->mac_size = 0;
2542 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002543 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546}
2547
Ronald Cron2dff3b22021-06-17 16:33:22 +02002548static psa_status_t psa_mac_finalize_alg_and_key_validation(
2549 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002550 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 psa_key_type_t key_type = psa_get_key_type(attributes);
2555 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (!PSA_ALG_IS_MAC(alg)) {
2558 return PSA_ERROR_INVALID_ARGUMENT;
2559 }
Steven Cooremane6804192021-03-19 18:28:56 +01002560
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002561 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 status = psa_mac_key_can_do(alg, key_type);
2563 if (status != PSA_SUCCESS) {
2564 return status;
2565 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002566
Steven Cooremand1a68f12021-05-10 11:13:41 +02002567 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002571 /* A very short MAC is too short for security since it can be
2572 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2573 * so we make this our minimum, even though 32 bits is still
2574 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002576 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2579 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002580 /* It's impossible to "truncate" to a larger length than the full length
2581 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002583 }
2584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002586 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2587 * that is disabled in the compile-time configuration. The result can
2588 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2589 * configuration into account. In this case, force a return of
2590 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2591 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2592 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2593 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2594 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002596 }
2597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002599}
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2602 mbedtls_svc_key_id_t key,
2603 psa_algorithm_t alg,
2604 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002605{
2606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2607 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002608 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002609 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610
2611 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002613 status = PSA_ERROR_BAD_STATE;
2614 goto exit;
2615 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616
2617 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 key,
2619 &slot,
2620 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2621 alg);
2622 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002626 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627 .core = slot->attr
2628 };
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2631 &operation->mac_size);
2632 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635
2636 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002637 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (is_sign) {
2639 status = psa_driver_wrapper_mac_sign_setup(operation,
2640 &attributes,
2641 slot->key.data,
2642 slot->key.bytes,
2643 alg);
2644 } else {
2645 status = psa_driver_wrapper_mac_verify_setup(operation,
2646 &attributes,
2647 slot->key.data,
2648 slot->key.bytes,
2649 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002651
Gilles Peskinefbfac682018-07-08 20:51:54 +02002652exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (status != PSA_SUCCESS) {
2654 psa_mac_abort(operation);
2655 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660}
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2663 mbedtls_svc_key_id_t key,
2664 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002665{
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002667}
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2670 mbedtls_svc_key_id_t key,
2671 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002672{
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002674}
2675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2677 const uint8_t *input,
2678 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679{
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->id == 0) {
2681 return PSA_ERROR_BAD_STATE;
2682 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002684 /* Don't require hash implementations to behave correctly on a
2685 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (input_length == 0) {
2687 return PSA_SUCCESS;
2688 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2691 input, input_length);
2692 if (status != PSA_SUCCESS) {
2693 psa_mac_abort(operation);
2694 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697}
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2700 uint8_t *mac,
2701 size_t mac_size,
2702 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002703{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2705 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002708 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002709 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002710 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002713 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002714 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002715 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002716
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002717 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2718 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002720 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002721 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002722 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002725 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002726 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002727 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 status = psa_driver_wrapper_mac_sign_finish(operation,
2730 mac, operation->mac_size,
2731 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002732
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002733exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002734 /* In case of success, set the potential excess room in the output buffer
2735 * to an invalid value, to avoid potentially leaking a longer MAC.
2736 * In case of error, set the output length and content to a safe default,
2737 * such that in case the caller misses an error check, the output would be
2738 * an unachievable MAC.
2739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002742 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743 }
mohammad16036df908f2018-04-02 08:34:15 -07002744
Paul Elliottdc42ca82023-02-24 18:11:59 +00002745 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002750}
2751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2753 const uint8_t *mac,
2754 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002756 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2757 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002760 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002761 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002762 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002765 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002766 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002767 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002770 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002771 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002772 }
2773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 status = psa_driver_wrapper_mac_verify_finish(operation,
2775 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002776
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002777exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781}
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2784 psa_algorithm_t alg,
2785 const uint8_t *input,
2786 size_t input_length,
2787 uint8_t *mac,
2788 size_t mac_size,
2789 size_t *mac_length,
2790 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791{
2792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002793 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2794 psa_key_slot_t *slot;
2795 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002796 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797
Ronald Cron51131b52021-06-17 17:17:20 +02002798 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 key,
2800 &slot,
2801 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2802 alg);
2803 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002804 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002806
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002807 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002808 .core = slot->attr
2809 };
2810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2812 &operation_mac_size);
2813 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002818 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002819 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002820 }
2821
2822 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 &attributes,
2824 slot->key.data, slot->key.bytes,
2825 alg,
2826 input, input_length,
2827 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002828
2829exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002830 /* In case of success, set the potential excess room in the output buffer
2831 * to an invalid value, to avoid potentially leaking a longer MAC.
2832 * In case of error, set the output length and content to a safe default,
2833 * such that in case the caller misses an error check, the output would be
2834 * an unachievable MAC.
2835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002837 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002838 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002839 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002840
2841 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002846}
2847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849 psa_algorithm_t alg,
2850 const uint8_t *input,
2851 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 uint8_t *mac,
2853 size_t mac_size,
2854 size_t *mac_length)
2855{
2856 return psa_mac_compute_internal(key, alg,
2857 input, input_length,
2858 mac, mac_size, mac_length, 1);
2859}
2860
2861psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2862 psa_algorithm_t alg,
2863 const uint8_t *input,
2864 size_t input_length,
2865 const uint8_t *mac,
2866 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002867{
2868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002869 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2870 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 status = psa_mac_compute_internal(key, alg,
2873 input, input_length,
2874 actual_mac, sizeof(actual_mac),
2875 &actual_mac_length, 0);
2876 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002877 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002881 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002882 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002883 }
Dave Rodgman78701152023-08-29 14:20:18 +01002884 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002885 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002886 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002887 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002888
2889exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002893}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002894
Gilles Peskinee59236f2018-01-27 23:32:46 +01002895/****************************************************************/
2896/* Asymmetric cryptography */
2897/****************************************************************/
2898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2900 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002901{
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (input_is_message) {
2903 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2904 return PSA_ERROR_INVALID_ARGUMENT;
2905 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2908 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2909 return PSA_ERROR_INVALID_ARGUMENT;
2910 }
2911 }
2912 } else {
2913 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2914 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002915 }
2916 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002919}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2922 int input_is_message,
2923 psa_algorithm_t alg,
2924 const uint8_t *input,
2925 size_t input_length,
2926 uint8_t *signature,
2927 size_t signature_size,
2928 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929{
2930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2931 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2932 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002933 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934
2935 *signature_length = 0;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 status = psa_sign_verify_check_alg(input_is_message, alg);
2938 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002939 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002941
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002942 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002943 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002944 * buffer can in principle be empty since it doesn't actually have
2945 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if (signature_size == 0) {
2947 return PSA_ERROR_BUFFER_TOO_SMALL;
2948 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002950 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 key, &slot,
2952 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2953 PSA_KEY_USAGE_SIGN_HASH,
2954 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961 status = PSA_ERROR_INVALID_ARGUMENT;
2962 goto exit;
2963 }
2964
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002965 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002967 };
2968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002970 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002971 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 signature, signature_size, signature_length);
2974 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002975
2976 status = psa_driver_wrapper_sign_hash(
2977 &attributes, slot->key.data, slot->key.bytes,
2978 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002980 }
2981
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002982
2983exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002984 psa_wipe_tag_output_buffer(signature, status, signature_size,
2985 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002990}
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2993 int input_is_message,
2994 psa_algorithm_t alg,
2995 const uint8_t *input,
2996 size_t input_length,
2997 const uint8_t *signature,
2998 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002999{
3000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3001 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3002 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 status = psa_sign_verify_check_alg(input_is_message, alg);
3005 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003006 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003008
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003009 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 key, &slot,
3011 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3012 PSA_KEY_USAGE_VERIFY_HASH,
3013 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (status != PSA_SUCCESS) {
3016 return status;
3017 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003018
3019 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003021 };
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003025 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003026 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 signature, signature_length);
3028 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003029 status = psa_driver_wrapper_verify_hash(
3030 &attributes, slot->key.data, slot->key.bytes,
3031 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003033 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003038
3039}
3040
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003041psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003042 const psa_key_attributes_t *attributes,
3043 const uint8_t *key_buffer,
3044 size_t key_buffer_size,
3045 psa_algorithm_t alg,
3046 const uint8_t *input,
3047 size_t input_length,
3048 uint8_t *signature,
3049 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003051{
3052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003055 size_t hash_length;
3056 uint8_t hash[PSA_HASH_MAX_SIZE];
3057
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003058 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 PSA_ALG_SIGN_GET_HASH(alg),
3060 input, input_length,
3061 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003064 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003066
3067 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 attributes, key_buffer, key_buffer_size,
3069 alg, hash, hash_length,
3070 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003071 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003074}
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3077 psa_algorithm_t alg,
3078 const uint8_t *input,
3079 size_t input_length,
3080 uint8_t *signature,
3081 size_t signature_size,
3082 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003083{
3084 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003085 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003087}
3088
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003089psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003090 const psa_key_attributes_t *attributes,
3091 const uint8_t *key_buffer,
3092 size_t key_buffer_size,
3093 psa_algorithm_t alg,
3094 const uint8_t *input,
3095 size_t input_length,
3096 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003098{
3099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003102 size_t hash_length;
3103 uint8_t hash[PSA_HASH_MAX_SIZE];
3104
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003105 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 PSA_ALG_SIGN_GET_HASH(alg),
3107 input, input_length,
3108 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003111 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003113
3114 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 attributes, key_buffer, key_buffer_size,
3116 alg, hash, hash_length,
3117 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003118 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003121}
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3124 psa_algorithm_t alg,
3125 const uint8_t *input,
3126 size_t input_length,
3127 const uint8_t *signature,
3128 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003129{
3130 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003131 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003133}
3134
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003135psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003136 const psa_key_attributes_t *attributes,
3137 const uint8_t *key_buffer, size_t key_buffer_size,
3138 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003140{
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3142 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3143 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003144#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3146 return mbedtls_psa_rsa_sign_hash(
3147 attributes,
3148 key_buffer, key_buffer_size,
3149 alg, hash, hash_length,
3150 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003151#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3152 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 } else {
3154 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3157 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003158#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3160 return mbedtls_psa_ecdsa_sign_hash(
3161 attributes,
3162 key_buffer, key_buffer_size,
3163 alg, hash, hash_length,
3164 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003165#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3166 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 } else {
3168 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003169 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003170 }
Ronald Cron4501c982021-03-19 20:09:32 +01003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 (void) key_buffer;
3173 (void) key_buffer_size;
3174 (void) hash;
3175 (void) hash_length;
3176 (void) signature;
3177 (void) signature_size;
3178 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003181}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3184 psa_algorithm_t alg,
3185 const uint8_t *hash,
3186 size_t hash_length,
3187 uint8_t *signature,
3188 size_t signature_size,
3189 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003190{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003191 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003192 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003194}
3195
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003196psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003197 const psa_key_attributes_t *attributes,
3198 const uint8_t *key_buffer, size_t key_buffer_size,
3199 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003201{
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3203 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3204 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003205#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3207 return mbedtls_psa_rsa_verify_hash(
3208 attributes,
3209 key_buffer, key_buffer_size,
3210 alg, hash, hash_length,
3211 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003212#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3213 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 } else {
3215 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3218 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3221 return mbedtls_psa_ecdsa_verify_hash(
3222 attributes,
3223 key_buffer, key_buffer_size,
3224 alg, hash, hash_length,
3225 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003226#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3227 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 } else {
3229 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003230 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003231 }
Ronald Cron4501c982021-03-19 20:09:32 +01003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 (void) key_buffer;
3234 (void) key_buffer_size;
3235 (void) hash;
3236 (void) hash_length;
3237 (void) signature;
3238 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003241}
3242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3244 psa_algorithm_t alg,
3245 const uint8_t *hash,
3246 size_t hash_length,
3247 const uint8_t *signature,
3248 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003249{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003250 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003251 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003253}
3254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3256 psa_algorithm_t alg,
3257 const uint8_t *input,
3258 size_t input_length,
3259 const uint8_t *salt,
3260 size_t salt_length,
3261 uint8_t *output,
3262 size_t output_size,
3263 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003267 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003268 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003269
Darryl Green5cc689a2018-07-24 15:34:10 +01003270 (void) input;
3271 (void) input_length;
3272 (void) salt;
3273 (void) output;
3274 (void) output_size;
3275
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003276 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3279 return PSA_ERROR_INVALID_ARGUMENT;
3280 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003281
Ronald Cron5c522922020-11-14 16:35:34 +01003282 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3284 if (status != PSA_SUCCESS) {
3285 return status;
3286 }
3287 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3288 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003289 status = PSA_ERROR_INVALID_ARGUMENT;
3290 goto exit;
3291 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003292
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003293 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003295 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003296
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003297 status = psa_driver_wrapper_asymmetric_encrypt(
3298 &attributes, slot->key.data, slot->key.bytes,
3299 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003305}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3308 psa_algorithm_t alg,
3309 const uint8_t *input,
3310 size_t input_length,
3311 const uint8_t *salt,
3312 size_t salt_length,
3313 uint8_t *output,
3314 size_t output_size,
3315 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003318 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003319 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003320 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003321
Darryl Green5cc689a2018-07-24 15:34:10 +01003322 (void) input;
3323 (void) input_length;
3324 (void) salt;
3325 (void) output;
3326 (void) output_size;
3327
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003328 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3331 return PSA_ERROR_INVALID_ARGUMENT;
3332 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003333
Ronald Cron5c522922020-11-14 16:35:34 +01003334 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3336 if (status != PSA_SUCCESS) {
3337 return status;
3338 }
3339 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003340 status = PSA_ERROR_INVALID_ARGUMENT;
3341 goto exit;
3342 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003343
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003344 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003346 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003348 status = psa_driver_wrapper_asymmetric_decrypt(
3349 &attributes, slot->key.data, slot->key.bytes,
3350 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003352
3353exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003357}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003358
Paul Elliott9fe12f62022-11-30 19:16:02 +00003359/****************************************************************/
3360/* Asymmetric interruptible cryptography */
3361/****************************************************************/
3362
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003363static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3364
Paul Elliott9fe12f62022-11-30 19:16:02 +00003365void psa_interruptible_set_max_ops(uint32_t max_ops)
3366{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003367 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003368}
3369
3370uint32_t psa_interruptible_get_max_ops(void)
3371{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003372 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003373}
3374
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375uint32_t psa_sign_hash_get_num_ops(
3376 const psa_sign_hash_interruptible_operation_t *operation)
3377{
Paul Elliott296ede92022-12-15 17:00:30 +00003378 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379}
3380
3381uint32_t psa_verify_hash_get_num_ops(
3382 const psa_verify_hash_interruptible_operation_t *operation)
3383{
Paul Elliott296ede92022-12-15 17:00:30 +00003384 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003385}
3386
Paul Elliott59ad9452022-12-18 15:09:02 +00003387static psa_status_t psa_sign_hash_abort_internal(
3388 psa_sign_hash_interruptible_operation_t *operation)
3389{
3390 if (operation->id == 0) {
3391 /* The object has (apparently) been initialized but it is not (yet)
3392 * in use. It's ok to call abort on such an object, and there's
3393 * nothing to do. */
3394 return PSA_SUCCESS;
3395 }
3396
3397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3398
3399 status = psa_driver_wrapper_sign_hash_abort(operation);
3400
3401 operation->id = 0;
3402
Paul Elliotte6145dc2023-02-07 12:51:21 +00003403 /* Do not clear either the error_occurred or num_ops elements here as they
3404 * only want to be cleared by the application calling abort, not by abort
3405 * being called at completion of an operation. */
3406
Paul Elliott59ad9452022-12-18 15:09:02 +00003407 return status;
3408}
3409
Paul Elliott9fe12f62022-11-30 19:16:02 +00003410psa_status_t psa_sign_hash_start(
3411 psa_sign_hash_interruptible_operation_t *operation,
3412 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3413 const uint8_t *hash, size_t hash_length)
3414{
3415 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3416 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3417 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003418 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419
Paul Elliottc9774412023-02-06 15:14:07 +00003420 /* Check that start has not been previously called, or operation has not
3421 * previously errored. */
3422 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 return PSA_ERROR_BAD_STATE;
3424 }
3425
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426 status = psa_sign_verify_check_alg(0, alg);
3427 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003428 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003429 return status;
3430 }
3431
3432 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3433 PSA_KEY_USAGE_SIGN_HASH,
3434 alg);
3435
3436 if (status != PSA_SUCCESS) {
3437 goto exit;
3438 }
3439
3440 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3441 status = PSA_ERROR_INVALID_ARGUMENT;
3442 goto exit;
3443 }
3444
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003445 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446 .core = slot->attr
3447 };
3448
Paul Elliott296ede92022-12-15 17:00:30 +00003449 /* Ensure ops count gets reset, in case of operation re-use. */
3450 operation->num_ops = 0;
3451
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3453 slot->key.data,
3454 slot->key.bytes, alg,
3455 hash, hash_length);
3456exit:
3457
3458 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003459 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003460 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461 }
3462
3463 unlock_status = psa_unlock_key_slot(slot);
3464
Paul Elliottc9774412023-02-06 15:14:07 +00003465 if (unlock_status != PSA_SUCCESS) {
3466 operation->error_occurred = 1;
3467 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468
Paul Elliottc9774412023-02-06 15:14:07 +00003469 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003470}
3471
3472
3473psa_status_t psa_sign_hash_complete(
3474 psa_sign_hash_interruptible_operation_t *operation,
3475 uint8_t *signature, size_t signature_size,
3476 size_t *signature_length)
3477{
3478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3479
3480 *signature_length = 0;
3481
Paul Elliottc9774412023-02-06 15:14:07 +00003482 /* Check that start has been called first, and that operation has not
3483 * previously errored. */
3484 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003485 status = PSA_ERROR_BAD_STATE;
3486 goto exit;
3487 }
3488
Paul Elliott096abc42023-02-03 18:33:23 +00003489 /* Immediately reject a zero-length signature buffer. This guarantees that
3490 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003491 if (signature_size == 0) {
3492 status = PSA_ERROR_BUFFER_TOO_SMALL;
3493 goto exit;
3494 }
3495
3496 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3497 signature_size,
3498 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003499
Paul Elliott296ede92022-12-15 17:00:30 +00003500 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003501 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003502
Paul Elliottebe225c2023-02-10 14:32:53 +00003503exit:
3504
Paul Elliott59200a22023-02-21 15:07:40 +00003505 psa_wipe_tag_output_buffer(signature, status, signature_size,
3506 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507
Paul Elliott53bb3122023-02-10 14:22:22 +00003508 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003509 if (status != PSA_SUCCESS) {
3510 operation->error_occurred = 1;
3511 }
3512
Paul Elliott59ad9452022-12-18 15:09:02 +00003513 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514 }
3515
3516 return status;
3517}
3518
3519psa_status_t psa_sign_hash_abort(
3520 psa_sign_hash_interruptible_operation_t *operation)
3521{
Paul Elliott59ad9452022-12-18 15:09:02 +00003522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3523
3524 status = psa_sign_hash_abort_internal(operation);
3525
3526 /* We clear the number of ops done here, so that it is not cleared when
3527 * the operation fails or succeeds, only on manual abort. */
3528 operation->num_ops = 0;
3529
Paul Elliottc9774412023-02-06 15:14:07 +00003530 /* Likewise, failure state. */
3531 operation->error_occurred = 0;
3532
Paul Elliott59ad9452022-12-18 15:09:02 +00003533 return status;
3534}
3535
3536static psa_status_t psa_verify_hash_abort_internal(
3537 psa_verify_hash_interruptible_operation_t *operation)
3538{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003539 if (operation->id == 0) {
3540 /* The object has (apparently) been initialized but it is not (yet)
3541 * in use. It's ok to call abort on such an object, and there's
3542 * nothing to do. */
3543 return PSA_SUCCESS;
3544 }
3545
Paul Elliott59ad9452022-12-18 15:09:02 +00003546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3547
3548 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003549
3550 operation->id = 0;
3551
Paul Elliotte6145dc2023-02-07 12:51:21 +00003552 /* Do not clear either the error_occurred or num_ops elements here as they
3553 * only want to be cleared by the application calling abort, not by abort
3554 * being called at completion of an operation. */
3555
Paul Elliott59ad9452022-12-18 15:09:02 +00003556 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557}
3558
3559psa_status_t psa_verify_hash_start(
3560 psa_verify_hash_interruptible_operation_t *operation,
3561 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3562 const uint8_t *hash, size_t hash_length,
3563 const uint8_t *signature, size_t signature_length)
3564{
3565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3566 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3567 psa_key_slot_t *slot;
3568
Paul Elliottc9774412023-02-06 15:14:07 +00003569 /* Check that start has not been previously called, or operation has not
3570 * previously errored. */
3571 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003572 return PSA_ERROR_BAD_STATE;
3573 }
3574
3575 status = psa_sign_verify_check_alg(0, alg);
3576 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003577 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003578 return status;
3579 }
3580
3581 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3582 PSA_KEY_USAGE_VERIFY_HASH,
3583 alg);
3584
3585 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003586 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587 return status;
3588 }
3589
3590 psa_key_attributes_t attributes = {
3591 .core = slot->attr
3592 };
3593
Paul Elliott296ede92022-12-15 17:00:30 +00003594 /* Ensure ops count gets reset, in case of operation re-use. */
3595 operation->num_ops = 0;
3596
Paul Elliott9fe12f62022-11-30 19:16:02 +00003597 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3598 slot->key.data,
3599 slot->key.bytes,
3600 alg, hash, hash_length,
3601 signature, signature_length);
3602
3603 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003604 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003605 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003606 }
3607
3608 unlock_status = psa_unlock_key_slot(slot);
3609
Paul Elliottc9774412023-02-06 15:14:07 +00003610 if (unlock_status != PSA_SUCCESS) {
3611 operation->error_occurred = 1;
3612 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613
Paul Elliottc9774412023-02-06 15:14:07 +00003614 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003615}
3616
3617psa_status_t psa_verify_hash_complete(
3618 psa_verify_hash_interruptible_operation_t *operation)
3619{
3620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3621
Paul Elliottc9774412023-02-06 15:14:07 +00003622 /* Check that start has been called first, and that operation has not
3623 * previously errored. */
3624 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625 status = PSA_ERROR_BAD_STATE;
3626 goto exit;
3627 }
3628
3629 status = psa_driver_wrapper_verify_hash_complete(operation);
3630
Paul Elliott296ede92022-12-15 17:00:30 +00003631 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003632 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003633 operation);
3634
Paul Elliottebe225c2023-02-10 14:32:53 +00003635exit:
3636
Paul Elliott9fe12f62022-11-30 19:16:02 +00003637 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003638 if (status != PSA_SUCCESS) {
3639 operation->error_occurred = 1;
3640 }
3641
Paul Elliott59ad9452022-12-18 15:09:02 +00003642 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003643 }
3644
3645 return status;
3646}
3647
3648psa_status_t psa_verify_hash_abort(
3649 psa_verify_hash_interruptible_operation_t *operation)
3650{
Paul Elliott59ad9452022-12-18 15:09:02 +00003651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003652
Paul Elliott59ad9452022-12-18 15:09:02 +00003653 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003654
Paul Elliott59ad9452022-12-18 15:09:02 +00003655 /* We clear the number of ops done here, so that it is not cleared when
3656 * the operation fails or succeeds, only on manual abort. */
3657 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003658
Paul Elliottc9774412023-02-06 15:14:07 +00003659 /* Likewise, failure state. */
3660 operation->error_occurred = 0;
3661
Paul Elliott59ad9452022-12-18 15:09:02 +00003662 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003663}
3664
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665/****************************************************************/
3666/* Asymmetric interruptible cryptography internal */
3667/* implementations */
3668/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003669
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3671{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003672
Paul Elliott588f8ed2022-12-02 18:10:26 +00003673#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3674 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3675 defined(MBEDTLS_ECP_RESTARTABLE)
3676
3677 /* Internal implementation uses zero to indicate infinite number max ops,
3678 * therefore avoid this value, and set to minimum possible. */
3679 if (max_ops == 0) {
3680 max_ops = 1;
3681 }
3682
Paul Elliott588f8ed2022-12-02 18:10:26 +00003683 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003684#else
3685 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3687 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3688 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3689}
3690
Paul Elliott588f8ed2022-12-02 18:10:26 +00003691uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003692 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693{
3694#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3695 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3696 defined(MBEDTLS_ECP_RESTARTABLE)
3697
Paul Elliott93d9ca82023-02-15 18:14:21 +00003698 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699#else
3700 (void) operation;
3701 return 0;
3702#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3703 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3704 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3705}
3706
3707uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003708 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709{
3710 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3711 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3712 defined(MBEDTLS_ECP_RESTARTABLE)
3713
Paul Elliott93d9ca82023-02-15 18:14:21 +00003714 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715#else
3716 (void) operation;
3717 return 0;
3718#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3719 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3720 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3721}
3722
3723psa_status_t mbedtls_psa_sign_hash_start(
3724 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3725 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3726 size_t key_buffer_size, psa_algorithm_t alg,
3727 const uint8_t *hash, size_t hash_length)
3728{
3729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003730 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott068fe072023-01-16 13:59:15 +00003732 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3733 return PSA_ERROR_NOT_SUPPORTED;
3734 }
3735
3736 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003737 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003738 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003739
3740#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003741 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3742 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003743
Paul Elliotta1c94092023-02-15 16:38:04 +00003744 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3745
Paul Elliott93d9ca82023-02-15 18:14:21 +00003746 /* Ensure num_ops is zero'ed in case of context re-use. */
3747 operation->num_ops = 0;
3748
Paul Elliott068fe072023-01-16 13:59:15 +00003749 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3750 attributes->core.bits,
3751 key_buffer,
3752 key_buffer_size,
3753 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754
Paul Elliott068fe072023-01-16 13:59:15 +00003755 if (status != PSA_SUCCESS) {
3756 return status;
3757 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003758
Paul Elliott1bc59df2023-02-05 13:41:57 +00003759 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003760 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
Paul Elliott068fe072023-01-16 13:59:15 +00003762 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003763 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003764 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
Paul Elliott0290a762023-02-09 14:30:24 +00003766 /* We only need to store the same length of hash as the private key size
3767 * here, it would be truncated by the internal implementation anyway. */
3768 required_hash_length = (hash_length < operation->coordinate_bytes ?
3769 hash_length : operation->coordinate_bytes);
3770
Paul Elliottba70ad42023-02-15 18:23:53 +00003771 if (required_hash_length > sizeof(operation->hash)) {
3772 /* Shouldn't happen, but better safe than sorry. */
3773 return PSA_ERROR_CORRUPTION_DETECTED;
3774 }
3775
Paul Elliott0290a762023-02-09 14:30:24 +00003776 memcpy(operation->hash, hash, required_hash_length);
3777 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003778
3779 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780
3781#else
Paul Elliott068fe072023-01-16 13:59:15 +00003782 (void) operation;
3783 (void) key_buffer;
3784 (void) key_buffer_size;
3785 (void) alg;
3786 (void) hash;
3787 (void) hash_length;
3788 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003789 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott068fe072023-01-16 13:59:15 +00003791 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3793 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3794 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795}
3796
3797psa_status_t mbedtls_psa_sign_hash_complete(
3798 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3799 uint8_t *signature, size_t signature_size,
3800 size_t *signature_length)
3801{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3803 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3804 defined(MBEDTLS_ECP_RESTARTABLE)
3805
Paul Elliotta1c94092023-02-15 16:38:04 +00003806 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003807 mbedtls_mpi r;
3808 mbedtls_mpi s;
3809
Paul Elliott46845252023-02-03 14:59:11 +00003810 mbedtls_mpi_init(&r);
3811 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003813 /* Ensure max_ops is set to the current value (or default). */
3814 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3815
Paul Elliotta1c94092023-02-15 16:38:04 +00003816 if (signature_size < 2 * operation->coordinate_bytes) {
3817 status = PSA_ERROR_BUFFER_TOO_SMALL;
3818 goto exit;
3819 }
3820
Paul Elliott588f8ed2022-12-02 18:10:26 +00003821 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003822
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3824 status = mbedtls_to_psa_error(
3825 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003826 &r,
3827 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828 &operation->ctx->d,
3829 operation->hash,
3830 operation->hash_length,
3831 operation->md_alg,
3832 mbedtls_psa_get_random,
3833 MBEDTLS_PSA_RANDOM_STATE,
3834 &operation->restart_ctx));
3835#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003836 status = PSA_ERROR_NOT_SUPPORTED;
3837 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3839 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840 status = mbedtls_to_psa_error(
3841 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003842 &r,
3843 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844 &operation->ctx->d,
3845 operation->hash,
3846 operation->hash_length,
3847 mbedtls_psa_get_random,
3848 MBEDTLS_PSA_RANDOM_STATE,
3849 mbedtls_psa_get_random,
3850 MBEDTLS_PSA_RANDOM_STATE,
3851 &operation->restart_ctx));
3852 }
3853
Paul Elliottf8e5b562023-02-19 18:43:45 +00003854 /* Hide the fact that the restart context only holds a delta of number of
3855 * ops done during the last operation, not an absolute value. */
3856 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3857
Paul Elliott724bd252023-02-08 12:35:08 +00003858 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003859 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003860 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003862 operation->coordinate_bytes)
3863 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003864
3865 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003866 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867 }
3868
3869 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003870 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003871 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003872 operation->coordinate_bytes,
3873 operation->coordinate_bytes)
3874 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003875
3876 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003877 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878 }
3879
Paul Elliott1bc59df2023-02-05 13:41:57 +00003880 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott724bd252023-02-08 12:35:08 +00003882 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003883 }
Paul Elliott724bd252023-02-08 12:35:08 +00003884
3885exit:
3886
3887 mbedtls_mpi_free(&r);
3888 mbedtls_mpi_free(&s);
3889 return status;
3890
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891 #else
3892
3893 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003894 (void) signature;
3895 (void) signature_size;
3896 (void) signature_length;
3897
3898 return PSA_ERROR_NOT_SUPPORTED;
3899
3900#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3901 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3902 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3903}
3904
3905psa_status_t mbedtls_psa_sign_hash_abort(
3906 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3907{
3908
3909#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3910 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3911 defined(MBEDTLS_ECP_RESTARTABLE)
3912
3913 if (operation->ctx) {
3914 mbedtls_ecdsa_free(operation->ctx);
3915 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003916 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003917 }
3918
3919 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3920
Paul Elliott93d9ca82023-02-15 18:14:21 +00003921 operation->num_ops = 0;
3922
Paul Elliott588f8ed2022-12-02 18:10:26 +00003923 return PSA_SUCCESS;
3924
3925#else
3926
3927 (void) operation;
3928
3929 return PSA_ERROR_NOT_SUPPORTED;
3930
3931#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3932 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3933 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3934}
3935
3936psa_status_t mbedtls_psa_verify_hash_start(
3937 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3938 const psa_key_attributes_t *attributes,
3939 const uint8_t *key_buffer, size_t key_buffer_size,
3940 psa_algorithm_t alg,
3941 const uint8_t *hash, size_t hash_length,
3942 const uint8_t *signature, size_t signature_length)
3943{
3944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003945 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003946 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliott068fe072023-01-16 13:59:15 +00003948 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3949 return PSA_ERROR_NOT_SUPPORTED;
3950 }
3951
3952 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003953 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003954 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003955
3956#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003957 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3958 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959
Paul Elliotta1c94092023-02-15 16:38:04 +00003960 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3961 mbedtls_mpi_init(&operation->r);
3962 mbedtls_mpi_init(&operation->s);
3963
Paul Elliott93d9ca82023-02-15 18:14:21 +00003964 /* Ensure num_ops is zero'ed in case of context re-use. */
3965 operation->num_ops = 0;
3966
Paul Elliott068fe072023-01-16 13:59:15 +00003967 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3968 attributes->core.bits,
3969 key_buffer,
3970 key_buffer_size,
3971 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Paul Elliott068fe072023-01-16 13:59:15 +00003973 if (status != PSA_SUCCESS) {
3974 return status;
3975 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliottc569fc22023-02-10 13:02:54 +00003977 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978
Paul Elliott1bc59df2023-02-05 13:41:57 +00003979 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003980 return PSA_ERROR_INVALID_SIGNATURE;
3981 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003982
Paul Elliott068fe072023-01-16 13:59:15 +00003983 status = mbedtls_to_psa_error(
3984 mbedtls_mpi_read_binary(&operation->r,
3985 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003986 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003987
Paul Elliott068fe072023-01-16 13:59:15 +00003988 if (status != PSA_SUCCESS) {
3989 return status;
3990 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003991
Paul Elliott068fe072023-01-16 13:59:15 +00003992 status = mbedtls_to_psa_error(
3993 mbedtls_mpi_read_binary(&operation->s,
3994 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003995 coordinate_bytes,
3996 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997
Paul Elliott068fe072023-01-16 13:59:15 +00003998 if (status != PSA_SUCCESS) {
3999 return status;
4000 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001
Paul Elliott2c9843f2023-02-15 17:32:42 +00004002 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004003
Paul Elliott2c9843f2023-02-15 17:32:42 +00004004 if (status != PSA_SUCCESS) {
4005 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004006 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007
Paul Elliott0290a762023-02-09 14:30:24 +00004008 /* We only need to store the same length of hash as the private key size
4009 * here, it would be truncated by the internal implementation anyway. */
4010 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4011 coordinate_bytes);
4012
Paul Elliottba70ad42023-02-15 18:23:53 +00004013 if (required_hash_length > sizeof(operation->hash)) {
4014 /* Shouldn't happen, but better safe than sorry. */
4015 return PSA_ERROR_CORRUPTION_DETECTED;
4016 }
4017
Paul Elliott0290a762023-02-09 14:30:24 +00004018 memcpy(operation->hash, hash, required_hash_length);
4019 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004020
4021 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022#else
Paul Elliott068fe072023-01-16 13:59:15 +00004023 (void) operation;
4024 (void) key_buffer;
4025 (void) key_buffer_size;
4026 (void) alg;
4027 (void) hash;
4028 (void) hash_length;
4029 (void) signature;
4030 (void) signature_length;
4031 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004032 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004033 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004034
Paul Elliott068fe072023-01-16 13:59:15 +00004035 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004036#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4037 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4038 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004039}
4040
4041psa_status_t mbedtls_psa_verify_hash_complete(
4042 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4043{
4044
4045#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4046 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4047 defined(MBEDTLS_ECP_RESTARTABLE)
4048
Paul Elliottf8e5b562023-02-19 18:43:45 +00004049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4050
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004051 /* Ensure max_ops is set to the current value (or default). */
4052 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4053
Paul Elliottf8e5b562023-02-19 18:43:45 +00004054 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004055 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4056 operation->hash,
4057 operation->hash_length,
4058 &operation->ctx->Q,
4059 &operation->r,
4060 &operation->s,
4061 &operation->restart_ctx));
4062
Paul Elliottf8e5b562023-02-19 18:43:45 +00004063 /* Hide the fact that the restart context only holds a delta of number of
4064 * ops done during the last operation, not an absolute value. */
4065 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4066
4067 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004068#else
4069 (void) operation;
4070
4071 return PSA_ERROR_NOT_SUPPORTED;
4072
4073#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4074 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4075 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4076}
4077
4078psa_status_t mbedtls_psa_verify_hash_abort(
4079 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4080{
4081
4082#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4083 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4084 defined(MBEDTLS_ECP_RESTARTABLE)
4085
4086 if (operation->ctx) {
4087 mbedtls_ecdsa_free(operation->ctx);
4088 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004089 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004090 }
4091
4092 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4093
Paul Elliott93d9ca82023-02-15 18:14:21 +00004094 operation->num_ops = 0;
4095
Paul Elliott588f8ed2022-12-02 18:10:26 +00004096 mbedtls_mpi_free(&operation->r);
4097 mbedtls_mpi_free(&operation->s);
4098
4099 return PSA_SUCCESS;
4100
4101#else
4102 (void) operation;
4103
4104 return PSA_ERROR_NOT_SUPPORTED;
4105
4106#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4107 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4108 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4109}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004110
mohammad1603503973b2018-03-12 15:59:30 +02004111/****************************************************************/
4112/* Symmetric cryptography */
4113/****************************************************************/
4114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4116 mbedtls_svc_key_id_t key,
4117 psa_algorithm_t alg,
4118 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004119{
4120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4121 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004122 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4124 PSA_KEY_USAGE_ENCRYPT :
4125 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004126 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004127
4128 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004130 status = PSA_ERROR_BAD_STATE;
4131 goto exit;
4132 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004135 status = PSA_ERROR_INVALID_ARGUMENT;
4136 goto exit;
4137 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4140 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004143
Ronald Cron49fafa92021-03-10 08:34:23 +01004144 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004145 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004146 * so we only set it (in the driver wrapper) after resources have been
4147 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004148 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004150 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004152 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 }
4154 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004155
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004156 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004158 };
4159
Ronald Cronab99ac22020-10-01 16:38:28 +02004160 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 if (cipher_operation == MBEDTLS_ENCRYPT) {
4162 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4163 &attributes,
4164 slot->key.data,
4165 slot->key.bytes,
4166 alg);
4167 } else {
4168 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4169 &attributes,
4170 slot->key.data,
4171 slot->key.bytes,
4172 alg);
4173 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004174
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (status != PSA_SUCCESS) {
4177 psa_cipher_abort(operation);
4178 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004183}
4184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4186 mbedtls_svc_key_id_t key,
4187 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004188{
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004190}
4191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4193 mbedtls_svc_key_id_t key,
4194 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004195{
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004197}
4198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4200 uint8_t *iv,
4201 size_t iv_size,
4202 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004203{
Ronald Cron6d051732020-10-01 14:10:20 +02004204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004205 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004206 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004209 status = PSA_ERROR_BAD_STATE;
4210 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004211 }
4212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004214 status = PSA_ERROR_BAD_STATE;
4215 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004216 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004217
Ronald Cron2fb90522021-07-05 12:31:44 +02004218 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004220 status = PSA_ERROR_BUFFER_TOO_SMALL;
4221 goto exit;
4222 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004225 status = PSA_ERROR_GENERIC_ERROR;
4226 goto exit;
4227 }
4228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 status = psa_generate_random(local_iv, default_iv_length);
4230 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 }
Ronald Cron5618a392021-03-26 09:52:26 +01004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 status = psa_driver_wrapper_cipher_set_iv(operation,
4235 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004236
4237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (status == PSA_SUCCESS) {
4239 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004240 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004241 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004243 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004245 }
Ronald Cron6d051732020-10-01 14:10:20 +02004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004248}
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4251 const uint8_t *iv,
4252 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004253{
Ronald Cron6d051732020-10-01 14:10:20 +02004254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004257 status = PSA_ERROR_BAD_STATE;
4258 goto exit;
4259 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004262 status = PSA_ERROR_BAD_STATE;
4263 goto exit;
4264 }
Ronald Crona0d68172021-03-26 10:15:08 +01004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004267 status = PSA_ERROR_INVALID_ARGUMENT;
4268 goto exit;
4269 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 status = psa_driver_wrapper_cipher_set_iv(operation,
4272 iv,
4273 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004274
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004277 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 } else {
4279 psa_cipher_abort(operation);
4280 }
4281 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004282}
4283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4285 const uint8_t *input,
4286 size_t input_length,
4287 uint8_t *output,
4288 size_t output_size,
4289 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004290{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004294 status = PSA_ERROR_BAD_STATE;
4295 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004296 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004299 status = PSA_ERROR_BAD_STATE;
4300 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004301 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 status = psa_driver_wrapper_cipher_update(operation,
4304 input,
4305 input_length,
4306 output,
4307 output_size,
4308 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004309
4310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 if (status != PSA_SUCCESS) {
4312 psa_cipher_abort(operation);
4313 }
Ronald Cron6d051732020-10-01 14:10:20 +02004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004316}
4317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4319 uint8_t *output,
4320 size_t output_size,
4321 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004322{
David Saadab4ecc272019-02-14 13:48:10 +02004323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004326 status = PSA_ERROR_BAD_STATE;
4327 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004328 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004331 status = PSA_ERROR_BAD_STATE;
4332 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004333 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 status = psa_driver_wrapper_cipher_finish(operation,
4336 output,
4337 output_size,
4338 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004339
4340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 if (status == PSA_SUCCESS) {
4342 return psa_cipher_abort(operation);
4343 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004344 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004348 }
mohammad1603503973b2018-03-12 15:59:30 +02004349}
4350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004352{
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004354 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004355 * in use. It's ok to call abort on such an object, and there's
4356 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004358 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004361
Ronald Cron49fafa92021-03-10 08:34:23 +01004362 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004363 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004364 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004367}
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4370 psa_algorithm_t alg,
4371 const uint8_t *input,
4372 size_t input_length,
4373 uint8_t *output,
4374 size_t output_size,
4375 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004376{
4377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004379 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004380 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4381 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004382 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004383
David Horstmanne9a88ab2023-11-29 17:07:13 +00004384 SWAP_FOR_LOCAL_INPUT(input, input_length);
4385 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannc6977b42023-11-27 17:43:05 +00004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004388 status = PSA_ERROR_INVALID_ARGUMENT;
4389 goto exit;
4390 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4393 PSA_KEY_USAGE_ENCRYPT,
4394 alg);
4395 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004396 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004398
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004399 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004401 };
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4404 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004405 status = PSA_ERROR_GENERIC_ERROR;
4406 goto exit;
4407 }
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (default_iv_length > 0) {
4410 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004411 status = PSA_ERROR_BUFFER_TOO_SMALL;
4412 goto exit;
4413 }
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 status = psa_generate_random(local_iv, default_iv_length);
4416 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004419 }
4420
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004421 status = psa_driver_wrapper_cipher_encrypt(
4422 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004423 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004424 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004426
4427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 unlock_status = psa_unlock_key_slot(slot);
4429 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004430 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004431 }
Ronald Cron23919522021-07-08 19:00:07 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (status == PSA_SUCCESS) {
4434 if (default_iv_length > 0) {
4435 memcpy(output, local_iv, default_iv_length);
4436 }
4437 *output_length += default_iv_length;
4438 } else {
4439 *output_length = 0;
4440 }
4441
David Horstmanne9a88ab2023-11-29 17:07:13 +00004442 FREE_LOCAL_INPUT(input);
4443 FREE_LOCAL_OUTPUT(output);
David Horstmannc6977b42023-11-27 17:43:05 +00004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004446}
4447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4449 psa_algorithm_t alg,
4450 const uint8_t *input,
4451 size_t input_length,
4452 uint8_t *output,
4453 size_t output_size,
4454 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004455{
4456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004457 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004458 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004459 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004462 status = PSA_ERROR_INVALID_ARGUMENT;
4463 goto exit;
4464 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4467 PSA_KEY_USAGE_DECRYPT,
4468 alg);
4469 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004470 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004472
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004473 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004475 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4478 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004479 status = PSA_ERROR_INVALID_ARGUMENT;
4480 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004482 status = PSA_ERROR_INVALID_ARGUMENT;
4483 goto exit;
4484 }
4485
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004486 status = psa_driver_wrapper_cipher_decrypt(
4487 &attributes, slot->key.data, slot->key.bytes,
4488 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004490
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004491exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 unlock_status = psa_unlock_key_slot(slot);
4493 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004494 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004498 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 }
Ronald Cron23919522021-07-08 19:00:07 +02004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004502}
4503
4504
mohammad16035955c982018-04-26 00:53:03 +03004505/****************************************************************/
4506/* AEAD */
4507/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004508
Paul Elliottbaff51c2021-09-28 17:44:45 +01004509/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004510static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004511{
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004513}
4514
4515/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004516static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4517 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004518{
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004522#if defined(PSA_WANT_ALG_GCM)
4523 case PSA_ALG_GCM:
4524 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 * arbitrarily large nonces. Please note that we do not generally
4526 * recommend the usage of nonces of greater length than
4527 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4528 * size, which can then lead to collisions if you encrypt a very
4529 * large number of messages.*/
4530 if (nonce_length != 0) {
4531 return PSA_SUCCESS;
4532 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004533 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004534#endif /* PSA_WANT_ALG_GCM */
4535#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004536 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (nonce_length >= 7 && nonce_length <= 13) {
4538 return PSA_SUCCESS;
4539 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004540 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004541#endif /* PSA_WANT_ALG_CCM */
4542#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004543 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (nonce_length == 12) {
4545 return PSA_SUCCESS;
4546 } else if (nonce_length == 8) {
4547 return PSA_ERROR_NOT_SUPPORTED;
4548 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004549 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004550#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004551 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004552 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004554 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004557}
4558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004560{
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4562 return PSA_ERROR_INVALID_ARGUMENT;
4563 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004566}
4567
Gilles Peskine449bd832023-01-11 14:50:10 +01004568psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4569 psa_algorithm_t alg,
4570 const uint8_t *nonce,
4571 size_t nonce_length,
4572 const uint8_t *additional_data,
4573 size_t additional_data_length,
4574 const uint8_t *plaintext,
4575 size_t plaintext_length,
4576 uint8_t *ciphertext,
4577 size_t ciphertext_size,
4578 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004579{
Ronald Cron215633c2021-03-16 17:15:37 +01004580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4581 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004582
mohammad1603f08a5502018-06-03 15:05:47 +03004583 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 status = psa_aead_check_algorithm(alg);
4586 if (status != PSA_SUCCESS) {
4587 return status;
4588 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004589
Ronald Cron9a986162021-03-26 12:40:07 +01004590 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4592 if (status != PSA_SUCCESS) {
4593 return status;
4594 }
mohammad16035955c982018-04-26 00:53:03 +03004595
Ronald Cron215633c2021-03-16 17:15:37 +01004596 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004598 };
mohammad16035955c982018-04-26 00:53:03 +03004599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 status = psa_aead_check_nonce_length(alg, nonce_length);
4601 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004602 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004604
Ronald Cronde822812021-03-17 16:08:20 +01004605 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004606 &attributes, slot->key.data, slot->key.bytes,
4607 alg,
4608 nonce, nonce_length,
4609 additional_data, additional_data_length,
4610 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4614 memset(ciphertext, 0, ciphertext_size);
4615 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004616
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004621}
4622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4624 psa_algorithm_t alg,
4625 const uint8_t *nonce,
4626 size_t nonce_length,
4627 const uint8_t *additional_data,
4628 size_t additional_data_length,
4629 const uint8_t *ciphertext,
4630 size_t ciphertext_length,
4631 uint8_t *plaintext,
4632 size_t plaintext_size,
4633 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004634{
Ronald Cron215633c2021-03-16 17:15:37 +01004635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4636 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004637
Gilles Peskineee652a32018-06-01 19:23:52 +02004638 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 status = psa_aead_check_algorithm(alg);
4641 if (status != PSA_SUCCESS) {
4642 return status;
4643 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004644
Ronald Cron9a986162021-03-26 12:40:07 +01004645 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4647 if (status != PSA_SUCCESS) {
4648 return status;
4649 }
mohammad16035955c982018-04-26 00:53:03 +03004650
Ronald Cron215633c2021-03-16 17:15:37 +01004651 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004653 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 status = psa_aead_check_nonce_length(alg, nonce_length);
4656 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004659
Ronald Cronde822812021-03-17 16:08:20 +01004660 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004661 &attributes, slot->key.data, slot->key.bytes,
4662 alg,
4663 nonce, nonce_length,
4664 additional_data, additional_data_length,
4665 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (status != PSA_SUCCESS && plaintext_size != 0) {
4669 memset(plaintext, 0, plaintext_size);
4670 }
mohammad160360a64d02018-06-03 17:20:42 +03004671
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 return status;
mohammad16035955c982018-04-26 00:53:03 +03004676}
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4679{
4680 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004683#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004685 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4687 return PSA_ERROR_INVALID_ARGUMENT;
4688 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004689 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004690#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004691
Przemek Stekiel86679c72022-10-06 17:06:56 +02004692#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004694 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4696 return PSA_ERROR_INVALID_ARGUMENT;
4697 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004698 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004699#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004700
Przemek Stekiel86679c72022-10-06 17:06:56 +02004701#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004703 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if (tag_len != 16) {
4705 return PSA_ERROR_INVALID_ARGUMENT;
4706 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004707 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004708#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004709
4710 default:
4711 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004715}
4716
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004718static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4719 int is_encrypt,
4720 mbedtls_svc_key_id_t key,
4721 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004722{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4724 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4725 psa_key_slot_t *slot = NULL;
4726 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004727 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 status = psa_aead_check_algorithm(alg);
4730 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004735 status = PSA_ERROR_BAD_STATE;
4736 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004737 }
4738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (operation->nonce_set || operation->lengths_set ||
4740 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004741 status = PSA_ERROR_BAD_STATE;
4742 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004743 }
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4752 alg);
4753 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004756
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004757 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004758 .core = slot->attr
4759 };
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004762 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (is_encrypt) {
4766 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4767 &attributes,
4768 slot->key.data,
4769 slot->key.bytes,
4770 alg);
4771 } else {
4772 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4773 &attributes,
4774 slot->key.data,
4775 slot->key.bytes,
4776 alg);
4777 }
4778 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004779 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004783
4784exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004788 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004790 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 } else {
4792 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004793 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004796}
4797
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4800 mbedtls_svc_key_id_t key,
4801 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004802{
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004804}
4805
4806/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4808 mbedtls_svc_key_id_t key,
4809 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004810{
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812}
4813
4814/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4816 uint8_t *nonce,
4817 size_t nonce_size,
4818 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819{
4820 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004821 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004822 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004824 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004827 status = PSA_ERROR_BAD_STATE;
4828 goto exit;
4829 }
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004832 status = PSA_ERROR_BAD_STATE;
4833 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834 }
4835
Paul Elliotte193ea82021-10-01 13:00:16 +01004836 /* For CCM, this size may not be correct according to the PSA
4837 * specification. The PSA Crypto 1.0.1 specification states:
4838 *
4839 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4840 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4841 *
4842 * However this restriction that L has to be the smallest integer is not
4843 * applied in practice, and it is not implementable here since the
4844 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4846 operation->alg);
4847 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004848 status = PSA_ERROR_BUFFER_TOO_SMALL;
4849 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 status = psa_generate_random(local_nonce, required_nonce_size);
4853 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004854 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004858
Paul Elliott39dc6b82021-05-11 19:16:09 +01004859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (status == PSA_SUCCESS) {
4861 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004862 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 } else {
4864 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004865 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004868}
4869
4870/* Set the nonce for a multipart authenticated encryption or decryption
4871 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004872psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4873 const uint8_t *nonce,
4874 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004879 status = PSA_ERROR_BAD_STATE;
4880 goto exit;
4881 }
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004884 status = PSA_ERROR_BAD_STATE;
4885 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004886 }
4887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4889 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004890 status = PSA_ERROR_INVALID_ARGUMENT;
4891 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004892 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4895 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004896
Paul Elliott39dc6b82021-05-11 19:16:09 +01004897exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004899 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 } else {
4901 psa_aead_abort(operation);
4902 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004905}
4906
4907/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4909 size_t ad_length,
4910 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004911{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4913
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004915 status = PSA_ERROR_BAD_STATE;
4916 goto exit;
4917 }
4918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if (operation->lengths_set || operation->ad_started ||
4920 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004921 status = PSA_ERROR_BAD_STATE;
4922 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004923 }
4924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004926#if defined(PSA_WANT_ALG_GCM)
4927 case PSA_ALG_GCM:
4928 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 * bits. Without the guard this code will generate warnings on 32bit
4930 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004931#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 if (((uint64_t) ad_length) >> 61 != 0 ||
4933 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004934 status = PSA_ERROR_INVALID_ARGUMENT;
4935 goto exit;
4936 }
Paul Elliott325d3742021-09-27 17:56:28 +01004937#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004938 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004939#endif /* PSA_WANT_ALG_GCM */
4940#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004941 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004943 status = PSA_ERROR_INVALID_ARGUMENT;
4944 goto exit;
4945 }
4946 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004947#endif /* PSA_WANT_ALG_CCM */
4948#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004949 case PSA_ALG_CHACHA20_POLY1305:
4950 /* No length restrictions for ChaChaPoly. */
4951 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004952#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004953 default:
4954 break;
4955 }
Paul Elliott325d3742021-09-27 17:56:28 +01004956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4958 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004959
Paul Elliott39dc6b82021-05-11 19:16:09 +01004960exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004962 operation->ad_remaining = ad_length;
4963 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004964 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 } else {
4966 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004967 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004970}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004971
4972/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004973psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4974 const uint8_t *input,
4975 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004976{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004977 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4978
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004980 status = PSA_ERROR_BAD_STATE;
4981 goto exit;
4982 }
4983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004985 status = PSA_ERROR_BAD_STATE;
4986 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004987 }
4988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 if (operation->lengths_set) {
4990 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004991 status = PSA_ERROR_INVALID_ARGUMENT;
4992 goto exit;
4993 }
4994
4995 operation->ad_remaining -= input_length;
4996 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004997#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004999 status = PSA_ERROR_BAD_STATE;
5000 goto exit;
5001 }
5002#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 status = psa_driver_wrapper_aead_update_ad(operation, input,
5005 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005006
Paul Elliott39dc6b82021-05-11 19:16:09 +01005007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005009 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 } else {
5011 psa_aead_abort(operation);
5012 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005015}
5016
5017/* Encrypt or decrypt a message fragment in an active multipart AEAD
5018 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005019psa_status_t psa_aead_update(psa_aead_operation_t *operation,
5020 const uint8_t *input,
5021 size_t input_length,
5022 uint8_t *output,
5023 size_t output_size,
5024 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005025{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005026 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005027
5028 *output_length = 0;
5029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005031 status = PSA_ERROR_BAD_STATE;
5032 goto exit;
5033 }
5034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036 status = PSA_ERROR_BAD_STATE;
5037 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005038 }
5039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005041 /* Additional data length was supplied, but not all the additional
5042 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005044 status = PSA_ERROR_INVALID_ARGUMENT;
5045 goto exit;
5046 }
5047
5048 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005050 status = PSA_ERROR_INVALID_ARGUMENT;
5051 goto exit;
5052 }
5053
5054 operation->body_remaining -= input_length;
5055 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005056#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005058 status = PSA_ERROR_BAD_STATE;
5059 goto exit;
5060 }
5061#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005062
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5064 output, output_size,
5065 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005066
Paul Elliott39dc6b82021-05-11 19:16:09 +01005067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005069 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 } else {
5071 psa_aead_abort(operation);
5072 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075}
5076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005078{
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (operation->id == 0 || !operation->nonce_set) {
5080 return PSA_ERROR_BAD_STATE;
5081 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5084 operation->body_remaining != 0)) {
5085 return PSA_ERROR_INVALID_ARGUMENT;
5086 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005089}
5090
Paul Elliott302ff6b2021-04-20 18:10:30 +01005091/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5093 uint8_t *ciphertext,
5094 size_t ciphertext_size,
5095 size_t *ciphertext_length,
5096 uint8_t *tag,
5097 size_t tag_size,
5098 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005099{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005100 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5101
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005103 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 status = psa_aead_final_checks(operation);
5106 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005111 status = PSA_ERROR_BAD_STATE;
5112 goto exit;
5113 }
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5116 ciphertext_size,
5117 ciphertext_length,
5118 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005119
Paul Elliott39dc6b82021-05-11 19:16:09 +01005120exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005121
5122
Paul Elliottf47b0952021-05-21 18:02:33 +01005123 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005124 * the zero tag size, make sure the tag is set to something implausible.
5125 * Even if the operation succeeds, make sure we clear the rest of the
5126 * buffer to prevent potential leakage of anything previously placed in
5127 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005128 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005133}
5134
5135/* Finish authenticating and decrypting a message in a multipart AEAD
5136 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005137psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5138 uint8_t *plaintext,
5139 size_t plaintext_size,
5140 size_t *plaintext_length,
5141 const uint8_t *tag,
5142 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5145
Paul Elliott302ff6b2021-04-20 18:10:30 +01005146 *plaintext_length = 0;
5147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 status = psa_aead_final_checks(operation);
5149 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005154 status = PSA_ERROR_BAD_STATE;
5155 goto exit;
5156 }
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5159 plaintext_size,
5160 plaintext_length,
5161 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005162
5163exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005167}
5168
5169/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005170psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005171{
5172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005175 /* The object has (apparently) been initialized but it is not (yet)
5176 * in use. It's ok to call abort on such an object, and there's
5177 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005179 }
5180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005186}
5187
Gilles Peskinee59236f2018-01-27 23:32:46 +01005188/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005189/* Generators */
5190/****************************************************************/
5191
Przemek Stekiel69c46792022-06-10 12:59:51 +02005192#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005193 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005194 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305195 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305196 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005197#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005198#endif /* At least one builtin KDF */
5199
Przemek Stekiel69c46792022-06-10 12:59:51 +02005200#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005201 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5202 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5203static psa_status_t psa_key_derivation_start_hmac(
5204 psa_mac_operation_t *operation,
5205 psa_algorithm_t hash_alg,
5206 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005208{
5209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5212 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005214
Steven Cooreman72f736a2021-05-07 14:14:37 +02005215 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 status = psa_driver_wrapper_mac_sign_setup(operation,
5219 &attributes,
5220 hmac_key, hmac_key_length,
5221 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 psa_reset_key_attributes(&attributes);
5224 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005225}
5226#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005227
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005228#define HKDF_STATE_INIT 0 /* no input yet */
5229#define HKDF_STATE_STARTED 1 /* got salt */
5230#define HKDF_STATE_KEYED 2 /* got key */
5231#define HKDF_STATE_OUTPUT 3 /* output started */
5232
Gilles Peskinecbe66502019-05-16 16:59:18 +02005233static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235{
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5237 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5238 } else {
5239 return operation->alg;
5240 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005241}
5242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005244{
5245 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5247 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248 /* The object has (apparently) been initialized but it is not
5249 * in use. It's ok to call abort on such an object, and there's
5250 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005252#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5254 mbedtls_free(operation->ctx.hkdf.info);
5255 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5256 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005257#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005258#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5259 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5261 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5262 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5263 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005264 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005266 }
5267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005269 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005271 }
5272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005274 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005276 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005277#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005279 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005281 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005282#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005283 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005284
5285 /* We leave the fields Ai and output_block to be erased safely by the
5286 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005288#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5289 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5292 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5293 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5294 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005295#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305296#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305297 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305298 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005299 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305300 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305301 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305302
5303 status = PSA_SUCCESS;
5304 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305305#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005306 {
5307 status = PSA_ERROR_BAD_STATE;
5308 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 mbedtls_platform_zeroize(operation, sizeof(*operation));
5310 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005311}
5312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005315{
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005319 }
5320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005323}
5324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5326 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327{
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 if (operation->alg == 0) {
5329 return PSA_ERROR_BAD_STATE;
5330 }
5331 if (capacity > operation->capacity) {
5332 return PSA_ERROR_INVALID_ARGUMENT;
5333 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005336}
5337
Przemek Stekiel69c46792022-06-10 12:59:51 +02005338#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005339/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005340static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5341 psa_algorithm_t kdf_alg,
5342 uint8_t *output,
5343 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005344{
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5346 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005347 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005349#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005351#else
5352 const uint8_t last_block = 0xff;
5353#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 if (hkdf->state < HKDF_STATE_KEYED ||
5356 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005357#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005359#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 )) {
5361 return PSA_ERROR_BAD_STATE;
5362 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 hkdf->state = HKDF_STATE_OUTPUT;
5364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005366 /* Copy what remains of the current block */
5367 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005369 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 }
5371 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005372 output += n;
5373 output_length -= n;
5374 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005378 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005379 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005381 * object was corrupted or if this function is called directly
5382 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (hkdf->block_number == last_block) {
5384 return PSA_ERROR_BAD_STATE;
5385 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005386
5387 /* We need a new block */
5388 ++hkdf->block_number;
5389 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5392 hash_alg,
5393 hkdf->prk,
5394 hash_length);
5395 if (status != PSA_SUCCESS) {
5396 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005398
5399 if (hkdf->block_number != 1) {
5400 status = psa_mac_update(&hkdf->hmac,
5401 hkdf->output_block,
5402 hash_length);
5403 if (status != PSA_SUCCESS) {
5404 return status;
5405 }
5406 }
5407 status = psa_mac_update(&hkdf->hmac,
5408 hkdf->info,
5409 hkdf->info_length);
5410 if (status != PSA_SUCCESS) {
5411 return status;
5412 }
5413 status = psa_mac_update(&hkdf->hmac,
5414 &hkdf->block_number, 1);
5415 if (status != PSA_SUCCESS) {
5416 return status;
5417 }
5418 status = psa_mac_sign_finish(&hkdf->hmac,
5419 hkdf->output_block,
5420 sizeof(hkdf->output_block),
5421 &hmac_output_length);
5422 if (status != PSA_SUCCESS) {
5423 return status;
5424 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005425 }
5426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005428}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005429#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005430
John Durkop07cc04a2020-11-16 22:08:34 -08005431#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5432 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005433static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5434 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005436{
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5438 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005439 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5440 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005441 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005442
Janos Follath7742fee2019-06-17 12:58:10 +01005443 /* We can't be wanting more output after block 0xff, otherwise
5444 * the capacity check in psa_key_derivation_output_bytes() would have
5445 * prevented this call. It could happen only if the operation
5446 * object was corrupted or if this function is called directly
5447 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 if (tls12_prf->block_number == 0xff) {
5449 return PSA_ERROR_CORRUPTION_DETECTED;
5450 }
Janos Follath7742fee2019-06-17 12:58:10 +01005451
5452 /* We need a new block */
5453 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005454 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005455
5456 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5457 *
5458 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5459 *
5460 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5461 * HMAC_hash(secret, A(2) + seed) +
5462 * HMAC_hash(secret, A(3) + seed) + ...
5463 *
5464 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005466 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005467 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005468 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005469 * is currently extracted as `output_block` and where i is
5470 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005471 */
5472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_key_derivation_start_hmac(&hmac,
5474 hash_alg,
5475 tls12_prf->secret,
5476 tls12_prf->secret_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 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005483 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5484 * the variable seed and in this instance means it in the context of the
5485 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 status = psa_mac_update(&hmac,
5487 tls12_prf->label,
5488 tls12_prf->label_length);
5489 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005490 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 }
5492 status = psa_mac_update(&hmac,
5493 tls12_prf->seed,
5494 tls12_prf->seed_length);
5495 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005496 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
5498 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 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 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005504 }
5505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 status = psa_mac_sign_finish(&hmac,
5507 tls12_prf->Ai, hash_length,
5508 &hmac_output_length);
5509 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005510 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 }
5512 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005513 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005515
5516 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 status = psa_key_derivation_start_hmac(&hmac,
5518 hash_alg,
5519 tls12_prf->secret,
5520 tls12_prf->secret_length);
5521 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005522 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 }
5524 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5525 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005526 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 }
5528 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5529 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005530 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 }
5532 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5533 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005534 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 }
5536 status = psa_mac_sign_finish(&hmac,
5537 tls12_prf->output_block, hash_length,
5538 &hmac_output_length);
5539 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005540 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005542
Janos Follath7742fee2019-06-17 12:58:10 +01005543
5544cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 cleanup_status = psa_mac_abort(&hmac);
5546 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005547 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005551}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005552
Janos Follath844eb0e2019-06-19 12:10:49 +01005553static psa_status_t psa_key_derivation_tls12_prf_read(
5554 psa_tls12_prf_key_derivation_t *tls12_prf,
5555 psa_algorithm_t alg,
5556 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005558{
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5560 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005561 psa_status_t status;
5562 uint8_t offset, length;
5563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005565 case PSA_TLS12_PRF_STATE_LABEL_SET:
5566 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5567 break;
5568 case PSA_TLS12_PRF_STATE_OUTPUT:
5569 break;
5570 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005572 }
5573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005575 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 if (tls12_prf->left_in_block == 0) {
5577 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5578 alg);
5579 if (status != PSA_SUCCESS) {
5580 return status;
5581 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005582
5583 continue;
5584 }
5585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005587 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005589 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005591
5592 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005594 output += length;
5595 output_length -= length;
5596 tls12_prf->left_in_block -= length;
5597 }
5598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005600}
John Durkop07cc04a2020-11-16 22:08:34 -08005601#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5602 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005603
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005604#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5605static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5606 psa_tls12_ecjpake_to_pms_t *ecjpake,
5607 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005609{
5610 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005611 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 if (output_length != 32) {
5614 return PSA_ERROR_INVALID_ARGUMENT;
5615 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5618 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5619 &output_size);
5620 if (status != PSA_SUCCESS) {
5621 return status;
5622 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 if (output_size != output_length) {
5625 return PSA_ERROR_GENERIC_ERROR;
5626 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005629}
5630#endif
5631
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305632#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305633static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5634 psa_pbkdf2_key_derivation_t *pbkdf2,
5635 psa_algorithm_t prf_alg,
5636 uint8_t prf_output_length,
5637 psa_key_attributes_t *attributes)
5638{
5639 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305640 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305641 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305642 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305643 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305644 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305645 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305646
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305647 mac_operation.is_sign = 1;
5648 mac_operation.mac_size = prf_output_length;
5649 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305650
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305651 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5652 attributes,
5653 pbkdf2->password,
5654 pbkdf2->password_length,
5655 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305657 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305658 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305659 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5660 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305661 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305662 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305663 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305664 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305665 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305666 }
5667 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5668 &mac_output_length);
5669 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305670 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305671 }
5672
5673 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305674 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305675 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305676 }
5677
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305678 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305679
5680 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305681 /* We are passing prf_output_length as mac_size because the driver
5682 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305683 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305684 status = psa_driver_wrapper_mac_compute(attributes,
5685 pbkdf2->password,
5686 pbkdf2->password_length,
5687 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305688 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305689 &mac_output_length);
5690 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305691 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305692 }
5693
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305694 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305695 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305696
5697cleanup:
5698 /* Zeroise buffers to clear sensitive data from memory. */
5699 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5700 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305701}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305702
5703static psa_status_t psa_key_derivation_pbkdf2_read(
5704 psa_pbkdf2_key_derivation_t *pbkdf2,
5705 psa_algorithm_t kdf_alg,
5706 uint8_t *output,
5707 size_t output_length)
5708{
5709 psa_status_t status;
5710 psa_algorithm_t prf_alg;
5711 uint8_t prf_output_length;
5712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5713 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5714 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5715
5716 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5717 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5718 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5719 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305720 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5721 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305722 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305723 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305724 } else {
5725 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305726 }
5727
5728 switch (pbkdf2->state) {
5729 case PSA_PBKDF2_STATE_PASSWORD_SET:
5730 /* Initially we need a new block so bytes_used is equal to block size*/
5731 pbkdf2->bytes_used = prf_output_length;
5732 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5733 break;
5734 case PSA_PBKDF2_STATE_OUTPUT:
5735 break;
5736 default:
5737 return PSA_ERROR_BAD_STATE;
5738 }
5739
5740 while (output_length != 0) {
5741 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5742 if (n > output_length) {
5743 n = (uint8_t) output_length;
5744 }
5745 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5746 output += n;
5747 output_length -= n;
5748 pbkdf2->bytes_used += n;
5749
5750 if (output_length == 0) {
5751 break;
5752 }
5753
5754 /* We need a new block */
5755 pbkdf2->bytes_used = 0;
5756 pbkdf2->block_number++;
5757
5758 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5759 prf_output_length,
5760 &attributes);
5761 if (status != PSA_SUCCESS) {
5762 return status;
5763 }
5764 }
5765
5766 return PSA_SUCCESS;
5767}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305768#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305769
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005770psa_status_t psa_key_derivation_output_bytes(
5771 psa_key_derivation_operation_t *operation,
5772 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005774{
5775 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005779 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005781 }
5782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005784 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005785 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005786 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005787 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005788 goto exit;
5789 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005792 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005793 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5794 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005796 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005798 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800
Przemek Stekiel69c46792022-06-10 12:59:51 +02005801#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5803 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5804 output, output_length);
5805 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005806#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005807#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5808 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5810 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5811 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5812 kdf_alg, output,
5813 output_length);
5814 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005815#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5816 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005817#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005819 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5821 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005822#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305823#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305824 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305825 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5826 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305827 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305828#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005829
Gilles Peskineeab56e42018-07-12 17:12:33 +02005830 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005831 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005833 }
5834
5835exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005837 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005838 * This allows us to differentiate between exhausted operations and
5839 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5840 * operations. */
5841 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005843 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005847}
5848
David Brown7807bf72021-02-09 16:28:23 -07005849#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005850static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005851{
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if (data_size >= 8) {
5853 mbedtls_des_key_set_parity(data);
5854 }
5855 if (data_size >= 16) {
5856 mbedtls_des_key_set_parity(data + 8);
5857 }
5858 if (data_size >= 24) {
5859 mbedtls_des_key_set_parity(data + 16);
5860 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005861}
David Brown7807bf72021-02-09 16:28:23 -07005862#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005863
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005864/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 * ECC keys on a Weierstrass elliptic curve require the generation
5866 * of a private key which is an integer
5867 * in the range [1, N - 1], where N is the boundary of the private key domain:
5868 * N is the prime p for Diffie-Hellman, or the order of the
5869 * curve’s base point for ECC.
5870 *
5871 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5872 * This function generates the private key using the following process:
5873 *
5874 * 1. Draw a byte string of length ceiling(m/8) bytes.
5875 * 2. If m is not a multiple of 8, set the most significant
5876 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5877 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5878 * 4. If k > N - 2, discard the result and return to step 1.
5879 * 5. Output k + 1 as the private key.
5880 *
5881 * This method allows compliance to NIST standards, specifically the methods titled
5882 * Key-Pair Generation by Testing Candidates in the following publications:
5883 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5884 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5885 * Diffie-Hellman keys.
5886 *
5887 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5888 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5889 *
5890 * Note: Function allocates memory for *data buffer, so given *data should be
5891 * always NULL.
5892 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005893#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5894#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005895static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896 psa_key_slot_t *slot,
5897 size_t bits,
5898 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005899 uint8_t **data
5900 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903 mbedtls_mpi k;
5904 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005906 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005907 size_t m;
5908 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 mbedtls_mpi_init(&k);
5911 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005912
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005913 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005915 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005919 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005920 goto cleanup;
5921 }
5922
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005923 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005927
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005928 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005929 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005930 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005931
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005932 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005933
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005934 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005936
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005937 /* Note: This function is always called with *data == NULL and it
5938 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 *data = mbedtls_calloc(1, m_bytes);
5940 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005941 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942 goto cleanup;
5943 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005946 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005948 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005950
5951 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005953 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005954 * (8 * ceiling(m/8) - m) bits of the first byte in
5955 * the string to zero.
5956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005958 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005959 }
5960
5961 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 * big-endian byte string.
5963 */
5964 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005965
5966 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 * Result of comparison is returned. When it indicates error
5968 * then this function is called again.
5969 */
5970 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005971 }
5972
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005973 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5975 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005976cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if (ret != 0) {
5978 status = mbedtls_to_psa_error(ret);
5979 }
5980 if (status != PSA_SUCCESS) {
5981 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005982 *data = NULL;
5983 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 mbedtls_mpi_free(&k);
5985 mbedtls_mpi_free(&diff_N_2);
5986 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005987}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988
5989/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5990 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5991 *
5992 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5993 * draw a 32-byte string and process it as specified in
5994 * Elliptic Curves for Security [RFC7748] §5.
5995 *
5996 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5997 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5998 *
5999 * Note: Function allocates memory for *data buffer, so given *data should be
6000 * always NULL.
6001 */
6002
6003static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6004 size_t bits,
6005 psa_key_derivation_operation_t *operation,
6006 uint8_t **data
6007 )
6008{
6009 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
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 output_length = 32;
6015 break;
6016 case 448:
6017 output_length = 56;
6018 break;
6019 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006021 break;
6022 }
6023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (*data == NULL) {
6027 return PSA_ERROR_INSUFFICIENT_MEMORY;
6028 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006033 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006037 case 255:
6038 (*data)[0] &= 248;
6039 (*data)[31] &= 127;
6040 (*data)[31] |= 64;
6041 break;
6042 case 448:
6043 (*data)[0] &= 252;
6044 (*data)[55] |= 128;
6045 break;
6046 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006048 break;
6049 }
6050
6051 return status;
6052}
Valerio Setti86587ab2023-06-27 13:09:30 +02006053#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6054static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6055 psa_key_slot_t *slot, size_t bits,
6056 psa_key_derivation_operation_t *operation, uint8_t **data)
6057{
6058 (void) slot;
6059 (void) bits;
6060 (void) operation;
6061 (void) data;
6062 return PSA_ERROR_NOT_SUPPORTED;
6063}
6064
6065static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6066 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6067{
6068 (void) bits;
6069 (void) operation;
6070 (void) data;
6071 return PSA_ERROR_NOT_SUPPORTED;
6072}
6073#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6074#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006075
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006076static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006077 psa_key_slot_t *slot,
6078 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006080{
6081 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306083 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006085 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6088 return PSA_ERROR_INVALID_ARGUMENT;
6089 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006090
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006091#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006092 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6094 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6095 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006096 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6098 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 }
6101 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006102 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6104 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006105 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006107 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006108 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006109#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006110 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 if (key_type_is_raw_bytes(slot->attr.type)) {
6112 if (bits % 8 != 0) {
6113 return PSA_ERROR_INVALID_ARGUMENT;
6114 }
6115 data = mbedtls_calloc(1, bytes);
6116 if (data == NULL) {
6117 return PSA_ERROR_INSUFFICIENT_MEMORY;
6118 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 status = psa_key_derivation_output_bytes(operation, data, bytes);
6121 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006122 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 }
David Brown12ca5032021-02-11 11:02:00 -07006124#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6126 psa_des_set_key_parity(data, bytes);
6127 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006128#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 } else {
6130 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006131 }
Ronald Crondd04d422020-11-28 15:14:42 +01006132
Ronald Cronbf33c932020-11-28 18:06:53 +01006133 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006134 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006136 };
6137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6139 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6140 &storage_size);
6141 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 }
Archanad8a83dc2021-06-14 10:04:16 +05306144 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 status = psa_allocate_buffer_to_slot(slot, storage_size);
6146 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 }
Archanad8a83dc2021-06-14 10:04:16 +05306149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 status = psa_driver_wrapper_import_key(&attributes,
6151 data, bytes,
6152 slot->key.data,
6153 slot->key.bytes,
6154 &slot->key.bytes, &bits);
6155 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006156 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006158
6159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 mbedtls_free(data);
6161 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006162}
6163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6165 psa_key_derivation_operation_t *operation,
6166 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006167{
6168 psa_status_t status;
6169 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006170 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006171
Ronald Cron81709fc2020-11-14 12:10:32 +01006172 *key = MBEDTLS_SVC_KEY_ID_INIT;
6173
Gilles Peskine0f84d622019-09-12 19:03:13 +02006174 /* Reject any attempt to create a zero-length key so that we don't
6175 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (psa_get_key_bits(attributes) == 0) {
6177 return PSA_ERROR_INVALID_ARGUMENT;
6178 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006179
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 if (operation->alg == PSA_ALG_NONE) {
6181 return PSA_ERROR_BAD_STATE;
6182 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 if (!operation->can_output_key) {
6185 return PSA_ERROR_NOT_PERMITTED;
6186 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6189 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006190#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006192 /* Deriving a key in a secure element is not implemented yet. */
6193 status = PSA_ERROR_NOT_SUPPORTED;
6194 }
6195#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (status == PSA_SUCCESS) {
6197 status = psa_generate_derived_key_internal(slot,
6198 attributes->core.bits,
6199 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (status == PSA_SUCCESS) {
6202 status = psa_finish_key_creation(slot, driver, key);
6203 }
6204 if (status != PSA_SUCCESS) {
6205 psa_fail_key_creation(slot, driver);
6206 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006209}
6210
Gilles Peskineeab56e42018-07-12 17:12:33 +02006211
6212
6213/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006214/* Key derivation */
6215/****************************************************************/
6216
Steven Cooreman932ffb72021-02-15 12:14:32 +01006217#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006219{
6220#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6222 return 1;
6223 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006224#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006225#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6227 return 1;
6228 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006229#endif
6230#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6232 return 1;
6233 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006234#endif
6235#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6237 return 1;
6238 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006239#endif
6240#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6242 return 1;
6243 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006244#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006245#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6247 return 1;
6248 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006249#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306250#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6251 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6252 return 1;
6253 }
6254#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306255#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6256 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6257 return 1;
6258 }
6259#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006261}
6262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006264{
6265 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_status_t status = psa_hash_setup(&operation, alg);
6267 psa_hash_abort(&operation);
6268 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006269}
6270
Gilles Peskine969c5d62019-01-16 15:53:06 +01006271static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006272 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006274{
Janos Follath5fe19732019-06-20 15:09:30 +01006275 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6276 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006278
Gilles Peskine969c5d62019-01-16 15:53:06 +01006279 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (!is_kdf_alg_supported(kdf_alg)) {
6281 return PSA_ERROR_NOT_SUPPORTED;
6282 }
John Durkop07cc04a2020-11-16 22:08:34 -08006283
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006284 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306285 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6287 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306288 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6289 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6290 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306291 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306292 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 if (hash_size == 0) {
6294 return PSA_ERROR_NOT_SUPPORTED;
6295 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006296
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006297 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6298 * we might fail later, which is somewhat unfriendly and potentially
6299 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 psa_status_t status = psa_hash_try_support(hash_alg);
6301 if (status != PSA_SUCCESS) {
6302 return status;
6303 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006304 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6307 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6308 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6309 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006310 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006311#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006312 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6314 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006315 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006317#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6318 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 operation->capacity = 255 * hash_size;
6320 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006321}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006324{
6325#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (alg == PSA_ALG_ECDH) {
6327 return PSA_SUCCESS;
6328 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006329#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006330#if defined(PSA_WANT_ALG_FFDH)
6331 if (alg == PSA_ALG_FFDH) {
6332 return PSA_SUCCESS;
6333 }
6334#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006335 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006337}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006338
6339static int psa_key_derivation_allows_free_form_secret_input(
6340 psa_algorithm_t kdf_alg)
6341{
6342#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6343 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6344 return 0;
6345 }
6346#endif
6347 (void) kdf_alg;
6348 return 1;
6349}
John Durkop07cc04a2020-11-16 22:08:34 -08006350#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6353 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006354{
6355 psa_status_t status;
6356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 if (operation->alg != 0) {
6358 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006359 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6362 return PSA_ERROR_INVALID_ARGUMENT;
6363 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6364#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6365 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6366 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6367 status = psa_key_agreement_try_support(ka_alg);
6368 if (status != PSA_SUCCESS) {
6369 return status;
6370 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006371 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6372 return PSA_ERROR_INVALID_ARGUMENT;
6373 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6375#else
6376 return PSA_ERROR_NOT_SUPPORTED;
6377#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6378 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6379#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6380 status = psa_key_derivation_setup_kdf(operation, alg);
6381#else
6382 return PSA_ERROR_NOT_SUPPORTED;
6383#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6384 } else {
6385 return PSA_ERROR_INVALID_ARGUMENT;
6386 }
6387
6388 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006389 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 }
6391 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006392}
6393
Przemek Stekiel69c46792022-06-10 12:59:51 +02006394#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006395static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6396 psa_algorithm_t kdf_alg,
6397 psa_key_derivation_step_t step,
6398 const uint8_t *data,
6399 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006400{
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006402 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006404 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006405#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6407 return PSA_ERROR_INVALID_ARGUMENT;
6408 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006409#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (hkdf->state != HKDF_STATE_INIT) {
6411 return PSA_ERROR_BAD_STATE;
6412 } else {
6413 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6414 hash_alg,
6415 data, data_length);
6416 if (status != PSA_SUCCESS) {
6417 return status;
6418 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006419 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006421 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006422 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006423#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006425 /* We shouldn't be in different state as HKDF_EXPAND only allows
6426 * two inputs: SECRET (this case) and INFO which does not modify
6427 * the state. It could happen only if the hkdf
6428 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (hkdf->state != HKDF_STATE_INIT) {
6430 return PSA_ERROR_BAD_STATE;
6431 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006432
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006433 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6435 return PSA_ERROR_INVALID_ARGUMENT;
6436 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 memcpy(hkdf->prk, data, data_length);
6439 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006440#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006441 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006442 /* HKDF: If no salt was provided, use an empty salt.
6443 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006445#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6447 return PSA_ERROR_BAD_STATE;
6448 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6451 hash_alg,
6452 NULL, 0);
6453 if (status != PSA_SUCCESS) {
6454 return status;
6455 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006456 hkdf->state = HKDF_STATE_STARTED;
6457 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 if (hkdf->state != HKDF_STATE_STARTED) {
6459 return PSA_ERROR_BAD_STATE;
6460 }
6461 status = psa_mac_update(&hkdf->hmac,
6462 data, data_length);
6463 if (status != PSA_SUCCESS) {
6464 return status;
6465 }
6466 status = psa_mac_sign_finish(&hkdf->hmac,
6467 hkdf->prk,
6468 sizeof(hkdf->prk),
6469 &data_length);
6470 if (status != PSA_SUCCESS) {
6471 return status;
6472 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006473 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006474
Gilles Peskine2b522db2019-04-12 15:11:49 +02006475 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006476 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006477#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006479 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006481 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006483#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006484 {
6485 /* Block 0 is empty, and the next block will be
6486 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006488 }
6489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006491 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006492#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6494 return PSA_ERROR_INVALID_ARGUMENT;
6495 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006496#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006497#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6499 hkdf->state == HKDF_STATE_INIT) {
6500 return PSA_ERROR_BAD_STATE;
6501 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006502#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (hkdf->state == HKDF_STATE_OUTPUT) {
6504 return PSA_ERROR_BAD_STATE;
6505 }
6506 if (hkdf->info_set) {
6507 return PSA_ERROR_BAD_STATE;
6508 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006509 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (data_length != 0) {
6511 hkdf->info = mbedtls_calloc(1, data_length);
6512 if (hkdf->info == NULL) {
6513 return PSA_ERROR_INSUFFICIENT_MEMORY;
6514 }
6515 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006516 }
6517 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006519 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006521 }
6522}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006523#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006524
John Durkop07cc04a2020-11-16 22:08:34 -08006525#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6526 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006527static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6528 const uint8_t *data,
6529 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006530{
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6532 return PSA_ERROR_BAD_STATE;
6533 }
Janos Follathf08e2652019-06-13 09:05:41 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (data_length != 0) {
6536 prf->seed = mbedtls_calloc(1, data_length);
6537 if (prf->seed == NULL) {
6538 return PSA_ERROR_INSUFFICIENT_MEMORY;
6539 }
Janos Follathf08e2652019-06-13 09:05:41 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006542 prf->seed_length = data_length;
6543 }
Janos Follathf08e2652019-06-13 09:05:41 +01006544
Gilles Peskine43f958b2020-12-13 14:55:14 +01006545 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006548}
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6551 const uint8_t *data,
6552 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006553{
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6555 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6556 return PSA_ERROR_BAD_STATE;
6557 }
Janos Follath81550542019-06-13 14:26:34 +01006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 if (data_length != 0) {
6560 prf->secret = mbedtls_calloc(1, data_length);
6561 if (prf->secret == NULL) {
6562 return PSA_ERROR_INSUFFICIENT_MEMORY;
6563 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006566 prf->secret_length = data_length;
6567 }
Janos Follath81550542019-06-13 14:26:34 +01006568
Gilles Peskine43f958b2020-12-13 14:55:14 +01006569 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006572}
6573
Gilles Peskine449bd832023-01-11 14:50:10 +01006574static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6575 const uint8_t *data,
6576 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006577{
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6579 return PSA_ERROR_BAD_STATE;
6580 }
Janos Follath63028dd2019-06-13 09:15:47 +01006581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 if (data_length != 0) {
6583 prf->label = mbedtls_calloc(1, data_length);
6584 if (prf->label == NULL) {
6585 return PSA_ERROR_INSUFFICIENT_MEMORY;
6586 }
Janos Follath63028dd2019-06-13 09:15:47 +01006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006589 prf->label_length = data_length;
6590 }
Janos Follath63028dd2019-06-13 09:15:47 +01006591
Gilles Peskine43f958b2020-12-13 14:55:14 +01006592 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006595}
6596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6598 psa_key_derivation_step_t step,
6599 const uint8_t *data,
6600 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006601{
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006603 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006605 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006607 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006609 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006611 }
6612}
John Durkop07cc04a2020-11-16 22:08:34 -08006613#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6614 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6615
6616#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6617static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6618 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006619 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006621{
6622 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6624 4 + data_length + prf->other_secret_length :
6625 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6628 return PSA_ERROR_INVALID_ARGUMENT;
6629 }
John Durkop07cc04a2020-11-16 22:08:34 -08006630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 uint8_t *pms = mbedtls_calloc(1, pms_len);
6632 if (pms == NULL) {
6633 return PSA_ERROR_INSUFFICIENT_MEMORY;
6634 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006635 uint8_t *cur = pms;
6636
6637 /* pure-PSK:
6638 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006639 *
6640 * The premaster secret is formed as follows: if the PSK is N octets
6641 * long, concatenate a uint16 with the value N, N zero octets, a second
6642 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006643 *
6644 * mixed-PSK:
6645 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6646 * follows: concatenate a uint16 with the length of the other secret,
6647 * the other secret itself, uint16 with the length of PSK, and the
6648 * PSK itself.
6649 * For details please check:
6650 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6651 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6652 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006653 */
6654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6656 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6657 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6658 if (prf->other_secret_length != 0) {
6659 memcpy(cur, prf->other_secret, prf->other_secret_length);
6660 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006661 cur += prf->other_secret_length;
6662 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 } else {
6664 *cur++ = MBEDTLS_BYTE_1(data_length);
6665 *cur++ = MBEDTLS_BYTE_0(data_length);
6666 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006667 cur += data_length;
6668 }
6669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 *cur++ = MBEDTLS_BYTE_1(data_length);
6671 *cur++ = MBEDTLS_BYTE_0(data_length);
6672 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006673 cur += data_length;
6674
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006675 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006676
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006677 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006679}
Janos Follath6660f0e2019-06-17 08:44:03 +01006680
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6682 psa_tls12_prf_key_derivation_t *prf,
6683 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006685{
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6687 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006688 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006689
6690 if (data_length != 0) {
6691 prf->other_secret = mbedtls_calloc(1, data_length);
6692 if (prf->other_secret == NULL) {
6693 return PSA_ERROR_INSUFFICIENT_MEMORY;
6694 }
6695
6696 memcpy(prf->other_secret, data, data_length);
6697 prf->other_secret_length = data_length;
6698 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006699 prf->other_secret_length = 0;
6700 }
6701
6702 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006705}
6706
Janos Follath6660f0e2019-06-17 08:44:03 +01006707static psa_status_t psa_tls12_prf_psk_to_ms_input(
6708 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006709 psa_key_derivation_step_t step,
6710 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006712{
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006714 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 return psa_tls12_prf_psk_to_ms_set_key(prf,
6716 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006717 break;
6718 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6720 data,
6721 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006722 break;
6723 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006725 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006726
Przemek Stekiele47201b2022-04-19 13:53:28 +02006727 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006728}
John Durkop07cc04a2020-11-16 22:08:34 -08006729#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006730
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006731#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6732static psa_status_t psa_tls12_ecjpake_to_pms_input(
6733 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006734 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006735 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006737{
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6739 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6740 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006741 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006742
6743 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (data[0] != 0x04) {
6745 return PSA_ERROR_INVALID_ARGUMENT;
6746 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006747
6748 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006752}
6753#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306754
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306755#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306756static psa_status_t psa_pbkdf2_set_input_cost(
6757 psa_pbkdf2_key_derivation_t *pbkdf2,
6758 psa_key_derivation_step_t step,
6759 uint64_t data)
6760{
6761 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6762 return PSA_ERROR_INVALID_ARGUMENT;
6763 }
6764
6765 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6766 return PSA_ERROR_BAD_STATE;
6767 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306768
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306769 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306770 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306771 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306772
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306773 if (data == 0) {
6774 return PSA_ERROR_INVALID_ARGUMENT;
6775 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306776
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306777 pbkdf2->input_cost = data;
6778 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6779
6780 return PSA_SUCCESS;
6781}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306782
6783static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6784 const uint8_t *data,
6785 size_t data_length)
6786{
Gilles Peskineead17662023-08-20 22:02:51 +02006787 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6788 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6789 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6790 /* Appending to existing salt. No state change. */
6791 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306792 return PSA_ERROR_BAD_STATE;
6793 }
6794
Gilles Peskineca57d782023-07-20 19:08:06 +02006795 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006796 /* Appending an empty string, nothing to do. */
6797 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306798 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306799
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306800 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6801 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306802 return PSA_ERROR_INSUFFICIENT_MEMORY;
6803 }
6804
Gilles Peskineead17662023-08-20 22:02:51 +02006805 if (pbkdf2->salt_length != 0) {
6806 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6807 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306808 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306809 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306810 mbedtls_free(pbkdf2->salt);
6811 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306812 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306813 return PSA_SUCCESS;
6814}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306815
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306816#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306817static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306818 const uint8_t *input,
6819 size_t input_len,
6820 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306821 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306822{
6823 psa_status_t status = PSA_SUCCESS;
6824 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6825 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306826 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306827 } else {
6828 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306829 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306830 }
6831 return status;
6832}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306833#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306834
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306835#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306836static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6837 size_t input_len,
6838 uint8_t *output,
6839 size_t *output_len)
6840{
6841 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306842 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306844 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306845 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6846 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6847 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306848 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306849 * mac_size as the driver function sets mac_output_length = mac_size
6850 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306851 status = psa_driver_wrapper_mac_compute(&attributes,
6852 zeros, sizeof(zeros),
6853 PSA_ALG_CMAC, input, input_len,
6854 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306855 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6856 128U,
6857 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306858 output_len);
6859 } else {
6860 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306861 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306862 }
6863 return status;
6864}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306865#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306866
6867static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306868 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306869 const uint8_t *data,
6870 size_t data_length)
6871{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306872 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306873 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6874 return PSA_ERROR_BAD_STATE;
6875 }
6876
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306877#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306878 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6879 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6880 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6881 pbkdf2->password,
6882 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306883 } else
6884#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6885#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6886 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306887 status = psa_pbkdf2_cmac_set_password(data, data_length,
6888 pbkdf2->password,
6889 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306890 } else
6891#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6892 {
6893 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306894 }
6895
6896 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6897
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306898 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306899}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306900
6901static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306902 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306903 psa_key_derivation_step_t step,
6904 const uint8_t *data,
6905 size_t data_length)
6906{
6907 switch (step) {
6908 case PSA_KEY_DERIVATION_INPUT_SALT:
6909 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6910 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306911 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306912 default:
6913 return PSA_ERROR_INVALID_ARGUMENT;
6914 }
6915}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306916#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306917
Gilles Peskineb8965192019-09-24 16:21:10 +02006918/** Check whether the given key type is acceptable for the given
6919 * input step of a key derivation.
6920 *
6921 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6922 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6923 * Both secret and non-secret inputs can alternatively have the type
6924 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6925 * that the input was passed as a buffer rather than via a key object.
6926 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006927static int psa_key_derivation_check_input_type(
6928 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006930{
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006932 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 if (key_type == PSA_KEY_TYPE_DERIVE) {
6934 return PSA_SUCCESS;
6935 }
6936 if (key_type == PSA_KEY_TYPE_NONE) {
6937 return PSA_SUCCESS;
6938 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006940 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (key_type == PSA_KEY_TYPE_DERIVE) {
6942 return PSA_SUCCESS;
6943 }
6944 if (key_type == PSA_KEY_TYPE_NONE) {
6945 return PSA_SUCCESS;
6946 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006947 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006948 case PSA_KEY_DERIVATION_INPUT_LABEL:
6949 case PSA_KEY_DERIVATION_INPUT_SALT:
6950 case PSA_KEY_DERIVATION_INPUT_INFO:
6951 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6953 return PSA_SUCCESS;
6954 }
6955 if (key_type == PSA_KEY_TYPE_NONE) {
6956 return PSA_SUCCESS;
6957 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006958 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306959 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6960 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6961 return PSA_SUCCESS;
6962 }
6963 if (key_type == PSA_KEY_TYPE_DERIVE) {
6964 return PSA_SUCCESS;
6965 }
6966 if (key_type == PSA_KEY_TYPE_NONE) {
6967 return PSA_SUCCESS;
6968 }
6969 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006970 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006972}
6973
Janos Follathb80a94e2019-06-12 15:54:46 +01006974static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006975 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006976 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006977 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006978 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006980{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006981 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 status = psa_key_derivation_check_input_type(step, key_type);
6985 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006986 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006988
Przemek Stekiel69c46792022-06-10 12:59:51 +02006989#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6991 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6992 step, data, data_length);
6993 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006994#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006995#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6997 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6998 step, data, data_length);
6999 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007000#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7001#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7003 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7004 step, data, data_length);
7005 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007006#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007007#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007008 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007009 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7011 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007012#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307013#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307014 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307015 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7016 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307017 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307018#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007019 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007020 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007021 (void) data;
7022 (void) data_length;
7023 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007025 }
7026
Gilles Peskine224b0d62019-09-23 18:13:17 +02007027exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (status != PSA_SUCCESS) {
7029 psa_key_derivation_abort(operation);
7030 }
7031 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007032}
7033
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307034static psa_status_t psa_key_derivation_input_integer_internal(
7035 psa_key_derivation_operation_t *operation,
7036 psa_key_derivation_step_t step,
7037 uint64_t value)
7038{
7039 psa_status_t status;
7040 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7041
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307042#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307043 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307044 status = psa_pbkdf2_set_input_cost(
7045 &operation->ctx.pbkdf2, step, value);
7046 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307047#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307048 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307049 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307050 (void) value;
7051 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307052 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307053 }
7054
7055 if (status != PSA_SUCCESS) {
7056 psa_key_derivation_abort(operation);
7057 }
7058 return status;
7059}
7060
Janos Follath51f4a0f2019-06-14 11:35:55 +01007061psa_status_t psa_key_derivation_input_bytes(
7062 psa_key_derivation_operation_t *operation,
7063 psa_key_derivation_step_t step,
7064 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007066{
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 return psa_key_derivation_input_internal(operation, step,
7068 PSA_KEY_TYPE_NONE,
7069 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007070}
7071
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307072psa_status_t psa_key_derivation_input_integer(
7073 psa_key_derivation_operation_t *operation,
7074 psa_key_derivation_step_t step,
7075 uint64_t value)
7076{
7077 return psa_key_derivation_input_integer_internal(operation, step, value);
7078}
7079
Janos Follath51f4a0f2019-06-14 11:35:55 +01007080psa_status_t psa_key_derivation_input_key(
7081 psa_key_derivation_operation_t *operation,
7082 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007084{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007086 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007087 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007088
Ronald Cron5c522922020-11-14 16:35:34 +01007089 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7091 if (status != PSA_SUCCESS) {
7092 psa_key_derivation_abort(operation);
7093 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007094 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007095
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307096 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7097 * permission to output to a key object. */
7098 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7099 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007100 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 status = psa_key_derivation_input_internal(operation,
7104 step, slot->attr.type,
7105 slot->key.data,
7106 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007111}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007112
7113
7114
7115/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007116/* Key agreement */
7117/****************************************************************/
7118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7120 const uint8_t *key_buffer,
7121 size_t key_buffer_size,
7122 psa_algorithm_t alg,
7123 const uint8_t *peer_key,
7124 size_t peer_key_length,
7125 uint8_t *shared_secret,
7126 size_t shared_secret_size,
7127 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007128{
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007130#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007131 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7133 key_buffer_size, alg,
7134 peer_key, peer_key_length,
7135 shared_secret,
7136 shared_secret_size,
7137 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007138#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007139
7140#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7141 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007142 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007143 peer_key,
7144 peer_key_length,
7145 key_buffer,
7146 key_buffer_size,
7147 shared_secret,
7148 shared_secret_size,
7149 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007150#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7151
Gilles Peskine01d718c2018-09-18 12:01:02 +02007152 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007153 (void) attributes;
7154 (void) key_buffer;
7155 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007156 (void) peer_key;
7157 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007158 (void) shared_secret;
7159 (void) shared_secret_size;
7160 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007162 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007163}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007164
Aditya Deshpande17845b82022-10-13 17:21:01 +01007165/** Internal function for raw key agreement
7166 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007167 * to the driver's implementation if a driver is present.
7168 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007169 * (psa_key_agreement_raw_builtin).
7170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7172 psa_key_slot_t *private_key,
7173 const uint8_t *peer_key,
7174 size_t peer_key_length,
7175 uint8_t *shared_secret,
7176 size_t shared_secret_size,
7177 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007178{
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7180 return PSA_ERROR_NOT_SUPPORTED;
7181 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007182
7183 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007185 };
7186
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 return psa_driver_wrapper_key_agreement(&attributes,
7188 private_key->key.data,
7189 private_key->key.bytes, alg,
7190 peer_key, peer_key_length,
7191 shared_secret,
7192 shared_secret_size,
7193 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007194}
7195
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007196/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007197 * to potentially free embedded data structures and wipe confidential data.
7198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007199static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7200 psa_key_derivation_step_t step,
7201 psa_key_slot_t *private_key,
7202 const uint8_t *peer_key,
7203 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007204{
7205 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007206 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007207 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007209
7210 /* Step 1: run the secret agreement algorithm to generate the shared
7211 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 status = psa_key_agreement_raw_internal(ka_alg,
7213 private_key,
7214 peer_key, peer_key_length,
7215 shared_secret,
7216 sizeof(shared_secret),
7217 &shared_secret_length);
7218 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007221
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007222 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007223 * the shared secret. A shared secret is permitted wherever a key
7224 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 status = psa_key_derivation_input_internal(operation, step,
7226 PSA_KEY_TYPE_DERIVE,
7227 shared_secret,
7228 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7231 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007232}
7233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7235 psa_key_derivation_step_t step,
7236 mbedtls_svc_key_id_t private_key,
7237 const uint8_t *peer_key,
7238 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007239{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007241 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007242 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7245 return PSA_ERROR_INVALID_ARGUMENT;
7246 }
Ronald Cron5c522922020-11-14 16:35:34 +01007247 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7249 if (status != PSA_SUCCESS) {
7250 return status;
7251 }
7252 status = psa_key_agreement_internal(operation, step,
7253 slot,
7254 peer_key, peer_key_length);
7255 if (status != PSA_SUCCESS) {
7256 psa_key_derivation_abort(operation);
7257 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007258 /* If a private key has been added as SECRET, we allow the derived
7259 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007261 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007263 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007268}
7269
Gilles Peskine449bd832023-01-11 14:50:10 +01007270psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7271 mbedtls_svc_key_id_t private_key,
7272 const uint8_t *peer_key,
7273 size_t peer_key_length,
7274 uint8_t *output,
7275 size_t output_size,
7276 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007280 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007281 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284 status = PSA_ERROR_INVALID_ARGUMENT;
7285 goto exit;
7286 }
Ronald Cron5c522922020-11-14 16:35:34 +01007287 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7289 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007292
Gilles Peskine3e561302022-04-14 00:17:15 +02007293 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7294 * for the output size. The PSA specification only guarantees that this
7295 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7296 * but it might be nice to allow smaller buffers if the output fits.
7297 * At the time of writing this comment, with only ECDH implemented,
7298 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7299 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7300 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007301 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7303 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007304 status = PSA_ERROR_BUFFER_TOO_SMALL;
7305 goto exit;
7306 }
7307
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 status = psa_key_agreement_raw_internal(alg, slot,
7309 peer_key, peer_key_length,
7310 output, output_size,
7311 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007312
7313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007315 /* If an error happens and is not handled properly, the output
7316 * may be used as a key to protect sensitive data. Arrange for such
7317 * a key to be random, which is likely to result in decryption or
7318 * verification errors. This is better than filling the buffer with
7319 * some constant data such as zeros, which would result in the data
7320 * being protected with a reproducible, easily knowable key.
7321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007323 *output_length = output_size;
7324 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007327
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007329}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007330
7331
Gilles Peskine30524eb2020-11-13 17:02:26 +01007332
Gilles Peskine86a440b2018-11-12 18:39:40 +01007333/****************************************************************/
7334/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007335/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007336
Gilles Peskine672a7712023-04-28 21:00:28 +02007337#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7338#include "entropy_poll.h"
7339#endif
7340
Gilles Peskine30524eb2020-11-13 17:02:26 +01007341/** Initialize the PSA random generator.
7342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007343static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007344{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007345#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007347#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7348
Gilles Peskine30524eb2020-11-13 17:02:26 +01007349 /* Set default configuration if
7350 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 }
7354 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007357
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007359#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7360 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7361 /* The PSA entropy injection feature depends on using NV seed as an entropy
7362 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 mbedtls_entropy_add_source(&rng->entropy,
7364 mbedtls_nv_seed_poll, NULL,
7365 MBEDTLS_ENTROPY_BLOCK_SIZE,
7366 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007367#endif
7368
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007370#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007371}
7372
7373/** Deinitialize the PSA random generator.
7374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007375static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007376{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007377#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007379#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7381 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007382#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007383}
7384
7385/** Seed the PSA random generator.
7386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007387static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007388{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007389#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7390 /* Do nothing: the external RNG seeds itself. */
7391 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007393#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007394 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7396 drbg_seed, sizeof(drbg_seed) - 1);
7397 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007398#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007399}
7400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401psa_status_t psa_generate_random(uint8_t *output,
7402 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007403{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007404 GUARD_MODULE_INITIALIZED;
7405
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007406#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7407
7408 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7410 output, output_size,
7411 &output_length);
7412 if (status != PSA_SUCCESS) {
7413 return status;
7414 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007415 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007416 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if (output_length != output_size) {
7418 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7419 }
7420 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007421
7422#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7423
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007425 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7427 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7428 output_size);
7429 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7430 output, request_size);
7431 if (ret != 0) {
7432 return mbedtls_to_psa_error(ret);
7433 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007434 output_size -= request_size;
7435 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007436 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007438#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007439}
7440
Gilles Peskine8814fc42020-12-14 15:33:44 +01007441/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007442 *
7443 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7444 * `psa_generate_random(...)`. The state parameter is ignored since the
7445 * PSA API doesn't support passing an explicit state.
7446 *
7447 * In the non-external case, psa_generate_random() calls an
7448 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7449 * and semantics as mbedtls_psa_get_random(). As an optimization,
7450 * instead of doing this back-and-forth between the PSA API and the
7451 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7452 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007454#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7455int mbedtls_psa_get_random(void *p_rng,
7456 unsigned char *output,
7457 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007458{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007459 /* This function takes a pointer to the RNG state because that's what
7460 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7461 * its own state internally and doesn't let the caller access that state.
7462 * So we just ignore the state parameter, and in practice we'll pass
7463 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007464 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 psa_status_t status = psa_generate_random(output, output_size);
7466 if (status == PSA_SUCCESS) {
7467 return 0;
7468 } else {
7469 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7470 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007471}
7472#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7473
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007474#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007475psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7476 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007477{
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (global_data.initialized) {
7479 return PSA_ERROR_NOT_PERMITTED;
7480 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7483 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7484 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7485 return PSA_ERROR_INVALID_ARGUMENT;
7486 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007487
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007489}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007490#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007491
Ronald Cron3772afe2021-02-08 16:10:05 +01007492/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007493 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007494 * \param type The key type
7495 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007496 *
7497 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007498 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007499 * \retval #PSA_ERROR_INVALID_ARGUMENT
7500 * The size in bits of the key is not valid.
7501 * \retval #PSA_ERROR_NOT_SUPPORTED
7502 * The type and/or the size in bits of the key or the combination of
7503 * the two is not supported.
7504 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007505static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007507{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 if (key_type_is_raw_bytes(type)) {
7511 status = psa_validate_unstructured_key_bit_size(type, bits);
7512 if (status != PSA_SUCCESS) {
7513 return status;
7514 }
7515 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007516#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7518 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7519 return PSA_ERROR_NOT_SUPPORTED;
7520 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007521 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007522 return PSA_ERROR_NOT_SUPPORTED;
7523 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007524
Ronald Cron01b2aba2020-10-05 09:42:02 +02007525 /* Accept only byte-aligned keys, for the same reasons as
7526 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if (bits % 8 != 0) {
7528 return PSA_ERROR_NOT_SUPPORTED;
7529 }
7530 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007531#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007532
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007533#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007535 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 return PSA_SUCCESS;
7537 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007538#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007539
Valerio Settia55f0422023-07-10 15:34:41 +02007540#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007541 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007542 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007543 return PSA_ERROR_NOT_SUPPORTED;
7544 }
7545 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007546#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007547 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007549 }
7550
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007552}
7553
Ronald Cron55ed0592020-10-05 10:30:40 +02007554psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007555 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007557{
7558 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007559 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 if ((attributes->domain_parameters == NULL) &&
7562 (attributes->domain_parameters_size != 0)) {
7563 return PSA_ERROR_INVALID_ARGUMENT;
7564 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (key_type_is_raw_bytes(type)) {
7567 status = psa_generate_random(key_buffer, key_buffer_size);
7568 if (status != PSA_SUCCESS) {
7569 return status;
7570 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007571
David Brown12ca5032021-02-11 11:02:00 -07007572#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 if (type == PSA_KEY_TYPE_DES) {
7574 psa_des_set_key_parity(key_buffer, key_buffer_size);
7575 }
David Brown8107e312021-02-12 08:08:46 -07007576#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007578
Valerio Setti76df8c12023-07-11 14:11:28 +02007579#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7581 return mbedtls_psa_rsa_generate_key(attributes,
7582 key_buffer,
7583 key_buffer_size,
7584 key_buffer_length);
7585 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007586#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007587
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007588#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7590 return mbedtls_psa_ecp_generate_key(attributes,
7591 key_buffer,
7592 key_buffer_size,
7593 key_buffer_length);
7594 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007596
Valerio Settia55f0422023-07-10 15:34:41 +02007597#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007598 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7599 return mbedtls_psa_ffdh_generate_key(attributes,
7600 key_buffer,
7601 key_buffer_size,
7602 key_buffer_length);
7603 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007604#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007605 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 (void) key_buffer_length;
7607 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007608 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007611}
Darryl Green0c6575a2018-11-07 16:05:30 +00007612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7614 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007615{
7616 psa_status_t status;
7617 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007618 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007619 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007620
Ronald Cron81709fc2020-11-14 12:10:32 +01007621 *key = MBEDTLS_SVC_KEY_ID_INIT;
7622
Gilles Peskine0f84d622019-09-12 19:03:13 +02007623 /* Reject any attempt to create a zero-length key so that we don't
7624 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 if (psa_get_key_bits(attributes) == 0) {
7626 return PSA_ERROR_INVALID_ARGUMENT;
7627 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007628
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007629 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7631 return PSA_ERROR_INVALID_ARGUMENT;
7632 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007633
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7635 &slot, &driver);
7636 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 }
Gilles Peskine11792082019-08-06 18:36:36 +02007639
Ronald Cron977c2472020-10-13 08:32:21 +02007640 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307641 * storage ( thus not in the case of generating a key in a secure element
7642 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7643 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 if (slot->key.data == NULL) {
7645 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7646 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007647 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 attributes->core.type, attributes->core.bits);
7649 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007650 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007652
7653 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 attributes->core.type,
7655 attributes->core.bits);
7656 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007657 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 attributes, &key_buffer_size);
7659 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007660 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 }
Ronald Cron977c2472020-10-13 08:32:21 +02007662 }
Ronald Cron977c2472020-10-13 08:32:21 +02007663
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7665 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 }
Ronald Cron977c2472020-10-13 08:32:21 +02007668 }
7669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 status = psa_driver_wrapper_generate_key(attributes,
7671 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007672
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 if (status != PSA_SUCCESS) {
7674 psa_remove_key_data_from_memory(slot);
7675 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007676
Gilles Peskine11792082019-08-06 18:36:36 +02007677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 if (status == PSA_SUCCESS) {
7679 status = psa_finish_key_creation(slot, driver, key);
7680 }
7681 if (status != PSA_SUCCESS) {
7682 psa_fail_key_creation(slot, driver);
7683 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007686}
7687
Gilles Peskinee59236f2018-01-27 23:32:46 +01007688/****************************************************************/
7689/* Module setup */
7690/****************************************************************/
7691
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007692#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007693psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 void (* entropy_init)(mbedtls_entropy_context *ctx),
7695 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007696{
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7698 return PSA_ERROR_BAD_STATE;
7699 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007700 global_data.rng.entropy_init = entropy_init;
7701 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007703}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007704#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007705
Gilles Peskine449bd832023-01-11 14:50:10 +01007706void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007707{
Gilles Peskine449bd832023-01-11 14:50:10 +01007708 psa_wipe_all_key_slots();
7709 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7710 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007711 }
7712 /* Wipe all remaining data, including configuration.
7713 * In particular, this sets all state indicator to the value
7714 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007716
7717 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007719}
7720
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007721#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7722/** Recover a transaction that was interrupted by a power failure.
7723 *
7724 * This function is called during initialization, before psa_crypto_init()
7725 * returns. If this function returns a failure status, the initialization
7726 * fails.
7727 */
7728static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007730{
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007732 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7733 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 /* TODO - fall through to the failure case until this
7735 * is implemented.
7736 * https://github.com/ARMmbed/mbed-crypto/issues/218
7737 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007738 default:
7739 /* We found an unsupported transaction in the storage.
7740 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007742 }
7743}
7744#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7745
Gilles Peskine449bd832023-01-11 14:50:10 +01007746psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007747{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007748 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007749
Gilles Peskinec6b69072018-11-20 21:42:52 +01007750 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 if (global_data.initialized != 0) {
7752 return PSA_SUCCESS;
7753 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007754
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007755 /* Init drivers */
7756 status = psa_driver_wrapper_init();
7757 if (status != PSA_SUCCESS) {
7758 goto exit;
7759 }
7760 global_data.drivers_initialized = 1;
7761
Gilles Peskine30524eb2020-11-13 17:02:26 +01007762 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007764 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 status = mbedtls_psa_random_seed(&global_data.rng);
7766 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007769 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007770
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 status = psa_initialize_key_slots();
7772 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007775
Gilles Peskine4b734222019-07-24 15:56:31 +02007776#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 status = psa_crypto_load_transaction();
7778 if (status == PSA_SUCCESS) {
7779 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7780 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 }
7783 status = psa_crypto_stop_transaction();
7784 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007785 /* There's no transaction to complete. It's all good. */
7786 status = PSA_SUCCESS;
7787 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007788#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007789
Gilles Peskinec6b69072018-11-20 21:42:52 +01007790 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007791 global_data.initialized = 1;
7792
7793exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 if (status != PSA_SUCCESS) {
7795 mbedtls_psa_crypto_free();
7796 }
7797 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007798}
7799
Yanray Wangffc3c482023-07-11 12:01:04 +08007800#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007801psa_status_t psa_crypto_driver_pake_get_password_len(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
7803 size_t *password_len)
7804{
7805 if (inputs->password_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 *password_len = inputs->password_len;
7810
7811 return PSA_SUCCESS;
7812}
7813
7814psa_status_t psa_crypto_driver_pake_get_password(
7815 const psa_crypto_driver_pake_inputs_t *inputs,
7816 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7817{
7818 if (inputs->password_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821
7822 if (buffer_size < inputs->password_len) {
7823 return PSA_ERROR_BUFFER_TOO_SMALL;
7824 }
7825
7826 memcpy(buffer, inputs->password, inputs->password_len);
7827 *buffer_length = inputs->password_len;
7828
7829 return PSA_SUCCESS;
7830}
7831
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007832psa_status_t psa_crypto_driver_pake_get_user_len(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
7834 size_t *user_len)
7835{
7836 if (inputs->user_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
7840 *user_len = inputs->user_len;
7841
7842 return PSA_SUCCESS;
7843}
7844
7845psa_status_t psa_crypto_driver_pake_get_user(
7846 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007848{
7849 if (inputs->user_len == 0) {
7850 return PSA_ERROR_BAD_STATE;
7851 }
7852
Przemek Stekielc0e62502023-03-14 11:49:36 +01007853 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007854 return PSA_ERROR_BUFFER_TOO_SMALL;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 memcpy(user_id, inputs->user, inputs->user_len);
7858 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007859
7860 return PSA_SUCCESS;
7861}
7862
7863psa_status_t psa_crypto_driver_pake_get_peer_len(
7864 const psa_crypto_driver_pake_inputs_t *inputs,
7865 size_t *peer_len)
7866{
7867 if (inputs->peer_len == 0) {
7868 return PSA_ERROR_BAD_STATE;
7869 }
7870
7871 *peer_len = inputs->peer_len;
7872
7873 return PSA_SUCCESS;
7874}
7875
7876psa_status_t psa_crypto_driver_pake_get_peer(
7877 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007878 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007879{
7880 if (inputs->peer_len == 0) {
7881 return PSA_ERROR_BAD_STATE;
7882 }
7883
Przemek Stekielc0e62502023-03-14 11:49:36 +01007884 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007885 return PSA_ERROR_BUFFER_TOO_SMALL;
7886 }
7887
Przemek Stekielc0e62502023-03-14 11:49:36 +01007888 memcpy(peer_id, inputs->peer, inputs->peer_len);
7889 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007890
7891 return PSA_SUCCESS;
7892}
7893
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007894psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7895 const psa_crypto_driver_pake_inputs_t *inputs,
7896 psa_pake_cipher_suite_t *cipher_suite)
7897{
7898 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7899 return PSA_ERROR_BAD_STATE;
7900 }
7901
7902 *cipher_suite = inputs->cipher_suite;
7903
7904 return PSA_SUCCESS;
7905}
7906
Neil Armstronga7d08c32022-06-01 18:21:20 +02007907psa_status_t psa_pake_setup(
7908 psa_pake_operation_t *operation,
7909 const psa_pake_cipher_suite_t *cipher_suite)
7910{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007912
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007913 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914 status = PSA_ERROR_BAD_STATE;
7915 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 }
7917
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007918 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007920 status = PSA_ERROR_INVALID_ARGUMENT;
7921 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007922 }
7923
Przemek Stekiel51eac532022-12-07 11:04:51 +01007924 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7925
Przemek Stekiele12ed362022-12-21 12:54:46 +01007926 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007927 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7928 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007929 operation->data.inputs.cipher_suite = *cipher_suite;
7930
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007931#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007932 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007933 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007934 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007935
David Horstmann16f01512023-06-14 17:21:07 +01007936 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007937 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007938 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007939#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007940 {
7941 status = PSA_ERROR_NOT_SUPPORTED;
7942 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007943 }
7944
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007945 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7946
Przemek Stekiel51eac532022-12-07 11:04:51 +01007947 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007948exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007949 psa_pake_abort(operation);
7950 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007951}
7952
7953psa_status_t psa_pake_set_password_key(
7954 psa_pake_operation_t *operation,
7955 mbedtls_svc_key_id_t password)
7956{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7959 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007960 psa_key_attributes_t attributes;
7961 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007962
Przemek Stekiel51eac532022-12-07 11:04:51 +01007963 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007964 status = PSA_ERROR_BAD_STATE;
7965 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007966 }
7967
Przemek Stekiel6c764412022-11-22 14:05:12 +01007968 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7969 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007970 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007971 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007972 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007973 }
7974
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007975 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007976 .core = slot->attr
7977 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007978
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007979 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980
Przemek Stekiel51eac532022-12-07 11:04:51 +01007981 if (type != PSA_KEY_TYPE_PASSWORD &&
7982 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7983 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007984 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007985 }
7986
Przemek Stekiel51eac532022-12-07 11:04:51 +01007987 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7988 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007989 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007990 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007991 }
7992
7993 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7994 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007995 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007996exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007997 if (status != PSA_SUCCESS) {
7998 psa_pake_abort(operation);
7999 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008000 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008001 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008002}
8003
8004psa_status_t psa_pake_set_user(
8005 psa_pake_operation_t *operation,
8006 const uint8_t *user_id,
8007 size_t user_id_len)
8008{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008010
8011 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 status = PSA_ERROR_BAD_STATE;
8013 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008014 }
8015
Przemek Stekiel51eac532022-12-07 11:04:51 +01008016 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008017 status = PSA_ERROR_INVALID_ARGUMENT;
8018 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008019 }
8020
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008021 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008022 status = PSA_ERROR_BAD_STATE;
8023 goto exit;
8024 }
8025
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008026 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8027 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008028 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8029 goto exit;
8030 }
8031
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008032 memcpy(operation->data.inputs.user, user_id, user_id_len);
8033 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008034
8035 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008036exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008037 psa_pake_abort(operation);
8038 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008039}
8040
8041psa_status_t psa_pake_set_peer(
8042 psa_pake_operation_t *operation,
8043 const uint8_t *peer_id,
8044 size_t peer_id_len)
8045{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008046 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008047
8048 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008049 status = PSA_ERROR_BAD_STATE;
8050 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008051 }
8052
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008054 status = PSA_ERROR_INVALID_ARGUMENT;
8055 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008056 }
8057
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008058 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008059 status = PSA_ERROR_BAD_STATE;
8060 goto exit;
8061 }
8062
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008063 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8064 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008065 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8066 goto exit;
8067 }
8068
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008069 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8070 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008071
8072 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008073exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008074 psa_pake_abort(operation);
8075 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008076}
8077
8078psa_status_t psa_pake_set_role(
8079 psa_pake_operation_t *operation,
8080 psa_pake_role_t role)
8081{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008083
Przemek Stekiel51eac532022-12-07 11:04:51 +01008084 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008085 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008086 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008087 }
8088
Przemek Stekiel09104b82023-03-06 14:11:51 +01008089 switch (operation->alg) {
8090#if defined(PSA_WANT_ALG_JPAKE)
8091 case PSA_ALG_JPAKE:
8092 if (role == PSA_PAKE_ROLE_NONE) {
8093 return PSA_SUCCESS;
8094 }
8095 status = PSA_ERROR_INVALID_ARGUMENT;
8096 break;
8097#endif
8098 default:
8099 (void) role;
8100 status = PSA_ERROR_NOT_SUPPORTED;
8101 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008102 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008103exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008104 psa_pake_abort(operation);
8105 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008106}
8107
David Horstmanne7f21e62023-05-12 18:17:21 +01008108/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008109#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008110static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008111 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008112{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008113 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008114 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008115 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008116
David Horstmann024e5c52023-06-14 15:48:21 +01008117 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008118 is_x1 = (stage->outputs < 1);
8119 } else {
8120 is_x1 = (stage->inputs < 1);
8121 }
8122
David Horstmann74a3d8c2023-06-14 18:28:19 +01008123 key_share_step = is_x1 ?
8124 PSA_JPAKE_X1_STEP_KEY_SHARE :
8125 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008126 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008127 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8128 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8129 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8130 } else {
8131 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008132 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008133 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008134}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008135#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008136
Przemek Stekiel2797d372022-12-22 11:19:22 +01008137static psa_status_t psa_pake_complete_inputs(
8138 psa_pake_operation_t *operation)
8139{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008140 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008141 /* Create copy of the inputs on stack as inputs share memory
8142 with the driver context which will be setup by the driver. */
8143 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144
Przemek Stekielfde11282023-03-13 16:06:09 +01008145 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008146 return PSA_ERROR_BAD_STATE;
8147 }
8148
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008149 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008150 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8151 return PSA_ERROR_BAD_STATE;
8152 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008153 }
8154
Przemek Stekiel18620a32023-01-17 16:34:52 +01008155 /* Clear driver context */
8156 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8157
8158 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008159
8160 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008161 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008162
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008163 /* User and peer are translated to role. */
8164 mbedtls_free(inputs.user);
8165 mbedtls_free(inputs.peer);
8166
Przemek Stekiel2797d372022-12-22 11:19:22 +01008167 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008168#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008169 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008170 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008171 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008172#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008173 {
8174 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008175 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008176 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008177 return status;
8178}
8179
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008180#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008181static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008183 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008184 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008186 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8187 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8188 step != PSA_PAKE_STEP_ZK_PROOF) {
8189 return PSA_ERROR_INVALID_ARGUMENT;
8190 }
8191
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008192 psa_jpake_computation_stage_t *computation_stage =
8193 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008194
David Horstmann5da95602023-06-08 15:37:12 +01008195 if (computation_stage->round != PSA_JPAKE_FIRST &&
8196 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008197 return PSA_ERROR_BAD_STATE;
8198 }
8199
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 /* Check that the step we are given is the one we were expecting */
8201 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008202 return PSA_ERROR_BAD_STATE;
8203 }
8204
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8206 computation_stage->inputs == 0 &&
8207 computation_stage->outputs == 0) {
8208 /* Start of the round, so function decides whether we are inputting
8209 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008210 computation_stage->io_mode = io_mode;
8211 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 /* Middle of the round so the mode we are in must match the function
8213 * called by the user */
8214 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 }
8216
8217 return PSA_SUCCESS;
8218}
8219
David Horstmanne7f21e62023-05-12 18:17:21 +01008220static psa_status_t psa_jpake_epilogue(
8221 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008222 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223{
David Horstmanne7f21e62023-05-12 18:17:21 +01008224 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008225 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226
David Horstmanne7f21e62023-05-12 18:17:21 +01008227 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8228 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008229 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008230 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008231 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008232 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008233 }
8234 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008235 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008236 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008237 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008238 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008239 }
8240 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008241 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8242 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008243 /* End of a round, move to the next round */
8244 stage->inputs = 0;
8245 stage->outputs = 0;
8246 stage->round++;
8247 }
8248 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008249 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008250 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 return PSA_SUCCESS;
8253}
David Horstmanne7f21e62023-05-12 18:17:21 +01008254
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008255#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256
Neil Armstronga7d08c32022-06-01 18:21:20 +02008257psa_status_t psa_pake_output(
8258 psa_pake_operation_t *operation,
8259 psa_pake_step_t step,
8260 uint8_t *output,
8261 size_t output_size,
8262 size_t *output_length)
8263{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008265 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008266 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008267
8268 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008269 status = psa_pake_complete_inputs(operation);
8270 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008271 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008272 }
8273 }
8274
8275 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008276 status = PSA_ERROR_BAD_STATE;
8277 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008278 }
8279
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008280 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 status = PSA_ERROR_INVALID_ARGUMENT;
8282 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008283 }
8284
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 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_prologue(operation, step, 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 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008292 driver_step = convert_jpake_computation_stage_to_driver_step(
8293 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008295#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008296 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008297 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008298 status = PSA_ERROR_NOT_SUPPORTED;
8299 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008300 }
8301
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008302 status = psa_driver_wrapper_pake_output(operation, driver_step,
8303 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008304
8305 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008306 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008307 }
8308
8309 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008310#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008311 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008312 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008313 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008314 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008315 }
8316 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008317#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008319 status = PSA_ERROR_NOT_SUPPORTED;
8320 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008321 }
8322
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008323 return PSA_SUCCESS;
8324exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008325 psa_pake_abort(operation);
8326 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008327}
8328
8329psa_status_t psa_pake_input(
8330 psa_pake_operation_t *operation,
8331 psa_pake_step_t step,
8332 const uint8_t *input,
8333 size_t input_length)
8334{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008336 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008337 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8338 operation->primitive,
8339 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008340
8341 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008342 status = psa_pake_complete_inputs(operation);
8343 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008344 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008345 }
8346 }
8347
8348 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008349 status = PSA_ERROR_BAD_STATE;
8350 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008351 }
8352
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008353 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008354 status = PSA_ERROR_INVALID_ARGUMENT;
8355 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008356 }
8357
Przemek Stekiele12ed362022-12-21 12:54:46 +01008358 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_prologue(operation, step, 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 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008365 driver_step = convert_jpake_computation_stage_to_driver_step(
8366 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008367 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008368#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008369 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008370 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008371 status = PSA_ERROR_NOT_SUPPORTED;
8372 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008373 }
8374
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008375 status = psa_driver_wrapper_pake_input(operation, driver_step,
8376 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008377
8378 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008379 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008380 }
8381
8382 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008383#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008384 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008385 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008386 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008388 }
8389 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008390#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008391 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008392 status = PSA_ERROR_NOT_SUPPORTED;
8393 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008394 }
8395
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008396 return PSA_SUCCESS;
8397exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008398 psa_pake_abort(operation);
8399 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008400}
8401
8402psa_status_t psa_pake_get_implicit_key(
8403 psa_pake_operation_t *operation,
8404 psa_key_derivation_operation_t *output)
8405{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008407 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008408 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008409 size_t shared_key_len = 0;
8410
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008411 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008412 status = PSA_ERROR_BAD_STATE;
8413 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008414 }
8415
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008416#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008417 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008418 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008419 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008420 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008421 status = PSA_ERROR_BAD_STATE;
8422 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008423 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008424 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008425#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008426 {
8427 status = PSA_ERROR_NOT_SUPPORTED;
8428 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008429 }
8430
Przemek Stekiel0c781802022-11-29 14:53:13 +01008431 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8432 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008433 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008434 &shared_key_len);
8435
8436 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008437 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008438 }
8439
8440 status = psa_key_derivation_input_bytes(output,
8441 PSA_KEY_DERIVATION_INPUT_SECRET,
8442 shared_key,
8443 shared_key_len);
8444
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008445 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008446exit:
8447 abort_status = psa_pake_abort(operation);
8448 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008449}
8450
8451psa_status_t psa_pake_abort(
8452 psa_pake_operation_t *operation)
8453{
Przemek Stekield69dca92023-01-31 19:59:20 +01008454 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008455
Przemek Stekield69dca92023-01-31 19:59:20 +01008456 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008457 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008458 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008459
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008460 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8461 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008462 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008463 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008464 }
8465 if (operation->data.inputs.user != NULL) {
8466 mbedtls_free(operation->data.inputs.user);
8467 }
8468 if (operation->data.inputs.peer != NULL) {
8469 mbedtls_free(operation->data.inputs.peer);
8470 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008471 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008472 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008473
Przemek Stekield69dca92023-01-31 19:59:20 +01008474 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008475}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008476#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008477
David Horstmanne138dce2023-11-28 15:21:56 +00008478/* Memory copying test hooks */
8479#if defined(MBEDTLS_TEST_HOOKS)
8480void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8481void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8482void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8483void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8484#endif
David Horstmannfde97392023-10-30 20:29:43 +00008485
David Horstmann1b7279a2023-11-15 15:18:30 +00008486/** Copy from an input buffer to a local copy.
8487 *
8488 * \param[in] input Pointer to input buffer.
8489 * \param[in] input_len Length of the input buffer.
8490 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8491 * \param[out] input_copy_len Length of the local copy buffer.
8492 * \return #PSA_SUCCESS, if the buffer was successfully
8493 * copied.
8494 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8495 * copy is too small to hold contents of the
8496 * input buffer.
8497 */
8498MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008499psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8500 uint8_t *input_copy, size_t input_copy_len)
8501{
8502 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008503 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008504 }
8505
David Horstmann372b8bb2023-11-24 16:21:04 +00008506#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008507 if (psa_input_pre_copy_hook != NULL) {
8508 psa_input_pre_copy_hook(input, input_len);
8509 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008510#endif
8511
David Horstmann777e7412023-11-15 17:33:47 +00008512 if (input_len > 0) {
8513 memcpy(input_copy, input, input_len);
8514 }
David Horstmannfde97392023-10-30 20:29:43 +00008515
David Horstmann372b8bb2023-11-24 16:21:04 +00008516#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008517 if (psa_input_post_copy_hook != NULL) {
8518 psa_input_post_copy_hook(input, input_len);
8519 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008520#endif
8521
David Horstmannfde97392023-10-30 20:29:43 +00008522 return PSA_SUCCESS;
8523}
8524
David Horstmann1b7279a2023-11-15 15:18:30 +00008525/** Copy from a local output buffer into a user-supplied one.
8526 *
8527 * \param[in] output_copy Pointer to a local buffer containing the output.
8528 * \param[in] output_copy_len Length of the local buffer.
8529 * \param[out] output Pointer to user-supplied output buffer.
8530 * \param[out] output_len Length of the user-supplied output buffer.
8531 * \return #PSA_SUCCESS, if the buffer was successfully
8532 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008533 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008534 * user-supplied output buffer is too small to
8535 * hold the contents of the local buffer.
8536 */
8537MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008538psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8539 uint8_t *output, size_t output_len)
8540{
8541 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008542 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008543 }
David Horstmann777e7412023-11-15 17:33:47 +00008544
David Horstmann372b8bb2023-11-24 16:21:04 +00008545#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008546 if (psa_output_pre_copy_hook != NULL) {
8547 psa_output_pre_copy_hook(output, output_len);
8548 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008549#endif
8550
David Horstmann777e7412023-11-15 17:33:47 +00008551 if (output_copy_len > 0) {
8552 memcpy(output, output_copy, output_copy_len);
8553 }
8554
David Horstmann372b8bb2023-11-24 16:21:04 +00008555#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008556 if (psa_output_post_copy_hook != NULL) {
8557 psa_output_post_copy_hook(output, output_len);
8558 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008559#endif
8560
David Horstmann8978f5c2023-10-30 20:37:12 +00008561 return PSA_SUCCESS;
8562}
8563
David Horstmannf1734052023-11-20 17:15:32 +00008564psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8565 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008566{
8567 psa_status_t status;
8568
David Horstmann9db14482023-11-23 15:50:37 +00008569 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008570
David Horstmannc5cc1c32023-11-15 18:11:26 +00008571 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008572 return PSA_SUCCESS;
8573 }
8574
David Horstmannf1734052023-11-20 17:15:32 +00008575 local_input->buffer = mbedtls_calloc(input_len, 1);
8576 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008577 /* Since we dealt with the zero-length case above, we know that
8578 * a NULL return value means a failure of allocation. */
8579 return PSA_ERROR_INSUFFICIENT_MEMORY;
8580 }
David Horstmannf1734052023-11-20 17:15:32 +00008581 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008582
David Horstmannf1734052023-11-20 17:15:32 +00008583 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008584
8585 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008586 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008587 if (status != PSA_SUCCESS) {
8588 goto error;
8589 }
8590
8591 return PSA_SUCCESS;
8592
8593error:
David Horstmannf1734052023-11-20 17:15:32 +00008594 mbedtls_free(local_input->buffer);
8595 local_input->buffer = NULL;
8596 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008597 return status;
8598}
8599
David Horstmannf1734052023-11-20 17:15:32 +00008600void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008601{
David Horstmannf1734052023-11-20 17:15:32 +00008602 mbedtls_free(local_input->buffer);
8603 local_input->buffer = NULL;
8604 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008605}
8606
David Horstmann89875a42023-11-20 12:54:09 +00008607psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8608 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008609{
David Horstmann9db14482023-11-23 15:50:37 +00008610 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008611
David Horstmannc5cc1c32023-11-15 18:11:26 +00008612 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008613 return PSA_SUCCESS;
8614 }
David Horstmann89875a42023-11-20 12:54:09 +00008615 local_output->buffer = mbedtls_calloc(output_len, 1);
8616 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008617 /* Since we dealt with the zero-length case above, we know that
8618 * a NULL return value means a failure of allocation. */
8619 return PSA_ERROR_INSUFFICIENT_MEMORY;
8620 }
David Horstmann89875a42023-11-20 12:54:09 +00008621 local_output->length = output_len;
8622 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008623
8624 return PSA_SUCCESS;
8625}
8626
David Horstmann89875a42023-11-20 12:54:09 +00008627psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008628{
David Horstmannc335a4e2023-11-15 15:57:32 +00008629 psa_status_t status;
8630
David Horstmann89875a42023-11-20 12:54:09 +00008631 if (local_output->buffer == NULL) {
8632 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008633 return PSA_SUCCESS;
8634 }
David Horstmann89875a42023-11-20 12:54:09 +00008635 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008636 /* We have an internal copy but nothing to copy back to. */
8637 return PSA_ERROR_CORRUPTION_DETECTED;
8638 }
8639
David Horstmann89875a42023-11-20 12:54:09 +00008640 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8641 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008642 if (status != PSA_SUCCESS) {
8643 return status;
8644 }
David Horstmann9467ea32023-11-08 17:44:18 +00008645
David Horstmann89875a42023-11-20 12:54:09 +00008646 mbedtls_free(local_output->buffer);
8647 local_output->buffer = NULL;
8648 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008649
8650 return PSA_SUCCESS;
8651}
8652
Gilles Peskinee59236f2018-01-27 23:32:46 +01008653#endif /* MBEDTLS_PSA_CRYPTO_C */