blob: 667d4f40701f6b1eaecb043cf41cb5a761a81af8 [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 Horstmann513101b2023-11-29 17:24:08 +0000113#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmanne9a88ab2023-11-29 17:07:13 +0000114/* Substitute an input buffer for a local copy of itself.
115 * Assumptions:
116 * - psa_status_t status exists
117 * - An exit label is declared
118 * - The name <buffer>_copy is not used for the given value of <buffer>
119 */
120#define SWAP_FOR_LOCAL_INPUT(input, length) \
121 psa_crypto_local_input_t input ## _copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
122 status = psa_crypto_local_input_alloc(input, length, &input ## _copy); \
123 if (status != PSA_SUCCESS) { \
124 goto exit; \
125 } \
126 input = input ## _copy.buffer;
127
128#define FREE_LOCAL_INPUT(input) \
129 psa_crypto_local_input_free(&input ## _copy);
130
131/* Substitute an output buffer for a local copy of itself.
132 * Assumptions:
133 * - psa_status_t status exists
134 * - An exit label is declared
135 * - The name <buffer>_copy is not used for the given value of <buffer>
136 */
137#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
138 psa_crypto_local_output_t output ## _copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
139 status = psa_crypto_local_output_alloc(output, length, &output ## _copy); \
140 if (status != PSA_SUCCESS) { \
141 goto exit; \
142 } \
143 output = output ## _copy.buffer;
144
145#define FREE_LOCAL_OUTPUT(output) \
146 psa_status_t local_output_free_status; \
147 local_output_free_status = psa_crypto_local_output_free(&output ## _copy); \
148 if (local_output_free_status != PSA_SUCCESS) { \
149 status = local_output_free_status; \
150 }
David Horstmann513101b2023-11-29 17:24:08 +0000151#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
152#define SWAP_FOR_LOCAL_INPUT(input, length)
153#define FREE_LOCAL_INPUT(input)
154#define SWAP_FOR_LOCAL_OUTPUT(output, length)
155#define FREE_LOCAL_OUTPUT(output)
156#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmanne9a88ab2023-11-29 17:07:13 +0000157
158
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100159int psa_can_do_hash(psa_algorithm_t hash_alg)
160{
161 (void) hash_alg;
162 return global_data.drivers_initialized;
163}
Valerio Settia55f0422023-07-10 15:34:41 +0200164#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200165 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200166 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200167static int psa_is_dh_key_size_valid(size_t bits)
168{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200169 if (bits != 2048 && bits != 3072 && bits != 4096 &&
170 bits != 6144 && bits != 8192) {
171 return 0;
172 }
173
174 return 1;
175}
Valerio Settia55f0422023-07-10 15:34:41 +0200176#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200177 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200178 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100181{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100182 /* Mbed TLS error codes can combine a high-level error code and a
183 * low-level error code. The low-level error usually reflects the
184 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 int low_level_ret = -(-ret & 0x007f);
186 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100187 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100190#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100194 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
195 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100196#endif
197
198#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200199 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
200 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
201 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
202 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
203 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200205 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200207 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100209#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200210
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100211#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000212 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_NOT_SUPPORTED;
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_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100222#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100223
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100224#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200225 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100227#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200228
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100229#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200230 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200232 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100234#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200235
Dave Rodgman509b5672023-08-16 19:26:23 +0100236#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100251#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100264#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100265
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100266#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100269#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100270
Gilles Peskinee59236f2018-01-27 23:32:46 +0100271 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
272 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
273 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100276#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200279 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100283#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100284
Gilles Peskine82e57d12020-11-13 21:31:17 +0100285#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100287 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
288 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100289 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100291 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
292 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100294 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100296#endif
297
Dave Rodgmandea266f2023-08-31 11:52:43 +0100298#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_MD_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
309#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100310
Dave Rodgman509b5672023-08-16 19:26:23 +0100311#if defined(MBEDTLS_BIGNUM_C)
312#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100313 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100315#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100316 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100318 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100320 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100322 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100324 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100326 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100328 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100330#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100331
Dave Rodgman509b5672023-08-16 19:26:23 +0100332#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100333 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100335 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
336 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100338#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
339 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100342#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100343 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
344 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
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_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
349 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353 case MBEDTLS_ERR_PK_INVALID_ALG:
354 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
355 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200359 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100361#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100362
Gilles Peskineff2d2002019-05-06 15:26:23 +0200363 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200365 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200367
Dave Rodgman509b5672023-08-16 19:26:23 +0100368#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100371 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100373 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100375 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
378 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100380 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100382 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100384 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100386#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200387
Dave Rodgman1dab4452023-09-01 09:59:51 +0100388#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300389 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300390 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300392 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300394 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300396 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
397 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300399 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100401 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100403
Dave Rodgman509b5672023-08-16 19:26:23 +0100404#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000405 case MBEDTLS_ERR_ECP_IN_PROGRESS:
406 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100407#endif
408#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000409
Janos Follatha13b9052019-11-22 12:48:59 +0000410 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300412
Gilles Peskinee59236f2018-01-27 23:32:46 +0100413 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100415 }
416}
417
Paul Elliottdc42ca82023-02-24 18:11:59 +0000418/**
419 * \brief For output buffers which contain "tags"
420 * (outputs that may be checked for validity like
421 * hashes, MACs and signatures), fill the unused
422 * part of the output buffer (the whole buffer on
423 * error, the trailing part on success) with
424 * something that isn't a valid tag (barring an
425 * attack on the tag and deliberately-crafted
426 * input), in case the caller doesn't check the
427 * return status properly.
428 *
429 * \param output_buffer Pointer to buffer to wipe. May not be NULL
430 * unless \p output_buffer_size is zero.
431 * \param status Status of function called to generate
432 * output_buffer originally
433 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
434 * could be NULL.
435 * \param output_buffer_length Length of data written to output_buffer, must be
436 * less than \p output_buffer_size
437 */
438static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000439 size_t output_buffer_size, size_t output_buffer_length)
440{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000441 size_t offset = 0;
442
443 if (output_buffer_size == 0) {
444 /* If output_buffer_size is 0 then we have nothing to do. We must not
445 call memset because output_buffer may be NULL in this case */
446 return;
447 }
448
449 if (status == PSA_SUCCESS) {
450 offset = output_buffer_length;
451 }
452
453 memset(output_buffer + offset, '!', output_buffer_size - offset);
454}
455
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200456
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200457
458
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459/****************************************************************/
460/* Key management */
461/****************************************************************/
462
Valerio Settia9aab1a2023-06-19 13:39:54 +0200463#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200464psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
465 size_t *bits)
466{
467 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200468#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200469 case MBEDTLS_ECP_DP_SECP192R1:
470 *bits = 192;
471 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100472#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200473#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200474 case MBEDTLS_ECP_DP_SECP224R1:
475 *bits = 224;
476 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100477#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200478#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200479 case MBEDTLS_ECP_DP_SECP256R1:
480 *bits = 256;
481 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100482#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200483#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200484 case MBEDTLS_ECP_DP_SECP384R1:
485 *bits = 384;
486 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100487#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200488#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200489 case MBEDTLS_ECP_DP_SECP521R1:
490 *bits = 521;
491 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100492#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200493#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200494 case MBEDTLS_ECP_DP_BP256R1:
495 *bits = 256;
496 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100497#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200498#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200499 case MBEDTLS_ECP_DP_BP384R1:
500 *bits = 384;
501 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100502#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200503#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200504 case MBEDTLS_ECP_DP_BP512R1:
505 *bits = 512;
506 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100507#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200508#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200509 case MBEDTLS_ECP_DP_CURVE25519:
510 *bits = 255;
511 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100512#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200513#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200514 case MBEDTLS_ECP_DP_SECP192K1:
515 *bits = 192;
516 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100517#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200518#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200519 case MBEDTLS_ECP_DP_SECP224K1:
520 *bits = 224;
521 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100522#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200523#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200524 case MBEDTLS_ECP_DP_SECP256K1:
525 *bits = 256;
526 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100527#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200528#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200529 case MBEDTLS_ECP_DP_CURVE448:
530 *bits = 448;
531 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100532#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200533 default:
534 *bits = 0;
535 return 0;
536 }
537}
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
540 size_t bits,
541 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200542{
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100544 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100547 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100549#endif
550#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100551 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100553#endif
554#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100555 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100557#endif
558#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100559 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100561#endif
562#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100563 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100565 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (bits_is_sloppy) {
567 return MBEDTLS_ECP_DP_SECP521R1;
568 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100569 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100570#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100571 }
572 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100573
Paul Elliott8ff510a2020-06-02 17:19:28 +0100574 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100577 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100579#endif
580#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100581 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100583#endif
584#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100585 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100587#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100588 }
589 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100590
Paul Elliott8ff510a2020-06-02 17:19:28 +0100591 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100593#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100594 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100596 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (bits_is_sloppy) {
598 return MBEDTLS_ECP_DP_CURVE25519;
599 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100600 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100601#endif
602#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100603 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100605#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100606 }
607 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100608
Paul Elliott8ff510a2020-06-02 17:19:28 +0100609 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100611#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100612 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100614#endif
615#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100616 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100618#endif
619#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100620 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100622#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100623 }
624 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200625 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100626
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100627 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200629}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200630#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
633 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634{
635 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200638 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200639 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200640 case PSA_KEY_TYPE_PASSWORD:
641 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200642 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100643#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200644 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (bits != 128 && bits != 192 && bits != 256) {
646 return PSA_ERROR_INVALID_ARGUMENT;
647 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200648 break;
649#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200650#if defined(PSA_WANT_KEY_TYPE_ARIA)
651 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (bits != 128 && bits != 192 && bits != 256) {
653 return PSA_ERROR_INVALID_ARGUMENT;
654 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200655 break;
656#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100657#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200658 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (bits != 128 && bits != 192 && bits != 256) {
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_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200665 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (bits != 64 && bits != 128 && bits != 192) {
667 return PSA_ERROR_INVALID_ARGUMENT;
668 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200669 break;
670#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100671#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200672 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (bits != 256) {
674 return PSA_ERROR_INVALID_ARGUMENT;
675 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200676 break;
677#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200678 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200680 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (bits % 8 != 0) {
682 return PSA_ERROR_INVALID_ARGUMENT;
683 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200686}
687
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100688/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100689 *
Steven Cooremancd640932021-03-08 12:00:27 +0100690 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
691 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100692 *
693 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100694 * \param[in] key_type The key type of the key to be used with the
695 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100696 *
697 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100698 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100699 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100700 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100701 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100702MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100703 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100705{
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (PSA_ALG_IS_HMAC(algorithm)) {
707 if (key_type == PSA_KEY_TYPE_HMAC) {
708 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100709 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100710 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
713 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
714 * key. */
715 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
716 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
717 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
718 * the block length (larger than 1) for block ciphers. */
719 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
720 return PSA_SUCCESS;
721 }
722 }
723 }
724
725 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100726}
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
729 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200730{
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (slot->key.data != NULL) {
732 return PSA_ERROR_ALREADY_EXISTS;
733 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 slot->key.data = mbedtls_calloc(1, buffer_length);
736 if (slot->key.data == NULL) {
737 return PSA_ERROR_INSUFFICIENT_MEMORY;
738 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200739
Ronald Cronea0f8a62020-11-25 17:52:23 +0100740 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200742}
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
745 const uint8_t *data,
746 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200747{
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 psa_status_t status = psa_allocate_buffer_to_slot(slot,
749 data_length);
750 if (status != PSA_SUCCESS) {
751 return status;
752 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 memcpy(slot->key.data, data, data_length);
755 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200756}
757
Ronald Crona0fe59f2020-11-29 11:14:53 +0100758psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100759 const psa_key_attributes_t *attributes,
760 const uint8_t *data, size_t data_length,
761 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100764 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
765 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200767 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (data_length == 0) {
769 return PSA_ERROR_NOT_SUPPORTED;
770 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (key_type_is_raw_bytes(type)) {
773 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
776 *bits);
777 if (status != PSA_SUCCESS) {
778 return status;
779 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200780
Ronald Crondd04d422020-11-28 15:14:42 +0100781 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100783 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 return PSA_SUCCESS;
787 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200788#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200789 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100790 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200791 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100792 return PSA_ERROR_INVALID_ARGUMENT;
793 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200794 return mbedtls_psa_ffdh_import_key(attributes,
795 data, data_length,
796 key_buffer, key_buffer_size,
797 key_buffer_length,
798 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100799 }
Valerio Settia55f0422023-07-10 15:34:41 +0200800#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200801 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200802#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
804 if (PSA_KEY_TYPE_IS_ECC(type)) {
805 return mbedtls_psa_ecp_import_key(attributes,
806 data, data_length,
807 key_buffer, key_buffer_size,
808 key_buffer_length,
809 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100810 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200811#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800812 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200813#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
814 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
816 if (PSA_KEY_TYPE_IS_RSA(type)) {
817 return mbedtls_psa_rsa_import_key(attributes,
818 data, data_length,
819 key_buffer, key_buffer_size,
820 key_buffer_length,
821 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200822 }
Valerio Settife478902023-07-25 12:27:19 +0200823#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
824 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800825 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100826 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100829}
830
Gilles Peskinef603c712019-01-19 13:40:11 +0100831/** Calculate the intersection of two algorithm usage policies.
832 *
833 * Return 0 (which allows no operation) on incompatibility.
834 */
835static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100836 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100837 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100839{
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (alg1 == alg2) {
842 return alg1;
843 }
Steven Cooreman03488022021-02-18 12:07:20 +0100844 /* If the policies are from the same hash-and-sign family, check
845 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
847 PSA_ALG_IS_SIGN_HASH(alg2) &&
848 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
849 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
850 return alg2;
851 }
852 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
853 return alg1;
854 }
Steven Cooreman03488022021-02-18 12:07:20 +0100855 }
856 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100857 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100858 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
860 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
861 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
862 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
863 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100864 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100865
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100866 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
868 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
869 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
870 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100871 }
872 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
874 (alg1_len <= alg2_len)) {
875 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100876 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
878 (alg2_len <= alg1_len)) {
879 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100880 }
881 }
882 /* If the policies are from the same MAC family, check whether one
883 * of them is a minimum-MAC-length policy. Calculate the most
884 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
886 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
887 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100888 /* Validate the combination of key type and algorithm. Since the base
889 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
891 return 0;
892 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100893
Steven Cooreman31a876d2021-03-03 20:47:40 +0100894 /* Get the (exact or at-least) output lengths for both sides of the
895 * requested intersection. None of the currently supported algorithms
896 * have an output length dependent on the actual key size, so setting it
897 * to a bogus value of 0 is currently OK.
898 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100899 * Note that for at-least-this-length wildcard algorithms, the output
900 * length is set to the shortest allowed length, which allows us to
901 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
903 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100904 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100905
Steven Cooremana1d83222021-02-25 10:20:29 +0100906 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
908 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
909 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100910 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100911
912 /* If only one is an at-least-this-length policy, the intersection would
913 * be the other (fixed-length) policy as long as said fixed length is
914 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
916 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100917 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
919 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100920 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100921
Steven Cooremancd640932021-03-08 12:00:27 +0100922 /* If none of them are wildcards, check whether they define the same tag
923 * length. This is still possible here when one is default-length and
924 * the other specific-length. Ensure to always return the
925 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (alg1_len == alg2_len) {
927 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
928 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100929 }
930 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100932}
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int psa_key_algorithm_permits(psa_key_type_t key_type,
935 psa_algorithm_t policy_alg,
936 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200937{
Gilles Peskine549ea862019-05-22 11:45:59 +0200938 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (requested_alg == policy_alg) {
940 return 1;
941 }
Steven Cooreman03488022021-02-18 12:07:20 +0100942 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
943 * and requested_alg is the same hash-and-sign family with any hash,
944 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
946 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
947 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
948 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100949 }
950 /* If policy_alg is a wildcard AEAD algorithm of the same base as
951 * the requested algorithm, check the requested tag length to be
952 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (PSA_ALG_IS_AEAD(policy_alg) &&
954 PSA_ALG_IS_AEAD(requested_alg) &&
955 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
956 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
957 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
958 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
959 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100960 }
Steven Cooremancd640932021-03-08 12:00:27 +0100961 /* If policy_alg is a MAC algorithm of the same base as the requested
962 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (PSA_ALG_IS_MAC(policy_alg) &&
964 PSA_ALG_IS_MAC(requested_alg) &&
965 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
966 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100967 /* Validate the combination of key type and algorithm. Since the policy
968 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
970 return 0;
971 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100972
Steven Cooreman31a876d2021-03-03 20:47:40 +0100973 /* Get both the requested output length for the algorithm which is to be
974 * verified, and the default output length for the base algorithm.
975 * Note that none of the currently supported algorithms have an output
976 * length dependent on actual key size, so setting it to a bogus value
977 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100978 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100980 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 key_type, 0,
982 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100983
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100984 /* If the policy is default-length, only allow an algorithm with
985 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
987 return requested_output_length == default_output_length;
988 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100989
990 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100991 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
993 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
994 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100995 }
996
Steven Cooremancd640932021-03-08 12:00:27 +0100997 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
998 * check for the requested MAC length to be equal to or longer than the
999 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
1001 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
1002 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001003 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001004 }
Steven Cooremance48e852020-10-05 16:02:45 +02001005 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001006 * a key derivation with that key agreement should also be allowed. This
1007 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
1009 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
1010 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
1011 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +02001012 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001013 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001015}
1016
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001017/** Test whether a policy permits an algorithm.
1018 *
1019 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +01001020 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001021 * \note This function requires providing the key type for which the policy is
1022 * being validated, since some algorithm policy definitions (e.g. MAC)
1023 * have different properties depending on what kind of cipher it is
1024 * combined with.
1025 *
Steven Cooreman7e39f052021-02-23 14:18:32 +01001026 * \retval PSA_SUCCESS When \p alg is a specific algorithm
1027 * allowed by the \p policy.
1028 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
1029 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
1030 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1033 psa_key_type_t key_type,
1034 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001035{
Steven Cooremand788fab2021-02-25 11:29:17 +01001036 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (alg == 0) {
1038 return PSA_ERROR_INVALID_ARGUMENT;
1039 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001040
Steven Cooreman7e39f052021-02-23 14:18:32 +01001041 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (PSA_ALG_IS_WILDCARD(alg)) {
1043 return PSA_ERROR_INVALID_ARGUMENT;
1044 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1047 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1048 return PSA_SUCCESS;
1049 } else {
1050 return PSA_ERROR_NOT_PERMITTED;
1051 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001052}
1053
Gilles Peskinef603c712019-01-19 13:40:11 +01001054/** Restrict a key policy based on a constraint.
1055 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001056 * \note This function requires providing the key type for which the policy is
1057 * being restricted, since some algorithm policy definitions (e.g. MAC)
1058 * have different properties depending on what kind of cipher it is
1059 * combined with.
1060 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001061 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001062 * \param[in,out] policy The policy to restrict.
1063 * \param[in] constraint The policy constraint to apply.
1064 *
1065 * \retval #PSA_SUCCESS
1066 * \c *policy contains the intersection of the original value of
1067 * \c *policy and \c *constraint.
1068 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001069 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001070 * \c *policy is unchanged.
1071 */
1072static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001073 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001074 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001076{
1077 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1079 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001080 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1082 constraint->alg2);
1083 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1084 return PSA_ERROR_INVALID_ARGUMENT;
1085 }
1086 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1087 return PSA_ERROR_INVALID_ARGUMENT;
1088 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001089 policy->usage &= constraint->usage;
1090 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001091 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001093}
1094
Przemek Stekiel061f6942022-11-30 10:51:35 +01001095/** Get the description of a key given its identifier and policy constraints
1096 * and lock it.
1097 *
1098 * The key must have allow all the usage flags set in \p usage. If \p alg is
1099 * nonzero, the key must allow operations with this algorithm. If \p alg is
1100 * zero, the algorithm is not checked.
1101 *
1102 * In case of a persistent key, the function loads the description of the key
1103 * into a key slot if not already done.
1104 *
1105 * On success, the returned key slot is locked. It is the responsibility of
1106 * the caller to unlock the key slot when it does not access it anymore.
1107 */
1108static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001109 mbedtls_svc_key_id_t key,
1110 psa_key_slot_t **p_slot,
1111 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001113{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001115 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 status = psa_get_and_lock_key_slot(key, p_slot);
1118 if (status != PSA_SUCCESS) {
1119 return status;
1120 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001121 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001122
1123 /* Enforce that usage policy for the key slot contains all the flags
1124 * required by the usage parameter. There is one exception: public
1125 * keys can always be exported, so we treat public key objects as
1126 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001128 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001132 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001133 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001134 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001135
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001136 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (alg != 0) {
1138 status = psa_key_policy_permits(&slot->attr.policy,
1139 slot->attr.type,
1140 alg);
1141 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001142 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001144 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001147
1148error:
1149 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001153}
Darryl Green940d72c2018-07-13 13:18:51 +01001154
Ronald Cron5c522922020-11-14 16:35:34 +01001155/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001156 *
1157 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001158 * available, as opposed to a key in a secure element and/or to be used
1159 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001160 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001161 * This is a temporary function that may be used instead of
1162 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1163 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001164 *
Ronald Cron5c522922020-11-14 16:35:34 +01001165 * On success, the returned key slot is locked. It is the responsibility of the
1166 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001167 */
Ronald Cron5c522922020-11-14 16:35:34 +01001168static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1169 mbedtls_svc_key_id_t key,
1170 psa_key_slot_t **p_slot,
1171 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001173{
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1175 usage, alg);
1176 if (status != PSA_SUCCESS) {
1177 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001178 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1181 psa_unlock_key_slot(*p_slot);
1182 *p_slot = NULL;
1183 return PSA_ERROR_NOT_SUPPORTED;
1184 }
1185
1186 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001187}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001190{
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001192 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001194
Ronald Cronea0f8a62020-11-25 17:52:23 +01001195 slot->key.data = NULL;
1196 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001199}
1200
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001201/** Completely wipe a slot in memory, including its policy.
1202 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001204{
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 /*
1208 * As the return error code may not be handled in case of multiple errors,
1209 * do our best to report an unexpected lock counter. Assert with
1210 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1211 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1212 * function is called as part of the execution of a test suite, the
1213 * execution of the test suite is stopped in error if the assertion fails.
1214 */
1215 if (slot->lock_count != 1) {
1216 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001217 status = PSA_ERROR_CORRUPTION_DETECTED;
1218 }
1219
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001220 /* Multipart operations may still be using the key. This is safe
1221 * because all multipart operation objects are independent from
1222 * the key slot: if they need to access the key after the setup
1223 * phase, they have a copy of the key. Note that this means that
1224 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001225 /* At this point, key material and other type-specific content has
1226 * been wiped. Clear remaining metadata. We can call memset and not
1227 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 memset(slot, 0, sizeof(*slot));
1229 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001230}
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001234 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001235 psa_status_t status; /* status of the last operation */
1236 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238 psa_se_drv_table_entry_t *driver;
1239#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (mbedtls_svc_key_id_is_null(key)) {
1242 return PSA_SUCCESS;
1243 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001244
Ronald Cronf2911112020-10-29 17:51:10 +01001245 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001246 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001247 * key, this will load the key description from persistent memory if not
1248 * done yet. We cannot avoid this loading as without it we don't know if
1249 * the key is operated by an SE or not and this information is needed by
1250 * the current implementation.
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 status = psa_get_and_lock_key_slot(key, &slot);
1253 if (status != PSA_SUCCESS) {
1254 return status;
1255 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001256
Ronald Cronf2911112020-10-29 17:51:10 +01001257 /*
1258 * If the key slot containing the key description is under access by the
1259 * library (apart from the present access), the key cannot be destroyed
1260 * yet. For the time being, just return in error. Eventually (to be
1261 * implemented), the key should be destroyed when all accesses have
1262 * stopped.
1263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (slot->lock_count > 1) {
1265 psa_unlock_key_slot(slot);
1266 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001267 }
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001270 /* Refuse the destruction of a read-only key (which may or may not work
1271 * if we attempt it, depending on whether the key is merely read-only
1272 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001273 * Just do the best we can, which is to wipe the copy in memory
1274 * (done in this function's cleanup code). */
1275 overall_status = PSA_ERROR_NOT_PERMITTED;
1276 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001277 }
1278
Gilles Peskine354f7672019-07-12 23:46:38 +02001279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1281 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001282 /* For a key in a secure element, we need to do three things:
1283 * remove the key file in internal storage, destroy the
1284 * key inside the secure element, and update the driver's
1285 * persistent data. Start a transaction that will encompass these
1286 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001290 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 status = psa_crypto_save_transaction();
1292 if (status != PSA_SUCCESS) {
1293 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001294 /* We should still try to destroy the key in the secure
1295 * element and the key metadata in storage. This is especially
1296 * important if the error is that the storage is full.
1297 * But how to do it exactly without risking an inconsistent
1298 * state after a reset?
1299 * https://github.com/ARMmbed/mbed-crypto/issues/215
1300 */
1301 overall_status = status;
1302 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 status = psa_destroy_se_key(driver,
1306 psa_key_slot_get_slot_number(slot));
1307 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001308 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001310 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1312
Darryl Greend49a4992018-06-18 17:27:26 +01001313#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1315 status = psa_destroy_persistent_key(slot->attr.id);
1316 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001317 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001319
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001320 /* TODO: other slots may have a copy of the same key. We should
1321 * invalidate them.
1322 * https://github.com/ARMmbed/mbed-crypto/issues/214
1323 */
Darryl Greend49a4992018-06-18 17:27:26 +01001324 }
1325#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001326
Gilles Peskinefc762652019-07-22 19:30:34 +02001327#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (driver != NULL) {
1329 status = psa_save_se_persistent_data(driver);
1330 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001331 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 }
1333 status = psa_crypto_stop_transaction();
1334 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001335 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001337 }
1338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1339
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001342 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001344 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
1346 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347}
1348
Valerio Settib2bcedb2023-07-10 11:24:00 +02001349#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001350 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001351static psa_status_t psa_get_rsa_public_exponent(
1352 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001354{
1355 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001357 uint8_t *buffer = NULL;
1358 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1362 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001363 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 }
1365 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001366 /* It's the default value, which is reported as an empty string,
1367 * so there's nothing to do. */
1368 goto exit;
1369 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 buflen = mbedtls_mpi_size(&mpi);
1372 buffer = mbedtls_calloc(1, buflen);
1373 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001374 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1375 goto exit;
1376 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1378 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001379 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001381 attributes->domain_parameters = buffer;
1382 attributes->domain_parameters_size = buflen;
1383
1384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 mbedtls_mpi_free(&mpi);
1386 if (ret != 0) {
1387 mbedtls_free(buffer);
1388 }
1389 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001390}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001391#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001392 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001393
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001394/** Retrieve all the publicly-accessible attributes of a key.
1395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1397 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001400 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001401 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1406 if (status != PSA_SUCCESS) {
1407 return status;
1408 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001409
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001410 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1412 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001413
1414#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1416 psa_set_key_slot_number(attributes,
1417 psa_key_slot_get_slot_number(slot));
1418 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001419#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001422#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1423 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001424 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001425 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001426 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001427 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001428 * is not yet implemented.
1429 * https://github.com/ARMmbed/mbed-crypto/issues/216
1430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001432 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001433
Ronald Cron90857082020-11-25 14:58:33 +01001434 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 slot->attr.type,
1436 slot->key.data,
1437 slot->key.bytes,
1438 &rsa);
1439 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001440 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 status = psa_get_rsa_public_exponent(rsa,
1444 attributes);
1445 mbedtls_rsa_free(rsa);
1446 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001447 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001448 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001449#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1450 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001451 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001452 default:
1453 /* Nothing else to do. */
1454 break;
1455 }
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (status != PSA_SUCCESS) {
1458 psa_reset_key_attributes(attributes);
1459 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464}
1465
Gilles Peskinec8000c02019-08-02 20:15:51 +02001466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1467psa_status_t psa_get_key_slot_number(
1468 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001470{
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001472 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return PSA_SUCCESS;
1474 } else {
1475 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001476 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001477}
1478#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1481 size_t key_buffer_size,
1482 uint8_t *data,
1483 size_t data_size,
1484 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001485{
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (key_buffer_size > data_size) {
1487 return PSA_ERROR_BUFFER_TOO_SMALL;
1488 }
1489 memcpy(data, key_buffer, key_buffer_size);
1490 memset(data + key_buffer_size, 0,
1491 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001492 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001494}
1495
Ronald Cron7285cda2020-11-26 14:46:37 +01001496psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001497 const psa_key_attributes_t *attributes,
1498 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500{
Ronald Crond18b5f82020-11-25 16:46:05 +01001501 psa_key_type_t type = attributes->core.type;
1502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (key_type_is_raw_bytes(type) ||
1504 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001505 PSA_KEY_TYPE_IS_ECC(type) ||
1506 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return psa_export_key_buffer_internal(
1508 key_buffer, key_buffer_size,
1509 data, data_size, data_length);
1510 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001511 /* This shouldn't happen in the reference implementation, but
1512 it is valid for a special-purpose implementation to omit
1513 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001515 }
1516}
1517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1519 uint8_t *data,
1520 size_t data_size,
1521 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001522{
1523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1524 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1525 psa_key_slot_t *slot;
1526
Ronald Crone907e552021-01-18 13:22:38 +01001527 /* Reject a zero-length output buffer now, since this can never be a
1528 * valid key representation. This way we know that data must be a valid
1529 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (data_size == 0) {
1531 return PSA_ERROR_BUFFER_TOO_SMALL;
1532 }
Ronald Crone907e552021-01-18 13:22:38 +01001533
Ronald Cron9486f9d2020-11-26 13:36:23 +01001534 /* Set the key to empty now, so that even when there are errors, we always
1535 * set data_length to a value between 0 and data_size. On error, setting
1536 * the key to empty is a good choice because an empty key representation is
1537 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001538 *data_length = 0;
1539
Ronald Cron9486f9d2020-11-26 13:36:23 +01001540 /* Export requires the EXPORT flag. There is an exception for public keys,
1541 * which don't require any flag, but
1542 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1543 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1545 PSA_KEY_USAGE_EXPORT, 0);
1546 if (status != PSA_SUCCESS) {
1547 return status;
1548 }
mohammad160306e79202018-03-28 13:17:44 +03001549
Ronald Crond18b5f82020-11-25 16:46:05 +01001550 psa_key_attributes_t attributes = {
1551 .core = slot->attr
1552 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 status = psa_driver_wrapper_export_key(&attributes,
1554 slot->key.data, slot->key.bytes,
1555 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001560}
1561
Ronald Cron7285cda2020-11-26 14:46:37 +01001562psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001563 const psa_key_attributes_t *attributes,
1564 const uint8_t *key_buffer,
1565 size_t key_buffer_size,
1566 uint8_t *data,
1567 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001569{
Ronald Crond18b5f82020-11-25 16:46:05 +01001570 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001571
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001572 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001573 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1574 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001575 /* Exporting public -> public */
1576 return psa_export_key_buffer_internal(
1577 key_buffer, key_buffer_size,
1578 data, data_size, data_length);
1579 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001580#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001581 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1582 return mbedtls_psa_rsa_export_public_key(attributes,
1583 key_buffer,
1584 key_buffer_size,
1585 data,
1586 data_size,
1587 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001588#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001589 /* We don't know how to convert a private RSA key to public. */
1590 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001591#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001592 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001593 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001594#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001595 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1596 return mbedtls_psa_ecp_export_public_key(attributes,
1597 key_buffer,
1598 key_buffer_size,
1599 data,
1600 data_size,
1601 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001603 /* We don't know how to convert a private ECC key to public */
1604 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001605#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001606 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001607 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001608#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001609 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001610 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001611 key_buffer,
1612 key_buffer_size,
1613 data, data_size,
1614 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001615#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001616 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001617#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001618 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001620 (void) key_buffer;
1621 (void) key_buffer_size;
1622 (void) data;
1623 (void) data_size;
1624 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001626 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627}
Ronald Crond18b5f82020-11-25 16:46:05 +01001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1630 uint8_t *data,
1631 size_t data_size,
1632 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001633{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001635 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001636 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001637 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001638
Ronald Crone907e552021-01-18 13:22:38 +01001639 /* Reject a zero-length output buffer now, since this can never be a
1640 * valid key representation. This way we know that data must be a valid
1641 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (data_size == 0) {
1643 return PSA_ERROR_BUFFER_TOO_SMALL;
1644 }
Ronald Crone907e552021-01-18 13:22:38 +01001645
Darryl Greendd8fb772018-11-07 16:00:44 +00001646 /* Set the key to empty now, so that even when there are errors, we always
1647 * set data_length to a value between 0 and data_size. On error, setting
1648 * the key to empty is a good choice because an empty key representation is
1649 * unlikely to be accepted anywhere. */
1650 *data_length = 0;
1651
1652 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1654 if (status != PSA_SUCCESS) {
1655 return status;
1656 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1659 status = PSA_ERROR_INVALID_ARGUMENT;
1660 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001661 }
1662
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001663 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001664 .core = slot->attr
1665 };
Ronald Cron67227982020-11-26 15:16:05 +01001666 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001667 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001669
1670exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001674}
1675
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001676MBEDTLS_STATIC_ASSERT(
1677 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1678 "One or more key attribute flag is listed as both external-only and dual-use")
1679MBEDTLS_STATIC_ASSERT(
1680 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1681 "One or more key attribute flag is listed as both internal-only and dual-use")
1682MBEDTLS_STATIC_ASSERT(
1683 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1684 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001685
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001686/** Validate that a key policy is internally well-formed.
1687 *
1688 * This function only rejects invalid policies. It does not validate the
1689 * consistency of the policy with respect to other attributes of the key
1690 * such as the key type.
1691 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001693{
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1695 PSA_KEY_USAGE_COPY |
1696 PSA_KEY_USAGE_ENCRYPT |
1697 PSA_KEY_USAGE_DECRYPT |
1698 PSA_KEY_USAGE_SIGN_MESSAGE |
1699 PSA_KEY_USAGE_VERIFY_MESSAGE |
1700 PSA_KEY_USAGE_SIGN_HASH |
1701 PSA_KEY_USAGE_VERIFY_HASH |
1702 PSA_KEY_USAGE_VERIFY_DERIVATION |
1703 PSA_KEY_USAGE_DERIVE)) != 0) {
1704 return PSA_ERROR_INVALID_ARGUMENT;
1705 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001708}
1709
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001710/** Validate the internal consistency of key attributes.
1711 *
1712 * This function only rejects invalid attribute values. If does not
1713 * validate the consistency of the attributes with any key data that may
1714 * be involved in the creation of the key.
1715 *
1716 * Call this function early in the key creation process.
1717 *
1718 * \param[in] attributes Key attributes for the new key.
1719 * \param[out] p_drv On any return, the driver for the key, if any.
1720 * NULL for a transparent key.
1721 *
1722 */
1723static psa_status_t psa_validate_key_attributes(
1724 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001726{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001727 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1729 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 status = psa_validate_key_location(lifetime, p_drv);
1732 if (status != PSA_SUCCESS) {
1733 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001734 }
1735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 status = psa_validate_key_persistence(lifetime);
1737 if (status != PSA_SUCCESS) {
1738 return status;
1739 }
1740
1741 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1742 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1743 return PSA_ERROR_INVALID_ARGUMENT;
1744 }
1745 } else {
1746 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1747 return PSA_ERROR_INVALID_ARGUMENT;
1748 }
1749 }
1750
1751 status = psa_validate_key_policy(&attributes->core.policy);
1752 if (status != PSA_SUCCESS) {
1753 return status;
1754 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001755
1756 /* Refuse to create overly large keys.
1757 * Note that this doesn't trigger on import if the attributes don't
1758 * explicitly specify a size (so psa_get_key_bits returns 0), so
1759 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1761 return PSA_ERROR_NOT_SUPPORTED;
1762 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763
Gilles Peskine91e8c332019-08-02 19:19:39 +02001764 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1766 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1767 return PSA_ERROR_INVALID_ARGUMENT;
1768 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001771}
1772
Gilles Peskine4747d192019-04-17 15:05:45 +02001773/** Prepare a key slot to receive key material.
1774 *
1775 * This function allocates a key slot and sets its metadata.
1776 *
1777 * If this function fails, call psa_fail_key_creation().
1778 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 * This function is intended to be used as follows:
1780 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001781 * it with the specified attributes, and in case of a volatile key assign it
1782 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001783 * -# Populate the slot with the key material.
1784 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1785 * In case of failure at any step, stop the sequence and call
1786 * psa_fail_key_creation().
1787 *
Ronald Cron5c522922020-11-14 16:35:34 +01001788 * On success, the key slot is locked. It is the responsibility of the caller
1789 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001790 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001791 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001792 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001793 * \param[out] p_slot On success, a pointer to the prepared slot.
1794 * \param[out] p_drv On any return, the driver for the key, if any.
1795 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001796 *
1797 * \retval #PSA_SUCCESS
1798 * The key slot is ready to receive key material.
1799 * \return If this function fails, the key slot is an invalid state.
1800 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001801 */
1802static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001803 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001804 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001805 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001807{
1808 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001809 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001810 psa_key_slot_t *slot;
1811
Gilles Peskinedf179142019-07-15 22:02:14 +02001812 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001813 *p_drv = NULL;
1814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 status = psa_validate_key_attributes(attributes, p_drv);
1816 if (status != PSA_SUCCESS) {
1817 return status;
1818 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1821 if (status != PSA_SUCCESS) {
1822 return status;
1823 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001824 slot = *p_slot;
1825
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001826 /* We're storing the declared bit-size of the key. It's up to each
1827 * creation mechanism to verify that this information is correct.
1828 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001829 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001830 * is optional (import, copy). In case of a volatile key, assign it the
1831 * volatile key identifier associated to the slot returned to contain its
1832 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001833
1834 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001836#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1837 slot->attr.id = volatile_key_id;
1838#else
1839 slot->attr.id.key_id = volatile_key_id;
1840#endif
1841 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001842
Gilles Peskine91e8c332019-08-02 19:19:39 +02001843 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001844 * external-only flags, query `attributes`. Thanks to the check
1845 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001846 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001847 * may have set. */
1848 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001849
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001851 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001852 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001853 * create the key file in internal storage, create the
1854 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001855 * persistent data. This is done by starting a transaction that will
1856 * encompass these three actions.
1857 * For registering a volatile key, we just need to find an appropriate
1858 * slot number inside the SE. Since the key is designated volatile, creating
1859 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001860 /* The first thing to do is to find a slot number for the new key.
1861 * We save the slot number in persistent storage as part of the
1862 * transaction data. It will be needed to recover if the power
1863 * fails during the key creation process, to clean up on the secure
1864 * element side after restarting. Obtaining a slot number from the
1865 * secure element driver updates its persistent state, but we do not yet
1866 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001867 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001869 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1871 &slot_number);
1872 if (status != PSA_SUCCESS) {
1873 return status;
1874 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1877 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001878 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001879 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001880 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 status = psa_crypto_save_transaction();
1882 if (status != PSA_SUCCESS) {
1883 (void) psa_crypto_stop_transaction();
1884 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001885 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001886 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001887
1888 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001890 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001893 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001895 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001896#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1897
Ryan Everett975d4112023-11-16 13:37:51 +00001898 slot->status = PSA_SLOT_OCCUPIED;
1899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001901}
Gilles Peskine4747d192019-04-17 15:05:45 +02001902
1903/** Finalize the creation of a key once its key material has been set.
1904 *
1905 * This entails writing the key to persistent storage.
1906 *
1907 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001908 * See the documentation of psa_start_key_creation() for the intended use
1909 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001910 *
Ronald Cron5c522922020-11-14 16:35:34 +01001911 * If the finalization succeeds, the function unlocks the key slot (it was
1912 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1913 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001914 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001915 * \param[in,out] slot Pointer to the slot with key material.
1916 * \param[in] driver The secure element driver for the key,
1917 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001918 * \param[out] key On success, identifier of the key. Note that the
1919 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001920 *
1921 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001922 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1924 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1925 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1926 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1927 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1928 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001929 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001930 * \return If this function fails, the key slot is an invalid state.
1931 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001933static psa_status_t psa_finish_key_creation(
1934 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001935 psa_se_drv_table_entry_t *driver,
1936 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001937{
1938 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001939 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001940 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001941
1942#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001944#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001946 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001947 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001949
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001950 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1951 sizeof(data.slot_number),
1952 "Slot number size does not match psa_se_key_data_storage_t");
1953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1955 status = psa_save_persistent_key(&slot->attr,
1956 (uint8_t *) &data,
1957 sizeof(data));
1958 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1960 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001961 /* Key material is saved in export representation in the slot, so
1962 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 status = psa_save_persistent_key(&slot->attr,
1964 slot->key.data,
1965 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001966 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001967 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001968#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1969
Gilles Peskinecbaff462019-07-12 23:46:04 +02001970#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001971 /* Finish the transaction for a key creation. This does not
1972 * happen when registering an existing key. Detect this case
1973 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001974 * creation of a persistent key in a secure element requires a transaction,
1975 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (driver != NULL &&
1977 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1978 status = psa_save_se_persistent_data(driver);
1979 if (status != PSA_SUCCESS) {
1980 psa_destroy_persistent_key(slot->attr.id);
1981 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001982 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001984 }
1985#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001988 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 status = psa_unlock_key_slot(slot);
1990 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001991 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001993 }
Ronald Cron50972942020-11-14 11:28:25 +01001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001996}
1997
1998/** Abort the creation of a key.
1999 *
2000 * You may call this function after calling psa_start_key_creation(),
2001 * or after psa_finish_key_creation() fails. In other circumstances, this
2002 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002003 * See the documentation of psa_start_key_creation() for the intended use
2004 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002005 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 * \param[in,out] slot Pointer to the slot with key material.
2007 * \param[in] driver The secure element driver for the key,
2008 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010static void psa_fail_key_creation(psa_key_slot_t *slot,
2011 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02002012{
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 (void) driver;
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02002016 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 }
Gilles Peskine011e4282019-06-26 18:34:38 +02002018
2019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002020 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002021 * element, and the failure happened later (when saving metadata
2022 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002023 * element.
2024 * https://github.com/ARMmbed/mbed-crypto/issues/217
2025 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002026
Gilles Peskined7729582019-08-05 15:55:54 +02002027 /* Abort the ongoing transaction if any (there may not be one if
2028 * the creation process failed before starting one, or if the
2029 * key creation is a registration of a key in a secure element).
2030 * Earlier functions must already have done what it takes to undo any
2031 * partial creation. All that's left is to update the transaction data
2032 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002034#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002037}
2038
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002039/** Validate optional attributes during key creation.
2040 *
2041 * Some key attributes are optional during key creation. If they are
2042 * specified in the attributes structure, check that they are consistent
2043 * with the data in the slot.
2044 *
2045 * This function should be called near the end of key creation, after
2046 * the slot in memory is fully populated but before saving persistent data.
2047 */
2048static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051{
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (attributes->core.type != 0) {
2053 if (attributes->core.type != slot->attr.type) {
2054 return PSA_ERROR_INVALID_ARGUMENT;
2055 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002056 }
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002059#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2060 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2062 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002063 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002064 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002066
Ronald Cron90857082020-11-25 14:58:33 +01002067 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 slot->attr.type,
2069 slot->key.data,
2070 slot->key.bytes,
2071 &rsa);
2072 if (status != PSA_SUCCESS) {
2073 return status;
2074 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 mbedtls_mpi_init(&actual);
2077 mbedtls_mpi_init(&required);
2078 ret = mbedtls_rsa_export(rsa,
2079 NULL, NULL, NULL, NULL, &actual);
2080 mbedtls_rsa_free(rsa);
2081 mbedtls_free(rsa);
2082 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
2085 ret = mbedtls_mpi_read_binary(&required,
2086 attributes->domain_parameters,
2087 attributes->domain_parameters_size);
2088 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 }
2091 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
2094rsa_exit:
2095 mbedtls_mpi_free(&actual);
2096 mbedtls_mpi_free(&required);
2097 if (ret != 0) {
2098 return mbedtls_to_psa_error(ret);
2099 }
2100 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002101#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2102 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002103 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002104 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002106 }
2107 }
2108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (attributes->core.bits != 0) {
2110 if (attributes->core.bits != slot->attr.bits) {
2111 return PSA_ERROR_INVALID_ARGUMENT;
2112 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113 }
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002116}
2117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2119 const uint8_t *data,
2120 size_t data_length,
2121 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002122{
2123 psa_status_t status;
2124 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002125 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002126 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302127 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128
Ronald Cron81709fc2020-11-14 12:10:32 +01002129 *key = MBEDTLS_SVC_KEY_ID_INIT;
2130
Gilles Peskine0f84d622019-09-12 19:03:13 +02002131 /* Reject zero-length symmetric keys (including raw data key objects).
2132 * This also rejects any key which might be encoded as an empty string,
2133 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (data_length == 0) {
2135 return PSA_ERROR_INVALID_ARGUMENT;
2136 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002137
Archana449608b2021-09-08 15:36:05 +05302138 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (data_length > SIZE_MAX / 8) {
2140 return PSA_ERROR_NOT_SUPPORTED;
2141 }
Archana6ed4bda2021-08-04 10:47:15 +05302142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2144 &slot, &driver);
2145 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002149 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302150 * storage ( thus not in the case of importing a key in a secure element
2151 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2152 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (slot->key.data == NULL) {
2154 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302155 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 attributes, data, data_length, &storage_size);
2157 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Archanad8a83dc2021-06-14 10:04:16 +05302160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_allocate_buffer_to_slot(slot, storage_size);
2162 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002165 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002166
2167 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 status = psa_driver_wrapper_import_key(attributes,
2169 data, data_length,
2170 slot->key.data,
2171 slot->key.bytes,
2172 &slot->key.bytes, &bits);
2173 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002178 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002180 status = PSA_ERROR_INVALID_ARGUMENT;
2181 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002182 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002183
Archana6ed4bda2021-08-04 10:47:15 +05302184 /* Enforce a size limit, and in particular ensure that the bit
2185 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302187 status = PSA_ERROR_NOT_SUPPORTED;
2188 goto exit;
2189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 status = psa_validate_optional_attributes(slot, attributes);
2191 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (status != PSA_SUCCESS) {
2198 psa_fail_key_creation(slot, driver);
2199 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002202}
2203
Gilles Peskined7729582019-08-05 15:55:54 +02002204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2205psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002207{
2208 psa_status_t status;
2209 psa_key_slot_t *slot = NULL;
2210 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002212
2213 /* Leaving attributes unspecified is not currently supported.
2214 * It could make sense to query the key type and size from the
2215 * secure element, but not all secure elements support this
2216 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2218 return PSA_ERROR_NOT_SUPPORTED;
2219 }
2220 if (psa_get_key_bits(attributes) == 0) {
2221 return PSA_ERROR_NOT_SUPPORTED;
2222 }
Gilles Peskined7729582019-08-05 15:55:54 +02002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2225 &slot, &driver);
2226 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 }
Gilles Peskined7729582019-08-05 15:55:54 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002231
2232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (status != PSA_SUCCESS) {
2234 psa_fail_key_creation(slot, driver);
2235 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002236
Gilles Peskined7729582019-08-05 15:55:54 +02002237 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 psa_close_key(key);
2239 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002240}
2241#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2244 const psa_key_attributes_t *specified_attributes,
2245 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002249 psa_key_slot_t *source_slot = NULL;
2250 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002251 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002252 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302253 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002254
Ronald Cron81709fc2020-11-14 12:10:32 +01002255 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2256
Archana8a180362021-07-05 02:18:48 +05302257 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2259 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 status = psa_validate_optional_attributes(source_slot,
2264 specified_attributes);
2265 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002266 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002268
Archana9d17bf42021-09-10 06:22:44 +05302269 /* The target key type and number of bits have been validated by
2270 * psa_validate_optional_attributes() to be either equal to zero or
2271 * equal to the ones of the source key. So it is safe to inherit
2272 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302273 * */
Archana9d17bf42021-09-10 06:22:44 +05302274 actual_attributes.core.bits = source_slot->attr.bits;
2275 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302276
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_restrict_key_policy(source_slot->attr.type,
2279 &actual_attributes.core.policy,
2280 &source_slot->attr.policy);
2281 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2286 &target_slot, &driver);
2287 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002288 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 }
2290 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2291 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302292 /*
Archana9d17bf42021-09-10 06:22:44 +05302293 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302294 * the source key would need to be exported as plaintext and re-imported
2295 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302296 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302297 * appropriate API invocations from the application, if needed.
2298 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002299 status = PSA_ERROR_NOT_SUPPORTED;
2300 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002301 }
Archana8a180362021-07-05 02:18:48 +05302302 /*
2303 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302304 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302305 * - For opaque keys this translates to an invocation of the drivers'
2306 * copy_key entry point through the dispatch layer.
2307 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2309 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2310 &storage_size);
2311 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 }
Archana374fe5b2021-09-08 15:50:28 +05302314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2316 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302317 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 }
Archana374fe5b2021-09-08 15:50:28 +05302319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 status = psa_driver_wrapper_copy_key(&actual_attributes,
2321 source_slot->key.data,
2322 source_slot->key.bytes,
2323 target_slot->key.data,
2324 target_slot->key.bytes,
2325 &target_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 }
2329 } else {
2330 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302331 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 source_slot->key.bytes);
2333 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 }
Archana8a180362021-07-05 02:18:48 +05302336 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (status != PSA_SUCCESS) {
2340 psa_fail_key_creation(target_slot, driver);
2341 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002346}
2347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002348
2349
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002350/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002351/* Message digests */
2352/****************************************************************/
2353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002356 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (operation->id == 0) {
2358 return PSA_SUCCESS;
2359 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002362 operation->id = 0;
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365}
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2368 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002371
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002372 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002374 status = PSA_ERROR_BAD_STATE;
2375 goto exit;
2376 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002379 status = PSA_ERROR_INVALID_ARGUMENT;
2380 goto exit;
2381 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002382
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002383 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2384 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002388
2389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (status != PSA_SUCCESS) {
2391 psa_hash_abort(operation);
2392 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002393
2394 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395}
2396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2398 const uint8_t *input,
2399 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002404 status = PSA_ERROR_BAD_STATE;
2405 goto exit;
2406 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002407
Steven Cooremanc8288352021-03-04 14:02:19 +01002408 /* Don't require hash implementations to behave correctly on a
2409 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (input_length == 0) {
2411 return PSA_SUCCESS;
2412 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415
2416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (status != PSA_SUCCESS) {
2418 psa_hash_abort(operation);
2419 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002422}
2423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2425 uint8_t *hash,
2426 size_t hash_size,
2427 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002429 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (operation->id == 0) {
2431 return PSA_ERROR_BAD_STATE;
2432 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433
Steven Cooreman1e582352021-02-18 17:24:37 +01002434 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 operation, hash, hash_size, hash_length);
2436 psa_hash_abort(operation);
2437 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438}
2439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2441 const uint8_t *hash,
2442 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002443{
Ronald Cron69a63422021-10-18 09:47:58 +02002444 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002445 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002446 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 operation,
2448 actual_hash, sizeof(actual_hash),
2449 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002456 status = PSA_ERROR_INVALID_SIGNATURE;
2457 goto exit;
2458 }
2459
Dave Rodgman78701152023-08-29 14:20:18 +01002460 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002461 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002463
2464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2466 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002467 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471}
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473psa_status_t psa_hash_compute(psa_algorithm_t alg,
2474 const uint8_t *input, size_t input_length,
2475 uint8_t *hash, size_t hash_size,
2476 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002477{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002478 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (!PSA_ALG_IS_HASH(alg)) {
2480 return PSA_ERROR_INVALID_ARGUMENT;
2481 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2484 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002485}
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487psa_status_t psa_hash_compare(psa_algorithm_t alg,
2488 const uint8_t *input, size_t input_length,
2489 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002490{
Ronald Cron69a63422021-10-18 09:47:58 +02002491 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002492 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (!PSA_ALG_IS_HASH(alg)) {
2495 return PSA_ERROR_INVALID_ARGUMENT;
2496 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002497
Steven Cooreman1e582352021-02-18 17:24:37 +01002498 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 alg, input, input_length,
2500 actual_hash, sizeof(actual_hash),
2501 &actual_hash_length);
2502 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002503 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 }
2505 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002506 status = PSA_ERROR_INVALID_SIGNATURE;
2507 goto exit;
2508 }
Dave Rodgman78701152023-08-29 14:20:18 +01002509 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002510 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002512
2513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2515 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002516}
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2519 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002520{
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (source_operation->id == 0 ||
2522 target_operation->id != 0) {
2523 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002524 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2527 target_operation);
2528 if (status != PSA_SUCCESS) {
2529 psa_hash_abort(target_operation);
2530 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002533}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002534
2535
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002536/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537/* MAC */
2538/****************************************************************/
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002541{
Steven Cooremane6804192021-03-19 18:28:56 +01002542 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (operation->id == 0) {
2544 return PSA_SUCCESS;
2545 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002548 operation->mac_size = 0;
2549 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002550 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553}
2554
Ronald Cron2dff3b22021-06-17 16:33:22 +02002555static psa_status_t psa_mac_finalize_alg_and_key_validation(
2556 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002557 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 psa_key_type_t key_type = psa_get_key_type(attributes);
2562 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (!PSA_ALG_IS_MAC(alg)) {
2565 return PSA_ERROR_INVALID_ARGUMENT;
2566 }
Steven Cooremane6804192021-03-19 18:28:56 +01002567
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002568 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 status = psa_mac_key_can_do(alg, key_type);
2570 if (status != PSA_SUCCESS) {
2571 return status;
2572 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002573
Steven Cooremand1a68f12021-05-10 11:13:41 +02002574 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002578 /* A very short MAC is too short for security since it can be
2579 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2580 * so we make this our minimum, even though 32 bits is still
2581 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002583 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2586 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002587 /* It's impossible to "truncate" to a larger length than the full length
2588 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002590 }
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002593 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2594 * that is disabled in the compile-time configuration. The result can
2595 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2596 * configuration into account. In this case, force a return of
2597 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2598 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2599 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2600 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2601 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002603 }
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002606}
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2609 mbedtls_svc_key_id_t key,
2610 psa_algorithm_t alg,
2611 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002612{
2613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2614 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002615 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002616 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617
2618 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002620 status = PSA_ERROR_BAD_STATE;
2621 goto exit;
2622 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623
2624 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 key,
2626 &slot,
2627 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2628 alg);
2629 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002633 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634 .core = slot->attr
2635 };
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2638 &operation->mac_size);
2639 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642
2643 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002644 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (is_sign) {
2646 status = psa_driver_wrapper_mac_sign_setup(operation,
2647 &attributes,
2648 slot->key.data,
2649 slot->key.bytes,
2650 alg);
2651 } else {
2652 status = psa_driver_wrapper_mac_verify_setup(operation,
2653 &attributes,
2654 slot->key.data,
2655 slot->key.bytes,
2656 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002658
Gilles Peskinefbfac682018-07-08 20:51:54 +02002659exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (status != PSA_SUCCESS) {
2661 psa_mac_abort(operation);
2662 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667}
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669psa_status_t psa_mac_sign_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, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002674}
2675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2677 mbedtls_svc_key_id_t key,
2678 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002679{
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002681}
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2684 const uint8_t *input,
2685 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686{
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (operation->id == 0) {
2688 return PSA_ERROR_BAD_STATE;
2689 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002691 /* Don't require hash implementations to behave correctly on a
2692 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (input_length == 0) {
2694 return PSA_SUCCESS;
2695 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2698 input, input_length);
2699 if (status != PSA_SUCCESS) {
2700 psa_mac_abort(operation);
2701 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704}
2705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2707 uint8_t *mac,
2708 size_t mac_size,
2709 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002710{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2712 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002715 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002716 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002717 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (!operation->is_sign) {
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 Cooreman72f736a2021-05-07 14:14:37 +02002723
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002724 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2725 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002727 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002728 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002729 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002732 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002733 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002734 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 status = psa_driver_wrapper_mac_sign_finish(operation,
2737 mac, operation->mac_size,
2738 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002739
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002740exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002741 /* In case of success, set the potential excess room in the output buffer
2742 * to an invalid value, to avoid potentially leaking a longer MAC.
2743 * In case of error, set the output length and content to a safe default,
2744 * such that in case the caller misses an error check, the output would be
2745 * an unachievable MAC.
2746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002748 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002749 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002750 }
mohammad16036df908f2018-04-02 08:34:15 -07002751
Paul Elliottdc42ca82023-02-24 18:11:59 +00002752 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002757}
2758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2760 const uint8_t *mac,
2761 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2764 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002767 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002768 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002769 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002772 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002773 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002774 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002777 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002778 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002779 }
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 status = psa_driver_wrapper_mac_verify_finish(operation,
2782 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002783
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002784exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788}
2789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2791 psa_algorithm_t alg,
2792 const uint8_t *input,
2793 size_t input_length,
2794 uint8_t *mac,
2795 size_t mac_size,
2796 size_t *mac_length,
2797 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002798{
2799 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002800 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2801 psa_key_slot_t *slot;
2802 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002803 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804
Ronald Cron51131b52021-06-17 17:17:20 +02002805 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 key,
2807 &slot,
2808 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2809 alg);
2810 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002811 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002813
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002814 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002815 .core = slot->attr
2816 };
2817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2819 &operation_mac_size);
2820 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002825 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002826 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002827 }
2828
2829 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 &attributes,
2831 slot->key.data, slot->key.bytes,
2832 alg,
2833 input, input_length,
2834 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002835
2836exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002837 /* In case of success, set the potential excess room in the output buffer
2838 * to an invalid value, to avoid potentially leaking a longer MAC.
2839 * In case of error, set the output length and content to a safe default,
2840 * such that in case the caller misses an error check, the output would be
2841 * an unachievable MAC.
2842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002844 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002845 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002846 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002847
2848 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002853}
2854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002856 psa_algorithm_t alg,
2857 const uint8_t *input,
2858 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 uint8_t *mac,
2860 size_t mac_size,
2861 size_t *mac_length)
2862{
2863 return psa_mac_compute_internal(key, alg,
2864 input, input_length,
2865 mac, mac_size, mac_length, 1);
2866}
2867
2868psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2869 psa_algorithm_t alg,
2870 const uint8_t *input,
2871 size_t input_length,
2872 const uint8_t *mac,
2873 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002874{
2875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002876 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2877 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 status = psa_mac_compute_internal(key, alg,
2880 input, input_length,
2881 actual_mac, sizeof(actual_mac),
2882 &actual_mac_length, 0);
2883 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002884 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002888 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002889 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002890 }
Dave Rodgman78701152023-08-29 14:20:18 +01002891 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002892 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002893 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002894 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002895
2896exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002900}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002901
Gilles Peskinee59236f2018-01-27 23:32:46 +01002902/****************************************************************/
2903/* Asymmetric cryptography */
2904/****************************************************************/
2905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2907 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002908{
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 if (input_is_message) {
2910 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2911 return PSA_ERROR_INVALID_ARGUMENT;
2912 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2915 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2916 return PSA_ERROR_INVALID_ARGUMENT;
2917 }
2918 }
2919 } else {
2920 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2921 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002922 }
2923 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002926}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2929 int input_is_message,
2930 psa_algorithm_t alg,
2931 const uint8_t *input,
2932 size_t input_length,
2933 uint8_t *signature,
2934 size_t signature_size,
2935 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002936{
2937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2938 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2939 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002940 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002941
2942 *signature_length = 0;
2943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 status = psa_sign_verify_check_alg(input_is_message, alg);
2945 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002946 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002948
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002949 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002950 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002951 * buffer can in principle be empty since it doesn't actually have
2952 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 if (signature_size == 0) {
2954 return PSA_ERROR_BUFFER_TOO_SMALL;
2955 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002956
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002957 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 key, &slot,
2959 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2960 PSA_KEY_USAGE_SIGN_HASH,
2961 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002964 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968 status = PSA_ERROR_INVALID_ARGUMENT;
2969 goto exit;
2970 }
2971
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002972 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002974 };
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002977 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002978 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002979 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 signature, signature_size, signature_length);
2981 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002982
2983 status = psa_driver_wrapper_sign_hash(
2984 &attributes, slot->key.data, slot->key.bytes,
2985 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987 }
2988
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002989
2990exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002991 psa_wipe_tag_output_buffer(signature, status, signature_size,
2992 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002997}
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
3000 int input_is_message,
3001 psa_algorithm_t alg,
3002 const uint8_t *input,
3003 size_t input_length,
3004 const uint8_t *signature,
3005 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003006{
3007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3008 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3009 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 status = psa_sign_verify_check_alg(input_is_message, alg);
3012 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003013 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003015
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003016 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 key, &slot,
3018 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
3019 PSA_KEY_USAGE_VERIFY_HASH,
3020 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (status != PSA_SUCCESS) {
3023 return status;
3024 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003025
3026 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003028 };
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003031 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003032 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003033 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 signature, signature_length);
3035 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003036 status = psa_driver_wrapper_verify_hash(
3037 &attributes, slot->key.data, slot->key.bytes,
3038 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003040 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003045
3046}
3047
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003048psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003049 const psa_key_attributes_t *attributes,
3050 const uint8_t *key_buffer,
3051 size_t key_buffer_size,
3052 psa_algorithm_t alg,
3053 const uint8_t *input,
3054 size_t input_length,
3055 uint8_t *signature,
3056 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003058{
3059 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003062 size_t hash_length;
3063 uint8_t hash[PSA_HASH_MAX_SIZE];
3064
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003065 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 PSA_ALG_SIGN_GET_HASH(alg),
3067 input, input_length,
3068 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003071 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003073
3074 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 attributes, key_buffer, key_buffer_size,
3076 alg, hash, hash_length,
3077 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003078 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003081}
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3084 psa_algorithm_t alg,
3085 const uint8_t *input,
3086 size_t input_length,
3087 uint8_t *signature,
3088 size_t signature_size,
3089 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003090{
3091 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003092 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003094}
3095
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003096psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003097 const psa_key_attributes_t *attributes,
3098 const uint8_t *key_buffer,
3099 size_t key_buffer_size,
3100 psa_algorithm_t alg,
3101 const uint8_t *input,
3102 size_t input_length,
3103 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003105{
3106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003109 size_t hash_length;
3110 uint8_t hash[PSA_HASH_MAX_SIZE];
3111
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003112 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 PSA_ALG_SIGN_GET_HASH(alg),
3114 input, input_length,
3115 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003118 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003120
3121 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 attributes, key_buffer, key_buffer_size,
3123 alg, hash, hash_length,
3124 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003125 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003128}
3129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3131 psa_algorithm_t alg,
3132 const uint8_t *input,
3133 size_t input_length,
3134 const uint8_t *signature,
3135 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003136{
3137 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003138 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003140}
3141
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003142psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003143 const psa_key_attributes_t *attributes,
3144 const uint8_t *key_buffer, size_t key_buffer_size,
3145 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003147{
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3149 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3150 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003151#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3153 return mbedtls_psa_rsa_sign_hash(
3154 attributes,
3155 key_buffer, key_buffer_size,
3156 alg, hash, hash_length,
3157 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003158#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3159 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 } else {
3161 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003162 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3164 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3167 return mbedtls_psa_ecdsa_sign_hash(
3168 attributes,
3169 key_buffer, key_buffer_size,
3170 alg, hash, hash_length,
3171 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003172#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3173 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 } else {
3175 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003176 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003177 }
Ronald Cron4501c982021-03-19 20:09:32 +01003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 (void) key_buffer;
3180 (void) key_buffer_size;
3181 (void) hash;
3182 (void) hash_length;
3183 (void) signature;
3184 (void) signature_size;
3185 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003188}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3191 psa_algorithm_t alg,
3192 const uint8_t *hash,
3193 size_t hash_length,
3194 uint8_t *signature,
3195 size_t signature_size,
3196 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003197{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003198 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003199 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003201}
3202
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003203psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003204 const psa_key_attributes_t *attributes,
3205 const uint8_t *key_buffer, size_t key_buffer_size,
3206 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003208{
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3210 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3211 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003212#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3214 return mbedtls_psa_rsa_verify_hash(
3215 attributes,
3216 key_buffer, key_buffer_size,
3217 alg, hash, hash_length,
3218 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003219#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3220 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 } else {
3222 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3225 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003226#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3228 return mbedtls_psa_ecdsa_verify_hash(
3229 attributes,
3230 key_buffer, key_buffer_size,
3231 alg, hash, hash_length,
3232 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003233#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3234 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 } else {
3236 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003237 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003238 }
Ronald Cron4501c982021-03-19 20:09:32 +01003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 (void) key_buffer;
3241 (void) key_buffer_size;
3242 (void) hash;
3243 (void) hash_length;
3244 (void) signature;
3245 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003248}
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3251 psa_algorithm_t alg,
3252 const uint8_t *hash,
3253 size_t hash_length,
3254 const uint8_t *signature,
3255 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003256{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003257 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003258 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003260}
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3263 psa_algorithm_t alg,
3264 const uint8_t *input,
3265 size_t input_length,
3266 const uint8_t *salt,
3267 size_t salt_length,
3268 uint8_t *output,
3269 size_t output_size,
3270 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003273 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003274 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003275 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003276
Darryl Green5cc689a2018-07-24 15:34:10 +01003277 (void) input;
3278 (void) input_length;
3279 (void) salt;
3280 (void) output;
3281 (void) output_size;
3282
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003283 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3286 return PSA_ERROR_INVALID_ARGUMENT;
3287 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003288
Ronald Cron5c522922020-11-14 16:35:34 +01003289 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3291 if (status != PSA_SUCCESS) {
3292 return status;
3293 }
3294 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3295 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003296 status = PSA_ERROR_INVALID_ARGUMENT;
3297 goto exit;
3298 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003299
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003300 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003302 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003303
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003304 status = psa_driver_wrapper_asymmetric_encrypt(
3305 &attributes, slot->key.data, slot->key.bytes,
3306 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003312}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3315 psa_algorithm_t alg,
3316 const uint8_t *input,
3317 size_t input_length,
3318 const uint8_t *salt,
3319 size_t salt_length,
3320 uint8_t *output,
3321 size_t output_size,
3322 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003325 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003326 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003327 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003328
Darryl Green5cc689a2018-07-24 15:34:10 +01003329 (void) input;
3330 (void) input_length;
3331 (void) salt;
3332 (void) output;
3333 (void) output_size;
3334
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003335 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003336
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3338 return PSA_ERROR_INVALID_ARGUMENT;
3339 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003340
Ronald Cron5c522922020-11-14 16:35:34 +01003341 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3343 if (status != PSA_SUCCESS) {
3344 return status;
3345 }
3346 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003347 status = PSA_ERROR_INVALID_ARGUMENT;
3348 goto exit;
3349 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003350
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003351 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003353 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003355 status = psa_driver_wrapper_asymmetric_decrypt(
3356 &attributes, slot->key.data, slot->key.bytes,
3357 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003359
3360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003364}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003365
Paul Elliott9fe12f62022-11-30 19:16:02 +00003366/****************************************************************/
3367/* Asymmetric interruptible cryptography */
3368/****************************************************************/
3369
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003370static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3371
Paul Elliott9fe12f62022-11-30 19:16:02 +00003372void psa_interruptible_set_max_ops(uint32_t max_ops)
3373{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003374 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003375}
3376
3377uint32_t psa_interruptible_get_max_ops(void)
3378{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003379 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380}
3381
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382uint32_t psa_sign_hash_get_num_ops(
3383 const psa_sign_hash_interruptible_operation_t *operation)
3384{
Paul Elliott296ede92022-12-15 17:00:30 +00003385 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003386}
3387
3388uint32_t psa_verify_hash_get_num_ops(
3389 const psa_verify_hash_interruptible_operation_t *operation)
3390{
Paul Elliott296ede92022-12-15 17:00:30 +00003391 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392}
3393
Paul Elliott59ad9452022-12-18 15:09:02 +00003394static psa_status_t psa_sign_hash_abort_internal(
3395 psa_sign_hash_interruptible_operation_t *operation)
3396{
3397 if (operation->id == 0) {
3398 /* The object has (apparently) been initialized but it is not (yet)
3399 * in use. It's ok to call abort on such an object, and there's
3400 * nothing to do. */
3401 return PSA_SUCCESS;
3402 }
3403
3404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3405
3406 status = psa_driver_wrapper_sign_hash_abort(operation);
3407
3408 operation->id = 0;
3409
Paul Elliotte6145dc2023-02-07 12:51:21 +00003410 /* Do not clear either the error_occurred or num_ops elements here as they
3411 * only want to be cleared by the application calling abort, not by abort
3412 * being called at completion of an operation. */
3413
Paul Elliott59ad9452022-12-18 15:09:02 +00003414 return status;
3415}
3416
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417psa_status_t psa_sign_hash_start(
3418 psa_sign_hash_interruptible_operation_t *operation,
3419 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3420 const uint8_t *hash, size_t hash_length)
3421{
3422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3423 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3424 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003425 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003426
Paul Elliottc9774412023-02-06 15:14:07 +00003427 /* Check that start has not been previously called, or operation has not
3428 * previously errored. */
3429 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003430 return PSA_ERROR_BAD_STATE;
3431 }
3432
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433 status = psa_sign_verify_check_alg(0, alg);
3434 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003435 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003436 return status;
3437 }
3438
3439 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3440 PSA_KEY_USAGE_SIGN_HASH,
3441 alg);
3442
3443 if (status != PSA_SUCCESS) {
3444 goto exit;
3445 }
3446
3447 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3448 status = PSA_ERROR_INVALID_ARGUMENT;
3449 goto exit;
3450 }
3451
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003452 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003453 .core = slot->attr
3454 };
3455
Paul Elliott296ede92022-12-15 17:00:30 +00003456 /* Ensure ops count gets reset, in case of operation re-use. */
3457 operation->num_ops = 0;
3458
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3460 slot->key.data,
3461 slot->key.bytes, alg,
3462 hash, hash_length);
3463exit:
3464
3465 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003466 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003467 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003468 }
3469
3470 unlock_status = psa_unlock_key_slot(slot);
3471
Paul Elliottc9774412023-02-06 15:14:07 +00003472 if (unlock_status != PSA_SUCCESS) {
3473 operation->error_occurred = 1;
3474 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003475
Paul Elliottc9774412023-02-06 15:14:07 +00003476 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477}
3478
3479
3480psa_status_t psa_sign_hash_complete(
3481 psa_sign_hash_interruptible_operation_t *operation,
3482 uint8_t *signature, size_t signature_size,
3483 size_t *signature_length)
3484{
3485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3486
3487 *signature_length = 0;
3488
Paul Elliottc9774412023-02-06 15:14:07 +00003489 /* Check that start has been called first, and that operation has not
3490 * previously errored. */
3491 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003492 status = PSA_ERROR_BAD_STATE;
3493 goto exit;
3494 }
3495
Paul Elliott096abc42023-02-03 18:33:23 +00003496 /* Immediately reject a zero-length signature buffer. This guarantees that
3497 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003498 if (signature_size == 0) {
3499 status = PSA_ERROR_BUFFER_TOO_SMALL;
3500 goto exit;
3501 }
3502
3503 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3504 signature_size,
3505 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506
Paul Elliott296ede92022-12-15 17:00:30 +00003507 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003508 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003509
Paul Elliottebe225c2023-02-10 14:32:53 +00003510exit:
3511
Paul Elliott59200a22023-02-21 15:07:40 +00003512 psa_wipe_tag_output_buffer(signature, status, signature_size,
3513 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003514
Paul Elliott53bb3122023-02-10 14:22:22 +00003515 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003516 if (status != PSA_SUCCESS) {
3517 operation->error_occurred = 1;
3518 }
3519
Paul Elliott59ad9452022-12-18 15:09:02 +00003520 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003521 }
3522
3523 return status;
3524}
3525
3526psa_status_t psa_sign_hash_abort(
3527 psa_sign_hash_interruptible_operation_t *operation)
3528{
Paul Elliott59ad9452022-12-18 15:09:02 +00003529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3530
3531 status = psa_sign_hash_abort_internal(operation);
3532
3533 /* We clear the number of ops done here, so that it is not cleared when
3534 * the operation fails or succeeds, only on manual abort. */
3535 operation->num_ops = 0;
3536
Paul Elliottc9774412023-02-06 15:14:07 +00003537 /* Likewise, failure state. */
3538 operation->error_occurred = 0;
3539
Paul Elliott59ad9452022-12-18 15:09:02 +00003540 return status;
3541}
3542
3543static psa_status_t psa_verify_hash_abort_internal(
3544 psa_verify_hash_interruptible_operation_t *operation)
3545{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 if (operation->id == 0) {
3547 /* The object has (apparently) been initialized but it is not (yet)
3548 * in use. It's ok to call abort on such an object, and there's
3549 * nothing to do. */
3550 return PSA_SUCCESS;
3551 }
3552
Paul Elliott59ad9452022-12-18 15:09:02 +00003553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3554
3555 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003556
3557 operation->id = 0;
3558
Paul Elliotte6145dc2023-02-07 12:51:21 +00003559 /* Do not clear either the error_occurred or num_ops elements here as they
3560 * only want to be cleared by the application calling abort, not by abort
3561 * being called at completion of an operation. */
3562
Paul Elliott59ad9452022-12-18 15:09:02 +00003563 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003564}
3565
3566psa_status_t psa_verify_hash_start(
3567 psa_verify_hash_interruptible_operation_t *operation,
3568 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3569 const uint8_t *hash, size_t hash_length,
3570 const uint8_t *signature, size_t signature_length)
3571{
3572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3573 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3574 psa_key_slot_t *slot;
3575
Paul Elliottc9774412023-02-06 15:14:07 +00003576 /* Check that start has not been previously called, or operation has not
3577 * previously errored. */
3578 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003579 return PSA_ERROR_BAD_STATE;
3580 }
3581
3582 status = psa_sign_verify_check_alg(0, alg);
3583 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003584 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585 return status;
3586 }
3587
3588 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3589 PSA_KEY_USAGE_VERIFY_HASH,
3590 alg);
3591
3592 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003593 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003594 return status;
3595 }
3596
3597 psa_key_attributes_t attributes = {
3598 .core = slot->attr
3599 };
3600
Paul Elliott296ede92022-12-15 17:00:30 +00003601 /* Ensure ops count gets reset, in case of operation re-use. */
3602 operation->num_ops = 0;
3603
Paul Elliott9fe12f62022-11-30 19:16:02 +00003604 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3605 slot->key.data,
3606 slot->key.bytes,
3607 alg, hash, hash_length,
3608 signature, signature_length);
3609
3610 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003611 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003612 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613 }
3614
3615 unlock_status = psa_unlock_key_slot(slot);
3616
Paul Elliottc9774412023-02-06 15:14:07 +00003617 if (unlock_status != PSA_SUCCESS) {
3618 operation->error_occurred = 1;
3619 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003620
Paul Elliottc9774412023-02-06 15:14:07 +00003621 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622}
3623
3624psa_status_t psa_verify_hash_complete(
3625 psa_verify_hash_interruptible_operation_t *operation)
3626{
3627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3628
Paul Elliottc9774412023-02-06 15:14:07 +00003629 /* Check that start has been called first, and that operation has not
3630 * previously errored. */
3631 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003632 status = PSA_ERROR_BAD_STATE;
3633 goto exit;
3634 }
3635
3636 status = psa_driver_wrapper_verify_hash_complete(operation);
3637
Paul Elliott296ede92022-12-15 17:00:30 +00003638 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003639 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003640 operation);
3641
Paul Elliottebe225c2023-02-10 14:32:53 +00003642exit:
3643
Paul Elliott9fe12f62022-11-30 19:16:02 +00003644 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003645 if (status != PSA_SUCCESS) {
3646 operation->error_occurred = 1;
3647 }
3648
Paul Elliott59ad9452022-12-18 15:09:02 +00003649 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003650 }
3651
3652 return status;
3653}
3654
3655psa_status_t psa_verify_hash_abort(
3656 psa_verify_hash_interruptible_operation_t *operation)
3657{
Paul Elliott59ad9452022-12-18 15:09:02 +00003658 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003659
Paul Elliott59ad9452022-12-18 15:09:02 +00003660 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003661
Paul Elliott59ad9452022-12-18 15:09:02 +00003662 /* We clear the number of ops done here, so that it is not cleared when
3663 * the operation fails or succeeds, only on manual abort. */
3664 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003665
Paul Elliottc9774412023-02-06 15:14:07 +00003666 /* Likewise, failure state. */
3667 operation->error_occurred = 0;
3668
Paul Elliott59ad9452022-12-18 15:09:02 +00003669 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003670}
3671
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672/****************************************************************/
3673/* Asymmetric interruptible cryptography internal */
3674/* implementations */
3675/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003676
Paul Elliott588f8ed2022-12-02 18:10:26 +00003677void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3678{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003679
Paul Elliott588f8ed2022-12-02 18:10:26 +00003680#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3681 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3682 defined(MBEDTLS_ECP_RESTARTABLE)
3683
3684 /* Internal implementation uses zero to indicate infinite number max ops,
3685 * therefore avoid this value, and set to minimum possible. */
3686 if (max_ops == 0) {
3687 max_ops = 1;
3688 }
3689
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003691#else
3692 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3694 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3695 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3696}
3697
Paul Elliott588f8ed2022-12-02 18:10:26 +00003698uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003699 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003700{
3701#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3702 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3703 defined(MBEDTLS_ECP_RESTARTABLE)
3704
Paul Elliott93d9ca82023-02-15 18:14:21 +00003705 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706#else
3707 (void) operation;
3708 return 0;
3709#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3710 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3711 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3712}
3713
3714uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003715 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716{
3717 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3718 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3719 defined(MBEDTLS_ECP_RESTARTABLE)
3720
Paul Elliott93d9ca82023-02-15 18:14:21 +00003721 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722#else
3723 (void) operation;
3724 return 0;
3725#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3726 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3727 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3728}
3729
3730psa_status_t mbedtls_psa_sign_hash_start(
3731 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3732 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3733 size_t key_buffer_size, psa_algorithm_t alg,
3734 const uint8_t *hash, size_t hash_length)
3735{
3736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003737 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott068fe072023-01-16 13:59:15 +00003739 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3740 return PSA_ERROR_NOT_SUPPORTED;
3741 }
3742
3743 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003744 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003745 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003746
3747#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003748 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3749 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750
Paul Elliotta1c94092023-02-15 16:38:04 +00003751 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3752
Paul Elliott93d9ca82023-02-15 18:14:21 +00003753 /* Ensure num_ops is zero'ed in case of context re-use. */
3754 operation->num_ops = 0;
3755
Paul Elliott068fe072023-01-16 13:59:15 +00003756 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3757 attributes->core.bits,
3758 key_buffer,
3759 key_buffer_size,
3760 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003761
Paul Elliott068fe072023-01-16 13:59:15 +00003762 if (status != PSA_SUCCESS) {
3763 return status;
3764 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
Paul Elliott1bc59df2023-02-05 13:41:57 +00003766 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003767 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768
Paul Elliott068fe072023-01-16 13:59:15 +00003769 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003770 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003771 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772
Paul Elliott0290a762023-02-09 14:30:24 +00003773 /* We only need to store the same length of hash as the private key size
3774 * here, it would be truncated by the internal implementation anyway. */
3775 required_hash_length = (hash_length < operation->coordinate_bytes ?
3776 hash_length : operation->coordinate_bytes);
3777
Paul Elliottba70ad42023-02-15 18:23:53 +00003778 if (required_hash_length > sizeof(operation->hash)) {
3779 /* Shouldn't happen, but better safe than sorry. */
3780 return PSA_ERROR_CORRUPTION_DETECTED;
3781 }
3782
Paul Elliott0290a762023-02-09 14:30:24 +00003783 memcpy(operation->hash, hash, required_hash_length);
3784 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003785
3786 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
3788#else
Paul Elliott068fe072023-01-16 13:59:15 +00003789 (void) operation;
3790 (void) key_buffer;
3791 (void) key_buffer_size;
3792 (void) alg;
3793 (void) hash;
3794 (void) hash_length;
3795 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003796 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003797
Paul Elliott068fe072023-01-16 13:59:15 +00003798 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3800 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3801 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802}
3803
3804psa_status_t mbedtls_psa_sign_hash_complete(
3805 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3806 uint8_t *signature, size_t signature_size,
3807 size_t *signature_length)
3808{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003809#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3810 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3811 defined(MBEDTLS_ECP_RESTARTABLE)
3812
Paul Elliotta1c94092023-02-15 16:38:04 +00003813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003814 mbedtls_mpi r;
3815 mbedtls_mpi s;
3816
Paul Elliott46845252023-02-03 14:59:11 +00003817 mbedtls_mpi_init(&r);
3818 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003820 /* Ensure max_ops is set to the current value (or default). */
3821 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3822
Paul Elliotta1c94092023-02-15 16:38:04 +00003823 if (signature_size < 2 * operation->coordinate_bytes) {
3824 status = PSA_ERROR_BUFFER_TOO_SMALL;
3825 goto exit;
3826 }
3827
Paul Elliott588f8ed2022-12-02 18:10:26 +00003828 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003829
Paul Elliott588f8ed2022-12-02 18:10:26 +00003830#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3831 status = mbedtls_to_psa_error(
3832 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003833 &r,
3834 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003835 &operation->ctx->d,
3836 operation->hash,
3837 operation->hash_length,
3838 operation->md_alg,
3839 mbedtls_psa_get_random,
3840 MBEDTLS_PSA_RANDOM_STATE,
3841 &operation->restart_ctx));
3842#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003843 status = PSA_ERROR_NOT_SUPPORTED;
3844 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3846 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847 status = mbedtls_to_psa_error(
3848 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003849 &r,
3850 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851 &operation->ctx->d,
3852 operation->hash,
3853 operation->hash_length,
3854 mbedtls_psa_get_random,
3855 MBEDTLS_PSA_RANDOM_STATE,
3856 mbedtls_psa_get_random,
3857 MBEDTLS_PSA_RANDOM_STATE,
3858 &operation->restart_ctx));
3859 }
3860
Paul Elliottf8e5b562023-02-19 18:43:45 +00003861 /* Hide the fact that the restart context only holds a delta of number of
3862 * ops done during the last operation, not an absolute value. */
3863 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3864
Paul Elliott724bd252023-02-08 12:35:08 +00003865 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003867 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003868 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003869 operation->coordinate_bytes)
3870 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003871
3872 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003873 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003874 }
3875
3876 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003877 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003878 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003879 operation->coordinate_bytes,
3880 operation->coordinate_bytes)
3881 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
3883 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003884 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885 }
3886
Paul Elliott1bc59df2023-02-05 13:41:57 +00003887 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888
Paul Elliott724bd252023-02-08 12:35:08 +00003889 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890 }
Paul Elliott724bd252023-02-08 12:35:08 +00003891
3892exit:
3893
3894 mbedtls_mpi_free(&r);
3895 mbedtls_mpi_free(&s);
3896 return status;
3897
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898 #else
3899
3900 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901 (void) signature;
3902 (void) signature_size;
3903 (void) signature_length;
3904
3905 return PSA_ERROR_NOT_SUPPORTED;
3906
3907#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3908 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3909 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3910}
3911
3912psa_status_t mbedtls_psa_sign_hash_abort(
3913 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3914{
3915
3916#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3917 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3918 defined(MBEDTLS_ECP_RESTARTABLE)
3919
3920 if (operation->ctx) {
3921 mbedtls_ecdsa_free(operation->ctx);
3922 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003923 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003924 }
3925
3926 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3927
Paul Elliott93d9ca82023-02-15 18:14:21 +00003928 operation->num_ops = 0;
3929
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930 return PSA_SUCCESS;
3931
3932#else
3933
3934 (void) operation;
3935
3936 return PSA_ERROR_NOT_SUPPORTED;
3937
3938#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3939 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3940 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3941}
3942
3943psa_status_t mbedtls_psa_verify_hash_start(
3944 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3945 const psa_key_attributes_t *attributes,
3946 const uint8_t *key_buffer, size_t key_buffer_size,
3947 psa_algorithm_t alg,
3948 const uint8_t *hash, size_t hash_length,
3949 const uint8_t *signature, size_t signature_length)
3950{
3951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003952 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003953 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954
Paul Elliott068fe072023-01-16 13:59:15 +00003955 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3956 return PSA_ERROR_NOT_SUPPORTED;
3957 }
3958
3959 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003960 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003961 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
3963#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003964 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3965 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliotta1c94092023-02-15 16:38:04 +00003967 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3968 mbedtls_mpi_init(&operation->r);
3969 mbedtls_mpi_init(&operation->s);
3970
Paul Elliott93d9ca82023-02-15 18:14:21 +00003971 /* Ensure num_ops is zero'ed in case of context re-use. */
3972 operation->num_ops = 0;
3973
Paul Elliott068fe072023-01-16 13:59:15 +00003974 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3975 attributes->core.bits,
3976 key_buffer,
3977 key_buffer_size,
3978 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003979
Paul Elliott068fe072023-01-16 13:59:15 +00003980 if (status != PSA_SUCCESS) {
3981 return status;
3982 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003983
Paul Elliottc569fc22023-02-10 13:02:54 +00003984 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003985
Paul Elliott1bc59df2023-02-05 13:41:57 +00003986 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003987 return PSA_ERROR_INVALID_SIGNATURE;
3988 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003989
Paul Elliott068fe072023-01-16 13:59:15 +00003990 status = mbedtls_to_psa_error(
3991 mbedtls_mpi_read_binary(&operation->r,
3992 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003993 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003994
Paul Elliott068fe072023-01-16 13:59:15 +00003995 if (status != PSA_SUCCESS) {
3996 return status;
3997 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003998
Paul Elliott068fe072023-01-16 13:59:15 +00003999 status = mbedtls_to_psa_error(
4000 mbedtls_mpi_read_binary(&operation->s,
4001 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00004002 coordinate_bytes,
4003 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00004004
Paul Elliott068fe072023-01-16 13:59:15 +00004005 if (status != PSA_SUCCESS) {
4006 return status;
4007 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004008
Paul Elliott2c9843f2023-02-15 17:32:42 +00004009 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00004010
Paul Elliott2c9843f2023-02-15 17:32:42 +00004011 if (status != PSA_SUCCESS) {
4012 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00004013 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00004014
Paul Elliott0290a762023-02-09 14:30:24 +00004015 /* We only need to store the same length of hash as the private key size
4016 * here, it would be truncated by the internal implementation anyway. */
4017 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
4018 coordinate_bytes);
4019
Paul Elliottba70ad42023-02-15 18:23:53 +00004020 if (required_hash_length > sizeof(operation->hash)) {
4021 /* Shouldn't happen, but better safe than sorry. */
4022 return PSA_ERROR_CORRUPTION_DETECTED;
4023 }
4024
Paul Elliott0290a762023-02-09 14:30:24 +00004025 memcpy(operation->hash, hash, required_hash_length);
4026 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00004027
4028 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004029#else
Paul Elliott068fe072023-01-16 13:59:15 +00004030 (void) operation;
4031 (void) key_buffer;
4032 (void) key_buffer_size;
4033 (void) alg;
4034 (void) hash;
4035 (void) hash_length;
4036 (void) signature;
4037 (void) signature_length;
4038 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004039 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004040 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004041
Paul Elliott068fe072023-01-16 13:59:15 +00004042 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004043#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4044 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4045 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004046}
4047
4048psa_status_t mbedtls_psa_verify_hash_complete(
4049 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4050{
4051
4052#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4053 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4054 defined(MBEDTLS_ECP_RESTARTABLE)
4055
Paul Elliottf8e5b562023-02-19 18:43:45 +00004056 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4057
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004058 /* Ensure max_ops is set to the current value (or default). */
4059 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4060
Paul Elliottf8e5b562023-02-19 18:43:45 +00004061 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004062 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4063 operation->hash,
4064 operation->hash_length,
4065 &operation->ctx->Q,
4066 &operation->r,
4067 &operation->s,
4068 &operation->restart_ctx));
4069
Paul Elliottf8e5b562023-02-19 18:43:45 +00004070 /* Hide the fact that the restart context only holds a delta of number of
4071 * ops done during the last operation, not an absolute value. */
4072 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4073
4074 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004075#else
4076 (void) operation;
4077
4078 return PSA_ERROR_NOT_SUPPORTED;
4079
4080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4081 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4082 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4083}
4084
4085psa_status_t mbedtls_psa_verify_hash_abort(
4086 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4087{
4088
4089#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4090 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4091 defined(MBEDTLS_ECP_RESTARTABLE)
4092
4093 if (operation->ctx) {
4094 mbedtls_ecdsa_free(operation->ctx);
4095 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004096 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004097 }
4098
4099 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4100
Paul Elliott93d9ca82023-02-15 18:14:21 +00004101 operation->num_ops = 0;
4102
Paul Elliott588f8ed2022-12-02 18:10:26 +00004103 mbedtls_mpi_free(&operation->r);
4104 mbedtls_mpi_free(&operation->s);
4105
4106 return PSA_SUCCESS;
4107
4108#else
4109 (void) operation;
4110
4111 return PSA_ERROR_NOT_SUPPORTED;
4112
4113#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4114 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4115 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4116}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004117
mohammad1603503973b2018-03-12 15:59:30 +02004118/****************************************************************/
4119/* Symmetric cryptography */
4120/****************************************************************/
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4123 mbedtls_svc_key_id_t key,
4124 psa_algorithm_t alg,
4125 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004126{
4127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4128 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004129 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4131 PSA_KEY_USAGE_ENCRYPT :
4132 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004133 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004134
4135 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004137 status = PSA_ERROR_BAD_STATE;
4138 goto exit;
4139 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004142 status = PSA_ERROR_INVALID_ARGUMENT;
4143 goto exit;
4144 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4147 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004150
Ronald Cron49fafa92021-03-10 08:34:23 +01004151 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004152 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004153 * so we only set it (in the driver wrapper) after resources have been
4154 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004155 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004157 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004159 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 }
4161 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004162
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004163 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004165 };
4166
Ronald Cronab99ac22020-10-01 16:38:28 +02004167 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 if (cipher_operation == MBEDTLS_ENCRYPT) {
4169 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4170 &attributes,
4171 slot->key.data,
4172 slot->key.bytes,
4173 alg);
4174 } else {
4175 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4176 &attributes,
4177 slot->key.data,
4178 slot->key.bytes,
4179 alg);
4180 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004181
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004182exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (status != PSA_SUCCESS) {
4184 psa_cipher_abort(operation);
4185 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004186
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004190}
4191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192psa_status_t psa_cipher_encrypt_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_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004197}
4198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4200 mbedtls_svc_key_id_t key,
4201 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004202{
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004204}
4205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4207 uint8_t *iv,
4208 size_t iv_size,
4209 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004210{
Ronald Cron6d051732020-10-01 14:10:20 +02004211 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004212 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004213 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004216 status = PSA_ERROR_BAD_STATE;
4217 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004218 }
4219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004221 status = PSA_ERROR_BAD_STATE;
4222 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004223 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004224
Ronald Cron2fb90522021-07-05 12:31:44 +02004225 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004227 status = PSA_ERROR_BUFFER_TOO_SMALL;
4228 goto exit;
4229 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004232 status = PSA_ERROR_GENERIC_ERROR;
4233 goto exit;
4234 }
4235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 status = psa_generate_random(local_iv, default_iv_length);
4237 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 }
Ronald Cron5618a392021-03-26 09:52:26 +01004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 status = psa_driver_wrapper_cipher_set_iv(operation,
4242 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004243
4244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 if (status == PSA_SUCCESS) {
4246 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004247 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004248 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004250 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004252 }
Ronald Cron6d051732020-10-01 14:10:20 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004255}
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4258 const uint8_t *iv,
4259 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004260{
Ronald Cron6d051732020-10-01 14:10:20 +02004261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264 status = PSA_ERROR_BAD_STATE;
4265 goto exit;
4266 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269 status = PSA_ERROR_BAD_STATE;
4270 goto exit;
4271 }
Ronald Crona0d68172021-03-26 10:15:08 +01004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004274 status = PSA_ERROR_INVALID_ARGUMENT;
4275 goto exit;
4276 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 status = psa_driver_wrapper_cipher_set_iv(operation,
4279 iv,
4280 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004281
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004284 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 } else {
4286 psa_cipher_abort(operation);
4287 }
4288 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004289}
4290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4292 const uint8_t *input,
4293 size_t input_length,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004297{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004298 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301 status = PSA_ERROR_BAD_STATE;
4302 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004303 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004306 status = PSA_ERROR_BAD_STATE;
4307 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004308 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_driver_wrapper_cipher_update(operation,
4311 input,
4312 input_length,
4313 output,
4314 output_size,
4315 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004316
4317exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (status != PSA_SUCCESS) {
4319 psa_cipher_abort(operation);
4320 }
Ronald Cron6d051732020-10-01 14:10:20 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004323}
4324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4326 uint8_t *output,
4327 size_t output_size,
4328 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004329{
David Saadab4ecc272019-02-14 13:48:10 +02004330 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004333 status = PSA_ERROR_BAD_STATE;
4334 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004335 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004336
Gilles Peskine449bd832023-01-11 14:50:10 +01004337 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004338 status = PSA_ERROR_BAD_STATE;
4339 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004340 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 status = psa_driver_wrapper_cipher_finish(operation,
4343 output,
4344 output_size,
4345 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004346
4347exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (status == PSA_SUCCESS) {
4349 return psa_cipher_abort(operation);
4350 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004351 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004355 }
mohammad1603503973b2018-03-12 15:59:30 +02004356}
4357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004359{
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004361 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004362 * in use. It's ok to call abort on such an object, and there's
4363 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004365 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004368
Ronald Cron49fafa92021-03-10 08:34:23 +01004369 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004370 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004371 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004374}
4375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4377 psa_algorithm_t alg,
4378 const uint8_t *input,
4379 size_t input_length,
4380 uint8_t *output,
4381 size_t output_size,
4382 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004383{
4384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004385 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004386 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004387 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4388 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004389 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004390
David Horstmanne9a88ab2023-11-29 17:07:13 +00004391 SWAP_FOR_LOCAL_INPUT(input, input_length);
4392 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannc6977b42023-11-27 17:43:05 +00004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004395 status = PSA_ERROR_INVALID_ARGUMENT;
4396 goto exit;
4397 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4400 PSA_KEY_USAGE_ENCRYPT,
4401 alg);
4402 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004403 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004405
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004406 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004408 };
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4411 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004412 status = PSA_ERROR_GENERIC_ERROR;
4413 goto exit;
4414 }
4415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if (default_iv_length > 0) {
4417 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004418 status = PSA_ERROR_BUFFER_TOO_SMALL;
4419 goto exit;
4420 }
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 status = psa_generate_random(local_iv, default_iv_length);
4423 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004424 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004426 }
4427
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004428 status = psa_driver_wrapper_cipher_encrypt(
4429 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004430 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004431 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004433
4434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 unlock_status = psa_unlock_key_slot(slot);
4436 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004437 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004438 }
Ronald Cron23919522021-07-08 19:00:07 +02004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 if (status == PSA_SUCCESS) {
4441 if (default_iv_length > 0) {
4442 memcpy(output, local_iv, default_iv_length);
4443 }
4444 *output_length += default_iv_length;
4445 } else {
4446 *output_length = 0;
4447 }
4448
David Horstmanne9a88ab2023-11-29 17:07:13 +00004449 FREE_LOCAL_INPUT(input);
4450 FREE_LOCAL_OUTPUT(output);
David Horstmannc6977b42023-11-27 17:43:05 +00004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004453}
4454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4456 psa_algorithm_t alg,
4457 const uint8_t *input,
4458 size_t input_length,
4459 uint8_t *output,
4460 size_t output_size,
4461 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004462{
4463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004464 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004465 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004466 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004469 status = PSA_ERROR_INVALID_ARGUMENT;
4470 goto exit;
4471 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004472
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4474 PSA_KEY_USAGE_DECRYPT,
4475 alg);
4476 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004477 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004479
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004480 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004482 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4485 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004486 status = PSA_ERROR_INVALID_ARGUMENT;
4487 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004489 status = PSA_ERROR_INVALID_ARGUMENT;
4490 goto exit;
4491 }
4492
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004493 status = psa_driver_wrapper_cipher_decrypt(
4494 &attributes, slot->key.data, slot->key.bytes,
4495 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004497
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004498exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 unlock_status = psa_unlock_key_slot(slot);
4500 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004501 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004505 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 }
Ronald Cron23919522021-07-08 19:00:07 +02004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004509}
4510
4511
mohammad16035955c982018-04-26 00:53:03 +03004512/****************************************************************/
4513/* AEAD */
4514/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004515
Paul Elliottbaff51c2021-09-28 17:44:45 +01004516/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004517static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004518{
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004520}
4521
4522/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004523static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4524 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004525{
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004529#if defined(PSA_WANT_ALG_GCM)
4530 case PSA_ALG_GCM:
4531 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 * arbitrarily large nonces. Please note that we do not generally
4533 * recommend the usage of nonces of greater length than
4534 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4535 * size, which can then lead to collisions if you encrypt a very
4536 * large number of messages.*/
4537 if (nonce_length != 0) {
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_GCM */
4542#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004543 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (nonce_length >= 7 && nonce_length <= 13) {
4545 return PSA_SUCCESS;
4546 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004547 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004548#endif /* PSA_WANT_ALG_CCM */
4549#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004550 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 if (nonce_length == 12) {
4552 return PSA_SUCCESS;
4553 } else if (nonce_length == 8) {
4554 return PSA_ERROR_NOT_SUPPORTED;
4555 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004556 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004557#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004558 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004559 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004561 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004564}
4565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004567{
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4569 return PSA_ERROR_INVALID_ARGUMENT;
4570 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004573}
4574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4576 psa_algorithm_t alg,
4577 const uint8_t *nonce,
4578 size_t nonce_length,
4579 const uint8_t *additional_data,
4580 size_t additional_data_length,
4581 const uint8_t *plaintext,
4582 size_t plaintext_length,
4583 uint8_t *ciphertext,
4584 size_t ciphertext_size,
4585 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004586{
Ronald Cron215633c2021-03-16 17:15:37 +01004587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4588 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004589
mohammad1603f08a5502018-06-03 15:05:47 +03004590 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 status = psa_aead_check_algorithm(alg);
4593 if (status != PSA_SUCCESS) {
4594 return status;
4595 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004596
Ronald Cron9a986162021-03-26 12:40:07 +01004597 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4599 if (status != PSA_SUCCESS) {
4600 return status;
4601 }
mohammad16035955c982018-04-26 00:53:03 +03004602
Ronald Cron215633c2021-03-16 17:15:37 +01004603 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004605 };
mohammad16035955c982018-04-26 00:53:03 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 status = psa_aead_check_nonce_length(alg, nonce_length);
4608 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004609 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004611
Ronald Cronde822812021-03-17 16:08:20 +01004612 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004613 &attributes, slot->key.data, slot->key.bytes,
4614 alg,
4615 nonce, nonce_length,
4616 additional_data, additional_data_length,
4617 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4621 memset(ciphertext, 0, ciphertext_size);
4622 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004623
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004628}
4629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4631 psa_algorithm_t alg,
4632 const uint8_t *nonce,
4633 size_t nonce_length,
4634 const uint8_t *additional_data,
4635 size_t additional_data_length,
4636 const uint8_t *ciphertext,
4637 size_t ciphertext_length,
4638 uint8_t *plaintext,
4639 size_t plaintext_size,
4640 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004641{
Ronald Cron215633c2021-03-16 17:15:37 +01004642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4643 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004644
Gilles Peskineee652a32018-06-01 19:23:52 +02004645 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 status = psa_aead_check_algorithm(alg);
4648 if (status != PSA_SUCCESS) {
4649 return status;
4650 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004651
Ronald Cron9a986162021-03-26 12:40:07 +01004652 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4654 if (status != PSA_SUCCESS) {
4655 return status;
4656 }
mohammad16035955c982018-04-26 00:53:03 +03004657
Ronald Cron215633c2021-03-16 17:15:37 +01004658 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004660 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 status = psa_aead_check_nonce_length(alg, nonce_length);
4663 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004666
Ronald Cronde822812021-03-17 16:08:20 +01004667 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004668 &attributes, slot->key.data, slot->key.bytes,
4669 alg,
4670 nonce, nonce_length,
4671 additional_data, additional_data_length,
4672 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (status != PSA_SUCCESS && plaintext_size != 0) {
4676 memset(plaintext, 0, plaintext_size);
4677 }
mohammad160360a64d02018-06-03 17:20:42 +03004678
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004679exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 return status;
mohammad16035955c982018-04-26 00:53:03 +03004683}
4684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4686{
4687 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004690#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004692 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4694 return PSA_ERROR_INVALID_ARGUMENT;
4695 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004696 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004697#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004698
Przemek Stekiel86679c72022-10-06 17:06:56 +02004699#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004701 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4703 return PSA_ERROR_INVALID_ARGUMENT;
4704 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004705 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004706#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004707
Przemek Stekiel86679c72022-10-06 17:06:56 +02004708#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004710 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (tag_len != 16) {
4712 return PSA_ERROR_INVALID_ARGUMENT;
4713 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004714 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004715#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004716
4717 default:
4718 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004720 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004722}
4723
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004724/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004725static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4726 int is_encrypt,
4727 mbedtls_svc_key_id_t key,
4728 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004729{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4731 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4732 psa_key_slot_t *slot = NULL;
4733 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004734 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 status = psa_aead_check_algorithm(alg);
4737 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004738 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004742 status = PSA_ERROR_BAD_STATE;
4743 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004744 }
4745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (operation->nonce_set || operation->lengths_set ||
4747 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748 status = PSA_ERROR_BAD_STATE;
4749 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004750 }
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4759 alg);
4760 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004761 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004763
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004764 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004765 .core = slot->attr
4766 };
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (is_encrypt) {
4773 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4774 &attributes,
4775 slot->key.data,
4776 slot->key.bytes,
4777 alg);
4778 } else {
4779 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4780 &attributes,
4781 slot->key.data,
4782 slot->key.bytes,
4783 alg);
4784 }
4785 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004790
4791exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004795 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004797 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 } else {
4799 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004800 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004803}
4804
Paul Elliott302ff6b2021-04-20 18:10:30 +01004805/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004806psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4807 mbedtls_svc_key_id_t key,
4808 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809{
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004811}
4812
4813/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4815 mbedtls_svc_key_id_t key,
4816 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817{
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819}
4820
4821/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004822psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4823 uint8_t *nonce,
4824 size_t nonce_size,
4825 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826{
4827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004828 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004829 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004830
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004831 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004834 status = PSA_ERROR_BAD_STATE;
4835 goto exit;
4836 }
4837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004839 status = PSA_ERROR_BAD_STATE;
4840 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004841 }
4842
Paul Elliotte193ea82021-10-01 13:00:16 +01004843 /* For CCM, this size may not be correct according to the PSA
4844 * specification. The PSA Crypto 1.0.1 specification states:
4845 *
4846 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4847 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4848 *
4849 * However this restriction that L has to be the smallest integer is not
4850 * applied in practice, and it is not implementable here since the
4851 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4853 operation->alg);
4854 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004855 status = PSA_ERROR_BUFFER_TOO_SMALL;
4856 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004857 }
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 status = psa_generate_random(local_nonce, required_nonce_size);
4860 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004861 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004865
Paul Elliott39dc6b82021-05-11 19:16:09 +01004866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (status == PSA_SUCCESS) {
4868 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004869 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 } else {
4871 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004872 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875}
4876
4877/* Set the nonce for a multipart authenticated encryption or decryption
4878 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004879psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4880 const uint8_t *nonce,
4881 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004882{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004883 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004886 status = PSA_ERROR_BAD_STATE;
4887 goto exit;
4888 }
4889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004891 status = PSA_ERROR_BAD_STATE;
4892 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004893 }
4894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4896 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004897 status = PSA_ERROR_INVALID_ARGUMENT;
4898 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004899 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4902 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004903
Paul Elliott39dc6b82021-05-11 19:16:09 +01004904exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004906 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 } else {
4908 psa_aead_abort(operation);
4909 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004912}
4913
4914/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004915psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4916 size_t ad_length,
4917 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004922 status = PSA_ERROR_BAD_STATE;
4923 goto exit;
4924 }
4925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 if (operation->lengths_set || operation->ad_started ||
4927 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928 status = PSA_ERROR_BAD_STATE;
4929 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930 }
4931
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004933#if defined(PSA_WANT_ALG_GCM)
4934 case PSA_ALG_GCM:
4935 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 * bits. Without the guard this code will generate warnings on 32bit
4937 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004938#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (((uint64_t) ad_length) >> 61 != 0 ||
4940 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004941 status = PSA_ERROR_INVALID_ARGUMENT;
4942 goto exit;
4943 }
Paul Elliott325d3742021-09-27 17:56:28 +01004944#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004945 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004946#endif /* PSA_WANT_ALG_GCM */
4947#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004948 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004950 status = PSA_ERROR_INVALID_ARGUMENT;
4951 goto exit;
4952 }
4953 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004954#endif /* PSA_WANT_ALG_CCM */
4955#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004956 case PSA_ALG_CHACHA20_POLY1305:
4957 /* No length restrictions for ChaChaPoly. */
4958 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004959#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004960 default:
4961 break;
4962 }
Paul Elliott325d3742021-09-27 17:56:28 +01004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4965 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004966
Paul Elliott39dc6b82021-05-11 19:16:09 +01004967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004969 operation->ad_remaining = ad_length;
4970 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 } else {
4973 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004974 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004975
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004977}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004978
4979/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004980psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4981 const uint8_t *input,
4982 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004983{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004987 status = PSA_ERROR_BAD_STATE;
4988 goto exit;
4989 }
4990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004992 status = PSA_ERROR_BAD_STATE;
4993 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994 }
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->lengths_set) {
4997 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004998 status = PSA_ERROR_INVALID_ARGUMENT;
4999 goto exit;
5000 }
5001
5002 operation->ad_remaining -= input_length;
5003 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005004#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005006 status = PSA_ERROR_BAD_STATE;
5007 goto exit;
5008 }
5009#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 status = psa_driver_wrapper_aead_update_ad(operation, input,
5012 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005013
Paul Elliott39dc6b82021-05-11 19:16:09 +01005014exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005016 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 } else {
5018 psa_aead_abort(operation);
5019 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005022}
5023
5024/* Encrypt or decrypt a message fragment in an active multipart AEAD
5025 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005026psa_status_t psa_aead_update(psa_aead_operation_t *operation,
5027 const uint8_t *input,
5028 size_t input_length,
5029 uint8_t *output,
5030 size_t output_size,
5031 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005032{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005033 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005034
5035 *output_length = 0;
5036
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005038 status = PSA_ERROR_BAD_STATE;
5039 goto exit;
5040 }
5041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005043 status = PSA_ERROR_BAD_STATE;
5044 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005045 }
5046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005048 /* Additional data length was supplied, but not all the additional
5049 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005051 status = PSA_ERROR_INVALID_ARGUMENT;
5052 goto exit;
5053 }
5054
5055 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005057 status = PSA_ERROR_INVALID_ARGUMENT;
5058 goto exit;
5059 }
5060
5061 operation->body_remaining -= input_length;
5062 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005063#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005065 status = PSA_ERROR_BAD_STATE;
5066 goto exit;
5067 }
5068#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5071 output, output_size,
5072 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005073
Paul Elliott39dc6b82021-05-11 19:16:09 +01005074exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005076 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 } else {
5078 psa_aead_abort(operation);
5079 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005082}
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005085{
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (operation->id == 0 || !operation->nonce_set) {
5087 return PSA_ERROR_BAD_STATE;
5088 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5091 operation->body_remaining != 0)) {
5092 return PSA_ERROR_INVALID_ARGUMENT;
5093 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005094
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005096}
5097
Paul Elliott302ff6b2021-04-20 18:10:30 +01005098/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005099psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5100 uint8_t *ciphertext,
5101 size_t ciphertext_size,
5102 size_t *ciphertext_length,
5103 uint8_t *tag,
5104 size_t tag_size,
5105 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005106{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5108
Paul Elliott302ff6b2021-04-20 18:10:30 +01005109 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005110 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 status = psa_aead_final_checks(operation);
5113 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005118 status = PSA_ERROR_BAD_STATE;
5119 goto exit;
5120 }
5121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5123 ciphertext_size,
5124 ciphertext_length,
5125 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005126
Paul Elliott39dc6b82021-05-11 19:16:09 +01005127exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005128
5129
Paul Elliottf47b0952021-05-21 18:02:33 +01005130 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005131 * the zero tag size, make sure the tag is set to something implausible.
5132 * Even if the operation succeeds, make sure we clear the rest of the
5133 * buffer to prevent potential leakage of anything previously placed in
5134 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005135 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140}
5141
5142/* Finish authenticating and decrypting a message in a multipart AEAD
5143 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005144psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5145 uint8_t *plaintext,
5146 size_t plaintext_size,
5147 size_t *plaintext_length,
5148 const uint8_t *tag,
5149 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005150{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005151 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5152
Paul Elliott302ff6b2021-04-20 18:10:30 +01005153 *plaintext_length = 0;
5154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 status = psa_aead_final_checks(operation);
5156 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005161 status = PSA_ERROR_BAD_STATE;
5162 goto exit;
5163 }
5164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5166 plaintext_size,
5167 plaintext_length,
5168 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005169
5170exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005174}
5175
5176/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005178{
5179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005182 /* The object has (apparently) been initialized but it is not (yet)
5183 * in use. It's ok to call abort on such an object, and there's
5184 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005186 }
5187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005193}
5194
Gilles Peskinee59236f2018-01-27 23:32:46 +01005195/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005196/* Generators */
5197/****************************************************************/
5198
Przemek Stekiel69c46792022-06-10 12:59:51 +02005199#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005200 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005201 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305202 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305203 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005204#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005205#endif /* At least one builtin KDF */
5206
Przemek Stekiel69c46792022-06-10 12:59:51 +02005207#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005208 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5209 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5210static psa_status_t psa_key_derivation_start_hmac(
5211 psa_mac_operation_t *operation,
5212 psa_algorithm_t hash_alg,
5213 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005215{
5216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5219 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5220 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005221
Steven Cooreman72f736a2021-05-07 14:14:37 +02005222 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 status = psa_driver_wrapper_mac_sign_setup(operation,
5226 &attributes,
5227 hmac_key, hmac_key_length,
5228 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 psa_reset_key_attributes(&attributes);
5231 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005232}
5233#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005234
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235#define HKDF_STATE_INIT 0 /* no input yet */
5236#define HKDF_STATE_STARTED 1 /* got salt */
5237#define HKDF_STATE_KEYED 2 /* got key */
5238#define HKDF_STATE_OUTPUT 3 /* output started */
5239
Gilles Peskinecbe66502019-05-16 16:59:18 +02005240static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005242{
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5244 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5245 } else {
5246 return operation->alg;
5247 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005248}
5249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005251{
5252 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5254 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005255 /* The object has (apparently) been initialized but it is not
5256 * in use. It's ok to call abort on such an object, and there's
5257 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005259#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5261 mbedtls_free(operation->ctx.hkdf.info);
5262 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5263 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005264#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005265#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5266 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5268 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5269 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5270 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005271 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005273 }
5274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005276 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005278 }
5279
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005281 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005283 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005284#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005286 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005288 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005289#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005290 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005291
5292 /* We leave the fields Ai and output_block to be erased safely by the
5293 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005295#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5296 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005297#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5299 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5300 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5301 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005302#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305303#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305304 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305305 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005306 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305307 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305308 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305309
5310 status = PSA_SUCCESS;
5311 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305312#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005313 {
5314 status = PSA_ERROR_BAD_STATE;
5315 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 mbedtls_platform_zeroize(operation, sizeof(*operation));
5317 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005318}
5319
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005322{
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005326 }
5327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005330}
5331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5333 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334{
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (operation->alg == 0) {
5336 return PSA_ERROR_BAD_STATE;
5337 }
5338 if (capacity > operation->capacity) {
5339 return PSA_ERROR_INVALID_ARGUMENT;
5340 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005343}
5344
Przemek Stekiel69c46792022-06-10 12:59:51 +02005345#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005346/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005347static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5348 psa_algorithm_t kdf_alg,
5349 uint8_t *output,
5350 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005351{
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5353 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005354 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005355 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005358#else
5359 const uint8_t last_block = 0xff;
5360#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (hkdf->state < HKDF_STATE_KEYED ||
5363 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005364#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005366#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 )) {
5368 return PSA_ERROR_BAD_STATE;
5369 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005370 hkdf->state = HKDF_STATE_OUTPUT;
5371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005373 /* Copy what remains of the current block */
5374 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 }
5378 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005379 output += n;
5380 output_length -= n;
5381 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005383 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005385 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005386 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005388 * object was corrupted or if this function is called directly
5389 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 if (hkdf->block_number == last_block) {
5391 return PSA_ERROR_BAD_STATE;
5392 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005393
5394 /* We need a new block */
5395 ++hkdf->block_number;
5396 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5399 hash_alg,
5400 hkdf->prk,
5401 hash_length);
5402 if (status != PSA_SUCCESS) {
5403 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005405
5406 if (hkdf->block_number != 1) {
5407 status = psa_mac_update(&hkdf->hmac,
5408 hkdf->output_block,
5409 hash_length);
5410 if (status != PSA_SUCCESS) {
5411 return status;
5412 }
5413 }
5414 status = psa_mac_update(&hkdf->hmac,
5415 hkdf->info,
5416 hkdf->info_length);
5417 if (status != PSA_SUCCESS) {
5418 return status;
5419 }
5420 status = psa_mac_update(&hkdf->hmac,
5421 &hkdf->block_number, 1);
5422 if (status != PSA_SUCCESS) {
5423 return status;
5424 }
5425 status = psa_mac_sign_finish(&hkdf->hmac,
5426 hkdf->output_block,
5427 sizeof(hkdf->output_block),
5428 &hmac_output_length);
5429 if (status != PSA_SUCCESS) {
5430 return status;
5431 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005432 }
5433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005435}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005436#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005437
John Durkop07cc04a2020-11-16 22:08:34 -08005438#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5439 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005440static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5441 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005443{
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5445 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005446 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5447 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005448 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005449
Janos Follath7742fee2019-06-17 12:58:10 +01005450 /* We can't be wanting more output after block 0xff, otherwise
5451 * the capacity check in psa_key_derivation_output_bytes() would have
5452 * prevented this call. It could happen only if the operation
5453 * object was corrupted or if this function is called directly
5454 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (tls12_prf->block_number == 0xff) {
5456 return PSA_ERROR_CORRUPTION_DETECTED;
5457 }
Janos Follath7742fee2019-06-17 12:58:10 +01005458
5459 /* We need a new block */
5460 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005461 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005462
5463 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5464 *
5465 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5466 *
5467 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5468 * HMAC_hash(secret, A(2) + seed) +
5469 * HMAC_hash(secret, A(3) + seed) + ...
5470 *
5471 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005472 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005473 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005474 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005475 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005476 * is currently extracted as `output_block` and where i is
5477 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005478 */
5479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 status = psa_key_derivation_start_hmac(&hmac,
5481 hash_alg,
5482 tls12_prf->secret,
5483 tls12_prf->secret_length);
5484 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005485 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005487
5488 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005490 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5491 * the variable seed and in this instance means it in the context of the
5492 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 status = psa_mac_update(&hmac,
5494 tls12_prf->label,
5495 tls12_prf->label_length);
5496 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005497 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 }
5499 status = psa_mac_update(&hmac,
5500 tls12_prf->seed,
5501 tls12_prf->seed_length);
5502 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 }
5505 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5508 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005509 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005511 }
5512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 status = psa_mac_sign_finish(&hmac,
5514 tls12_prf->Ai, hash_length,
5515 &hmac_output_length);
5516 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005517 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 }
5519 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005520 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005522
5523 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 status = psa_key_derivation_start_hmac(&hmac,
5525 hash_alg,
5526 tls12_prf->secret,
5527 tls12_prf->secret_length);
5528 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005529 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 }
5531 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5532 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005533 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 }
5535 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5536 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005537 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 }
5539 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5540 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005541 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 }
5543 status = psa_mac_sign_finish(&hmac,
5544 tls12_prf->output_block, hash_length,
5545 &hmac_output_length);
5546 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005547 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005549
Janos Follath7742fee2019-06-17 12:58:10 +01005550
5551cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 cleanup_status = psa_mac_abort(&hmac);
5553 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005554 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005558}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005559
Janos Follath844eb0e2019-06-19 12:10:49 +01005560static psa_status_t psa_key_derivation_tls12_prf_read(
5561 psa_tls12_prf_key_derivation_t *tls12_prf,
5562 psa_algorithm_t alg,
5563 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005565{
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5567 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005568 psa_status_t status;
5569 uint8_t offset, length;
5570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005572 case PSA_TLS12_PRF_STATE_LABEL_SET:
5573 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5574 break;
5575 case PSA_TLS12_PRF_STATE_OUTPUT:
5576 break;
5577 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005579 }
5580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005582 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (tls12_prf->left_in_block == 0) {
5584 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5585 alg);
5586 if (status != PSA_SUCCESS) {
5587 return status;
5588 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005589
5590 continue;
5591 }
5592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005594 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005596 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005598
5599 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005601 output += length;
5602 output_length -= length;
5603 tls12_prf->left_in_block -= length;
5604 }
5605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005607}
John Durkop07cc04a2020-11-16 22:08:34 -08005608#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5609 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005610
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005611#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5612static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5613 psa_tls12_ecjpake_to_pms_t *ecjpake,
5614 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005616{
5617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005618 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (output_length != 32) {
5621 return PSA_ERROR_INVALID_ARGUMENT;
5622 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5625 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5626 &output_size);
5627 if (status != PSA_SUCCESS) {
5628 return status;
5629 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 if (output_size != output_length) {
5632 return PSA_ERROR_GENERIC_ERROR;
5633 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005636}
5637#endif
5638
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305639#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305640static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5641 psa_pbkdf2_key_derivation_t *pbkdf2,
5642 psa_algorithm_t prf_alg,
5643 uint8_t prf_output_length,
5644 psa_key_attributes_t *attributes)
5645{
5646 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305647 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305649 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305650 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305651 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305652 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305654 mac_operation.is_sign = 1;
5655 mac_operation.mac_size = prf_output_length;
5656 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305657
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305658 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5659 attributes,
5660 pbkdf2->password,
5661 pbkdf2->password_length,
5662 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305663 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305664 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305665 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305666 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5667 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305668 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305669 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305670 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305671 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305672 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305673 }
5674 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5675 &mac_output_length);
5676 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305677 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305678 }
5679
5680 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305681 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305682 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305683 }
5684
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305685 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305686
5687 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305688 /* We are passing prf_output_length as mac_size because the driver
5689 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305690 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305691 status = psa_driver_wrapper_mac_compute(attributes,
5692 pbkdf2->password,
5693 pbkdf2->password_length,
5694 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305695 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305696 &mac_output_length);
5697 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305698 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305699 }
5700
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305701 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305702 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305703
5704cleanup:
5705 /* Zeroise buffers to clear sensitive data from memory. */
5706 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5707 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305708}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305709
5710static psa_status_t psa_key_derivation_pbkdf2_read(
5711 psa_pbkdf2_key_derivation_t *pbkdf2,
5712 psa_algorithm_t kdf_alg,
5713 uint8_t *output,
5714 size_t output_length)
5715{
5716 psa_status_t status;
5717 psa_algorithm_t prf_alg;
5718 uint8_t prf_output_length;
5719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5720 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5721 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5722
5723 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5724 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5725 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5726 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305727 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5728 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305729 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305730 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305731 } else {
5732 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305733 }
5734
5735 switch (pbkdf2->state) {
5736 case PSA_PBKDF2_STATE_PASSWORD_SET:
5737 /* Initially we need a new block so bytes_used is equal to block size*/
5738 pbkdf2->bytes_used = prf_output_length;
5739 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5740 break;
5741 case PSA_PBKDF2_STATE_OUTPUT:
5742 break;
5743 default:
5744 return PSA_ERROR_BAD_STATE;
5745 }
5746
5747 while (output_length != 0) {
5748 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5749 if (n > output_length) {
5750 n = (uint8_t) output_length;
5751 }
5752 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5753 output += n;
5754 output_length -= n;
5755 pbkdf2->bytes_used += n;
5756
5757 if (output_length == 0) {
5758 break;
5759 }
5760
5761 /* We need a new block */
5762 pbkdf2->bytes_used = 0;
5763 pbkdf2->block_number++;
5764
5765 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5766 prf_output_length,
5767 &attributes);
5768 if (status != PSA_SUCCESS) {
5769 return status;
5770 }
5771 }
5772
5773 return PSA_SUCCESS;
5774}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305775#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305776
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005777psa_status_t psa_key_derivation_output_bytes(
5778 psa_key_derivation_operation_t *operation,
5779 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005781{
5782 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005786 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005788 }
5789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005792 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005794 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005795 goto exit;
5796 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005799 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5801 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005803 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005805 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005806 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005807
Przemek Stekiel69c46792022-06-10 12:59:51 +02005808#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5810 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5811 output, output_length);
5812 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005813#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005814#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5815 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5817 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5818 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5819 kdf_alg, output,
5820 output_length);
5821 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005822#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5823 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005824#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005826 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5828 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005829#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305830#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305831 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305832 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5833 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305834 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305835#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005836
Gilles Peskineeab56e42018-07-12 17:12:33 +02005837 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005838 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005840 }
5841
5842exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005844 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005845 * This allows us to differentiate between exhausted operations and
5846 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5847 * operations. */
5848 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005850 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005852 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005854}
5855
David Brown7807bf72021-02-09 16:28:23 -07005856#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005857static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005858{
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (data_size >= 8) {
5860 mbedtls_des_key_set_parity(data);
5861 }
5862 if (data_size >= 16) {
5863 mbedtls_des_key_set_parity(data + 8);
5864 }
5865 if (data_size >= 24) {
5866 mbedtls_des_key_set_parity(data + 16);
5867 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005868}
David Brown7807bf72021-02-09 16:28:23 -07005869#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005870
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005871/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 * ECC keys on a Weierstrass elliptic curve require the generation
5873 * of a private key which is an integer
5874 * in the range [1, N - 1], where N is the boundary of the private key domain:
5875 * N is the prime p for Diffie-Hellman, or the order of the
5876 * curve’s base point for ECC.
5877 *
5878 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5879 * This function generates the private key using the following process:
5880 *
5881 * 1. Draw a byte string of length ceiling(m/8) bytes.
5882 * 2. If m is not a multiple of 8, set the most significant
5883 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5884 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5885 * 4. If k > N - 2, discard the result and return to step 1.
5886 * 5. Output k + 1 as the private key.
5887 *
5888 * This method allows compliance to NIST standards, specifically the methods titled
5889 * Key-Pair Generation by Testing Candidates in the following publications:
5890 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5891 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5892 * Diffie-Hellman keys.
5893 *
5894 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5895 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5896 *
5897 * Note: Function allocates memory for *data buffer, so given *data should be
5898 * always NULL.
5899 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005900#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5901#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005902static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005903 psa_key_slot_t *slot,
5904 size_t bits,
5905 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906 uint8_t **data
5907 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005908{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005909 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005910 mbedtls_mpi k;
5911 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005913 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005914 size_t m;
5915 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005916
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 mbedtls_mpi_init(&k);
5918 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005919
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005920 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005922 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005926 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005927 goto cleanup;
5928 }
5929
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005930 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005934
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005935 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005936 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005937 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005938
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005939 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005940
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005941 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005943
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005944 /* Note: This function is always called with *data == NULL and it
5945 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 *data = mbedtls_calloc(1, m_bytes);
5947 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005948 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005949 goto cleanup;
5950 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005953 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005955 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005957
5958 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005960 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005961 * (8 * ceiling(m/8) - m) bits of the first byte in
5962 * the string to zero.
5963 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005965 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005966 }
5967
5968 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 * big-endian byte string.
5970 */
5971 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005972
5973 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 * Result of comparison is returned. When it indicates error
5975 * then this function is called again.
5976 */
5977 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005978 }
5979
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005980 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5982 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005983cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (ret != 0) {
5985 status = mbedtls_to_psa_error(ret);
5986 }
5987 if (status != PSA_SUCCESS) {
5988 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005989 *data = NULL;
5990 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 mbedtls_mpi_free(&k);
5992 mbedtls_mpi_free(&diff_N_2);
5993 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005994}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005995
5996/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5997 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5998 *
5999 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
6000 * draw a 32-byte string and process it as specified in
6001 * Elliptic Curves for Security [RFC7748] §5.
6002 *
6003 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
6004 * draw a 56-byte string and process it as specified in [RFC7748] §5.
6005 *
6006 * Note: Function allocates memory for *data buffer, so given *data should be
6007 * always NULL.
6008 */
6009
6010static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6011 size_t bits,
6012 psa_key_derivation_operation_t *operation,
6013 uint8_t **data
6014 )
6015{
6016 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006020 case 255:
6021 output_length = 32;
6022 break;
6023 case 448:
6024 output_length = 56;
6025 break;
6026 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006028 break;
6029 }
6030
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (*data == NULL) {
6034 return PSA_ERROR_INSUFFICIENT_MEMORY;
6035 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006040 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006044 case 255:
6045 (*data)[0] &= 248;
6046 (*data)[31] &= 127;
6047 (*data)[31] |= 64;
6048 break;
6049 case 448:
6050 (*data)[0] &= 252;
6051 (*data)[55] |= 128;
6052 break;
6053 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006055 break;
6056 }
6057
6058 return status;
6059}
Valerio Setti86587ab2023-06-27 13:09:30 +02006060#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6061static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6062 psa_key_slot_t *slot, size_t bits,
6063 psa_key_derivation_operation_t *operation, uint8_t **data)
6064{
6065 (void) slot;
6066 (void) bits;
6067 (void) operation;
6068 (void) data;
6069 return PSA_ERROR_NOT_SUPPORTED;
6070}
6071
6072static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6073 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6074{
6075 (void) bits;
6076 (void) operation;
6077 (void) data;
6078 return PSA_ERROR_NOT_SUPPORTED;
6079}
6080#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6081#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006082
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006083static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006084 psa_key_slot_t *slot,
6085 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006087{
6088 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306090 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006091 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006092 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6095 return PSA_ERROR_INVALID_ARGUMENT;
6096 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006097
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006098#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006099 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6101 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6102 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006103 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6105 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 }
6108 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006109 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6111 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006114 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006115 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006116#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006117 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (key_type_is_raw_bytes(slot->attr.type)) {
6119 if (bits % 8 != 0) {
6120 return PSA_ERROR_INVALID_ARGUMENT;
6121 }
6122 data = mbedtls_calloc(1, bytes);
6123 if (data == NULL) {
6124 return PSA_ERROR_INSUFFICIENT_MEMORY;
6125 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 status = psa_key_derivation_output_bytes(operation, data, bytes);
6128 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
David Brown12ca5032021-02-11 11:02:00 -07006131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6133 psa_des_set_key_parity(data, bytes);
6134 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006135#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 } else {
6137 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006138 }
Ronald Crondd04d422020-11-28 15:14:42 +01006139
Ronald Cronbf33c932020-11-28 18:06:53 +01006140 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006141 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006143 };
6144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6146 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6147 &storage_size);
6148 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 }
Archanad8a83dc2021-06-14 10:04:16 +05306151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 status = psa_allocate_buffer_to_slot(slot, storage_size);
6153 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306154 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 }
Archanad8a83dc2021-06-14 10:04:16 +05306156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 status = psa_driver_wrapper_import_key(&attributes,
6158 data, bytes,
6159 slot->key.data,
6160 slot->key.bytes,
6161 &slot->key.bytes, &bits);
6162 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006163 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006165
6166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 mbedtls_free(data);
6168 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006169}
6170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6172 psa_key_derivation_operation_t *operation,
6173 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006174{
6175 psa_status_t status;
6176 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006177 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006178
Ronald Cron81709fc2020-11-14 12:10:32 +01006179 *key = MBEDTLS_SVC_KEY_ID_INIT;
6180
Gilles Peskine0f84d622019-09-12 19:03:13 +02006181 /* Reject any attempt to create a zero-length key so that we don't
6182 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (psa_get_key_bits(attributes) == 0) {
6184 return PSA_ERROR_INVALID_ARGUMENT;
6185 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006186
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (operation->alg == PSA_ALG_NONE) {
6188 return PSA_ERROR_BAD_STATE;
6189 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (!operation->can_output_key) {
6192 return PSA_ERROR_NOT_PERMITTED;
6193 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6196 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006197#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006199 /* Deriving a key in a secure element is not implemented yet. */
6200 status = PSA_ERROR_NOT_SUPPORTED;
6201 }
6202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (status == PSA_SUCCESS) {
6204 status = psa_generate_derived_key_internal(slot,
6205 attributes->core.bits,
6206 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006207 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (status == PSA_SUCCESS) {
6209 status = psa_finish_key_creation(slot, driver, key);
6210 }
6211 if (status != PSA_SUCCESS) {
6212 psa_fail_key_creation(slot, driver);
6213 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006216}
6217
Gilles Peskineeab56e42018-07-12 17:12:33 +02006218
6219
6220/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006221/* Key derivation */
6222/****************************************************************/
6223
Steven Cooreman932ffb72021-02-15 12:14:32 +01006224#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006225static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006226{
6227#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6229 return 1;
6230 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006231#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006232#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6234 return 1;
6235 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006236#endif
6237#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6239 return 1;
6240 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006241#endif
6242#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6244 return 1;
6245 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006246#endif
6247#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6249 return 1;
6250 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006251#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006252#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6254 return 1;
6255 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006256#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306257#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6258 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6259 return 1;
6260 }
6261#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306262#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6263 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6264 return 1;
6265 }
6266#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006268}
6269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006271{
6272 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 psa_status_t status = psa_hash_setup(&operation, alg);
6274 psa_hash_abort(&operation);
6275 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006276}
6277
Gilles Peskine969c5d62019-01-16 15:53:06 +01006278static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006279 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006281{
Janos Follath5fe19732019-06-20 15:09:30 +01006282 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6283 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006285
Gilles Peskine969c5d62019-01-16 15:53:06 +01006286 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 if (!is_kdf_alg_supported(kdf_alg)) {
6288 return PSA_ERROR_NOT_SUPPORTED;
6289 }
John Durkop07cc04a2020-11-16 22:08:34 -08006290
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006291 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306292 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6294 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306295 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6296 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6297 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306298 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306299 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (hash_size == 0) {
6301 return PSA_ERROR_NOT_SUPPORTED;
6302 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006303
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006304 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6305 * we might fail later, which is somewhat unfriendly and potentially
6306 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 psa_status_t status = psa_hash_try_support(hash_alg);
6308 if (status != PSA_SUCCESS) {
6309 return status;
6310 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006311 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6314 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6315 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6316 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006317 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006318#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006319 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6321 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006322 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006324#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6325 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 operation->capacity = 255 * hash_size;
6327 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006328}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006331{
6332#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 if (alg == PSA_ALG_ECDH) {
6334 return PSA_SUCCESS;
6335 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006336#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006337#if defined(PSA_WANT_ALG_FFDH)
6338 if (alg == PSA_ALG_FFDH) {
6339 return PSA_SUCCESS;
6340 }
6341#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006342 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006344}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006345
6346static int psa_key_derivation_allows_free_form_secret_input(
6347 psa_algorithm_t kdf_alg)
6348{
6349#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6350 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6351 return 0;
6352 }
6353#endif
6354 (void) kdf_alg;
6355 return 1;
6356}
John Durkop07cc04a2020-11-16 22:08:34 -08006357#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6360 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006361{
6362 psa_status_t status;
6363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (operation->alg != 0) {
6365 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006366 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6369 return PSA_ERROR_INVALID_ARGUMENT;
6370 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6371#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6372 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6373 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6374 status = psa_key_agreement_try_support(ka_alg);
6375 if (status != PSA_SUCCESS) {
6376 return status;
6377 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006378 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6379 return PSA_ERROR_INVALID_ARGUMENT;
6380 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6382#else
6383 return PSA_ERROR_NOT_SUPPORTED;
6384#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6385 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6386#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6387 status = psa_key_derivation_setup_kdf(operation, alg);
6388#else
6389 return PSA_ERROR_NOT_SUPPORTED;
6390#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6391 } else {
6392 return PSA_ERROR_INVALID_ARGUMENT;
6393 }
6394
6395 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006396 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 }
6398 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006399}
6400
Przemek Stekiel69c46792022-06-10 12:59:51 +02006401#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006402static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6403 psa_algorithm_t kdf_alg,
6404 psa_key_derivation_step_t step,
6405 const uint8_t *data,
6406 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006407{
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006409 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006411 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006412#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6414 return PSA_ERROR_INVALID_ARGUMENT;
6415 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006416#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (hkdf->state != HKDF_STATE_INIT) {
6418 return PSA_ERROR_BAD_STATE;
6419 } else {
6420 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6421 hash_alg,
6422 data, data_length);
6423 if (status != PSA_SUCCESS) {
6424 return status;
6425 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006426 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006428 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006429 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006430#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006432 /* We shouldn't be in different state as HKDF_EXPAND only allows
6433 * two inputs: SECRET (this case) and INFO which does not modify
6434 * the state. It could happen only if the hkdf
6435 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 if (hkdf->state != HKDF_STATE_INIT) {
6437 return PSA_ERROR_BAD_STATE;
6438 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006439
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006440 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6442 return PSA_ERROR_INVALID_ARGUMENT;
6443 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 memcpy(hkdf->prk, data, data_length);
6446 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006447#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006448 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006449 /* HKDF: If no salt was provided, use an empty salt.
6450 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006452#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6454 return PSA_ERROR_BAD_STATE;
6455 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006456#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6458 hash_alg,
6459 NULL, 0);
6460 if (status != PSA_SUCCESS) {
6461 return status;
6462 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006463 hkdf->state = HKDF_STATE_STARTED;
6464 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (hkdf->state != HKDF_STATE_STARTED) {
6466 return PSA_ERROR_BAD_STATE;
6467 }
6468 status = psa_mac_update(&hkdf->hmac,
6469 data, data_length);
6470 if (status != PSA_SUCCESS) {
6471 return status;
6472 }
6473 status = psa_mac_sign_finish(&hkdf->hmac,
6474 hkdf->prk,
6475 sizeof(hkdf->prk),
6476 &data_length);
6477 if (status != PSA_SUCCESS) {
6478 return status;
6479 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006480 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006481
Gilles Peskine2b522db2019-04-12 15:11:49 +02006482 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006483 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006484#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006486 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006488 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006490#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006491 {
6492 /* Block 0 is empty, and the next block will be
6493 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006495 }
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006498 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006499#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6501 return PSA_ERROR_INVALID_ARGUMENT;
6502 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006503#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006504#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6506 hkdf->state == HKDF_STATE_INIT) {
6507 return PSA_ERROR_BAD_STATE;
6508 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006509#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 if (hkdf->state == HKDF_STATE_OUTPUT) {
6511 return PSA_ERROR_BAD_STATE;
6512 }
6513 if (hkdf->info_set) {
6514 return PSA_ERROR_BAD_STATE;
6515 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006516 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 if (data_length != 0) {
6518 hkdf->info = mbedtls_calloc(1, data_length);
6519 if (hkdf->info == NULL) {
6520 return PSA_ERROR_INSUFFICIENT_MEMORY;
6521 }
6522 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006523 }
6524 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006526 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006528 }
6529}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006530#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006531
John Durkop07cc04a2020-11-16 22:08:34 -08006532#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6533 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006534static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6535 const uint8_t *data,
6536 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006537{
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6539 return PSA_ERROR_BAD_STATE;
6540 }
Janos Follathf08e2652019-06-13 09:05:41 +01006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 if (data_length != 0) {
6543 prf->seed = mbedtls_calloc(1, data_length);
6544 if (prf->seed == NULL) {
6545 return PSA_ERROR_INSUFFICIENT_MEMORY;
6546 }
Janos Follathf08e2652019-06-13 09:05:41 +01006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006549 prf->seed_length = data_length;
6550 }
Janos Follathf08e2652019-06-13 09:05:41 +01006551
Gilles Peskine43f958b2020-12-13 14:55:14 +01006552 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006555}
6556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6558 const uint8_t *data,
6559 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006560{
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6562 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6563 return PSA_ERROR_BAD_STATE;
6564 }
Janos Follath81550542019-06-13 14:26:34 +01006565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 if (data_length != 0) {
6567 prf->secret = mbedtls_calloc(1, data_length);
6568 if (prf->secret == NULL) {
6569 return PSA_ERROR_INSUFFICIENT_MEMORY;
6570 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006573 prf->secret_length = data_length;
6574 }
Janos Follath81550542019-06-13 14:26:34 +01006575
Gilles Peskine43f958b2020-12-13 14:55:14 +01006576 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006579}
6580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6582 const uint8_t *data,
6583 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006584{
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6586 return PSA_ERROR_BAD_STATE;
6587 }
Janos Follath63028dd2019-06-13 09:15:47 +01006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 if (data_length != 0) {
6590 prf->label = mbedtls_calloc(1, data_length);
6591 if (prf->label == NULL) {
6592 return PSA_ERROR_INSUFFICIENT_MEMORY;
6593 }
Janos Follath63028dd2019-06-13 09:15:47 +01006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006596 prf->label_length = data_length;
6597 }
Janos Follath63028dd2019-06-13 09:15:47 +01006598
Gilles Peskine43f958b2020-12-13 14:55:14 +01006599 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006602}
6603
Gilles Peskine449bd832023-01-11 14:50:10 +01006604static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6605 psa_key_derivation_step_t step,
6606 const uint8_t *data,
6607 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006608{
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006610 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006612 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006614 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006616 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006618 }
6619}
John Durkop07cc04a2020-11-16 22:08:34 -08006620#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6621 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6622
6623#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6624static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6625 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006626 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006628{
6629 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6631 4 + data_length + prf->other_secret_length :
6632 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6635 return PSA_ERROR_INVALID_ARGUMENT;
6636 }
John Durkop07cc04a2020-11-16 22:08:34 -08006637
Gilles Peskine449bd832023-01-11 14:50:10 +01006638 uint8_t *pms = mbedtls_calloc(1, pms_len);
6639 if (pms == NULL) {
6640 return PSA_ERROR_INSUFFICIENT_MEMORY;
6641 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006642 uint8_t *cur = pms;
6643
6644 /* pure-PSK:
6645 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006646 *
6647 * The premaster secret is formed as follows: if the PSK is N octets
6648 * long, concatenate a uint16 with the value N, N zero octets, a second
6649 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006650 *
6651 * mixed-PSK:
6652 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6653 * follows: concatenate a uint16 with the length of the other secret,
6654 * the other secret itself, uint16 with the length of PSK, and the
6655 * PSK itself.
6656 * For details please check:
6657 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6658 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6659 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006660 */
6661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6663 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6664 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6665 if (prf->other_secret_length != 0) {
6666 memcpy(cur, prf->other_secret, prf->other_secret_length);
6667 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006668 cur += prf->other_secret_length;
6669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 } else {
6671 *cur++ = MBEDTLS_BYTE_1(data_length);
6672 *cur++ = MBEDTLS_BYTE_0(data_length);
6673 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006674 cur += data_length;
6675 }
6676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 *cur++ = MBEDTLS_BYTE_1(data_length);
6678 *cur++ = MBEDTLS_BYTE_0(data_length);
6679 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006680 cur += data_length;
6681
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006682 status = psa_tls12_prf_set_key(prf, pms, (size_t) (cur - pms));
John Durkop07cc04a2020-11-16 22:08:34 -08006683
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006684 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006686}
Janos Follath6660f0e2019-06-17 08:44:03 +01006687
Przemek Stekiele47201b2022-04-19 13:53:28 +02006688static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6689 psa_tls12_prf_key_derivation_t *prf,
6690 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006692{
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6694 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006696
6697 if (data_length != 0) {
6698 prf->other_secret = mbedtls_calloc(1, data_length);
6699 if (prf->other_secret == NULL) {
6700 return PSA_ERROR_INSUFFICIENT_MEMORY;
6701 }
6702
6703 memcpy(prf->other_secret, data, data_length);
6704 prf->other_secret_length = data_length;
6705 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006706 prf->other_secret_length = 0;
6707 }
6708
6709 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6710
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006712}
6713
Janos Follath6660f0e2019-06-17 08:44:03 +01006714static psa_status_t psa_tls12_prf_psk_to_ms_input(
6715 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006716 psa_key_derivation_step_t step,
6717 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006719{
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006721 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 return psa_tls12_prf_psk_to_ms_set_key(prf,
6723 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006724 break;
6725 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6727 data,
6728 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006729 break;
6730 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006732 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006733
Przemek Stekiele47201b2022-04-19 13:53:28 +02006734 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006735}
John Durkop07cc04a2020-11-16 22:08:34 -08006736#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006737
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006738#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6739static psa_status_t psa_tls12_ecjpake_to_pms_input(
6740 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006741 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006742 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006744{
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6746 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6747 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006748 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006749
6750 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 if (data[0] != 0x04) {
6752 return PSA_ERROR_INVALID_ARGUMENT;
6753 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006754
6755 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006759}
6760#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306761
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306762#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306763static psa_status_t psa_pbkdf2_set_input_cost(
6764 psa_pbkdf2_key_derivation_t *pbkdf2,
6765 psa_key_derivation_step_t step,
6766 uint64_t data)
6767{
6768 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6769 return PSA_ERROR_INVALID_ARGUMENT;
6770 }
6771
6772 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6773 return PSA_ERROR_BAD_STATE;
6774 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306775
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306776 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306777 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306778 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306779
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306780 if (data == 0) {
6781 return PSA_ERROR_INVALID_ARGUMENT;
6782 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306783
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306784 pbkdf2->input_cost = data;
6785 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6786
6787 return PSA_SUCCESS;
6788}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306789
6790static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6791 const uint8_t *data,
6792 size_t data_length)
6793{
Gilles Peskineead17662023-08-20 22:02:51 +02006794 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6795 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6796 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6797 /* Appending to existing salt. No state change. */
6798 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306799 return PSA_ERROR_BAD_STATE;
6800 }
6801
Gilles Peskineca57d782023-07-20 19:08:06 +02006802 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006803 /* Appending an empty string, nothing to do. */
6804 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306805 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306806
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306807 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6808 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306809 return PSA_ERROR_INSUFFICIENT_MEMORY;
6810 }
6811
Gilles Peskineead17662023-08-20 22:02:51 +02006812 if (pbkdf2->salt_length != 0) {
6813 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6814 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306815 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306816 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306817 mbedtls_free(pbkdf2->salt);
6818 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306819 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306820 return PSA_SUCCESS;
6821}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306822
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306823#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306824static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306825 const uint8_t *input,
6826 size_t input_len,
6827 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306828 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306829{
6830 psa_status_t status = PSA_SUCCESS;
6831 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6832 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306833 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306834 } else {
6835 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306836 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306837 }
6838 return status;
6839}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306840#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306841
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306842#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306843static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6844 size_t input_len,
6845 uint8_t *output,
6846 size_t *output_len)
6847{
6848 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306849 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306851 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306852 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6853 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6854 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306855 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306856 * mac_size as the driver function sets mac_output_length = mac_size
6857 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306858 status = psa_driver_wrapper_mac_compute(&attributes,
6859 zeros, sizeof(zeros),
6860 PSA_ALG_CMAC, input, input_len,
6861 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306862 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6863 128U,
6864 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306865 output_len);
6866 } else {
6867 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306868 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306869 }
6870 return status;
6871}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306872#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306873
6874static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306875 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306876 const uint8_t *data,
6877 size_t data_length)
6878{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306879 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306880 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6881 return PSA_ERROR_BAD_STATE;
6882 }
6883
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306884#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306885 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6886 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6887 status = psa_pbkdf2_hmac_set_password(hash_alg, 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_HMAC */
6892#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6893 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306894 status = psa_pbkdf2_cmac_set_password(data, data_length,
6895 pbkdf2->password,
6896 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306897 } else
6898#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6899 {
6900 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306901 }
6902
6903 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6904
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306905 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306906}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306907
6908static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306909 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306910 psa_key_derivation_step_t step,
6911 const uint8_t *data,
6912 size_t data_length)
6913{
6914 switch (step) {
6915 case PSA_KEY_DERIVATION_INPUT_SALT:
6916 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6917 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306918 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306919 default:
6920 return PSA_ERROR_INVALID_ARGUMENT;
6921 }
6922}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306923#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306924
Gilles Peskineb8965192019-09-24 16:21:10 +02006925/** Check whether the given key type is acceptable for the given
6926 * input step of a key derivation.
6927 *
6928 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6929 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6930 * Both secret and non-secret inputs can alternatively have the type
6931 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6932 * that the input was passed as a buffer rather than via a key object.
6933 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006934static int psa_key_derivation_check_input_type(
6935 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006937{
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 if (key_type == PSA_KEY_TYPE_DERIVE) {
6941 return PSA_SUCCESS;
6942 }
6943 if (key_type == PSA_KEY_TYPE_NONE) {
6944 return PSA_SUCCESS;
6945 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006946 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006947 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 if (key_type == PSA_KEY_TYPE_DERIVE) {
6949 return PSA_SUCCESS;
6950 }
6951 if (key_type == PSA_KEY_TYPE_NONE) {
6952 return PSA_SUCCESS;
6953 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006954 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955 case PSA_KEY_DERIVATION_INPUT_LABEL:
6956 case PSA_KEY_DERIVATION_INPUT_SALT:
6957 case PSA_KEY_DERIVATION_INPUT_INFO:
6958 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6960 return PSA_SUCCESS;
6961 }
6962 if (key_type == PSA_KEY_TYPE_NONE) {
6963 return PSA_SUCCESS;
6964 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006965 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306966 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6967 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6968 return PSA_SUCCESS;
6969 }
6970 if (key_type == PSA_KEY_TYPE_DERIVE) {
6971 return PSA_SUCCESS;
6972 }
6973 if (key_type == PSA_KEY_TYPE_NONE) {
6974 return PSA_SUCCESS;
6975 }
6976 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006977 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006979}
6980
Janos Follathb80a94e2019-06-12 15:54:46 +01006981static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006982 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006983 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006984 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006985 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006987{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006988 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006990
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 status = psa_key_derivation_check_input_type(step, key_type);
6992 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006993 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006995
Przemek Stekiel69c46792022-06-10 12:59:51 +02006996#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6998 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6999 step, data, data_length);
7000 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02007001#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08007002#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
7004 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
7005 step, data, data_length);
7006 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007007#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
7008#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
7010 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
7011 step, data, data_length);
7012 } else
John Durkop07cc04a2020-11-16 22:08:34 -08007013#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007014#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007016 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
7018 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04007019#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307020#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307021 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05307022 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
7023 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05307024 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307025#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007026 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007027 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01007028 (void) data;
7029 (void) data_length;
7030 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007032 }
7033
Gilles Peskine224b0d62019-09-23 18:13:17 +02007034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (status != PSA_SUCCESS) {
7036 psa_key_derivation_abort(operation);
7037 }
7038 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007039}
7040
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307041static psa_status_t psa_key_derivation_input_integer_internal(
7042 psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
7044 uint64_t value)
7045{
7046 psa_status_t status;
7047 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7048
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307049#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307050 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307051 status = psa_pbkdf2_set_input_cost(
7052 &operation->ctx.pbkdf2, step, value);
7053 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307054#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307055 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307056 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307057 (void) value;
7058 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307059 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307060 }
7061
7062 if (status != PSA_SUCCESS) {
7063 psa_key_derivation_abort(operation);
7064 }
7065 return status;
7066}
7067
Janos Follath51f4a0f2019-06-14 11:35:55 +01007068psa_status_t psa_key_derivation_input_bytes(
7069 psa_key_derivation_operation_t *operation,
7070 psa_key_derivation_step_t step,
7071 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007073{
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 return psa_key_derivation_input_internal(operation, step,
7075 PSA_KEY_TYPE_NONE,
7076 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007077}
7078
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307079psa_status_t psa_key_derivation_input_integer(
7080 psa_key_derivation_operation_t *operation,
7081 psa_key_derivation_step_t step,
7082 uint64_t value)
7083{
7084 return psa_key_derivation_input_integer_internal(operation, step, value);
7085}
7086
Janos Follath51f4a0f2019-06-14 11:35:55 +01007087psa_status_t psa_key_derivation_input_key(
7088 psa_key_derivation_operation_t *operation,
7089 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007091{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007094 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007095
Ronald Cron5c522922020-11-14 16:35:34 +01007096 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7098 if (status != PSA_SUCCESS) {
7099 psa_key_derivation_abort(operation);
7100 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007101 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007102
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307103 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7104 * permission to output to a key object. */
7105 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7106 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007107 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 status = psa_key_derivation_input_internal(operation,
7111 step, slot->attr.type,
7112 slot->key.data,
7113 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007116
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007118}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007119
7120
7121
7122/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007123/* Key agreement */
7124/****************************************************************/
7125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7127 const uint8_t *key_buffer,
7128 size_t key_buffer_size,
7129 psa_algorithm_t alg,
7130 const uint8_t *peer_key,
7131 size_t peer_key_length,
7132 uint8_t *shared_secret,
7133 size_t shared_secret_size,
7134 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007135{
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007137#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007138 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7140 key_buffer_size, alg,
7141 peer_key, peer_key_length,
7142 shared_secret,
7143 shared_secret_size,
7144 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007145#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007146
7147#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7148 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007149 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007150 peer_key,
7151 peer_key_length,
7152 key_buffer,
7153 key_buffer_size,
7154 shared_secret,
7155 shared_secret_size,
7156 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007157#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7158
Gilles Peskine01d718c2018-09-18 12:01:02 +02007159 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007160 (void) attributes;
7161 (void) key_buffer;
7162 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007163 (void) peer_key;
7164 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007165 (void) shared_secret;
7166 (void) shared_secret_size;
7167 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007169 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007170}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007171
Aditya Deshpande17845b82022-10-13 17:21:01 +01007172/** Internal function for raw key agreement
7173 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007174 * to the driver's implementation if a driver is present.
7175 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007176 * (psa_key_agreement_raw_builtin).
7177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7179 psa_key_slot_t *private_key,
7180 const uint8_t *peer_key,
7181 size_t peer_key_length,
7182 uint8_t *shared_secret,
7183 size_t shared_secret_size,
7184 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007185{
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7187 return PSA_ERROR_NOT_SUPPORTED;
7188 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007189
7190 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007192 };
7193
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 return psa_driver_wrapper_key_agreement(&attributes,
7195 private_key->key.data,
7196 private_key->key.bytes, alg,
7197 peer_key, peer_key_length,
7198 shared_secret,
7199 shared_secret_size,
7200 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007201}
7202
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007203/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007204 * to potentially free embedded data structures and wipe confidential data.
7205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007206static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7207 psa_key_derivation_step_t step,
7208 psa_key_slot_t *private_key,
7209 const uint8_t *peer_key,
7210 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007211{
7212 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007213 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007214 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007216
7217 /* Step 1: run the secret agreement algorithm to generate the shared
7218 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 status = psa_key_agreement_raw_internal(ka_alg,
7220 private_key,
7221 peer_key, peer_key_length,
7222 shared_secret,
7223 sizeof(shared_secret),
7224 &shared_secret_length);
7225 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007228
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007229 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007230 * the shared secret. A shared secret is permitted wherever a key
7231 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 status = psa_key_derivation_input_internal(operation, step,
7233 PSA_KEY_TYPE_DERIVE,
7234 shared_secret,
7235 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007236exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7238 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007239}
7240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7242 psa_key_derivation_step_t step,
7243 mbedtls_svc_key_id_t private_key,
7244 const uint8_t *peer_key,
7245 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007249 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007250
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7252 return PSA_ERROR_INVALID_ARGUMENT;
7253 }
Ronald Cron5c522922020-11-14 16:35:34 +01007254 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7256 if (status != PSA_SUCCESS) {
7257 return status;
7258 }
7259 status = psa_key_agreement_internal(operation, step,
7260 slot,
7261 peer_key, peer_key_length);
7262 if (status != PSA_SUCCESS) {
7263 psa_key_derivation_abort(operation);
7264 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007265 /* If a private key has been added as SECRET, we allow the derived
7266 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007268 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007270 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007275}
7276
Gilles Peskine449bd832023-01-11 14:50:10 +01007277psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7278 mbedtls_svc_key_id_t private_key,
7279 const uint8_t *peer_key,
7280 size_t peer_key_length,
7281 uint8_t *output,
7282 size_t output_size,
7283 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007286 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007287 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007288 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007289
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007291 status = PSA_ERROR_INVALID_ARGUMENT;
7292 goto exit;
7293 }
Ronald Cron5c522922020-11-14 16:35:34 +01007294 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7296 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007299
Gilles Peskine3e561302022-04-14 00:17:15 +02007300 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7301 * for the output size. The PSA specification only guarantees that this
7302 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7303 * but it might be nice to allow smaller buffers if the output fits.
7304 * At the time of writing this comment, with only ECDH implemented,
7305 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7306 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7307 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007308 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7310 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007311 status = PSA_ERROR_BUFFER_TOO_SMALL;
7312 goto exit;
7313 }
7314
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 status = psa_key_agreement_raw_internal(alg, slot,
7316 peer_key, peer_key_length,
7317 output, output_size,
7318 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007319
7320exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007322 /* If an error happens and is not handled properly, the output
7323 * may be used as a key to protect sensitive data. Arrange for such
7324 * a key to be random, which is likely to result in decryption or
7325 * verification errors. This is better than filling the buffer with
7326 * some constant data such as zeros, which would result in the data
7327 * being protected with a reproducible, easily knowable key.
7328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007330 *output_length = output_size;
7331 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007332
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007336}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007337
7338
Gilles Peskine30524eb2020-11-13 17:02:26 +01007339
Gilles Peskine86a440b2018-11-12 18:39:40 +01007340/****************************************************************/
7341/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007342/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007343
Gilles Peskine672a7712023-04-28 21:00:28 +02007344#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7345#include "entropy_poll.h"
7346#endif
7347
Gilles Peskine30524eb2020-11-13 17:02:26 +01007348/** Initialize the PSA random generator.
7349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007350static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007351{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007352#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007354#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7355
Gilles Peskine30524eb2020-11-13 17:02:26 +01007356 /* Set default configuration if
7357 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007359 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 }
7361 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007362 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007366#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7367 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7368 /* The PSA entropy injection feature depends on using NV seed as an entropy
7369 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 mbedtls_entropy_add_source(&rng->entropy,
7371 mbedtls_nv_seed_poll, NULL,
7372 MBEDTLS_ENTROPY_BLOCK_SIZE,
7373 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007374#endif
7375
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007377#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007378}
7379
7380/** Deinitialize the PSA random generator.
7381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007382static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007383{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007384#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007386#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7388 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007389#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007390}
7391
7392/** Seed the PSA random generator.
7393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007394static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007395{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007396#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7397 /* Do nothing: the external RNG seeds itself. */
7398 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007400#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007401 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7403 drbg_seed, sizeof(drbg_seed) - 1);
7404 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007405#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007406}
7407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408psa_status_t psa_generate_random(uint8_t *output,
7409 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007410{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007411 GUARD_MODULE_INITIALIZED;
7412
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007413#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7414
7415 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7417 output, output_size,
7418 &output_length);
7419 if (status != PSA_SUCCESS) {
7420 return status;
7421 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007422 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007423 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 if (output_length != output_size) {
7425 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7426 }
7427 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007428
7429#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007432 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7434 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7435 output_size);
7436 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7437 output, request_size);
7438 if (ret != 0) {
7439 return mbedtls_to_psa_error(ret);
7440 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007441 output_size -= request_size;
7442 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007443 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007445#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007446}
7447
Gilles Peskine8814fc42020-12-14 15:33:44 +01007448/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007449 *
7450 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7451 * `psa_generate_random(...)`. The state parameter is ignored since the
7452 * PSA API doesn't support passing an explicit state.
7453 *
7454 * In the non-external case, psa_generate_random() calls an
7455 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7456 * and semantics as mbedtls_psa_get_random(). As an optimization,
7457 * instead of doing this back-and-forth between the PSA API and the
7458 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7459 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007461#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7462int mbedtls_psa_get_random(void *p_rng,
7463 unsigned char *output,
7464 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007465{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007466 /* This function takes a pointer to the RNG state because that's what
7467 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7468 * its own state internally and doesn't let the caller access that state.
7469 * So we just ignore the state parameter, and in practice we'll pass
7470 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007471 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 psa_status_t status = psa_generate_random(output, output_size);
7473 if (status == PSA_SUCCESS) {
7474 return 0;
7475 } else {
7476 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7477 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007478}
7479#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7480
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007481#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007482psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7483 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007484{
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 if (global_data.initialized) {
7486 return PSA_ERROR_NOT_PERMITTED;
7487 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7490 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7491 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7492 return PSA_ERROR_INVALID_ARGUMENT;
7493 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007494
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007496}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007497#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007498
Ronald Cron3772afe2021-02-08 16:10:05 +01007499/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007500 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007501 * \param type The key type
7502 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007503 *
7504 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007505 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007506 * \retval #PSA_ERROR_INVALID_ARGUMENT
7507 * The size in bits of the key is not valid.
7508 * \retval #PSA_ERROR_NOT_SUPPORTED
7509 * The type and/or the size in bits of the key or the combination of
7510 * the two is not supported.
7511 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007512static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007514{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (key_type_is_raw_bytes(type)) {
7518 status = psa_validate_unstructured_key_bit_size(type, bits);
7519 if (status != PSA_SUCCESS) {
7520 return status;
7521 }
7522 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007523#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7525 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7526 return PSA_ERROR_NOT_SUPPORTED;
7527 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007528 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007529 return PSA_ERROR_NOT_SUPPORTED;
7530 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007531
Ronald Cron01b2aba2020-10-05 09:42:02 +02007532 /* Accept only byte-aligned keys, for the same reasons as
7533 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (bits % 8 != 0) {
7535 return PSA_ERROR_NOT_SUPPORTED;
7536 }
7537 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007538#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007539
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007540#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007542 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 return PSA_SUCCESS;
7544 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007545#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007546
Valerio Settia55f0422023-07-10 15:34:41 +02007547#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007548 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007549 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007550 return PSA_ERROR_NOT_SUPPORTED;
7551 }
7552 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007553#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007554 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007556 }
7557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007559}
7560
Ronald Cron55ed0592020-10-05 10:30:40 +02007561psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007562 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007564{
7565 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007566 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007567
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 if ((attributes->domain_parameters == NULL) &&
7569 (attributes->domain_parameters_size != 0)) {
7570 return PSA_ERROR_INVALID_ARGUMENT;
7571 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 if (key_type_is_raw_bytes(type)) {
7574 status = psa_generate_random(key_buffer, key_buffer_size);
7575 if (status != PSA_SUCCESS) {
7576 return status;
7577 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007578
David Brown12ca5032021-02-11 11:02:00 -07007579#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 if (type == PSA_KEY_TYPE_DES) {
7581 psa_des_set_key_parity(key_buffer, key_buffer_size);
7582 }
David Brown8107e312021-02-12 08:08:46 -07007583#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007585
Valerio Setti76df8c12023-07-11 14:11:28 +02007586#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7588 return mbedtls_psa_rsa_generate_key(attributes,
7589 key_buffer,
7590 key_buffer_size,
7591 key_buffer_length);
7592 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007593#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007594
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007595#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7597 return mbedtls_psa_ecp_generate_key(attributes,
7598 key_buffer,
7599 key_buffer_size,
7600 key_buffer_length);
7601 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007602#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007603
Valerio Settia55f0422023-07-10 15:34:41 +02007604#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007605 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7606 return mbedtls_psa_ffdh_generate_key(attributes,
7607 key_buffer,
7608 key_buffer_size,
7609 key_buffer_length);
7610 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007611#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007612 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 (void) key_buffer_length;
7614 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007615 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007616
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007618}
Darryl Green0c6575a2018-11-07 16:05:30 +00007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7621 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007622{
7623 psa_status_t status;
7624 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007625 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007626 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007627
Ronald Cron81709fc2020-11-14 12:10:32 +01007628 *key = MBEDTLS_SVC_KEY_ID_INIT;
7629
Gilles Peskine0f84d622019-09-12 19:03:13 +02007630 /* Reject any attempt to create a zero-length key so that we don't
7631 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 if (psa_get_key_bits(attributes) == 0) {
7633 return PSA_ERROR_INVALID_ARGUMENT;
7634 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007635
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007636 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7638 return PSA_ERROR_INVALID_ARGUMENT;
7639 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007640
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7642 &slot, &driver);
7643 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007644 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 }
Gilles Peskine11792082019-08-06 18:36:36 +02007646
Ronald Cron977c2472020-10-13 08:32:21 +02007647 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307648 * storage ( thus not in the case of generating a key in a secure element
7649 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7650 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 if (slot->key.data == NULL) {
7652 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7653 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007654 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 attributes->core.type, attributes->core.bits);
7656 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007659
7660 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 attributes->core.type,
7662 attributes->core.bits);
7663 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007664 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 attributes, &key_buffer_size);
7666 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007667 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 }
Ronald Cron977c2472020-10-13 08:32:21 +02007669 }
Ronald Cron977c2472020-10-13 08:32:21 +02007670
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7672 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007673 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 }
Ronald Cron977c2472020-10-13 08:32:21 +02007675 }
7676
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 status = psa_driver_wrapper_generate_key(attributes,
7678 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007679
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 if (status != PSA_SUCCESS) {
7681 psa_remove_key_data_from_memory(slot);
7682 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007683
Gilles Peskine11792082019-08-06 18:36:36 +02007684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 if (status == PSA_SUCCESS) {
7686 status = psa_finish_key_creation(slot, driver, key);
7687 }
7688 if (status != PSA_SUCCESS) {
7689 psa_fail_key_creation(slot, driver);
7690 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007693}
7694
Gilles Peskinee59236f2018-01-27 23:32:46 +01007695/****************************************************************/
7696/* Module setup */
7697/****************************************************************/
7698
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007699#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007700psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 void (* entropy_init)(mbedtls_entropy_context *ctx),
7702 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007703{
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7705 return PSA_ERROR_BAD_STATE;
7706 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007707 global_data.rng.entropy_init = entropy_init;
7708 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007710}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007711#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007712
Gilles Peskine449bd832023-01-11 14:50:10 +01007713void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007714{
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 psa_wipe_all_key_slots();
7716 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7717 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007718 }
7719 /* Wipe all remaining data, including configuration.
7720 * In particular, this sets all state indicator to the value
7721 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007723
7724 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007726}
7727
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007728#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7729/** Recover a transaction that was interrupted by a power failure.
7730 *
7731 * This function is called during initialization, before psa_crypto_init()
7732 * returns. If this function returns a failure status, the initialization
7733 * fails.
7734 */
7735static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007737{
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007739 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7740 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 /* TODO - fall through to the failure case until this
7742 * is implemented.
7743 * https://github.com/ARMmbed/mbed-crypto/issues/218
7744 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007745 default:
7746 /* We found an unsupported transaction in the storage.
7747 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007749 }
7750}
7751#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7752
Gilles Peskine449bd832023-01-11 14:50:10 +01007753psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007754{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007755 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007756
Gilles Peskinec6b69072018-11-20 21:42:52 +01007757 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 if (global_data.initialized != 0) {
7759 return PSA_SUCCESS;
7760 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007761
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007762 /* Init drivers */
7763 status = psa_driver_wrapper_init();
7764 if (status != PSA_SUCCESS) {
7765 goto exit;
7766 }
7767 global_data.drivers_initialized = 1;
7768
Gilles Peskine30524eb2020-11-13 17:02:26 +01007769 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007771 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 status = mbedtls_psa_random_seed(&global_data.rng);
7773 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007776 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007777
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 status = psa_initialize_key_slots();
7779 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007782
Gilles Peskine4b734222019-07-24 15:56:31 +02007783#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 status = psa_crypto_load_transaction();
7785 if (status == PSA_SUCCESS) {
7786 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7787 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007788 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 }
7790 status = psa_crypto_stop_transaction();
7791 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007792 /* There's no transaction to complete. It's all good. */
7793 status = PSA_SUCCESS;
7794 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007795#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007796
Gilles Peskinec6b69072018-11-20 21:42:52 +01007797 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007798 global_data.initialized = 1;
7799
7800exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 if (status != PSA_SUCCESS) {
7802 mbedtls_psa_crypto_free();
7803 }
7804 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007805}
7806
Yanray Wangffc3c482023-07-11 12:01:04 +08007807#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007808psa_status_t psa_crypto_driver_pake_get_password_len(
7809 const psa_crypto_driver_pake_inputs_t *inputs,
7810 size_t *password_len)
7811{
7812 if (inputs->password_len == 0) {
7813 return PSA_ERROR_BAD_STATE;
7814 }
7815
7816 *password_len = inputs->password_len;
7817
7818 return PSA_SUCCESS;
7819}
7820
7821psa_status_t psa_crypto_driver_pake_get_password(
7822 const psa_crypto_driver_pake_inputs_t *inputs,
7823 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7824{
7825 if (inputs->password_len == 0) {
7826 return PSA_ERROR_BAD_STATE;
7827 }
7828
7829 if (buffer_size < inputs->password_len) {
7830 return PSA_ERROR_BUFFER_TOO_SMALL;
7831 }
7832
7833 memcpy(buffer, inputs->password, inputs->password_len);
7834 *buffer_length = inputs->password_len;
7835
7836 return PSA_SUCCESS;
7837}
7838
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007839psa_status_t psa_crypto_driver_pake_get_user_len(
7840 const psa_crypto_driver_pake_inputs_t *inputs,
7841 size_t *user_len)
7842{
7843 if (inputs->user_len == 0) {
7844 return PSA_ERROR_BAD_STATE;
7845 }
7846
7847 *user_len = inputs->user_len;
7848
7849 return PSA_SUCCESS;
7850}
7851
7852psa_status_t psa_crypto_driver_pake_get_user(
7853 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007854 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007855{
7856 if (inputs->user_len == 0) {
7857 return PSA_ERROR_BAD_STATE;
7858 }
7859
Przemek Stekielc0e62502023-03-14 11:49:36 +01007860 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007861 return PSA_ERROR_BUFFER_TOO_SMALL;
7862 }
7863
Przemek Stekielc0e62502023-03-14 11:49:36 +01007864 memcpy(user_id, inputs->user, inputs->user_len);
7865 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007866
7867 return PSA_SUCCESS;
7868}
7869
7870psa_status_t psa_crypto_driver_pake_get_peer_len(
7871 const psa_crypto_driver_pake_inputs_t *inputs,
7872 size_t *peer_len)
7873{
7874 if (inputs->peer_len == 0) {
7875 return PSA_ERROR_BAD_STATE;
7876 }
7877
7878 *peer_len = inputs->peer_len;
7879
7880 return PSA_SUCCESS;
7881}
7882
7883psa_status_t psa_crypto_driver_pake_get_peer(
7884 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007885 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007886{
7887 if (inputs->peer_len == 0) {
7888 return PSA_ERROR_BAD_STATE;
7889 }
7890
Przemek Stekielc0e62502023-03-14 11:49:36 +01007891 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007892 return PSA_ERROR_BUFFER_TOO_SMALL;
7893 }
7894
Przemek Stekielc0e62502023-03-14 11:49:36 +01007895 memcpy(peer_id, inputs->peer, inputs->peer_len);
7896 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007897
7898 return PSA_SUCCESS;
7899}
7900
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007901psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7902 const psa_crypto_driver_pake_inputs_t *inputs,
7903 psa_pake_cipher_suite_t *cipher_suite)
7904{
7905 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7906 return PSA_ERROR_BAD_STATE;
7907 }
7908
7909 *cipher_suite = inputs->cipher_suite;
7910
7911 return PSA_SUCCESS;
7912}
7913
Neil Armstronga7d08c32022-06-01 18:21:20 +02007914psa_status_t psa_pake_setup(
7915 psa_pake_operation_t *operation,
7916 const psa_pake_cipher_suite_t *cipher_suite)
7917{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007919
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007920 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007921 status = PSA_ERROR_BAD_STATE;
7922 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007923 }
7924
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007925 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007926 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927 status = PSA_ERROR_INVALID_ARGUMENT;
7928 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007929 }
7930
Przemek Stekiel51eac532022-12-07 11:04:51 +01007931 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7932
Przemek Stekiele12ed362022-12-21 12:54:46 +01007933 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007934 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7935 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007936 operation->data.inputs.cipher_suite = *cipher_suite;
7937
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007938#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007939 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007940 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007941 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007942
David Horstmann16f01512023-06-14 17:21:07 +01007943 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007944 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007945 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007946#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007947 {
7948 status = PSA_ERROR_NOT_SUPPORTED;
7949 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007950 }
7951
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007952 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7953
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956 psa_pake_abort(operation);
7957 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007958}
7959
7960psa_status_t psa_pake_set_password_key(
7961 psa_pake_operation_t *operation,
7962 mbedtls_svc_key_id_t password)
7963{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007965 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7966 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007967 psa_key_attributes_t attributes;
7968 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007969
Przemek Stekiel51eac532022-12-07 11:04:51 +01007970 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007971 status = PSA_ERROR_BAD_STATE;
7972 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007973 }
7974
Przemek Stekiel6c764412022-11-22 14:05:12 +01007975 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7976 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007977 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007978 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007979 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980 }
7981
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007982 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007983 .core = slot->attr
7984 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007985
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007986 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007987
Przemek Stekiel51eac532022-12-07 11:04:51 +01007988 if (type != PSA_KEY_TYPE_PASSWORD &&
7989 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7990 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007991 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007992 }
7993
Przemek Stekiel51eac532022-12-07 11:04:51 +01007994 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7995 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007996 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007997 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007998 }
7999
8000 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
8001 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01008002 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008004 if (status != PSA_SUCCESS) {
8005 psa_pake_abort(operation);
8006 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008007 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008008 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008009}
8010
8011psa_status_t psa_pake_set_user(
8012 psa_pake_operation_t *operation,
8013 const uint8_t *user_id,
8014 size_t user_id_len)
8015{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008017
8018 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 status = PSA_ERROR_BAD_STATE;
8020 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008021 }
8022
Przemek Stekiel51eac532022-12-07 11:04:51 +01008023 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008024 status = PSA_ERROR_INVALID_ARGUMENT;
8025 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008026 }
8027
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008028 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008029 status = PSA_ERROR_BAD_STATE;
8030 goto exit;
8031 }
8032
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008033 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8034 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008035 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8036 goto exit;
8037 }
8038
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008039 memcpy(operation->data.inputs.user, user_id, user_id_len);
8040 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008041
8042 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008043exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008044 psa_pake_abort(operation);
8045 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008046}
8047
8048psa_status_t psa_pake_set_peer(
8049 psa_pake_operation_t *operation,
8050 const uint8_t *peer_id,
8051 size_t peer_id_len)
8052{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008054
8055 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056 status = PSA_ERROR_BAD_STATE;
8057 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008058 }
8059
Przemek Stekiel51eac532022-12-07 11:04:51 +01008060 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008061 status = PSA_ERROR_INVALID_ARGUMENT;
8062 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008063 }
8064
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008065 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008066 status = PSA_ERROR_BAD_STATE;
8067 goto exit;
8068 }
8069
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008070 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8071 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008072 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8073 goto exit;
8074 }
8075
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008076 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8077 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008078
8079 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008080exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008081 psa_pake_abort(operation);
8082 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008083}
8084
8085psa_status_t psa_pake_set_role(
8086 psa_pake_operation_t *operation,
8087 psa_pake_role_t role)
8088{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008090
Przemek Stekiel51eac532022-12-07 11:04:51 +01008091 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008092 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008093 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008094 }
8095
Przemek Stekiel09104b82023-03-06 14:11:51 +01008096 switch (operation->alg) {
8097#if defined(PSA_WANT_ALG_JPAKE)
8098 case PSA_ALG_JPAKE:
8099 if (role == PSA_PAKE_ROLE_NONE) {
8100 return PSA_SUCCESS;
8101 }
8102 status = PSA_ERROR_INVALID_ARGUMENT;
8103 break;
8104#endif
8105 default:
8106 (void) role;
8107 status = PSA_ERROR_NOT_SUPPORTED;
8108 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008109 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008110exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008111 psa_pake_abort(operation);
8112 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008113}
8114
David Horstmanne7f21e62023-05-12 18:17:21 +01008115/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008116#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008117static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008118 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008119{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008120 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008121 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008122 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008123
David Horstmann024e5c52023-06-14 15:48:21 +01008124 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008125 is_x1 = (stage->outputs < 1);
8126 } else {
8127 is_x1 = (stage->inputs < 1);
8128 }
8129
David Horstmann74a3d8c2023-06-14 18:28:19 +01008130 key_share_step = is_x1 ?
8131 PSA_JPAKE_X1_STEP_KEY_SHARE :
8132 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008133 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008134 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8135 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8136 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8137 } else {
8138 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008139 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008140 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008141}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008142#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008143
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144static psa_status_t psa_pake_complete_inputs(
8145 psa_pake_operation_t *operation)
8146{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008148 /* Create copy of the inputs on stack as inputs share memory
8149 with the driver context which will be setup by the driver. */
8150 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008151
Przemek Stekielfde11282023-03-13 16:06:09 +01008152 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008153 return PSA_ERROR_BAD_STATE;
8154 }
8155
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008156 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008157 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8158 return PSA_ERROR_BAD_STATE;
8159 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008160 }
8161
Przemek Stekiel18620a32023-01-17 16:34:52 +01008162 /* Clear driver context */
8163 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8164
8165 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008166
8167 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008168 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008169
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008170 /* User and peer are translated to role. */
8171 mbedtls_free(inputs.user);
8172 mbedtls_free(inputs.peer);
8173
Przemek Stekiel2797d372022-12-22 11:19:22 +01008174 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008175#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008176 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008177 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008178 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008179#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008180 {
8181 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008182 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008183 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008184 return status;
8185}
8186
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008187#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008188static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008189 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008190 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008191 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8194 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8195 step != PSA_PAKE_STEP_ZK_PROOF) {
8196 return PSA_ERROR_INVALID_ARGUMENT;
8197 }
8198
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008199 psa_jpake_computation_stage_t *computation_stage =
8200 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201
David Horstmann5da95602023-06-08 15:37:12 +01008202 if (computation_stage->round != PSA_JPAKE_FIRST &&
8203 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008204 return PSA_ERROR_BAD_STATE;
8205 }
8206
David Horstmanne7f21e62023-05-12 18:17:21 +01008207 /* Check that the step we are given is the one we were expecting */
8208 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008209 return PSA_ERROR_BAD_STATE;
8210 }
8211
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8213 computation_stage->inputs == 0 &&
8214 computation_stage->outputs == 0) {
8215 /* Start of the round, so function decides whether we are inputting
8216 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008217 computation_stage->io_mode = io_mode;
8218 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008219 /* Middle of the round so the mode we are in must match the function
8220 * called by the user */
8221 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 }
8223
8224 return PSA_SUCCESS;
8225}
8226
David Horstmanne7f21e62023-05-12 18:17:21 +01008227static psa_status_t psa_jpake_epilogue(
8228 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008229 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230{
David Horstmanne7f21e62023-05-12 18:17:21 +01008231 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008232 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233
David Horstmanne7f21e62023-05-12 18:17:21 +01008234 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8235 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008236 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008237 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008238 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008239 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008240 }
8241 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008242 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008243 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008244 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008245 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008246 }
8247 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008248 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8249 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008250 /* End of a round, move to the next round */
8251 stage->inputs = 0;
8252 stage->outputs = 0;
8253 stage->round++;
8254 }
8255 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008256 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008257 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008259 return PSA_SUCCESS;
8260}
David Horstmanne7f21e62023-05-12 18:17:21 +01008261
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008262#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263
Neil Armstronga7d08c32022-06-01 18:21:20 +02008264psa_status_t psa_pake_output(
8265 psa_pake_operation_t *operation,
8266 psa_pake_step_t step,
8267 uint8_t *output,
8268 size_t output_size,
8269 size_t *output_length)
8270{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008272 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008273 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008274
8275 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008276 status = psa_pake_complete_inputs(operation);
8277 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008279 }
8280 }
8281
8282 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008283 status = PSA_ERROR_BAD_STATE;
8284 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008285 }
8286
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008287 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288 status = PSA_ERROR_INVALID_ARGUMENT;
8289 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008290 }
8291
Przemek Stekiele12ed362022-12-21 12:54:46 +01008292 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008293#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008295 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008296 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008297 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008298 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008299 driver_step = convert_jpake_computation_stage_to_driver_step(
8300 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008301 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008302#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008303 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008304 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008305 status = PSA_ERROR_NOT_SUPPORTED;
8306 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008307 }
8308
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008309 status = psa_driver_wrapper_pake_output(operation, driver_step,
8310 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008311
8312 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008314 }
8315
8316 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008317#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008318 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008319 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008320 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008321 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008322 }
8323 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008324#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008326 status = PSA_ERROR_NOT_SUPPORTED;
8327 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 }
8329
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008330 return PSA_SUCCESS;
8331exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008332 psa_pake_abort(operation);
8333 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008334}
8335
8336psa_status_t psa_pake_input(
8337 psa_pake_operation_t *operation,
8338 psa_pake_step_t step,
8339 const uint8_t *input,
8340 size_t input_length)
8341{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008343 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008344 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8345 operation->primitive,
8346 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008347
8348 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008349 status = psa_pake_complete_inputs(operation);
8350 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008351 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008352 }
8353 }
8354
8355 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008356 status = PSA_ERROR_BAD_STATE;
8357 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008358 }
8359
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008360 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 status = PSA_ERROR_INVALID_ARGUMENT;
8362 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008363 }
8364
Przemek Stekiele12ed362022-12-21 12:54:46 +01008365 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008366#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008367 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008368 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008369 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008370 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008371 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008372 driver_step = convert_jpake_computation_stage_to_driver_step(
8373 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008374 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008375#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008376 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008377 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008378 status = PSA_ERROR_NOT_SUPPORTED;
8379 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008380 }
8381
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008382 status = psa_driver_wrapper_pake_input(operation, driver_step,
8383 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008384
8385 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008386 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008387 }
8388
8389 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008390#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008391 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008392 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008393 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008394 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008395 }
8396 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008397#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008398 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008399 status = PSA_ERROR_NOT_SUPPORTED;
8400 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008401 }
8402
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008403 return PSA_SUCCESS;
8404exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008405 psa_pake_abort(operation);
8406 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008407}
8408
8409psa_status_t psa_pake_get_implicit_key(
8410 psa_pake_operation_t *operation,
8411 psa_key_derivation_operation_t *output)
8412{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008413 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008414 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008415 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008416 size_t shared_key_len = 0;
8417
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008418 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008419 status = PSA_ERROR_BAD_STATE;
8420 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008421 }
8422
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008423#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008424 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008425 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008426 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008427 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008428 status = PSA_ERROR_BAD_STATE;
8429 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008430 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008431 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008432#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008433 {
8434 status = PSA_ERROR_NOT_SUPPORTED;
8435 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008436 }
8437
Przemek Stekiel0c781802022-11-29 14:53:13 +01008438 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8439 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008440 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008441 &shared_key_len);
8442
8443 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008444 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008445 }
8446
8447 status = psa_key_derivation_input_bytes(output,
8448 PSA_KEY_DERIVATION_INPUT_SECRET,
8449 shared_key,
8450 shared_key_len);
8451
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008452 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008453exit:
8454 abort_status = psa_pake_abort(operation);
8455 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008456}
8457
8458psa_status_t psa_pake_abort(
8459 psa_pake_operation_t *operation)
8460{
Przemek Stekield69dca92023-01-31 19:59:20 +01008461 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008462
Przemek Stekield69dca92023-01-31 19:59:20 +01008463 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008464 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008465 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008466
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008467 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8468 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008469 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008470 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008471 }
8472 if (operation->data.inputs.user != NULL) {
8473 mbedtls_free(operation->data.inputs.user);
8474 }
8475 if (operation->data.inputs.peer != NULL) {
8476 mbedtls_free(operation->data.inputs.peer);
8477 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008478 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008479 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008480
Przemek Stekield69dca92023-01-31 19:59:20 +01008481 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008482}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008483#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008484
David Horstmanne138dce2023-11-28 15:21:56 +00008485/* Memory copying test hooks */
8486#if defined(MBEDTLS_TEST_HOOKS)
8487void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8488void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
8489void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8490void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
8491#endif
David Horstmannfde97392023-10-30 20:29:43 +00008492
David Horstmann1b7279a2023-11-15 15:18:30 +00008493/** Copy from an input buffer to a local copy.
8494 *
8495 * \param[in] input Pointer to input buffer.
8496 * \param[in] input_len Length of the input buffer.
8497 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8498 * \param[out] input_copy_len Length of the local copy buffer.
8499 * \return #PSA_SUCCESS, if the buffer was successfully
8500 * copied.
8501 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8502 * copy is too small to hold contents of the
8503 * input buffer.
8504 */
8505MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008506psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8507 uint8_t *input_copy, size_t input_copy_len)
8508{
8509 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008510 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008511 }
8512
David Horstmann372b8bb2023-11-24 16:21:04 +00008513#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008514 if (psa_input_pre_copy_hook != NULL) {
8515 psa_input_pre_copy_hook(input, input_len);
8516 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008517#endif
8518
David Horstmann777e7412023-11-15 17:33:47 +00008519 if (input_len > 0) {
8520 memcpy(input_copy, input, input_len);
8521 }
David Horstmannfde97392023-10-30 20:29:43 +00008522
David Horstmann372b8bb2023-11-24 16:21:04 +00008523#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008524 if (psa_input_post_copy_hook != NULL) {
8525 psa_input_post_copy_hook(input, input_len);
8526 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008527#endif
8528
David Horstmannfde97392023-10-30 20:29:43 +00008529 return PSA_SUCCESS;
8530}
8531
David Horstmann1b7279a2023-11-15 15:18:30 +00008532/** Copy from a local output buffer into a user-supplied one.
8533 *
8534 * \param[in] output_copy Pointer to a local buffer containing the output.
8535 * \param[in] output_copy_len Length of the local buffer.
8536 * \param[out] output Pointer to user-supplied output buffer.
8537 * \param[out] output_len Length of the user-supplied output buffer.
8538 * \return #PSA_SUCCESS, if the buffer was successfully
8539 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008540 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008541 * user-supplied output buffer is too small to
8542 * hold the contents of the local buffer.
8543 */
8544MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008545psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8546 uint8_t *output, size_t output_len)
8547{
8548 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008549 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008550 }
David Horstmann777e7412023-11-15 17:33:47 +00008551
David Horstmann372b8bb2023-11-24 16:21:04 +00008552#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008553 if (psa_output_pre_copy_hook != NULL) {
8554 psa_output_pre_copy_hook(output, output_len);
8555 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008556#endif
8557
David Horstmann777e7412023-11-15 17:33:47 +00008558 if (output_copy_len > 0) {
8559 memcpy(output, output_copy, output_copy_len);
8560 }
8561
David Horstmann372b8bb2023-11-24 16:21:04 +00008562#if defined(MBEDTLS_TEST_HOOKS)
David Horstmanne138dce2023-11-28 15:21:56 +00008563 if (psa_output_post_copy_hook != NULL) {
8564 psa_output_post_copy_hook(output, output_len);
8565 }
David Horstmann372b8bb2023-11-24 16:21:04 +00008566#endif
8567
David Horstmann8978f5c2023-10-30 20:37:12 +00008568 return PSA_SUCCESS;
8569}
8570
David Horstmannf1734052023-11-20 17:15:32 +00008571psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8572 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008573{
8574 psa_status_t status;
8575
David Horstmann9db14482023-11-23 15:50:37 +00008576 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann4ac78852023-11-07 16:36:48 +00008577
David Horstmannc5cc1c32023-11-15 18:11:26 +00008578 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008579 return PSA_SUCCESS;
8580 }
8581
David Horstmannf1734052023-11-20 17:15:32 +00008582 local_input->buffer = mbedtls_calloc(input_len, 1);
8583 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008584 /* Since we dealt with the zero-length case above, we know that
8585 * a NULL return value means a failure of allocation. */
8586 return PSA_ERROR_INSUFFICIENT_MEMORY;
8587 }
David Horstmannf1734052023-11-20 17:15:32 +00008588 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008589
David Horstmannf1734052023-11-20 17:15:32 +00008590 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008591
8592 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008593 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008594 if (status != PSA_SUCCESS) {
8595 goto error;
8596 }
8597
8598 return PSA_SUCCESS;
8599
8600error:
David Horstmannf1734052023-11-20 17:15:32 +00008601 mbedtls_free(local_input->buffer);
8602 local_input->buffer = NULL;
8603 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008604 return status;
8605}
8606
David Horstmannf1734052023-11-20 17:15:32 +00008607void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008608{
David Horstmannf1734052023-11-20 17:15:32 +00008609 mbedtls_free(local_input->buffer);
8610 local_input->buffer = NULL;
8611 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008612}
8613
David Horstmann89875a42023-11-20 12:54:09 +00008614psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
8615 psa_crypto_local_output_t *local_output)
David Horstmannba3c7d62023-11-08 15:19:43 +00008616{
David Horstmann9db14482023-11-23 15:50:37 +00008617 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmannba3c7d62023-11-08 15:19:43 +00008618
David Horstmannc5cc1c32023-11-15 18:11:26 +00008619 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008620 return PSA_SUCCESS;
8621 }
David Horstmann89875a42023-11-20 12:54:09 +00008622 local_output->buffer = mbedtls_calloc(output_len, 1);
8623 if (local_output->buffer == NULL) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008624 /* Since we dealt with the zero-length case above, we know that
8625 * a NULL return value means a failure of allocation. */
8626 return PSA_ERROR_INSUFFICIENT_MEMORY;
8627 }
David Horstmann89875a42023-11-20 12:54:09 +00008628 local_output->length = output_len;
8629 local_output->original = output;
David Horstmannba3c7d62023-11-08 15:19:43 +00008630
8631 return PSA_SUCCESS;
8632}
8633
David Horstmann89875a42023-11-20 12:54:09 +00008634psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmann9467ea32023-11-08 17:44:18 +00008635{
David Horstmannc335a4e2023-11-15 15:57:32 +00008636 psa_status_t status;
8637
David Horstmann89875a42023-11-20 12:54:09 +00008638 if (local_output->buffer == NULL) {
8639 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008640 return PSA_SUCCESS;
8641 }
David Horstmann89875a42023-11-20 12:54:09 +00008642 if (local_output->original == NULL) {
David Horstmann9467ea32023-11-08 17:44:18 +00008643 /* We have an internal copy but nothing to copy back to. */
8644 return PSA_ERROR_CORRUPTION_DETECTED;
8645 }
8646
David Horstmann89875a42023-11-20 12:54:09 +00008647 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
8648 local_output->original, local_output->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008649 if (status != PSA_SUCCESS) {
8650 return status;
8651 }
David Horstmann9467ea32023-11-08 17:44:18 +00008652
David Horstmann89875a42023-11-20 12:54:09 +00008653 mbedtls_free(local_output->buffer);
8654 local_output->buffer = NULL;
8655 local_output->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008656
8657 return PSA_SUCCESS;
8658}
8659
Gilles Peskinee59236f2018-01-27 23:32:46 +01008660#endif /* MBEDTLS_PSA_CRYPTO_C */