blob: 414ed1d8a94da8d66af6d6fcfbbb54ddfabda1ef [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 Rodgman7ff79652023-11-03 12:04:52 +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"
Gilles Peskinee59236f2018-01-27 23:32:46 +010010
11#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030012
John Durkop07cc04a2020-11-16 22:08:34 -080013#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
14#include "check_crypto_config.h"
15#endif
16
Gilles Peskinee59236f2018-01-27 23:32:46 +010017#include "psa/crypto.h"
18
Ronald Crond6d28882020-12-14 14:56:02 +010019#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010020#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010021#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020022#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010024#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010025#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010026#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010027#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020029#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020030#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010031#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010032/* Include internal declarations that are useful for implementing persistently
33 * stored keys. */
34#include "psa_crypto_storage.h"
35
Gilles Peskineb2b64d32020-12-14 16:43:58 +010036#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038#include <stdlib.h>
39#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010042#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020044#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000045#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020046#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010047#include "mbedtls/blowfish.h"
48#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020049#include "mbedtls/chacha20.h"
50#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010051#include "mbedtls/cipher.h"
52#include "mbedtls/ccm.h"
53#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010054#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020055#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010056#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010057#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/error.h"
59#include "mbedtls/gcm.h"
60#include "mbedtls/md2.h"
61#include "mbedtls/md4.h"
62#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010063#include "mbedtls/md.h"
64#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010065#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010067#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000068#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/sha1.h"
72#include "mbedtls/sha256.h"
73#include "mbedtls/sha512.h"
74#include "mbedtls/xtea.h"
75
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020077
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078/****************************************************************/
79/* Global data, support functions and library management */
80/****************************************************************/
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020085}
86
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010087/* Values for psa_global_data_t::rng_state */
88#define RNG_NOT_INITIALIZED 0
89#define RNG_INITIALIZED 1
90#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010093 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010094 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010095 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +010096} psa_global_data_t;
97
98static psa_global_data_t global_data;
99
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100100#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
101mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
102 &global_data.rng.drbg;
103#endif
104
itayzafrir0adf0fc2018-09-06 16:24:41 +0300105#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (global_data.initialized == 0) \
107 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300108
David Horstmannc0a2c302023-11-29 17:24:08 +0000109#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
David Horstmann34980bd2023-11-29 17:07:13 +0000110/* Substitute an input buffer for a local copy of itself.
111 * Assumptions:
112 * - psa_status_t status exists
113 * - An exit label is declared
114 * - The name <buffer>_copy is not used for the given value of <buffer>
115 */
116#define SWAP_FOR_LOCAL_INPUT(input, length) \
117 psa_crypto_local_input_t input ## _copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
118 status = psa_crypto_local_input_alloc(input, length, &input ## _copy); \
119 if (status != PSA_SUCCESS) { \
120 goto exit; \
121 } \
122 input = input ## _copy.buffer;
123
124#define FREE_LOCAL_INPUT(input) \
125 psa_crypto_local_input_free(&input ## _copy);
126
127/* Substitute an output buffer for a local copy of itself.
128 * Assumptions:
129 * - psa_status_t status exists
130 * - An exit label is declared
131 * - The name <buffer>_copy is not used for the given value of <buffer>
132 */
133#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
134 psa_crypto_local_output_t output ## _copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
135 status = psa_crypto_local_output_alloc(output, length, &output ## _copy); \
136 if (status != PSA_SUCCESS) { \
137 goto exit; \
138 } \
139 output = output ## _copy.buffer;
140
141#define FREE_LOCAL_OUTPUT(output) \
142 psa_status_t local_output_free_status; \
143 local_output_free_status = psa_crypto_local_output_free(&output ## _copy); \
144 if (local_output_free_status != PSA_SUCCESS) { \
145 status = local_output_free_status; \
146 }
David Horstmannc0a2c302023-11-29 17:24:08 +0000147#else /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
148#define SWAP_FOR_LOCAL_INPUT(input, length)
149#define FREE_LOCAL_INPUT(input)
150#define SWAP_FOR_LOCAL_OUTPUT(output, length)
151#define FREE_LOCAL_OUTPUT(output)
152#endif /* MBEDTLS_PSA_COPY_CALLER_BUFFERS */
David Horstmann34980bd2023-11-29 17:07:13 +0000153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100156 /* Mbed TLS error codes can combine a high-level error code and a
157 * low-level error code. The low-level error usually reflects the
158 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 int low_level_ret = -(-ret & 0x007f);
160 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100161 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200184
Jaeden Amero93e21112019-02-20 13:57:28 +0000185#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000186 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000187#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
188 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
189#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
Jaeden Amero93e21112019-02-20 13:57:28 +0000195#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000196 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000197#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
198 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
199#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
205 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211
Gilles Peskine26869f22019-05-06 15:25:00 +0200212 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200214
215 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200217 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200219
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
237 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
241 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100242 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
243 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
247 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100251#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
253 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257
Gilles Peskinee59236f2018-01-27 23:32:46 +0100258 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
259 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
260 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262
263 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100265 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269
Gilles Peskine82e57d12020-11-13 21:31:17 +0100270#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100272 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
273 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100274 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100276 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
277 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100279 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100281#endif
282
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200283 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
284 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
285 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200287
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100290 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100292 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200296 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298
Gilles Peskinef76aa772018-10-29 19:24:33 +0100299 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100301 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100303 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100305 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100307 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100309 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100311 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100313 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100315
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
319 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
324 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100326 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100328 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
329 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100331 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100333 case MBEDTLS_ERR_PK_INVALID_ALG:
334 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
335 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100337 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341
Gilles Peskineff2d2002019-05-06 15:26:23 +0200342 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200344 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200346
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200347 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200349
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
359 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100361 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100363 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100365 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100367 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100369 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100371
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200372 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
373 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
374 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200376
Gilles Peskinea5905292018-02-07 20:59:33 +0100377 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100379 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100381
itayzafrir5c753392018-05-08 11:18:38 +0300382 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300383 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300385 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300387 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300389 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
390 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300392 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100394 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300396 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100398
Janos Follatha13b9052019-11-22 12:48:59 +0000399 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300401
Gilles Peskinee59236f2018-01-27 23:32:46 +0100402 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100404 }
405}
406
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200408
409
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100410/****************************************************************/
411/* Key management */
412/****************************************************************/
413
John Durkop9814fa22020-11-04 12:28:15 -0800414#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800415 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100416 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
417 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
418 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
420 size_t bits,
421 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200422{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#endif
430#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100433#endif
434#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#endif
438#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
442#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 if (bits_is_sloppy) {
447 return MBEDTLS_ECP_DP_SECP521R1;
448 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100468 }
469 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100470
Paul Elliott8ff510a2020-06-02 17:19:28 +0100471 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100473#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100474 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100476 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 if (bits_is_sloppy) {
478 return MBEDTLS_ECP_DP_CURVE25519;
479 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100486 }
487 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100488
Paul Elliott8ff510a2020-06-02 17:19:28 +0100489 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100491#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100492 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100494#endif
495#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100498#endif
499#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100500 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100502#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100503 }
504 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200505 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100506
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100507 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200509}
John Durkop9814fa22020-11-04 12:28:15 -0800510#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100511 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
512 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
513 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
514 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
517 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518{
519 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200521 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 case PSA_KEY_TYPE_DERIVE:
524 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100525#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 if (bits != 128 && bits != 192 && bits != 256) {
528 return PSA_ERROR_INVALID_ARGUMENT;
529 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530 break;
531#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200532#if defined(PSA_WANT_KEY_TYPE_ARIA)
533 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if (bits != 128 && bits != 192 && bits != 256) {
535 return PSA_ERROR_INVALID_ARGUMENT;
536 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200537 break;
538#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100539#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (bits != 128 && bits != 192 && bits != 256) {
542 return PSA_ERROR_INVALID_ARGUMENT;
543 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544 break;
545#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100546#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200547 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 if (bits != 64 && bits != 128 && bits != 192) {
549 return PSA_ERROR_INVALID_ARGUMENT;
550 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200551 break;
552#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100553#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200554 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 if (bits < 8 || bits > 2048) {
556 return PSA_ERROR_INVALID_ARGUMENT;
557 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200558 break;
559#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100560#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200561 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if (bits != 256) {
563 return PSA_ERROR_INVALID_ARGUMENT;
564 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200565 break;
566#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200569 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 if (bits % 8 != 0) {
571 return PSA_ERROR_INVALID_ARGUMENT;
572 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200575}
576
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100577/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100578 *
Steven Cooremancd640932021-03-08 12:00:27 +0100579 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
580 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100581 *
582 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 * \param[in] key_type The key type of the key to be used with the
584 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100585 *
586 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100587 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100588 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100589 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100590 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100591MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100592 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100594{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 if (PSA_ALG_IS_HMAC(algorithm)) {
596 if (key_type == PSA_KEY_TYPE_HMAC) {
597 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100598 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100599 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
602 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
603 * key. */
604 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
605 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
606 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
607 * the block length (larger than 1) for block ciphers. */
608 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
609 return PSA_SUCCESS;
610 }
611 }
612 }
613
614 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100615}
616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
618 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200619{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (slot->key.data != NULL) {
621 return PSA_ERROR_ALREADY_EXISTS;
622 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 slot->key.data = mbedtls_calloc(1, buffer_length);
625 if (slot->key.data == NULL) {
626 return PSA_ERROR_INSUFFICIENT_MEMORY;
627 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200628
Ronald Cronea0f8a62020-11-25 17:52:23 +0100629 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200631}
632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
634 const uint8_t *data,
635 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200636{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100637 psa_status_t status = psa_allocate_buffer_to_slot(slot,
638 data_length);
639 if (status != PSA_SUCCESS) {
640 return status;
641 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 memcpy(slot->key.data, data, data_length);
644 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200645}
646
Ronald Crona0fe59f2020-11-29 11:14:53 +0100647psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100648 const psa_key_attributes_t *attributes,
649 const uint8_t *data, size_t data_length,
650 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
654 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200656 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 if (data_length == 0) {
658 return PSA_ERROR_NOT_SUPPORTED;
659 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 if (key_type_is_raw_bytes(type)) {
662 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200663
Steven Cooreman75b74362020-07-28 14:30:13 +0200664 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 if (data_length > SIZE_MAX / 8) {
666 return PSA_ERROR_NOT_SUPPORTED;
667 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200668
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200669 /* Enforce a size limit, and in particular ensure that the bit
670 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100671 if ((*bits) > PSA_MAX_KEY_BITS) {
672 return PSA_ERROR_NOT_SUPPORTED;
673 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 status = validate_unstructured_key_bit_size(type, *bits);
676 if (status != PSA_SUCCESS) {
677 return status;
678 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200679
Ronald Crondd04d422020-11-28 15:14:42 +0100680 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100682 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 return PSA_SUCCESS;
686 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800687#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
689 if (PSA_KEY_TYPE_IS_ECC(type)) {
690 return mbedtls_psa_ecp_import_key(attributes,
691 data, data_length,
692 key_buffer, key_buffer_size,
693 key_buffer_length,
694 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100695 }
John Durkop9814fa22020-11-04 12:28:15 -0800696#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
697 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700698#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
700 if (PSA_KEY_TYPE_IS_RSA(type)) {
701 return mbedtls_psa_rsa_import_key(attributes,
702 data, data_length,
703 key_buffer, key_buffer_size,
704 key_buffer_length,
705 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200706 }
John Durkop9814fa22020-11-04 12:28:15 -0800707#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
708 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100709 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100712}
713
Gilles Peskinef603c712019-01-19 13:40:11 +0100714/** Calculate the intersection of two algorithm usage policies.
715 *
716 * Return 0 (which allows no operation) on incompatibility.
717 */
718static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100719 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100720 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100722{
Gilles Peskine549ea862019-05-22 11:45:59 +0200723 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 if (alg1 == alg2) {
725 return alg1;
726 }
Steven Cooreman03488022021-02-18 12:07:20 +0100727 /* If the policies are from the same hash-and-sign family, check
728 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
730 PSA_ALG_IS_SIGN_HASH(alg2) &&
731 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
732 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
733 return alg2;
734 }
735 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
736 return alg1;
737 }
Steven Cooreman03488022021-02-18 12:07:20 +0100738 }
739 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100740 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100741 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100742 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
743 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
744 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
745 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
746 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100747 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100748
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100749 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100750 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
751 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
752 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
753 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100754 }
755 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
757 (alg1_len <= alg2_len)) {
758 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100759 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
761 (alg2_len <= alg1_len)) {
762 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100763 }
764 }
765 /* If the policies are from the same MAC family, check whether one
766 * of them is a minimum-MAC-length policy. Calculate the most
767 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
769 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
770 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100771 /* Validate the combination of key type and algorithm. Since the base
772 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
774 return 0;
775 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100776
Steven Cooreman31a876d2021-03-03 20:47:40 +0100777 /* Get the (exact or at-least) output lengths for both sides of the
778 * requested intersection. None of the currently supported algorithms
779 * have an output length dependent on the actual key size, so setting it
780 * to a bogus value of 0 is currently OK.
781 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100782 * Note that for at-least-this-length wildcard algorithms, the output
783 * length is set to the shortest allowed length, which allows us to
784 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
786 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100787 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100788
Steven Cooremana1d83222021-02-25 10:20:29 +0100789 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
791 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
792 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100793 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100794
795 /* If only one is an at-least-this-length policy, the intersection would
796 * be the other (fixed-length) policy as long as said fixed length is
797 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
799 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100800 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
802 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100803 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100804
Steven Cooremancd640932021-03-08 12:00:27 +0100805 /* If none of them are wildcards, check whether they define the same tag
806 * length. This is still possible here when one is default-length and
807 * the other specific-length. Ensure to always return the
808 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100809 if (alg1_len == alg2_len) {
810 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
811 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100812 }
813 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100815}
816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817static int psa_key_algorithm_permits(psa_key_type_t key_type,
818 psa_algorithm_t policy_alg,
819 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200820{
Gilles Peskine549ea862019-05-22 11:45:59 +0200821 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if (requested_alg == policy_alg) {
823 return 1;
824 }
Steven Cooreman03488022021-02-18 12:07:20 +0100825 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
826 * and requested_alg is the same hash-and-sign family with any hash,
827 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
829 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
830 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
831 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100832 }
833 /* If policy_alg is a wildcard AEAD algorithm of the same base as
834 * the requested algorithm, check the requested tag length to be
835 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 if (PSA_ALG_IS_AEAD(policy_alg) &&
837 PSA_ALG_IS_AEAD(requested_alg) &&
838 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
839 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
840 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
841 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
842 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100843 }
Steven Cooremancd640932021-03-08 12:00:27 +0100844 /* If policy_alg is a MAC algorithm of the same base as the requested
845 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100846 if (PSA_ALG_IS_MAC(policy_alg) &&
847 PSA_ALG_IS_MAC(requested_alg) &&
848 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
849 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100850 /* Validate the combination of key type and algorithm. Since the policy
851 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100852 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
853 return 0;
854 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100855
Steven Cooreman31a876d2021-03-03 20:47:40 +0100856 /* Get both the requested output length for the algorithm which is to be
857 * verified, and the default output length for the base algorithm.
858 * Note that none of the currently supported algorithms have an output
859 * length dependent on actual key size, so setting it to a bogus value
860 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100861 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100863 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 key_type, 0,
865 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100866
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100867 /* If the policy is default-length, only allow an algorithm with
868 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
870 return requested_output_length == default_output_length;
871 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100872
873 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100874 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
876 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
877 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100878 }
879
Steven Cooremancd640932021-03-08 12:00:27 +0100880 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
881 * check for the requested MAC length to be equal to or longer than the
882 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
884 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
885 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100886 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200887 }
Steven Cooremance48e852020-10-05 16:02:45 +0200888 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200889 * a key derivation with that key agreement should also be allowed. This
890 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
892 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
893 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
894 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200895 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100896 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200898}
899
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100900/** Test whether a policy permits an algorithm.
901 *
902 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100903 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100904 * \note This function requires providing the key type for which the policy is
905 * being validated, since some algorithm policy definitions (e.g. MAC)
906 * have different properties depending on what kind of cipher it is
907 * combined with.
908 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100909 * \retval PSA_SUCCESS When \p alg is a specific algorithm
910 * allowed by the \p policy.
911 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
912 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
913 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100915static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
916 psa_key_type_t key_type,
917 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918{
Steven Cooremand788fab2021-02-25 11:29:17 +0100919 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 if (alg == 0) {
921 return PSA_ERROR_INVALID_ARGUMENT;
922 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100923
Steven Cooreman7e39f052021-02-23 14:18:32 +0100924 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 if (PSA_ALG_IS_WILDCARD(alg)) {
926 return PSA_ERROR_INVALID_ARGUMENT;
927 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
930 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
931 return PSA_SUCCESS;
932 } else {
933 return PSA_ERROR_NOT_PERMITTED;
934 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100935}
936
Gilles Peskinef603c712019-01-19 13:40:11 +0100937/** Restrict a key policy based on a constraint.
938 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100939 * \note This function requires providing the key type for which the policy is
940 * being restricted, since some algorithm policy definitions (e.g. MAC)
941 * have different properties depending on what kind of cipher it is
942 * combined with.
943 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100944 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100945 * \param[in,out] policy The policy to restrict.
946 * \param[in] constraint The policy constraint to apply.
947 *
948 * \retval #PSA_SUCCESS
949 * \c *policy contains the intersection of the original value of
950 * \c *policy and \c *constraint.
951 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100952 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100953 * \c *policy is unchanged.
954 */
955static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100956 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100957 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100958 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100959{
960 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 psa_key_policy_algorithm_intersection(key_type, policy->alg,
962 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200963 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
965 constraint->alg2);
966 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
967 return PSA_ERROR_INVALID_ARGUMENT;
968 }
969 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
970 return PSA_ERROR_INVALID_ARGUMENT;
971 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100972 policy->usage &= constraint->usage;
973 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200974 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100975 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100976}
977
Ronald Cron5c522922020-11-14 16:35:34 +0100978/** Get the description of a key given its identifier and policy constraints
979 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980 *
Ronald Cron5c522922020-11-14 16:35:34 +0100981 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100982 * nonzero, the key must allow operations with this algorithm. If \p alg is
983 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100984 *
Ronald Cron5c522922020-11-14 16:35:34 +0100985 * In case of a persistent key, the function loads the description of the key
986 * into a key slot if not already done.
987 *
988 * On success, the returned key slot is locked. It is the responsibility of
989 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990 */
Ronald Cron5c522922020-11-14 16:35:34 +0100991static psa_status_t psa_get_and_lock_key_slot_with_policy(
992 mbedtls_svc_key_id_t key,
993 psa_key_slot_t **p_slot,
994 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100995 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100996{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
998 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 status = psa_get_and_lock_key_slot(key, p_slot);
1001 if (status != PSA_SUCCESS) {
1002 return status;
1003 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001004 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001005
1006 /* Enforce that usage policy for the key slot contains all the flags
1007 * required by the usage parameter. There is one exception: public
1008 * keys can always be exported, so we treat public key objects as
1009 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001011 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001012 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001015 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001016 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001017 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001018
Shaun Case0e7791f2021-12-20 21:14:10 -08001019 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001020 if (alg != 0) {
1021 status = psa_key_policy_permits(&slot->attr.policy,
1022 slot->attr.type,
1023 alg);
1024 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001025 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001027 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001030
1031error:
1032 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001033 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001036}
Darryl Green940d72c2018-07-13 13:18:51 +01001037
Ronald Cron5c522922020-11-14 16:35:34 +01001038/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001039 *
1040 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001041 * available, as opposed to a key in a secure element and/or to be used
1042 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001043 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001044 * This is a temporary function that may be used instead of
1045 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1046 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001047 *
Ronald Cron5c522922020-11-14 16:35:34 +01001048 * On success, the returned key slot is locked. It is the responsibility of the
1049 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001050 */
Ronald Cron5c522922020-11-14 16:35:34 +01001051static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1052 mbedtls_svc_key_id_t key,
1053 psa_key_slot_t **p_slot,
1054 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001056{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1058 usage, alg);
1059 if (status != PSA_SUCCESS) {
1060 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001061 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1064 psa_unlock_key_slot(*p_slot);
1065 *p_slot = NULL;
1066 return PSA_ERROR_NOT_SUPPORTED;
1067 }
1068
1069 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001070}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001073{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001074 /* Data pointer will always be either a valid pointer or NULL in an
1075 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 if (slot->key.data != NULL) {
1077 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1078 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001081 slot->key.data = NULL;
1082 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001085}
1086
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001087/** Completely wipe a slot in memory, including its policy.
1088 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001089psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001090{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001092
1093 /*
1094 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001095 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001096 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1097 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001098 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001099 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001101#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001102 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001103#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001104 status = PSA_ERROR_CORRUPTION_DETECTED;
1105 }
1106
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001107 /* Multipart operations may still be using the key. This is safe
1108 * because all multipart operation objects are independent from
1109 * the key slot: if they need to access the key after the setup
1110 * phase, they have a copy of the key. Note that this means that
1111 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001112 /* At this point, key material and other type-specific content has
1113 * been wiped. Clear remaining metadata. We can call memset and not
1114 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 memset(slot, 0, sizeof(*slot));
1116 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001117}
1118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001121 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001122 psa_status_t status; /* status of the last operation */
1123 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001124#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1125 psa_se_drv_table_entry_t *driver;
1126#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 if (mbedtls_svc_key_id_is_null(key)) {
1129 return PSA_SUCCESS;
1130 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001131
Ronald Cronf2911112020-10-29 17:51:10 +01001132 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001133 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001134 * key, this will load the key description from persistent memory if not
1135 * done yet. We cannot avoid this loading as without it we don't know if
1136 * the key is operated by an SE or not and this information is needed by
1137 * the current implementation.
1138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 status = psa_get_and_lock_key_slot(key, &slot);
1140 if (status != PSA_SUCCESS) {
1141 return status;
1142 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001143
Ronald Cronf2911112020-10-29 17:51:10 +01001144 /*
1145 * If the key slot containing the key description is under access by the
1146 * library (apart from the present access), the key cannot be destroyed
1147 * yet. For the time being, just return in error. Eventually (to be
1148 * implemented), the key should be destroyed when all accesses have
1149 * stopped.
1150 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 if (slot->lock_count > 1) {
1152 psa_unlock_key_slot(slot);
1153 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001154 }
1155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001157 /* Refuse the destruction of a read-only key (which may or may not work
1158 * if we attempt it, depending on whether the key is merely read-only
1159 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001160 * Just do the best we can, which is to wipe the copy in memory
1161 * (done in this function's cleanup code). */
1162 overall_status = PSA_ERROR_NOT_PERMITTED;
1163 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001164 }
1165
Gilles Peskine354f7672019-07-12 23:46:38 +02001166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1168 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001169 /* For a key in a secure element, we need to do three things:
1170 * remove the key file in internal storage, destroy the
1171 * key inside the secure element, and update the driver's
1172 * persistent data. Start a transaction that will encompass these
1173 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001175 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001176 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001177 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 status = psa_crypto_save_transaction();
1179 if (status != PSA_SUCCESS) {
1180 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 /* We should still try to destroy the key in the secure
1182 * element and the key metadata in storage. This is especially
1183 * important if the error is that the storage is full.
1184 * But how to do it exactly without risking an inconsistent
1185 * state after a reset?
1186 * https://github.com/ARMmbed/mbed-crypto/issues/215
1187 */
1188 overall_status = status;
1189 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001190 }
1191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001192 status = psa_destroy_se_key(driver,
1193 psa_key_slot_get_slot_number(slot));
1194 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001195 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001197 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001198#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1199
Darryl Greend49a4992018-06-18 17:27:26 +01001200#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1202 status = psa_destroy_persistent_key(slot->attr.id);
1203 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001204 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001206
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001207 /* TODO: other slots may have a copy of the same key. We should
1208 * invalidate them.
1209 * https://github.com/ARMmbed/mbed-crypto/issues/214
1210 */
Darryl Greend49a4992018-06-18 17:27:26 +01001211 }
1212#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001213
Gilles Peskinefc762652019-07-22 19:30:34 +02001214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 if (driver != NULL) {
1216 status = psa_save_se_persistent_data(driver);
1217 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001218 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 }
1220 status = psa_crypto_stop_transaction();
1221 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001224 }
1225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1226
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001227exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001229 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001230 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001231 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 }
1233 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
John Durkop07cc04a2020-11-16 22:08:34 -08001236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001237 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001238static psa_status_t psa_get_rsa_public_exponent(
1239 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001241{
1242 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001244 uint8_t *buffer = NULL;
1245 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001248 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1249 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001250 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251 }
1252 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001253 /* It's the default value, which is reported as an empty string,
1254 * so there's nothing to do. */
1255 goto exit;
1256 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 buflen = mbedtls_mpi_size(&mpi);
1259 buffer = mbedtls_calloc(1, buflen);
1260 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1262 goto exit;
1263 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1265 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001266 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001268 attributes->domain_parameters = buffer;
1269 attributes->domain_parameters_size = buflen;
1270
1271exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001272 mbedtls_mpi_free(&mpi);
1273 if (ret != 0) {
1274 mbedtls_free(buffer);
1275 }
1276 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001277}
John Durkop07cc04a2020-11-16 22:08:34 -08001278#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001279 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001280
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001281/** Retrieve all the publicly-accessible attributes of a key.
1282 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1284 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001287 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001288 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001292 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1293 if (status != PSA_SUCCESS) {
1294 return status;
1295 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001296
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001297 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1299 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300
1301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1303 psa_set_key_slot_number(attributes,
1304 psa_key_slot_get_slot_number(slot));
1305 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001309#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001310 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001311 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001313 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001314 * is not yet implemented.
1315 * https://github.com/ARMmbed/mbed-crypto/issues/216
1316 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001318 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001319
Ronald Cron90857082020-11-25 14:58:33 +01001320 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 slot->attr.type,
1322 slot->key.data,
1323 slot->key.bytes,
1324 &rsa);
1325 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001326 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 status = psa_get_rsa_public_exponent(rsa,
1330 attributes);
1331 mbedtls_rsa_free(rsa);
1332 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001333 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001335#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001336 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001337 default:
1338 /* Nothing else to do. */
1339 break;
1340 }
1341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 if (status != PSA_SUCCESS) {
1343 psa_reset_key_attributes(attributes);
1344 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001348 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349}
1350
Gilles Peskinec8000c02019-08-02 20:15:51 +02001351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1352psa_status_t psa_get_key_slot_number(
1353 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001355{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001357 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 return PSA_SUCCESS;
1359 } else {
1360 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001361 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001362}
1363#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1366 size_t key_buffer_size,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001370{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 if (key_buffer_size > data_size) {
1372 return PSA_ERROR_BUFFER_TOO_SMALL;
1373 }
1374 memcpy(data, key_buffer, key_buffer_size);
1375 memset(data + key_buffer_size, 0,
1376 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001377 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001379}
1380
Ronald Cron7285cda2020-11-26 14:46:37 +01001381psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001382 const psa_key_attributes_t *attributes,
1383 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001384 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385{
Ronald Crond18b5f82020-11-25 16:46:05 +01001386 psa_key_type_t type = attributes->core.type;
1387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 if (key_type_is_raw_bytes(type) ||
1389 PSA_KEY_TYPE_IS_RSA(type) ||
1390 PSA_KEY_TYPE_IS_ECC(type)) {
1391 return psa_export_key_buffer_internal(
1392 key_buffer, key_buffer_size,
1393 data, data_size, data_length);
1394 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001395 /* This shouldn't happen in the reference implementation, but
1396 it is valid for a special-purpose implementation to omit
1397 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001398 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399 }
1400}
1401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1403 uint8_t *data,
1404 size_t data_size,
1405 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001406{
1407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1409 psa_key_slot_t *slot;
1410
Ronald Crone907e552021-01-18 13:22:38 +01001411 /* Reject a zero-length output buffer now, since this can never be a
1412 * valid key representation. This way we know that data must be a valid
1413 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 if (data_size == 0) {
1415 return PSA_ERROR_BUFFER_TOO_SMALL;
1416 }
Ronald Crone907e552021-01-18 13:22:38 +01001417
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 /* Set the key to empty now, so that even when there are errors, we always
1419 * set data_length to a value between 0 and data_size. On error, setting
1420 * the key to empty is a good choice because an empty key representation is
1421 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001422 *data_length = 0;
1423
Ronald Cron9486f9d2020-11-26 13:36:23 +01001424 /* Export requires the EXPORT flag. There is an exception for public keys,
1425 * which don't require any flag, but
1426 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1427 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1429 PSA_KEY_USAGE_EXPORT, 0);
1430 if (status != PSA_SUCCESS) {
1431 return status;
1432 }
mohammad160306e79202018-03-28 13:17:44 +03001433
Ronald Crond18b5f82020-11-25 16:46:05 +01001434 psa_key_attributes_t attributes = {
1435 .core = slot->attr
1436 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 status = psa_driver_wrapper_export_key(&attributes,
1438 slot->key.data, slot->key.bytes,
1439 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001441 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001444}
1445
Ronald Cron7285cda2020-11-26 14:46:37 +01001446psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001447 const psa_key_attributes_t *attributes,
1448 const uint8_t *key_buffer,
1449 size_t key_buffer_size,
1450 uint8_t *data,
1451 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001453{
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1457 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001458 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 return psa_export_key_buffer_internal(
1460 key_buffer, key_buffer_size,
1461 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001462 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001465#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1467 return mbedtls_psa_rsa_export_public_key(attributes,
1468 key_buffer,
1469 key_buffer_size,
1470 data,
1471 data_size,
1472 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001473#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001476#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1477 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001479#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1481 return mbedtls_psa_ecp_export_public_key(attributes,
1482 key_buffer,
1483 key_buffer_size,
1484 data,
1485 data_size,
1486 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001487#else
1488 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001490#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1491 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001492 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001494 /* This shouldn't happen in the reference implementation, but
1495 it is valid for a special-purpose implementation to omit
1496 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001498 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499}
Ronald Crond18b5f82020-11-25 16:46:05 +01001500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1502 uint8_t *data,
1503 size_t data_size,
1504 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001505{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001507 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001508 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001509 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001510
Ronald Crone907e552021-01-18 13:22:38 +01001511 /* Reject a zero-length output buffer now, since this can never be a
1512 * valid key representation. This way we know that data must be a valid
1513 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001514 if (data_size == 0) {
1515 return PSA_ERROR_BUFFER_TOO_SMALL;
1516 }
Ronald Crone907e552021-01-18 13:22:38 +01001517
Darryl Greendd8fb772018-11-07 16:00:44 +00001518 /* Set the key to empty now, so that even when there are errors, we always
1519 * set data_length to a value between 0 and data_size. On error, setting
1520 * the key to empty is a good choice because an empty key representation is
1521 * unlikely to be accepted anywhere. */
1522 *data_length = 0;
1523
1524 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1526 if (status != PSA_SUCCESS) {
1527 return status;
1528 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1531 status = PSA_ERROR_INVALID_ARGUMENT;
1532 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001533 }
1534
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001535 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001536 .core = slot->attr
1537 };
Ronald Cron67227982020-11-26 15:16:05 +01001538 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001539 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001540 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001541
1542exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001545 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001546}
1547
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001548MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1549 "One or more key attribute flag is listed as both external-only and dual-use")
1550MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1551 "One or more key attribute flag is listed as both internal-only and dual-use")
1552MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1553 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001554
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001555/** Validate that a key policy is internally well-formed.
1556 *
1557 * This function only rejects invalid policies. It does not validate the
1558 * consistency of the policy with respect to other attributes of the key
1559 * such as the key type.
1560 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001562{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1564 PSA_KEY_USAGE_COPY |
1565 PSA_KEY_USAGE_ENCRYPT |
1566 PSA_KEY_USAGE_DECRYPT |
1567 PSA_KEY_USAGE_SIGN_MESSAGE |
1568 PSA_KEY_USAGE_VERIFY_MESSAGE |
1569 PSA_KEY_USAGE_SIGN_HASH |
1570 PSA_KEY_USAGE_VERIFY_HASH |
1571 PSA_KEY_USAGE_DERIVE)) != 0) {
1572 return PSA_ERROR_INVALID_ARGUMENT;
1573 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001576}
1577
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001578/** Validate the internal consistency of key attributes.
1579 *
1580 * This function only rejects invalid attribute values. If does not
1581 * validate the consistency of the attributes with any key data that may
1582 * be involved in the creation of the key.
1583 *
1584 * Call this function early in the key creation process.
1585 *
1586 * \param[in] attributes Key attributes for the new key.
1587 * \param[out] p_drv On any return, the driver for the key, if any.
1588 * NULL for a transparent key.
1589 *
1590 */
1591static psa_status_t psa_validate_key_attributes(
1592 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001593 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001594{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001595 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1597 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 status = psa_validate_key_location(lifetime, p_drv);
1600 if (status != PSA_SUCCESS) {
1601 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001602 }
1603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 status = psa_validate_key_persistence(lifetime);
1605 if (status != PSA_SUCCESS) {
1606 return status;
1607 }
1608
1609 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1610 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1611 return PSA_ERROR_INVALID_ARGUMENT;
1612 }
1613 } else {
1614 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1615 return PSA_ERROR_INVALID_ARGUMENT;
1616 }
1617 }
1618
1619 status = psa_validate_key_policy(&attributes->core.policy);
1620 if (status != PSA_SUCCESS) {
1621 return status;
1622 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001623
1624 /* Refuse to create overly large keys.
1625 * Note that this doesn't trigger on import if the attributes don't
1626 * explicitly specify a size (so psa_get_key_bits returns 0), so
1627 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1629 return PSA_ERROR_NOT_SUPPORTED;
1630 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001631
Gilles Peskine91e8c332019-08-02 19:19:39 +02001632 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1634 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1635 return PSA_ERROR_INVALID_ARGUMENT;
1636 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001639}
1640
Gilles Peskine4747d192019-04-17 15:05:45 +02001641/** Prepare a key slot to receive key material.
1642 *
1643 * This function allocates a key slot and sets its metadata.
1644 *
1645 * If this function fails, call psa_fail_key_creation().
1646 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 * This function is intended to be used as follows:
1648 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001649 * it with the specified attributes, and in case of a volatile key assign it
1650 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001651 * -# Populate the slot with the key material.
1652 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1653 * In case of failure at any step, stop the sequence and call
1654 * psa_fail_key_creation().
1655 *
Ronald Cron5c522922020-11-14 16:35:34 +01001656 * On success, the key slot is locked. It is the responsibility of the caller
1657 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001658 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001659 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001660 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 * \param[out] p_slot On success, a pointer to the prepared slot.
1662 * \param[out] p_drv On any return, the driver for the key, if any.
1663 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001664 *
1665 * \retval #PSA_SUCCESS
1666 * The key slot is ready to receive key material.
1667 * \return If this function fails, the key slot is an invalid state.
1668 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 */
1670static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001671 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001673 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001675{
1676 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001677 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 psa_key_slot_t *slot;
1679
Gilles Peskinedf179142019-07-15 22:02:14 +02001680 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 *p_drv = NULL;
1682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001683 status = psa_validate_key_attributes(attributes, p_drv);
1684 if (status != PSA_SUCCESS) {
1685 return status;
1686 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1689 if (status != PSA_SUCCESS) {
1690 return status;
1691 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 slot = *p_slot;
1693
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001694 /* We're storing the declared bit-size of the key. It's up to each
1695 * creation mechanism to verify that this information is correct.
1696 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001697 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001698 * is optional (import, copy). In case of a volatile key, assign it the
1699 * volatile key identifier associated to the slot returned to contain its
1700 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001701
1702 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001704#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1705 slot->attr.id = volatile_key_id;
1706#else
1707 slot->attr.id.key_id = volatile_key_id;
1708#endif
1709 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001710
Gilles Peskine91e8c332019-08-02 19:19:39 +02001711 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001712 * external-only flags, query `attributes`. Thanks to the check
1713 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001714 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001715 * may have set. */
1716 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001717
Gilles Peskinecbaff462019-07-12 23:46:04 +02001718#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001719 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001721 * create the key file in internal storage, create the
1722 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001723 * persistent data. This is done by starting a transaction that will
1724 * encompass these three actions.
1725 * For registering a volatile key, we just need to find an appropriate
1726 * slot number inside the SE. Since the key is designated volatile, creating
1727 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001728 /* The first thing to do is to find a slot number for the new key.
1729 * We save the slot number in persistent storage as part of the
1730 * transaction data. It will be needed to recover if the power
1731 * fails during the key creation process, to clean up on the secure
1732 * element side after restarting. Obtaining a slot number from the
1733 * secure element driver updates its persistent state, but we do not yet
1734 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001735 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001737 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001738 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1739 &slot_number);
1740 if (status != PSA_SUCCESS) {
1741 return status;
1742 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1745 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001746 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001747 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001748 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 status = psa_crypto_save_transaction();
1750 if (status != PSA_SUCCESS) {
1751 (void) psa_crypto_stop_transaction();
1752 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001753 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001754 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001755
1756 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001758 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001760 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001761 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001762 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001763 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001766 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001767}
Gilles Peskine4747d192019-04-17 15:05:45 +02001768
1769/** Finalize the creation of a key once its key material has been set.
1770 *
1771 * This entails writing the key to persistent storage.
1772 *
1773 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001774 * See the documentation of psa_start_key_creation() for the intended use
1775 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 *
Ronald Cron5c522922020-11-14 16:35:34 +01001777 * If the finalization succeeds, the function unlocks the key slot (it was
1778 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1779 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001780 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 * \param[in,out] slot Pointer to the slot with key material.
1782 * \param[in] driver The secure element driver for the key,
1783 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 * \param[out] key On success, identifier of the key. Note that the
1785 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001786 *
1787 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001788 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001789 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1790 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1791 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1792 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1793 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1794 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001795 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001796 * \return If this function fails, the key slot is an invalid state.
1797 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001798 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001799static psa_status_t psa_finish_key_creation(
1800 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001801 psa_se_drv_table_entry_t *driver,
1802 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001803{
1804 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001805 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001806 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001807
1808#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001810#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001811 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001812 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001813 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001815
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001816 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1817 sizeof(data.slot_number),
1818 "Slot number size does not match psa_se_key_data_storage_t");
1819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1821 status = psa_save_persistent_key(&slot->attr,
1822 (uint8_t *) &data,
1823 sizeof(data));
1824 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001825#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1826 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001827 /* Key material is saved in export representation in the slot, so
1828 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 status = psa_save_persistent_key(&slot->attr,
1830 slot->key.data,
1831 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001832 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001833 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001834#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1835
Gilles Peskinecbaff462019-07-12 23:46:04 +02001836#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001837 /* Finish the transaction for a key creation. This does not
1838 * happen when registering an existing key. Detect this case
1839 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001840 * creation of a persistent key in a secure element requires a transaction,
1841 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 if (driver != NULL &&
1843 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1844 status = psa_save_se_persistent_data(driver);
1845 if (status != PSA_SUCCESS) {
1846 psa_destroy_persistent_key(slot->attr.id);
1847 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001848 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850 }
1851#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001854 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 status = psa_unlock_key_slot(slot);
1856 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001857 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001859 }
Ronald Cron50972942020-11-14 11:28:25 +01001860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001862}
1863
1864/** Abort the creation of a key.
1865 *
1866 * You may call this function after calling psa_start_key_creation(),
1867 * or after psa_finish_key_creation() fails. In other circumstances, this
1868 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001869 * See the documentation of psa_start_key_creation() for the intended use
1870 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 * \param[in,out] slot Pointer to the slot with key material.
1873 * \param[in] driver The secure element driver for the key,
1874 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876static void psa_fail_key_creation(psa_key_slot_t *slot,
1877 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001878{
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 (void) driver;
1880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001881 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001884
1885#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001886 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 * element, and the failure happened later (when saving metadata
1888 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001889 * element.
1890 * https://github.com/ARMmbed/mbed-crypto/issues/217
1891 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001892
Gilles Peskined7729582019-08-05 15:55:54 +02001893 /* Abort the ongoing transaction if any (there may not be one if
1894 * the creation process failed before starting one, or if the
1895 * key creation is a registration of a key in a secure element).
1896 * Earlier functions must already have done what it takes to undo any
1897 * partial creation. All that's left is to update the transaction data
1898 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001900#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001902 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001903}
1904
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001905/** Validate optional attributes during key creation.
1906 *
1907 * Some key attributes are optional during key creation. If they are
1908 * specified in the attributes structure, check that they are consistent
1909 * with the data in the slot.
1910 *
1911 * This function should be called near the end of key creation, after
1912 * the slot in memory is fully populated but before saving persistent data.
1913 */
1914static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 if (attributes->core.type != 0) {
1919 if (attributes->core.type != slot->attr.type) {
1920 return PSA_ERROR_INVALID_ARGUMENT;
1921 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 }
1923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001925#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1927 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001928 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001929 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001931
Ronald Cron90857082020-11-25 14:58:33 +01001932 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 slot->attr.type,
1934 slot->key.data,
1935 slot->key.bytes,
1936 &rsa);
1937 if (status != PSA_SUCCESS) {
1938 return status;
1939 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941 mbedtls_mpi_init(&actual);
1942 mbedtls_mpi_init(&required);
1943 ret = mbedtls_rsa_export(rsa,
1944 NULL, NULL, NULL, NULL, &actual);
1945 mbedtls_rsa_free(rsa);
1946 mbedtls_free(rsa);
1947 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 }
1950 ret = mbedtls_mpi_read_binary(&required,
1951 attributes->domain_parameters,
1952 attributes->domain_parameters_size);
1953 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001955 }
1956 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001958 }
1959rsa_exit:
1960 mbedtls_mpi_free(&actual);
1961 mbedtls_mpi_free(&required);
1962 if (ret != 0) {
1963 return mbedtls_to_psa_error(ret);
1964 }
1965 } else
John Durkop9814fa22020-11-04 12:28:15 -08001966#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1967 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001968 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001969 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 }
1971 }
1972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001973 if (attributes->core.bits != 0) {
1974 if (attributes->core.bits != slot->attr.bits) {
1975 return PSA_ERROR_INVALID_ARGUMENT;
1976 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 }
1978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001979 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001980}
1981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1983 const uint8_t *data,
1984 size_t data_length,
1985 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001986{
1987 psa_status_t status;
1988 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001989 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991
Ronald Cron81709fc2020-11-14 12:10:32 +01001992 *key = MBEDTLS_SVC_KEY_ID_INIT;
1993
Gilles Peskine0f84d622019-09-12 19:03:13 +02001994 /* Reject zero-length symmetric keys (including raw data key objects).
1995 * This also rejects any key which might be encoded as an empty string,
1996 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 if (data_length == 0) {
1998 return PSA_ERROR_INVALID_ARGUMENT;
1999 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2002 &slot, &driver);
2003 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002007 /* In the case of a transparent key or an opaque key stored in local
2008 * storage (thus not in the case of generating a key in a secure element
2009 * or cryptoprocessor with storage), we have to allocate a buffer to
2010 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011 if (slot->key.data == NULL) {
2012 status = psa_allocate_buffer_to_slot(slot, data_length);
2013 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002014 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002016 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002017
2018 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 status = psa_driver_wrapper_import_key(attributes,
2020 data, data_length,
2021 slot->key.data,
2022 slot->key.bytes,
2023 &slot->key.bytes, &bits);
2024 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002025 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002026 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002029 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002031 status = PSA_ERROR_INVALID_ARGUMENT;
2032 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002033 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 status = psa_validate_optional_attributes(slot, attributes);
2036 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002037 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002038 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 if (status != PSA_SUCCESS) {
2043 psa_fail_key_creation(slot, driver);
2044 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002047}
2048
Gilles Peskined7729582019-08-05 15:55:54 +02002049#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2050psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002052{
2053 psa_status_t status;
2054 psa_key_slot_t *slot = NULL;
2055 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002056 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002057
2058 /* Leaving attributes unspecified is not currently supported.
2059 * It could make sense to query the key type and size from the
2060 * secure element, but not all secure elements support this
2061 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2063 return PSA_ERROR_NOT_SUPPORTED;
2064 }
2065 if (psa_get_key_bits(attributes) == 0) {
2066 return PSA_ERROR_NOT_SUPPORTED;
2067 }
Gilles Peskined7729582019-08-05 15:55:54 +02002068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2070 &slot, &driver);
2071 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002072 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002073 }
Gilles Peskined7729582019-08-05 15:55:54 +02002074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002075 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002076
2077exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 if (status != PSA_SUCCESS) {
2079 psa_fail_key_creation(slot, driver);
2080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002081
Gilles Peskined7729582019-08-05 15:55:54 +02002082 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 psa_close_key(key);
2084 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002085}
2086#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2089 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002090{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 psa_status_t status = psa_copy_key_material_into_slot(target,
2092 source->key.data,
2093 source->key.bytes);
2094 if (status != PSA_SUCCESS) {
2095 return status;
2096 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002097
Steven Cooreman398aee52020-10-13 14:35:45 +02002098 target->attr.type = source->attr.type;
2099 target->attr.bits = source->attr.bits;
2100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002102}
2103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2105 const psa_key_attributes_t *specified_attributes,
2106 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002107{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002109 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 psa_key_slot_t *source_slot = NULL;
2111 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002113 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114
Ronald Cron81709fc2020-11-14 12:10:32 +01002115 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2116
Ronald Cron5c522922020-11-14 16:35:34 +01002117 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002118 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2119 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002120 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 status = psa_validate_optional_attributes(source_slot,
2124 specified_attributes);
2125 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 status = psa_restrict_key_policy(source_slot->attr.type,
2130 &actual_attributes.core.policy,
2131 &source_slot->attr.policy);
2132 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2137 &target_slot, &driver);
2138 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002141
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002144 /* Copying to a secure element is not implemented yet. */
2145 status = PSA_ERROR_NOT_SUPPORTED;
2146 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002147 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002151 /*
2152 * Copying through an opaque driver is not implemented yet, consider
2153 * a lifetime with an external location as an invalid parameter for
2154 * now.
2155 */
2156 status = PSA_ERROR_INVALID_ARGUMENT;
2157 goto exit;
2158 }
2159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 status = psa_copy_key_material(source_slot, target_slot);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002166exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if (status != PSA_SUCCESS) {
2168 psa_fail_key_creation(target_slot, driver);
2169 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002174}
2175
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002176
2177
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002178/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002179/* Message digests */
2180/****************************************************************/
2181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002183{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002184 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 if (operation->id == 0) {
2186 return PSA_SUCCESS;
2187 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002189 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002190 operation->id = 0;
2191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193}
2194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2196 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002198 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002200 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002202 status = PSA_ERROR_BAD_STATE;
2203 goto exit;
2204 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002207 status = PSA_ERROR_INVALID_ARGUMENT;
2208 goto exit;
2209 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002210
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002211 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2212 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002213 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002216
2217exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 if (status != PSA_SUCCESS) {
2219 psa_hash_abort(operation);
2220 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002221
2222 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223}
2224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2226 const uint8_t *input,
2227 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002232 status = PSA_ERROR_BAD_STATE;
2233 goto exit;
2234 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002235
Steven Cooremanc8288352021-03-04 14:02:19 +01002236 /* Don't require hash implementations to behave correctly on a
2237 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 if (input_length == 0) {
2239 return PSA_SUCCESS;
2240 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002243
2244exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 if (status != PSA_SUCCESS) {
2246 psa_hash_abort(operation);
2247 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250}
2251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2253 uint8_t *hash,
2254 size_t hash_size,
2255 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002257 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (operation->id == 0) {
2259 return PSA_ERROR_BAD_STATE;
2260 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261
Steven Cooreman1e582352021-02-18 17:24:37 +01002262 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 operation, hash, hash_size, hash_length);
2264 psa_hash_abort(operation);
2265 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266}
2267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2269 const uint8_t *hash,
2270 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002272 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002274 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275 operation,
2276 actual_hash, sizeof(actual_hash),
2277 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002279 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002280 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002284 status = PSA_ERROR_INVALID_SIGNATURE;
2285 goto exit;
2286 }
2287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002289 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002290 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002291
2292exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2294 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002295 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299}
2300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002301psa_status_t psa_hash_compute(psa_algorithm_t alg,
2302 const uint8_t *input, size_t input_length,
2303 uint8_t *hash, size_t hash_size,
2304 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002305{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002306 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 if (!PSA_ALG_IS_HASH(alg)) {
2308 return PSA_ERROR_INVALID_ARGUMENT;
2309 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2312 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002313}
2314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315psa_status_t psa_hash_compare(psa_algorithm_t alg,
2316 const uint8_t *input, size_t input_length,
2317 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002318{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002319 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002320 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322 if (!PSA_ALG_IS_HASH(alg)) {
2323 return PSA_ERROR_INVALID_ARGUMENT;
2324 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002325
Steven Cooreman1e582352021-02-18 17:24:37 +01002326 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 alg, input, input_length,
2328 actual_hash, sizeof(actual_hash),
2329 &actual_hash_length);
2330 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002331 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 }
2333 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002334 status = PSA_ERROR_INVALID_SIGNATURE;
2335 goto exit;
2336 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002338 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002340
2341exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2343 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002344}
2345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2347 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002348{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (source_operation->id == 0 ||
2350 target_operation->id != 0) {
2351 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002352 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2355 target_operation);
2356 if (status != PSA_SUCCESS) {
2357 psa_hash_abort(target_operation);
2358 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002361}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362
2363
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002365/* MAC */
2366/****************************************************************/
2367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002369{
Steven Cooreman07897832021-03-19 18:28:56 +01002370 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 if (operation->id == 0) {
2372 return PSA_SUCCESS;
2373 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002376 operation->mac_size = 0;
2377 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002378 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381}
2382
Ronald Cron1c650a12021-06-17 16:33:22 +02002383static psa_status_t psa_mac_finalize_alg_and_key_validation(
2384 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002385 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 psa_key_type_t key_type = psa_get_key_type(attributes);
2390 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 if (!PSA_ALG_IS_MAC(alg)) {
2393 return PSA_ERROR_INVALID_ARGUMENT;
2394 }
Steven Cooreman07897832021-03-19 18:28:56 +01002395
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002396 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 status = psa_mac_key_can_do(alg, key_type);
2398 if (status != PSA_SUCCESS) {
2399 return status;
2400 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002401
Steven Cooremana6474de2021-05-10 11:13:41 +02002402 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002406 /* A very short MAC is too short for security since it can be
2407 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2408 * so we make this our minimum, even though 32 bits is still
2409 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002411 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2414 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002415 /* It's impossible to "truncate" to a larger length than the full length
2416 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002418 }
2419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002421 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2422 * that is disabled in the compile-time configuration. The result can
2423 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2424 * configuration into account. In this case, force a return of
2425 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2426 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2427 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2428 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2429 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002431 }
2432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002433 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002434}
2435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2437 mbedtls_svc_key_id_t key,
2438 psa_algorithm_t alg,
2439 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002440{
2441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2442 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002443 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002444 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002445
2446 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002448 status = PSA_ERROR_BAD_STATE;
2449 goto exit;
2450 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002451
2452 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 key,
2454 &slot,
2455 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2456 alg);
2457 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002458 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002460
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002461 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002462 .core = slot->attr
2463 };
2464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002465 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2466 &operation->mac_size);
2467 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002468 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002470
2471 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002472 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473 if (is_sign) {
2474 status = psa_driver_wrapper_mac_sign_setup(operation,
2475 &attributes,
2476 slot->key.data,
2477 slot->key.bytes,
2478 alg);
2479 } else {
2480 status = psa_driver_wrapper_mac_verify_setup(operation,
2481 &attributes,
2482 slot->key.data,
2483 slot->key.bytes,
2484 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002486
Gilles Peskinefbfac682018-07-08 20:51:54 +02002487exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 if (status != PSA_SUCCESS) {
2489 psa_mac_abort(operation);
2490 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495}
2496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2498 mbedtls_svc_key_id_t key,
2499 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002500{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002502}
2503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2505 mbedtls_svc_key_id_t key,
2506 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002507{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002509}
2510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002511psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2512 const uint8_t *input,
2513 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (operation->id == 0) {
2516 return PSA_ERROR_BAD_STATE;
2517 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518
Steven Cooreman11743f92021-03-19 18:38:46 +01002519 /* Don't require hash implementations to behave correctly on a
2520 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (input_length == 0) {
2522 return PSA_SUCCESS;
2523 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2526 input, input_length);
2527 if (status != PSA_SUCCESS) {
2528 psa_mac_abort(operation);
2529 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532}
2533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2535 uint8_t *mac,
2536 size_t mac_size,
2537 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002538{
Steven Cooreman87885df2021-03-19 19:04:39 +01002539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2540 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002543 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002544 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002545 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002548 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002549 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002550 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002551
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002552 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2553 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002555 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002556 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002557 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002560 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002561 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002562 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 status = psa_driver_wrapper_mac_sign_finish(operation,
2565 mac, operation->mac_size,
2566 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002567
Dave Rodgman478ab542021-06-25 09:09:02 +01002568exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002569 /* In case of success, set the potential excess room in the output buffer
2570 * to an invalid value, to avoid potentially leaking a longer MAC.
2571 * In case of error, set the output length and content to a safe default,
2572 * such that in case the caller misses an error check, the output would be
2573 * an unachievable MAC.
2574 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002575 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002576 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002577 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002578 }
mohammad16036df908f2018-04-02 08:34:15 -07002579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 if (mac_size > operation->mac_size) {
2581 memset(&mac[operation->mac_size], '!',
2582 mac_size - operation->mac_size);
2583 }
Ronald Cron882eb782021-06-18 13:05:48 +02002584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002588}
2589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2591 const uint8_t *mac,
2592 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593{
Steven Cooreman87885df2021-03-19 19:04:39 +01002594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2595 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002598 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002599 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002600 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002603 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002604 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002605 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002608 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002609 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002610 }
2611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 status = psa_driver_wrapper_mac_verify_finish(operation,
2613 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002614
Dave Rodgman478ab542021-06-25 09:09:02 +01002615exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002618 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619}
2620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2622 psa_algorithm_t alg,
2623 const uint8_t *input,
2624 size_t input_length,
2625 uint8_t *mac,
2626 size_t mac_size,
2627 size_t *mac_length,
2628 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002629{
2630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002631 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002632 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002633 psa_key_slot_t *slot;
2634 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635
Ronald Crondbb86462021-06-17 17:17:20 +02002636 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 key,
2638 &slot,
2639 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2640 alg);
2641 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002642 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002644
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002645 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002646 .core = slot->attr
2647 };
2648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2650 &operation_mac_size);
2651 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002652 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002653 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002655 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002656 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002657 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002658 }
2659
2660 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 &attributes,
2662 slot->key.data, slot->key.bytes,
2663 alg,
2664 input, input_length,
2665 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002666
2667exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002668 /* In case of success, set the potential excess room in the output buffer
2669 * to an invalid value, to avoid potentially leaking a longer MAC.
2670 * In case of error, set the output length and content to a safe default,
2671 * such that in case the caller misses an error check, the output would be
2672 * an unachievable MAC.
2673 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002675 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002676 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002677 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 if (mac_size > operation_mac_size) {
2679 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2680 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002685}
2686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002688 psa_algorithm_t alg,
2689 const uint8_t *input,
2690 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 uint8_t *mac,
2692 size_t mac_size,
2693 size_t *mac_length)
2694{
2695 return psa_mac_compute_internal(key, alg,
2696 input, input_length,
2697 mac, mac_size, mac_length, 1);
2698}
2699
2700psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2701 psa_algorithm_t alg,
2702 const uint8_t *input,
2703 size_t input_length,
2704 const uint8_t *mac,
2705 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002706{
2707 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002708 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2709 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 status = psa_mac_compute_internal(key, alg,
2712 input, input_length,
2713 actual_mac, sizeof(actual_mac),
2714 &actual_mac_length, 0);
2715 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002716 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002720 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002721 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002722 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002723 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002724 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002725 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002726 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002727
2728exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002729 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002731 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002732}
Gilles Peskine20035e32018-02-03 22:44:14 +01002733
Gilles Peskine20035e32018-02-03 22:44:14 +01002734/****************************************************************/
2735/* Asymmetric cryptography */
2736/****************************************************************/
2737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2739 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002740{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002741 if (input_is_message) {
2742 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2743 return PSA_ERROR_INVALID_ARGUMENT;
2744 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2747 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2748 return PSA_ERROR_INVALID_ARGUMENT;
2749 }
2750 }
2751 } else {
2752 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2753 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002754 }
2755 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002758}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2761 int input_is_message,
2762 psa_algorithm_t alg,
2763 const uint8_t *input,
2764 size_t input_length,
2765 uint8_t *signature,
2766 size_t signature_size,
2767 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002768{
2769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2770 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002771 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002772 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002773
2774 *signature_length = 0;
2775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 status = psa_sign_verify_check_alg(input_is_message, alg);
2777 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002778 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002780
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002781 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002782 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002783 * buffer can in principle be empty since it doesn't actually have
2784 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 if (signature_size == 0) {
2786 return PSA_ERROR_BUFFER_TOO_SMALL;
2787 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002788
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002789 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 key, &slot,
2791 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2792 PSA_KEY_USAGE_SIGN_HASH,
2793 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002796 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002800 status = PSA_ERROR_INVALID_ARGUMENT;
2801 goto exit;
2802 }
2803
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002804 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806 };
2807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002809 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002810 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002811 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 signature, signature_size, signature_length);
2813 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002814
2815 status = psa_driver_wrapper_sign_hash(
2816 &attributes, slot->key.data, slot->key.bytes,
2817 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002819 }
2820
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002821
2822exit:
2823 /* Fill the unused part of the output buffer (the whole buffer on error,
2824 * the trailing part on success) with something that isn't a valid signature
2825 * (barring an attack on the signature and deliberately-crafted input),
2826 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002827 if (status == PSA_SUCCESS) {
2828 memset(signature + *signature_length, '!',
2829 signature_size - *signature_length);
2830 } else {
2831 memset(signature, '!', signature_size);
2832 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002833 /* If signature_size is 0 then we have nothing to do. We must not call
2834 * memset because signature may be NULL in this case. */
2835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002838 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002839}
2840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2842 int input_is_message,
2843 psa_algorithm_t alg,
2844 const uint8_t *input,
2845 size_t input_length,
2846 const uint8_t *signature,
2847 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002848{
2849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2850 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2851 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 status = psa_sign_verify_check_alg(input_is_message, alg);
2854 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002855 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002856 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002857
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002858 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002859 key, &slot,
2860 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2861 PSA_KEY_USAGE_VERIFY_HASH,
2862 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002864 if (status != PSA_SUCCESS) {
2865 return status;
2866 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002867
2868 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002870 };
2871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002873 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002874 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002875 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 signature, signature_length);
2877 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002878 status = psa_driver_wrapper_verify_hash(
2879 &attributes, slot->key.data, slot->key.bytes,
2880 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002882 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002887
2888}
2889
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002890psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002891 const psa_key_attributes_t *attributes,
2892 const uint8_t *key_buffer,
2893 size_t key_buffer_size,
2894 psa_algorithm_t alg,
2895 const uint8_t *input,
2896 size_t input_length,
2897 uint8_t *signature,
2898 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002900{
2901 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002904 size_t hash_length;
2905 uint8_t hash[PSA_HASH_MAX_SIZE];
2906
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002907 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002908 PSA_ALG_SIGN_GET_HASH(alg),
2909 input, input_length,
2910 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002912 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002913 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002915
2916 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 attributes, key_buffer, key_buffer_size,
2918 alg, hash, hash_length,
2919 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002920 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002923}
2924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002925psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2926 psa_algorithm_t alg,
2927 const uint8_t *input,
2928 size_t input_length,
2929 uint8_t *signature,
2930 size_t signature_size,
2931 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002932{
2933 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002934 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002936}
2937
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002938psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002939 const psa_key_attributes_t *attributes,
2940 const uint8_t *key_buffer,
2941 size_t key_buffer_size,
2942 psa_algorithm_t alg,
2943 const uint8_t *input,
2944 size_t input_length,
2945 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002946 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002947{
2948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002951 size_t hash_length;
2952 uint8_t hash[PSA_HASH_MAX_SIZE];
2953
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002954 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 PSA_ALG_SIGN_GET_HASH(alg),
2956 input, input_length,
2957 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002960 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002962
2963 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 attributes, key_buffer, key_buffer_size,
2965 alg, hash, hash_length,
2966 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002967 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002970}
2971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2973 psa_algorithm_t alg,
2974 const uint8_t *input,
2975 size_t input_length,
2976 const uint8_t *signature,
2977 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002978{
2979 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002980 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002982}
2983
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002984psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002985 const psa_key_attributes_t *attributes,
2986 const uint8_t *key_buffer, size_t key_buffer_size,
2987 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002989{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2991 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2992 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002993#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2995 return mbedtls_psa_rsa_sign_hash(
2996 attributes,
2997 key_buffer, key_buffer_size,
2998 alg, hash, hash_length,
2999 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003000#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3001 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003002 } else {
3003 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003004 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003005 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3006 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003007#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3009 return mbedtls_psa_ecdsa_sign_hash(
3010 attributes,
3011 key_buffer, key_buffer_size,
3012 alg, hash, hash_length,
3013 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003014#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3015 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 } else {
3017 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003018 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003019 }
Ronald Cron4501c982021-03-19 20:09:32 +01003020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003021 (void) key_buffer;
3022 (void) key_buffer_size;
3023 (void) hash;
3024 (void) hash_length;
3025 (void) signature;
3026 (void) signature_size;
3027 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003030}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3033 psa_algorithm_t alg,
3034 const uint8_t *hash,
3035 size_t hash_length,
3036 uint8_t *signature,
3037 size_t signature_size,
3038 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003039{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003040 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003041 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003043}
3044
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003045psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003046 const psa_key_attributes_t *attributes,
3047 const uint8_t *key_buffer, size_t key_buffer_size,
3048 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3052 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3053 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003054#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3056 return mbedtls_psa_rsa_verify_hash(
3057 attributes,
3058 key_buffer, key_buffer_size,
3059 alg, hash, hash_length,
3060 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003061#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3062 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063 } else {
3064 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003065 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003066 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3067 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003068#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003069 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3070 return mbedtls_psa_ecdsa_verify_hash(
3071 attributes,
3072 key_buffer, key_buffer_size,
3073 alg, hash, hash_length,
3074 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3076 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 } else {
3078 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003079 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003080 }
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 (void) key_buffer;
3083 (void) key_buffer_size;
3084 (void) hash;
3085 (void) hash_length;
3086 (void) signature;
3087 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003090}
3091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3093 psa_algorithm_t alg,
3094 const uint8_t *hash,
3095 size_t hash_length,
3096 const uint8_t *signature,
3097 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003098{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003099 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003100 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003102}
3103
John Durkop0e005192020-10-31 22:06:54 -07003104#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3106 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003107{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3109 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3110 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3111 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003112}
John Durkop0e005192020-10-31 22:06:54 -07003113#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3116 psa_algorithm_t alg,
3117 const uint8_t *input,
3118 size_t input_length,
3119 const uint8_t *salt,
3120 size_t salt_length,
3121 uint8_t *output,
3122 size_t output_size,
3123 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003125 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003126 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003127 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003128
Darryl Green5cc689a2018-07-24 15:34:10 +01003129 (void) input;
3130 (void) input_length;
3131 (void) salt;
3132 (void) output;
3133 (void) output_size;
3134
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003135 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3138 return PSA_ERROR_INVALID_ARGUMENT;
3139 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003140
Ronald Cron5c522922020-11-14 16:35:34 +01003141 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003142 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3143 if (status != PSA_SUCCESS) {
3144 return status;
3145 }
3146 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3147 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003148 status = PSA_ERROR_INVALID_ARGUMENT;
3149 goto exit;
3150 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003153#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003155 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3157 slot->key.data,
3158 slot->key.bytes,
3159 &rsa);
3160 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003161 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003165 status = PSA_ERROR_BUFFER_TOO_SMALL;
3166 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003167 }
Ronald Cron7207d572021-09-08 14:28:35 +02003168#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3169 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003171#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003172 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 mbedtls_rsa_pkcs1_encrypt(rsa,
3174 mbedtls_psa_get_random,
3175 MBEDTLS_PSA_RANDOM_STATE,
3176 MBEDTLS_RSA_PUBLIC,
3177 input_length,
3178 input,
3179 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003180#else
3181 status = PSA_ERROR_NOT_SUPPORTED;
3182#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 } else
3184 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003185#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003186 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003187 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003188 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3189 mbedtls_psa_get_random,
3190 MBEDTLS_PSA_RANDOM_STATE,
3191 MBEDTLS_RSA_PUBLIC,
3192 salt, salt_length,
3193 input_length,
3194 input,
3195 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003196#else
3197 status = PSA_ERROR_NOT_SUPPORTED;
3198#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003199 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003200 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201 }
Ronald Cron7207d572021-09-08 14:28:35 +02003202#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003204rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003205 if (status == PSA_SUCCESS) {
3206 *output_length = mbedtls_rsa_get_len(rsa);
3207 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003209 mbedtls_rsa_free(rsa);
3210 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003211#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003212 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003214 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003216
3217exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003221}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3224 psa_algorithm_t alg,
3225 const uint8_t *input,
3226 size_t input_length,
3227 const uint8_t *salt,
3228 size_t salt_length,
3229 uint8_t *output,
3230 size_t output_size,
3231 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003234 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003235 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003236
Darryl Green5cc689a2018-07-24 15:34:10 +01003237 (void) input;
3238 (void) input_length;
3239 (void) salt;
3240 (void) output;
3241 (void) output_size;
3242
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003243 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3246 return PSA_ERROR_INVALID_ARGUMENT;
3247 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003248
Ronald Cron5c522922020-11-14 16:35:34 +01003249 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003250 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3251 if (status != PSA_SUCCESS) {
3252 return status;
3253 }
3254 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003255 status = PSA_ERROR_INVALID_ARGUMENT;
3256 goto exit;
3257 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003260#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003262 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3264 slot->key.data,
3265 slot->key.bytes,
3266 &rsa);
3267 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003268 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003272 status = PSA_ERROR_INVALID_ARGUMENT;
3273 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003274 }
Ronald Cron7207d572021-09-08 14:28:35 +02003275#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3276 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003279#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003280 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 mbedtls_rsa_pkcs1_decrypt(rsa,
3282 mbedtls_psa_get_random,
3283 MBEDTLS_PSA_RANDOM_STATE,
3284 MBEDTLS_RSA_PRIVATE,
3285 output_length,
3286 input,
3287 output,
3288 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003289#else
3290 status = PSA_ERROR_NOT_SUPPORTED;
3291#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 } else
3293 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003294#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003296 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003297 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3298 mbedtls_psa_get_random,
3299 MBEDTLS_PSA_RANDOM_STATE,
3300 MBEDTLS_RSA_PRIVATE,
3301 salt, salt_length,
3302 output_length,
3303 input,
3304 output,
3305 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003306#else
3307 status = PSA_ERROR_NOT_SUPPORTED;
3308#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003310 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003312
Ronald Cron7207d572021-09-08 14:28:35 +02003313#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003315rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 mbedtls_rsa_free(rsa);
3317 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003318#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3319 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003321 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003322 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003323
3324exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003328}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003329
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003330
3331
mohammad1603503973b2018-03-12 15:59:30 +02003332/****************************************************************/
3333/* Symmetric cryptography */
3334/****************************************************************/
3335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3337 mbedtls_svc_key_id_t key,
3338 psa_algorithm_t alg,
3339 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003340{
3341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3342 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003343 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003344 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3346 PSA_KEY_USAGE_ENCRYPT :
3347 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003348
3349 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003351 status = PSA_ERROR_BAD_STATE;
3352 goto exit;
3353 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003356 status = PSA_ERROR_INVALID_ARGUMENT;
3357 goto exit;
3358 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003360 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3361 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003362 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003364
Ronald Cron49fafa92021-03-10 08:34:23 +01003365 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003366 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003367 * so we only set it (in the driver wrapper) after resources have been
3368 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003369 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003371 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003372 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003373 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003374 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003375 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003376 }
3377 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003378
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003379 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003380 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003381 };
3382
Ronald Cronab99ac22020-10-01 16:38:28 +02003383 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 if (cipher_operation == MBEDTLS_ENCRYPT) {
3385 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3386 &attributes,
3387 slot->key.data,
3388 slot->key.bytes,
3389 alg);
3390 } else {
3391 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3392 &attributes,
3393 slot->key.data,
3394 slot->key.bytes,
3395 alg);
3396 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003397
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003398exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 if (status != PSA_SUCCESS) {
3400 psa_cipher_abort(operation);
3401 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003406}
3407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3409 mbedtls_svc_key_id_t key,
3410 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003411{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003412 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003413}
3414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3416 mbedtls_svc_key_id_t key,
3417 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003418{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003420}
3421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3423 uint8_t *iv,
3424 size_t iv_size,
3425 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003426{
Ronald Cron6d051732020-10-01 14:10:20 +02003427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003428 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003429 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003432 status = PSA_ERROR_BAD_STATE;
3433 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003434 }
3435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003437 status = PSA_ERROR_BAD_STATE;
3438 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003439 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003440
Ronald Cronc423acb2021-07-05 12:31:44 +02003441 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003442 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003443 status = PSA_ERROR_BUFFER_TOO_SMALL;
3444 goto exit;
3445 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003448 status = PSA_ERROR_GENERIC_ERROR;
3449 goto exit;
3450 }
3451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 status = psa_generate_random(local_iv, default_iv_length);
3453 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003454 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 }
Ronald Cron5618a392021-03-26 09:52:26 +01003456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 status = psa_driver_wrapper_cipher_set_iv(operation,
3458 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003459
3460exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (status == PSA_SUCCESS) {
3462 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003463 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003464 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003466 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003468 }
Ronald Cron6d051732020-10-01 14:10:20 +02003469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003470 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003471}
3472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3474 const uint8_t *iv,
3475 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003476{
Ronald Cron6d051732020-10-01 14:10:20 +02003477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003480 status = PSA_ERROR_BAD_STATE;
3481 goto exit;
3482 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003485 status = PSA_ERROR_BAD_STATE;
3486 goto exit;
3487 }
Ronald Crona0d68172021-03-26 10:15:08 +01003488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003489 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003490 status = PSA_ERROR_INVALID_ARGUMENT;
3491 goto exit;
3492 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 status = psa_driver_wrapper_cipher_set_iv(operation,
3495 iv,
3496 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003497
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003498exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003500 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 } else {
3502 psa_cipher_abort(operation);
3503 }
3504 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003505}
3506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3508 const uint8_t *input,
3509 size_t input_length,
3510 uint8_t *output,
3511 size_t output_size,
3512 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003513{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003516 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003517 status = PSA_ERROR_BAD_STATE;
3518 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003519 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003521 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003522 status = PSA_ERROR_BAD_STATE;
3523 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003524 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 status = psa_driver_wrapper_cipher_update(operation,
3527 input,
3528 input_length,
3529 output,
3530 output_size,
3531 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003532
3533exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 if (status != PSA_SUCCESS) {
3535 psa_cipher_abort(operation);
3536 }
Ronald Cron6d051732020-10-01 14:10:20 +02003537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003539}
3540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3542 uint8_t *output,
3543 size_t output_size,
3544 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003545{
David Saadab4ecc272019-02-14 13:48:10 +02003546 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003548 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003549 status = PSA_ERROR_BAD_STATE;
3550 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003551 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003554 status = PSA_ERROR_BAD_STATE;
3555 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003556 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558 status = psa_driver_wrapper_cipher_finish(operation,
3559 output,
3560 output_size,
3561 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003562
3563exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003564 if (status == PSA_SUCCESS) {
3565 return psa_cipher_abort(operation);
3566 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003567 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003570 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003571 }
mohammad1603503973b2018-03-12 15:59:30 +02003572}
3573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003575{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003577 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003578 * in use. It's ok to call abort on such an object, and there's
3579 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003580 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003581 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003583 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003584
Ronald Cron49fafa92021-03-10 08:34:23 +01003585 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003586 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003587 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003590}
3591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003592psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3593 psa_algorithm_t alg,
3594 const uint8_t *input,
3595 size_t input_length,
3596 uint8_t *output,
3597 size_t output_size,
3598 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003599{
3600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003601 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003602 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003603 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003604 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3605 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003606
David Horstmann34980bd2023-11-29 17:07:13 +00003607 SWAP_FOR_LOCAL_INPUT(input, input_length);
3608 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannd2ad8862023-11-27 17:43:05 +00003609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003610 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003611 status = PSA_ERROR_INVALID_ARGUMENT;
3612 goto exit;
3613 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3616 PSA_KEY_USAGE_ENCRYPT,
3617 alg);
3618 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003619 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003621
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003622 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003624 };
3625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3627 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003628 status = PSA_ERROR_GENERIC_ERROR;
3629 goto exit;
3630 }
3631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 if (default_iv_length > 0) {
3633 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003634 status = PSA_ERROR_BUFFER_TOO_SMALL;
3635 goto exit;
3636 }
3637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 status = psa_generate_random(local_iv, default_iv_length);
3639 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003640 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003642 }
3643
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003644 status = psa_driver_wrapper_cipher_encrypt(
3645 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003646 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 mbedtls_buffer_offset(output, default_iv_length),
3648 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003649
3650exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 unlock_status = psa_unlock_key_slot(slot);
3652 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003653 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003654 }
Ronald Cron16377072021-07-08 19:00:07 +02003655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003656 if (status == PSA_SUCCESS) {
3657 if (default_iv_length > 0) {
3658 memcpy(output, local_iv, default_iv_length);
3659 }
3660 *output_length += default_iv_length;
3661 } else {
3662 *output_length = 0;
3663 }
3664
David Horstmann34980bd2023-11-29 17:07:13 +00003665 FREE_LOCAL_INPUT(input);
3666 FREE_LOCAL_OUTPUT(output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003669}
3670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003671psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3672 psa_algorithm_t alg,
3673 const uint8_t *input,
3674 size_t input_length,
3675 uint8_t *output,
3676 size_t output_size,
3677 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003678{
3679 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003680 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003681 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003682 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003684 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003685 status = PSA_ERROR_INVALID_ARGUMENT;
3686 goto exit;
3687 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3690 PSA_KEY_USAGE_DECRYPT,
3691 alg);
3692 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003693 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003695
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003696 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003697 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003698 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003700 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003701 status = PSA_ERROR_INVALID_ARGUMENT;
3702 goto exit;
3703 }
3704
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003705 status = psa_driver_wrapper_cipher_decrypt(
3706 &attributes, slot->key.data, slot->key.bytes,
3707 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003709
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003710exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 unlock_status = psa_unlock_key_slot(slot);
3712 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003713 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003717 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 }
Ronald Cron16377072021-07-08 19:00:07 +02003719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003721}
3722
3723
mohammad16035955c982018-04-26 00:53:03 +03003724/****************************************************************/
3725/* AEAD */
3726/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3729 psa_algorithm_t alg,
3730 const uint8_t *nonce,
3731 size_t nonce_length,
3732 const uint8_t *additional_data,
3733 size_t additional_data_length,
3734 const uint8_t *plaintext,
3735 size_t plaintext_length,
3736 uint8_t *ciphertext,
3737 size_t ciphertext_size,
3738 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003739{
Ronald Cron215633c2021-03-16 17:15:37 +01003740 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3741 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003742
mohammad1603f08a5502018-06-03 15:05:47 +03003743 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3746 return PSA_ERROR_NOT_SUPPORTED;
3747 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003748
Ronald Cron9a986162021-03-26 12:40:07 +01003749 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3751 if (status != PSA_SUCCESS) {
3752 return status;
3753 }
mohammad16035955c982018-04-26 00:53:03 +03003754
Ronald Cron215633c2021-03-16 17:15:37 +01003755 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003757 };
mohammad16035955c982018-04-26 00:53:03 +03003758
Ronald Cronde822812021-03-17 16:08:20 +01003759 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003760 &attributes, slot->key.data, slot->key.bytes,
3761 alg,
3762 nonce, nonce_length,
3763 additional_data, additional_data_length,
3764 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3768 memset(ciphertext, 0, ciphertext_size);
3769 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003774}
3775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3777 psa_algorithm_t alg,
3778 const uint8_t *nonce,
3779 size_t nonce_length,
3780 const uint8_t *additional_data,
3781 size_t additional_data_length,
3782 const uint8_t *ciphertext,
3783 size_t ciphertext_length,
3784 uint8_t *plaintext,
3785 size_t plaintext_size,
3786 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003787{
Ronald Cron215633c2021-03-16 17:15:37 +01003788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3789 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003790
Gilles Peskineee652a32018-06-01 19:23:52 +02003791 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3794 return PSA_ERROR_NOT_SUPPORTED;
3795 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003796
Ronald Cron9a986162021-03-26 12:40:07 +01003797 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3799 if (status != PSA_SUCCESS) {
3800 return status;
3801 }
mohammad16035955c982018-04-26 00:53:03 +03003802
Ronald Cron215633c2021-03-16 17:15:37 +01003803 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003805 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003806
Ronald Cronde822812021-03-17 16:08:20 +01003807 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003808 &attributes, slot->key.data, slot->key.bytes,
3809 alg,
3810 nonce, nonce_length,
3811 additional_data, additional_data_length,
3812 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 if (status != PSA_SUCCESS && plaintext_size != 0) {
3816 memset(plaintext, 0, plaintext_size);
3817 }
mohammad160360a64d02018-06-03 17:20:42 +03003818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 return status;
mohammad16035955c982018-04-26 00:53:03 +03003822}
3823
Gilles Peskinee59236f2018-01-27 23:32:46 +01003824/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003825/* Generators */
3826/****************************************************************/
3827
John Durkop07cc04a2020-11-16 22:08:34 -08003828#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3829 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3830 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3831#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003832#endif /* At least one builtin KDF */
3833
3834#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3835 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3836 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3837static psa_status_t psa_key_derivation_start_hmac(
3838 psa_mac_operation_t *operation,
3839 psa_algorithm_t hash_alg,
3840 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003842{
3843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3846 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3847 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003848
Steven Cooreman15f0d922021-05-07 14:14:37 +02003849 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003852 status = psa_driver_wrapper_mac_sign_setup(operation,
3853 &attributes,
3854 hmac_key, hmac_key_length,
3855 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 psa_reset_key_attributes(&attributes);
3858 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003859}
3860#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003861
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003862#define HKDF_STATE_INIT 0 /* no input yet */
3863#define HKDF_STATE_STARTED 1 /* got salt */
3864#define HKDF_STATE_KEYED 2 /* got key */
3865#define HKDF_STATE_OUTPUT 3 /* output started */
3866
Gilles Peskinecbe66502019-05-16 16:59:18 +02003867static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003869{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3871 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3872 } else {
3873 return operation->alg;
3874 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003875}
3876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003878{
3879 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3881 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003882 /* The object has (apparently) been initialized but it is not
3883 * in use. It's ok to call abort on such an object, and there's
3884 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 } else
John Durkopd0321952020-10-29 21:37:36 -07003886#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3888 mbedtls_free(operation->ctx.hkdf.info);
3889 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3890 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003891#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3892#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3893 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3895 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3896 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3897 if (operation->ctx.tls12_prf.secret != NULL) {
3898 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3899 operation->ctx.tls12_prf.secret_length);
3900 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003901 }
3902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003903 if (operation->ctx.tls12_prf.seed != NULL) {
3904 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3905 operation->ctx.tls12_prf.seed_length);
3906 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003907 }
3908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909 if (operation->ctx.tls12_prf.label != NULL) {
3910 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3911 operation->ctx.tls12_prf.label_length);
3912 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003913 }
3914
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003915 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003916
3917 /* We leave the fields Ai and output_block to be erased safely by the
3918 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003920#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3921 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003922 {
3923 status = PSA_ERROR_BAD_STATE;
3924 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 mbedtls_platform_zeroize(operation, sizeof(*operation));
3926 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003927}
3928
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003929psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003931{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003933 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003935 }
3936
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003937 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003939}
3940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3942 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003943{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 if (operation->alg == 0) {
3945 return PSA_ERROR_BAD_STATE;
3946 }
3947 if (capacity > operation->capacity) {
3948 return PSA_ERROR_INVALID_ARGUMENT;
3949 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003950 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003951 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003952}
3953
John Durkopd0321952020-10-29 21:37:36 -07003954#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003955/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003956 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3958 psa_algorithm_t hash_alg,
3959 uint8_t *output,
3960 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003961{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003962 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003963 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003964 psa_status_t status;
3965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003966 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3967 return PSA_ERROR_BAD_STATE;
3968 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003969 hkdf->state = HKDF_STATE_OUTPUT;
3970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003971 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003972 /* Copy what remains of the current block */
3973 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003975 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 }
3977 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003978 output += n;
3979 output_length -= n;
3980 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003981 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003982 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003983 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003984 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003985 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003986 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003987 * object was corrupted or if this function is called directly
3988 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 if (hkdf->block_number == 0xff) {
3990 return PSA_ERROR_BAD_STATE;
3991 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003992
3993 /* We need a new block */
3994 ++hkdf->block_number;
3995 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3998 hash_alg,
3999 hkdf->prk,
4000 hash_length);
4001 if (status != PSA_SUCCESS) {
4002 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004
4005 if (hkdf->block_number != 1) {
4006 status = psa_mac_update(&hkdf->hmac,
4007 hkdf->output_block,
4008 hash_length);
4009 if (status != PSA_SUCCESS) {
4010 return status;
4011 }
4012 }
4013 status = psa_mac_update(&hkdf->hmac,
4014 hkdf->info,
4015 hkdf->info_length);
4016 if (status != PSA_SUCCESS) {
4017 return status;
4018 }
4019 status = psa_mac_update(&hkdf->hmac,
4020 &hkdf->block_number, 1);
4021 if (status != PSA_SUCCESS) {
4022 return status;
4023 }
4024 status = psa_mac_sign_finish(&hkdf->hmac,
4025 hkdf->output_block,
4026 sizeof(hkdf->output_block),
4027 &hmac_output_length);
4028 if (status != PSA_SUCCESS) {
4029 return status;
4030 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004031 }
4032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004033 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004034}
John Durkop07cc04a2020-11-16 22:08:34 -08004035#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004036
John Durkop07cc04a2020-11-16 22:08:34 -08004037#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4038 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004039static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4040 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004042{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4044 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004045 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4046 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004047 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004048
Janos Follath7742fee2019-06-17 12:58:10 +01004049 /* We can't be wanting more output after block 0xff, otherwise
4050 * the capacity check in psa_key_derivation_output_bytes() would have
4051 * prevented this call. It could happen only if the operation
4052 * object was corrupted or if this function is called directly
4053 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004054 if (tls12_prf->block_number == 0xff) {
4055 return PSA_ERROR_CORRUPTION_DETECTED;
4056 }
Janos Follath7742fee2019-06-17 12:58:10 +01004057
4058 /* We need a new block */
4059 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004060 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004061
4062 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4063 *
4064 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4065 *
4066 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4067 * HMAC_hash(secret, A(2) + seed) +
4068 * HMAC_hash(secret, A(3) + seed) + ...
4069 *
4070 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004071 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004072 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004073 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004074 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004075 * is currently extracted as `output_block` and where i is
4076 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004077 */
4078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004079 status = psa_key_derivation_start_hmac(&hmac,
4080 hash_alg,
4081 tls12_prf->secret,
4082 tls12_prf->secret_length);
4083 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004084 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004086
4087 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004088 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004089 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4090 * the variable seed and in this instance means it in the context of the
4091 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004092 status = psa_mac_update(&hmac,
4093 tls12_prf->label,
4094 tls12_prf->label_length);
4095 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004096 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004097 }
4098 status = psa_mac_update(&hmac,
4099 tls12_prf->seed,
4100 tls12_prf->seed_length);
4101 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004102 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004103 }
4104 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004105 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4107 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004108 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004110 }
4111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 status = psa_mac_sign_finish(&hmac,
4113 tls12_prf->Ai, hash_length,
4114 &hmac_output_length);
4115 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004116 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 }
4118 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004119 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004120 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004121
4122 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 status = psa_key_derivation_start_hmac(&hmac,
4124 hash_alg,
4125 tls12_prf->secret,
4126 tls12_prf->secret_length);
4127 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004128 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004129 }
4130 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4131 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004132 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133 }
4134 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4135 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004136 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137 }
4138 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4139 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004140 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141 }
4142 status = psa_mac_sign_finish(&hmac,
4143 tls12_prf->output_block, hash_length,
4144 &hmac_output_length);
4145 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004146 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004148
Janos Follath7742fee2019-06-17 12:58:10 +01004149
4150cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004151 cleanup_status = psa_mac_abort(&hmac);
4152 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004153 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004157}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004158
Janos Follath844eb0e2019-06-19 12:10:49 +01004159static psa_status_t psa_key_derivation_tls12_prf_read(
4160 psa_tls12_prf_key_derivation_t *tls12_prf,
4161 psa_algorithm_t alg,
4162 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004164{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4166 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004167 psa_status_t status;
4168 uint8_t offset, length;
4169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004171 case PSA_TLS12_PRF_STATE_LABEL_SET:
4172 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4173 break;
4174 case PSA_TLS12_PRF_STATE_OUTPUT:
4175 break;
4176 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004178 }
4179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004180 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004181 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182 if (tls12_prf->left_in_block == 0) {
4183 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4184 alg);
4185 if (status != PSA_SUCCESS) {
4186 return status;
4187 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004188
4189 continue;
4190 }
4191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004193 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004195 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004197
4198 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004199 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004200 output += length;
4201 output_length -= length;
4202 tls12_prf->left_in_block -= length;
4203 }
4204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004205 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004206}
John Durkop07cc04a2020-11-16 22:08:34 -08004207#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4208 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004209
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004210psa_status_t psa_key_derivation_output_bytes(
4211 psa_key_derivation_operation_t *operation,
4212 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004214{
4215 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004220 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004221 }
4222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004223 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004225 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004226 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004227 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004228 goto exit;
4229 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004231 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004232 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004233 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4234 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004235 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004236 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004237 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004238 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004239 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004240
John Durkopd0321952020-10-29 21:37:36 -07004241#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004242 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4243 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4244 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4245 output, output_length);
4246 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004247#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4248#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4249 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4251 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4252 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4253 kdf_alg, output,
4254 output_length);
4255 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004256#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4257 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004258 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004259 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004261 }
4262
4263exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004265 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004266 * This allows us to differentiate between exhausted operations and
4267 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4268 * operations. */
4269 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004272 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004273 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004274 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004275}
4276
David Brown7807bf72021-02-09 16:28:23 -07004277#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004279{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004280 if (data_size >= 8) {
4281 mbedtls_des_key_set_parity(data);
4282 }
4283 if (data_size >= 16) {
4284 mbedtls_des_key_set_parity(data + 8);
4285 }
4286 if (data_size >= 24) {
4287 mbedtls_des_key_set_parity(data + 16);
4288 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004289}
David Brown7807bf72021-02-09 16:28:23 -07004290#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004291
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004292static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004293 psa_key_slot_t *slot,
4294 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004296{
4297 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004300 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302 if (!key_type_is_raw_bytes(slot->attr.type)) {
4303 return PSA_ERROR_INVALID_ARGUMENT;
4304 }
4305 if (bits % 8 != 0) {
4306 return PSA_ERROR_INVALID_ARGUMENT;
4307 }
4308 data = mbedtls_calloc(1, bytes);
4309 if (data == NULL) {
4310 return PSA_ERROR_INSUFFICIENT_MEMORY;
4311 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 status = psa_key_derivation_output_bytes(operation, data, bytes);
4314 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004315 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 }
David Brown12ca5032021-02-11 11:02:00 -07004317#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4319 psa_des_set_key_parity(data, bytes);
4320 }
David Brown8107e312021-02-12 08:08:46 -07004321#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 status = psa_allocate_buffer_to_slot(slot, bytes);
4324 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004325 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 }
Ronald Crondd04d422020-11-28 15:14:42 +01004327
Ronald Cronbf33c932020-11-28 18:06:53 +01004328 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004329 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004331 };
4332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333 status = psa_driver_wrapper_import_key(&attributes,
4334 data, bytes,
4335 slot->key.data,
4336 slot->key.bytes,
4337 &slot->key.bytes, &bits);
4338 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004339 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004341
4342exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004343 mbedtls_free(data);
4344 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004345}
4346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4348 psa_key_derivation_operation_t *operation,
4349 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004350{
4351 psa_status_t status;
4352 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004353 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004354
Ronald Cron81709fc2020-11-14 12:10:32 +01004355 *key = MBEDTLS_SVC_KEY_ID_INIT;
4356
Gilles Peskine0f84d622019-09-12 19:03:13 +02004357 /* Reject any attempt to create a zero-length key so that we don't
4358 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 if (psa_get_key_bits(attributes) == 0) {
4360 return PSA_ERROR_INVALID_ARGUMENT;
4361 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363 if (operation->alg == PSA_ALG_NONE) {
4364 return PSA_ERROR_BAD_STATE;
4365 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (!operation->can_output_key) {
4368 return PSA_ERROR_NOT_PERMITTED;
4369 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4372 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004373#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004375 /* Deriving a key in a secure element is not implemented yet. */
4376 status = PSA_ERROR_NOT_SUPPORTED;
4377 }
4378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 if (status == PSA_SUCCESS) {
4380 status = psa_generate_derived_key_internal(slot,
4381 attributes->core.bits,
4382 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004383 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 if (status == PSA_SUCCESS) {
4385 status = psa_finish_key_creation(slot, driver, key);
4386 }
4387 if (status != PSA_SUCCESS) {
4388 psa_fail_key_creation(slot, driver);
4389 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004392}
4393
Gilles Peskineeab56e42018-07-12 17:12:33 +02004394
4395
4396/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004397/* Key derivation */
4398/****************************************************************/
4399
Steven Cooreman932ffb72021-02-15 12:14:32 +01004400#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004402{
4403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004404 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4405 return 1;
4406 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004407#endif
4408#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004409 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4410 return 1;
4411 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004412#endif
4413#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4415 return 1;
4416 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004417#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004419}
4420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004422{
4423 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 psa_status_t status = psa_hash_setup(&operation, alg);
4425 psa_hash_abort(&operation);
4426 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004427}
4428
Gilles Peskine969c5d62019-01-16 15:53:06 +01004429static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004432{
Janos Follath5fe19732019-06-20 15:09:30 +01004433 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4434 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004435 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004436
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 if (!is_kdf_alg_supported(kdf_alg)) {
4439 return PSA_ERROR_NOT_SUPPORTED;
4440 }
John Durkop07cc04a2020-11-16 22:08:34 -08004441
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004442 /* All currently supported key derivation algorithms are based on a
4443 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4445 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4446 if (hash_size == 0) {
4447 return PSA_ERROR_NOT_SUPPORTED;
4448 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004449
4450 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4451 * we might fail later, which is somewhat unfriendly and potentially
4452 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004453 psa_status_t status = psa_hash_try_support(hash_alg);
4454 if (status != PSA_SUCCESS) {
4455 return status;
4456 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4459 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4460 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4461 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004462 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004463
Gilles Peskinecdacf042021-04-29 21:10:00 +02004464 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004466}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004468static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004469{
4470#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 if (alg == PSA_ALG_ECDH) {
4472 return PSA_SUCCESS;
4473 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004474#endif
4475 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004476 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004477}
John Durkop07cc04a2020-11-16 22:08:34 -08004478#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4481 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004482{
4483 psa_status_t status;
4484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 if (operation->alg != 0) {
4486 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004487 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004489 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4490 return PSA_ERROR_INVALID_ARGUMENT;
4491 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4492#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4493 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4494 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4495 status = psa_key_agreement_try_support(ka_alg);
4496 if (status != PSA_SUCCESS) {
4497 return status;
4498 }
4499 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4500#else
4501 return PSA_ERROR_NOT_SUPPORTED;
4502#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4503 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4504#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4505 status = psa_key_derivation_setup_kdf(operation, alg);
4506#else
4507 return PSA_ERROR_NOT_SUPPORTED;
4508#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4509 } else {
4510 return PSA_ERROR_INVALID_ARGUMENT;
4511 }
4512
4513 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004514 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004515 }
4516 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004517}
4518
John Durkopd0321952020-10-29 21:37:36 -07004519#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4521 psa_algorithm_t hash_alg,
4522 psa_key_derivation_step_t step,
4523 const uint8_t *data,
4524 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004525{
4526 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004528 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 if (hkdf->state != HKDF_STATE_INIT) {
4530 return PSA_ERROR_BAD_STATE;
4531 } else {
4532 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4533 hash_alg,
4534 data, data_length);
4535 if (status != PSA_SUCCESS) {
4536 return status;
4537 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004538 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004539 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004540 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004541 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543 if (hkdf->state == HKDF_STATE_INIT) {
4544 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4545 hash_alg,
4546 NULL, 0);
4547 if (status != PSA_SUCCESS) {
4548 return status;
4549 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004550 hkdf->state = HKDF_STATE_STARTED;
4551 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 if (hkdf->state != HKDF_STATE_STARTED) {
4553 return PSA_ERROR_BAD_STATE;
4554 }
4555 status = psa_mac_update(&hkdf->hmac,
4556 data, data_length);
4557 if (status != PSA_SUCCESS) {
4558 return status;
4559 }
4560 status = psa_mac_sign_finish(&hkdf->hmac,
4561 hkdf->prk,
4562 sizeof(hkdf->prk),
4563 &data_length);
4564 if (status != PSA_SUCCESS) {
4565 return status;
4566 }
4567 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004568 hkdf->block_number = 0;
4569 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004571 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 if (hkdf->state == HKDF_STATE_OUTPUT) {
4573 return PSA_ERROR_BAD_STATE;
4574 }
4575 if (hkdf->info_set) {
4576 return PSA_ERROR_BAD_STATE;
4577 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004578 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 if (data_length != 0) {
4580 hkdf->info = mbedtls_calloc(1, data_length);
4581 if (hkdf->info == NULL) {
4582 return PSA_ERROR_INSUFFICIENT_MEMORY;
4583 }
4584 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004585 }
4586 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004587 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004588 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004590 }
4591}
John Durkop07cc04a2020-11-16 22:08:34 -08004592#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004593
John Durkop07cc04a2020-11-16 22:08:34 -08004594#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4595 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4597 const uint8_t *data,
4598 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004599{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4601 return PSA_ERROR_BAD_STATE;
4602 }
Janos Follathf08e2652019-06-13 09:05:41 +01004603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604 if (data_length != 0) {
4605 prf->seed = mbedtls_calloc(1, data_length);
4606 if (prf->seed == NULL) {
4607 return PSA_ERROR_INSUFFICIENT_MEMORY;
4608 }
Janos Follathf08e2652019-06-13 09:05:41 +01004609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004610 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004611 prf->seed_length = data_length;
4612 }
Janos Follathf08e2652019-06-13 09:05:41 +01004613
Gilles Peskine43f958b2020-12-13 14:55:14 +01004614 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004617}
4618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4620 const uint8_t *data,
4621 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4624 return PSA_ERROR_BAD_STATE;
4625 }
Janos Follath81550542019-06-13 14:26:34 +01004626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 if (data_length != 0) {
4628 prf->secret = mbedtls_calloc(1, data_length);
4629 if (prf->secret == NULL) {
4630 return PSA_ERROR_INSUFFICIENT_MEMORY;
4631 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004634 prf->secret_length = data_length;
4635 }
Janos Follath81550542019-06-13 14:26:34 +01004636
Gilles Peskine43f958b2020-12-13 14:55:14 +01004637 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004640}
4641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004642static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4643 const uint8_t *data,
4644 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004645{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4647 return PSA_ERROR_BAD_STATE;
4648 }
Janos Follath63028dd2019-06-13 09:15:47 +01004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 if (data_length != 0) {
4651 prf->label = mbedtls_calloc(1, data_length);
4652 if (prf->label == NULL) {
4653 return PSA_ERROR_INSUFFICIENT_MEMORY;
4654 }
Janos Follath63028dd2019-06-13 09:15:47 +01004655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004657 prf->label_length = data_length;
4658 }
Janos Follath63028dd2019-06-13 09:15:47 +01004659
Gilles Peskine43f958b2020-12-13 14:55:14 +01004660 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004663}
4664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4666 psa_key_derivation_step_t step,
4667 const uint8_t *data,
4668 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004669{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004670 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004671 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004672 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004673 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004675 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004677 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004679 }
4680}
John Durkop07cc04a2020-11-16 22:08:34 -08004681#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4682 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4683
4684#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4685static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4686 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004687 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004689{
4690 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004691 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004692 uint8_t *cur = pms;
4693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004694 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4695 return PSA_ERROR_INVALID_ARGUMENT;
4696 }
John Durkop07cc04a2020-11-16 22:08:34 -08004697
4698 /* Quoting RFC 4279, Section 2:
4699 *
4700 * The premaster secret is formed as follows: if the PSK is N octets
4701 * long, concatenate a uint16 with the value N, N zero octets, a second
4702 * uint16 with the value N, and the PSK itself.
4703 */
4704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004705 *cur++ = MBEDTLS_BYTE_1(data_length);
4706 *cur++ = MBEDTLS_BYTE_0(data_length);
4707 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004708 cur += data_length;
4709 *cur++ = pms[0];
4710 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004712 cur += data_length;
4713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 mbedtls_platform_zeroize(pms, sizeof(pms));
4717 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004718}
Janos Follath6660f0e2019-06-17 08:44:03 +01004719
4720static psa_status_t psa_tls12_prf_psk_to_ms_input(
4721 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004722 psa_key_derivation_step_t step,
4723 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004724 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004725{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4727 return psa_tls12_prf_psk_to_ms_set_key(prf,
4728 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004729 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004732}
John Durkop07cc04a2020-11-16 22:08:34 -08004733#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734
Gilles Peskineb8965192019-09-24 16:21:10 +02004735/** Check whether the given key type is acceptable for the given
4736 * input step of a key derivation.
4737 *
4738 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4739 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4740 * Both secret and non-secret inputs can alternatively have the type
4741 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4742 * that the input was passed as a buffer rather than via a key object.
4743 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004744static int psa_key_derivation_check_input_type(
4745 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004747{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004748 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004749 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750 if (key_type == PSA_KEY_TYPE_DERIVE) {
4751 return PSA_SUCCESS;
4752 }
4753 if (key_type == PSA_KEY_TYPE_NONE) {
4754 return PSA_SUCCESS;
4755 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004756 break;
4757 case PSA_KEY_DERIVATION_INPUT_LABEL:
4758 case PSA_KEY_DERIVATION_INPUT_SALT:
4759 case PSA_KEY_DERIVATION_INPUT_INFO:
4760 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4762 return PSA_SUCCESS;
4763 }
4764 if (key_type == PSA_KEY_TYPE_NONE) {
4765 return PSA_SUCCESS;
4766 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004767 break;
4768 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004769 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004770}
4771
Janos Follathb80a94e2019-06-12 15:54:46 +01004772static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004774 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004775 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004776 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004779 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 status = psa_key_derivation_check_input_type(step, key_type);
4783 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004784 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004786
John Durkopd0321952020-10-29 21:37:36 -07004787#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4789 status = psa_hkdf_input(&operation->ctx.hkdf,
4790 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4791 step, data, data_length);
4792 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004793#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4794#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4796 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4797 step, data, data_length);
4798 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004799#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4800#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004801 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4802 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4803 step, data, data_length);
4804 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004805#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004806 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004808 (void) data;
4809 (void) data_length;
4810 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812 }
4813
Gilles Peskine224b0d62019-09-23 18:13:17 +02004814exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 if (status != PSA_SUCCESS) {
4816 psa_key_derivation_abort(operation);
4817 }
4818 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819}
4820
Janos Follath51f4a0f2019-06-14 11:35:55 +01004821psa_status_t psa_key_derivation_input_bytes(
4822 psa_key_derivation_operation_t *operation,
4823 psa_key_derivation_step_t step,
4824 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004826{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004827 return psa_key_derivation_input_internal(operation, step,
4828 PSA_KEY_TYPE_NONE,
4829 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004830}
4831
Janos Follath51f4a0f2019-06-14 11:35:55 +01004832psa_status_t psa_key_derivation_input_key(
4833 psa_key_derivation_operation_t *operation,
4834 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004836{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004838 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004839 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004840
Ronald Cron5c522922020-11-14 16:35:34 +01004841 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4843 if (status != PSA_SUCCESS) {
4844 psa_key_derivation_abort(operation);
4845 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004846 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004847
4848 /* Passing a key object as a SECRET input unlocks the permission
4849 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004851 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 status = psa_key_derivation_input_internal(operation,
4855 step, slot->attr.type,
4856 slot->key.data,
4857 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004859 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004863
4864
4865
4866/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004867/* Key agreement */
4868/****************************************************************/
4869
John Durkop6ba40d12020-11-10 08:50:04 -08004870#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4872 size_t peer_key_length,
4873 const mbedtls_ecp_keypair *our_key,
4874 uint8_t *shared_secret,
4875 size_t shared_secret_size,
4876 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004877{
Steven Cooremand4867872020-08-05 16:31:39 +02004878 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004879 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004880 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004881 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4883 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004884
Ronald Cron90857082020-11-25 14:58:33 +01004885 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4887 bits,
4888 peer_key,
4889 peer_key_length,
4890 &their_key);
4891 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004892 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004894
Jaeden Amero97271b32019-01-10 19:38:51 +00004895 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004896 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4897 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004898 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004900 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004901 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4902 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004903 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004904 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004905
Jaeden Amero97271b32019-01-10 19:38:51 +00004906 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 mbedtls_ecdh_calc_secret(&ecdh,
4908 shared_secret_length,
4909 shared_secret, shared_secret_size,
4910 mbedtls_psa_get_random,
4911 MBEDTLS_PSA_RANDOM_STATE));
4912 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004913 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004914 }
4915 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004916 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004918
4919exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004920 if (status != PSA_SUCCESS) {
4921 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4922 }
4923 mbedtls_ecdh_free(&ecdh);
4924 mbedtls_ecp_keypair_free(their_key);
4925 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004927 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004928}
John Durkop6ba40d12020-11-10 08:50:04 -08004929#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004930
Gilles Peskine01d718c2018-09-18 12:01:02 +02004931#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004933static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4934 psa_key_slot_t *private_key,
4935 const uint8_t *peer_key,
4936 size_t peer_key_length,
4937 uint8_t *shared_secret,
4938 size_t shared_secret_size,
4939 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004940{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004941 mbedtls_ecp_keypair *ecp = NULL;
4942 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004945#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004946 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004947 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4948 return PSA_ERROR_INVALID_ARGUMENT;
4949 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004950 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951 private_key->attr.type,
4952 private_key->attr.bits,
4953 private_key->key.data,
4954 private_key->key.bytes,
4955 &ecp);
4956 if (status != PSA_SUCCESS) {
4957 return status;
4958 }
4959 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4960 ecp,
4961 shared_secret, shared_secret_size,
4962 shared_secret_length);
4963 mbedtls_ecp_keypair_free(ecp);
4964 mbedtls_free(ecp);
4965 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004966#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004967 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004968 (void) ecp;
4969 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004970 (void) private_key;
4971 (void) peer_key;
4972 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004973 (void) shared_secret;
4974 (void) shared_secret_size;
4975 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004977 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004978}
4979
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004980/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004981 * to potentially free embedded data structures and wipe confidential data.
4982 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004983static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4984 psa_key_derivation_step_t step,
4985 psa_key_slot_t *private_key,
4986 const uint8_t *peer_key,
4987 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004988{
4989 psa_status_t status;
4990 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4991 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004992 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004993
4994 /* Step 1: run the secret agreement algorithm to generate the shared
4995 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004996 status = psa_key_agreement_raw_internal(ka_alg,
4997 private_key,
4998 peer_key, peer_key_length,
4999 shared_secret,
5000 sizeof(shared_secret),
5001 &shared_secret_length);
5002 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02005003 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02005005
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005006 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005007 * the shared secret. A shared secret is permitted wherever a key
5008 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009 status = psa_key_derivation_input_internal(operation, step,
5010 PSA_KEY_TYPE_DERIVE,
5011 shared_secret,
5012 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005013exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5015 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016}
5017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5019 psa_key_derivation_step_t step,
5020 mbedtls_svc_key_id_t private_key,
5021 const uint8_t *peer_key,
5022 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005023{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005025 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005026 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5029 return PSA_ERROR_INVALID_ARGUMENT;
5030 }
Ronald Cron5c522922020-11-14 16:35:34 +01005031 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005032 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5033 if (status != PSA_SUCCESS) {
5034 return status;
5035 }
5036 status = psa_key_agreement_internal(operation, step,
5037 slot,
5038 peer_key, peer_key_length);
5039 if (status != PSA_SUCCESS) {
5040 psa_key_derivation_abort(operation);
5041 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005042 /* If a private key has been added as SECRET, we allow the derived
5043 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005045 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005047 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005049 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005052}
5053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005054psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5055 mbedtls_svc_key_id_t private_key,
5056 const uint8_t *peer_key,
5057 size_t peer_key_length,
5058 uint8_t *output,
5059 size_t output_size,
5060 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005061{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005062 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005063 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005064 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005065 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005068 status = PSA_ERROR_INVALID_ARGUMENT;
5069 goto exit;
5070 }
Ronald Cron5c522922020-11-14 16:35:34 +01005071 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005072 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5073 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005074 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005076
Gilles Peskine42313fb2022-04-14 00:17:15 +02005077 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5078 * for the output size. The PSA specification only guarantees that this
5079 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5080 * but it might be nice to allow smaller buffers if the output fits.
5081 * At the time of writing this comment, with only ECDH implemented,
5082 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5083 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5084 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005085 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005086 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5087 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005088 status = PSA_ERROR_BUFFER_TOO_SMALL;
5089 goto exit;
5090 }
5091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005092 status = psa_key_agreement_raw_internal(alg, slot,
5093 peer_key, peer_key_length,
5094 output, output_size,
5095 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005096
5097exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005099 /* If an error happens and is not handled properly, the output
5100 * may be used as a key to protect sensitive data. Arrange for such
5101 * a key to be random, which is likely to result in decryption or
5102 * verification errors. This is better than filling the buffer with
5103 * some constant data such as zeros, which would result in the data
5104 * being protected with a reproducible, easily knowable key.
5105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005106 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005107 *output_length = output_size;
5108 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005110 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005113}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005114
5115
Gilles Peskine30524eb2020-11-13 17:02:26 +01005116
Gilles Peskine86a440b2018-11-12 18:39:40 +01005117/****************************************************************/
5118/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005119/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005120
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005121#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5122#include "mbedtls/entropy_poll.h"
5123#endif
5124
Gilles Peskine30524eb2020-11-13 17:02:26 +01005125/** Initialize the PSA random generator.
5126 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005128{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005129#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005131#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5132
Gilles Peskine30524eb2020-11-13 17:02:26 +01005133 /* Set default configuration if
5134 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005136 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 }
5138 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005139 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005143#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5144 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5145 /* The PSA entropy injection feature depends on using NV seed as an entropy
5146 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005147 mbedtls_entropy_add_source(&rng->entropy,
5148 mbedtls_nv_seed_poll, NULL,
5149 MBEDTLS_ENTROPY_BLOCK_SIZE,
5150 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005151#endif
5152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005153 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005154#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005155}
5156
5157/** Deinitialize the PSA random generator.
5158 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005159static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005160{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005161#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005163#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5165 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005166#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005167}
5168
5169/** Seed the PSA random generator.
5170 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005172{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005173#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5174 /* Do nothing: the external RNG seeds itself. */
5175 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005177#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005178 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005179 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5180 drbg_seed, sizeof(drbg_seed) - 1);
5181 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005182#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005183}
5184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185psa_status_t psa_generate_random(uint8_t *output,
5186 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005187{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005188 GUARD_MODULE_INITIALIZED;
5189
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005190#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5191
5192 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5194 output, output_size,
5195 &output_length);
5196 if (status != PSA_SUCCESS) {
5197 return status;
5198 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005199 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005200 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 if (output_length != output_size) {
5202 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5203 }
5204 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005205
5206#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005208 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005209 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5211 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5212 output_size);
5213 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5214 output, request_size);
5215 if (ret != 0) {
5216 return mbedtls_to_psa_error(ret);
5217 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005218 output_size -= request_size;
5219 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005220 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005222#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005223}
5224
Gilles Peskine8814fc42020-12-14 15:33:44 +01005225/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005226 *
5227 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5228 * `psa_generate_random(...)`. The state parameter is ignored since the
5229 * PSA API doesn't support passing an explicit state.
5230 *
5231 * In the non-external case, psa_generate_random() calls an
5232 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5233 * and semantics as mbedtls_psa_get_random(). As an optimization,
5234 * instead of doing this back-and-forth between the PSA API and the
5235 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5236 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5239int mbedtls_psa_get_random(void *p_rng,
5240 unsigned char *output,
5241 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005242{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005243 /* This function takes a pointer to the RNG state because that's what
5244 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5245 * its own state internally and doesn't let the caller access that state.
5246 * So we just ignore the state parameter, and in practice we'll pass
5247 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005248 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 psa_status_t status = psa_generate_random(output, output_size);
5250 if (status == PSA_SUCCESS) {
5251 return 0;
5252 } else {
5253 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5254 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005255}
5256#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5257
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005258#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5260 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005261{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005262 if (global_data.initialized) {
5263 return PSA_ERROR_NOT_PERMITTED;
5264 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005266 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5267 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5268 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5269 return PSA_ERROR_INVALID_ARGUMENT;
5270 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005273}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005274#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005275
Ronald Cron3772afe2021-02-08 16:10:05 +01005276/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005277 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005278 * \param type The key type
5279 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005280 *
5281 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005282 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005283 * \retval #PSA_ERROR_INVALID_ARGUMENT
5284 * The size in bits of the key is not valid.
5285 * \retval #PSA_ERROR_NOT_SUPPORTED
5286 * The type and/or the size in bits of the key or the combination of
5287 * the two is not supported.
5288 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005289static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005290 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005291{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005294 if (key_type_is_raw_bytes(type)) {
5295 status = validate_unstructured_key_bit_size(type, bits);
5296 if (status != PSA_SUCCESS) {
5297 return status;
5298 }
5299 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005300#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005301 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5302 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5303 return PSA_ERROR_NOT_SUPPORTED;
5304 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005305
Ronald Cron01b2aba2020-10-05 09:42:02 +02005306 /* Accept only byte-aligned keys, for the same reasons as
5307 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005308 if (bits % 8 != 0) {
5309 return PSA_ERROR_NOT_SUPPORTED;
5310 }
5311 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005312#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005313
Ronald Cron5c4d3862020-12-07 11:07:24 +01005314#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005315 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005316 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 return PSA_SUCCESS;
5318 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005319#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005320 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005321 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005322 }
5323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005325}
5326
Ronald Cron55ed0592020-10-05 10:30:40 +02005327psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005328 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005330{
5331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005332 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005334 if ((attributes->domain_parameters == NULL) &&
5335 (attributes->domain_parameters_size != 0)) {
5336 return PSA_ERROR_INVALID_ARGUMENT;
5337 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339 if (key_type_is_raw_bytes(type)) {
5340 status = psa_generate_random(key_buffer, key_buffer_size);
5341 if (status != PSA_SUCCESS) {
5342 return status;
5343 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005344
David Brown12ca5032021-02-11 11:02:00 -07005345#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 if (type == PSA_KEY_TYPE_DES) {
5347 psa_des_set_key_parity(key_buffer, key_buffer_size);
5348 }
David Brown8107e312021-02-12 08:08:46 -07005349#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005351
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005352#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5353 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5355 return mbedtls_psa_rsa_generate_key(attributes,
5356 key_buffer,
5357 key_buffer_size,
5358 key_buffer_length);
5359 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005360#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5361 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005362
John Durkop9814fa22020-11-04 12:28:15 -08005363#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5365 return mbedtls_psa_ecp_generate_key(attributes,
5366 key_buffer,
5367 key_buffer_size,
5368 key_buffer_length);
5369 } else
John Durkop9814fa22020-11-04 12:28:15 -08005370#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005371 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 (void) key_buffer_length;
5373 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005374 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005376 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005377}
Darryl Green0c6575a2018-11-07 16:05:30 +00005378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005379psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5380 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381{
5382 psa_status_t status;
5383 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005384 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005385 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005386
Ronald Cron81709fc2020-11-14 12:10:32 +01005387 *key = MBEDTLS_SVC_KEY_ID_INIT;
5388
Gilles Peskine0f84d622019-09-12 19:03:13 +02005389 /* Reject any attempt to create a zero-length key so that we don't
5390 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 if (psa_get_key_bits(attributes) == 0) {
5392 return PSA_ERROR_INVALID_ARGUMENT;
5393 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005394
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005395 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005396 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5397 return PSA_ERROR_INVALID_ARGUMENT;
5398 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005400 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5401 &slot, &driver);
5402 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005403 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005404 }
Gilles Peskine11792082019-08-06 18:36:36 +02005405
Ronald Cron977c2472020-10-13 08:32:21 +02005406 /* In the case of a transparent key or an opaque key stored in local
5407 * storage (thus not in the case of generating a key in a secure element
5408 * or cryptoprocessor with storage), we have to allocate a buffer to
5409 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 if (slot->key.data == NULL) {
5411 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5412 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005413 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005414 attributes->core.type, attributes->core.bits);
5415 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005416 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005418
5419 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 attributes->core.type,
5421 attributes->core.bits);
5422 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005423 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 attributes, &key_buffer_size);
5425 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005426 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 }
Ronald Cron977c2472020-10-13 08:32:21 +02005428 }
Ronald Cron977c2472020-10-13 08:32:21 +02005429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005430 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5431 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005432 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433 }
Ronald Cron977c2472020-10-13 08:32:21 +02005434 }
5435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005436 status = psa_driver_wrapper_generate_key(attributes,
5437 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 if (status != PSA_SUCCESS) {
5440 psa_remove_key_data_from_memory(slot);
5441 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005442
Gilles Peskine11792082019-08-06 18:36:36 +02005443exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005444 if (status == PSA_SUCCESS) {
5445 status = psa_finish_key_creation(slot, driver, key);
5446 }
5447 if (status != PSA_SUCCESS) {
5448 psa_fail_key_creation(slot, driver);
5449 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452}
5453
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454/****************************************************************/
5455/* Module setup */
5456/****************************************************************/
5457
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005458#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005459psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460 void (* entropy_init)(mbedtls_entropy_context *ctx),
5461 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005462{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5464 return PSA_ERROR_BAD_STATE;
5465 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005466 global_data.rng.entropy_init = entropy_init;
5467 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005469}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005470#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005473{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 psa_wipe_all_key_slots();
5475 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5476 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005477 }
5478 /* Wipe all remaining data, including configuration.
5479 * In particular, this sets all state indicator to the value
5480 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005481 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005482
5483 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005485}
5486
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005487#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5488/** Recover a transaction that was interrupted by a power failure.
5489 *
5490 * This function is called during initialization, before psa_crypto_init()
5491 * returns. If this function returns a failure status, the initialization
5492 * fails.
5493 */
5494static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005496{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005498 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5499 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 /* TODO - fall through to the failure case until this
5501 * is implemented.
5502 * https://github.com/ARMmbed/mbed-crypto/issues/218
5503 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005504 default:
5505 /* We found an unsupported transaction in the storage.
5506 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005508 }
5509}
5510#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005512psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005513{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005514 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005515
Gilles Peskinec6b69072018-11-20 21:42:52 +01005516 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517 if (global_data.initialized != 0) {
5518 return PSA_SUCCESS;
5519 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005520
Gilles Peskine30524eb2020-11-13 17:02:26 +01005521 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005522 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005523 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005524 status = mbedtls_psa_random_seed(&global_data.rng);
5525 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005527 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005528 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005530 status = psa_initialize_key_slots();
5531 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005532 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005533 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005534
Ronald Cron088d5d02021-04-10 16:57:30 +02005535 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005536 status = psa_driver_wrapper_init();
5537 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005538 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005539 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005540
Gilles Peskine4b734222019-07-24 15:56:31 +02005541#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 status = psa_crypto_load_transaction();
5543 if (status == PSA_SUCCESS) {
5544 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5545 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005546 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 }
5548 status = psa_crypto_stop_transaction();
5549 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005550 /* There's no transaction to complete. It's all good. */
5551 status = PSA_SUCCESS;
5552 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005553#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005554
Gilles Peskinec6b69072018-11-20 21:42:52 +01005555 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556 global_data.initialized = 1;
5557
5558exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 if (status != PSA_SUCCESS) {
5560 mbedtls_psa_crypto_free();
5561 }
5562 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005563}
5564
David Horstmann114d8242023-12-07 18:39:17 +00005565/* Memory copying test hooks. These are called before input copy, after input
5566 * copy, before output copy and after output copy, respectively.
5567 * They are used by memory-poisoning tests to temporarily unpoison buffers
5568 * while they are copied. */
David Horstmann63f82f72023-11-28 15:21:56 +00005569#if defined(MBEDTLS_TEST_HOOKS)
5570void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5571void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5572void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5573void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5574#endif
5575
David Horstmanndf493552023-11-15 15:18:30 +00005576/** Copy from an input buffer to a local copy.
5577 *
5578 * \param[in] input Pointer to input buffer.
5579 * \param[in] input_len Length of the input buffer.
5580 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5581 * \param[out] input_copy_len Length of the local copy buffer.
5582 * \return #PSA_SUCCESS, if the buffer was successfully
5583 * copied.
5584 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5585 * copy is too small to hold contents of the
5586 * input buffer.
5587 */
5588MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005589psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5590 uint8_t *input_copy, size_t input_copy_len)
5591{
5592 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005593 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005594 }
5595
David Horstmann0760b152023-11-24 16:21:04 +00005596#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005597 if (psa_input_pre_copy_hook != NULL) {
5598 psa_input_pre_copy_hook(input, input_len);
5599 }
David Horstmann0760b152023-11-24 16:21:04 +00005600#endif
5601
David Horstmann660027f2023-11-15 17:33:47 +00005602 if (input_len > 0) {
5603 memcpy(input_copy, input, input_len);
5604 }
David Horstmann957f9802023-10-30 20:29:43 +00005605
David Horstmann0760b152023-11-24 16:21:04 +00005606#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005607 if (psa_input_post_copy_hook != NULL) {
5608 psa_input_post_copy_hook(input, input_len);
5609 }
David Horstmann0760b152023-11-24 16:21:04 +00005610#endif
5611
David Horstmann957f9802023-10-30 20:29:43 +00005612 return PSA_SUCCESS;
5613}
5614
David Horstmanndf493552023-11-15 15:18:30 +00005615/** Copy from a local output buffer into a user-supplied one.
5616 *
5617 * \param[in] output_copy Pointer to a local buffer containing the output.
5618 * \param[in] output_copy_len Length of the local buffer.
5619 * \param[out] output Pointer to user-supplied output buffer.
5620 * \param[out] output_len Length of the user-supplied output buffer.
5621 * \return #PSA_SUCCESS, if the buffer was successfully
5622 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005623 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005624 * user-supplied output buffer is too small to
5625 * hold the contents of the local buffer.
5626 */
5627MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005628psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5629 uint8_t *output, size_t output_len)
5630{
5631 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005632 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005633 }
David Horstmann660027f2023-11-15 17:33:47 +00005634
David Horstmann0760b152023-11-24 16:21:04 +00005635#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005636 if (psa_output_pre_copy_hook != NULL) {
5637 psa_output_pre_copy_hook(output, output_len);
5638 }
David Horstmann0760b152023-11-24 16:21:04 +00005639#endif
5640
David Horstmann660027f2023-11-15 17:33:47 +00005641 if (output_copy_len > 0) {
5642 memcpy(output, output_copy, output_copy_len);
5643 }
5644
David Horstmann0760b152023-11-24 16:21:04 +00005645#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005646 if (psa_output_post_copy_hook != NULL) {
5647 psa_output_post_copy_hook(output, output_len);
5648 }
David Horstmann0760b152023-11-24 16:21:04 +00005649#endif
5650
David Horstmann2bd296e2023-10-30 20:37:12 +00005651 return PSA_SUCCESS;
5652}
5653
David Horstmann81a0d572023-11-20 17:15:32 +00005654psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5655 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005656{
5657 psa_status_t status;
5658
David Horstmann0d52c712023-11-23 15:50:37 +00005659 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005660
David Horstmann726bf052023-11-15 18:11:26 +00005661 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005662 return PSA_SUCCESS;
5663 }
5664
David Horstmann81a0d572023-11-20 17:15:32 +00005665 local_input->buffer = mbedtls_calloc(input_len, 1);
5666 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005667 /* Since we dealt with the zero-length case above, we know that
5668 * a NULL return value means a failure of allocation. */
5669 return PSA_ERROR_INSUFFICIENT_MEMORY;
5670 }
David Horstmann81a0d572023-11-20 17:15:32 +00005671 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005672
David Horstmann81a0d572023-11-20 17:15:32 +00005673 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005674
5675 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005676 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005677 if (status != PSA_SUCCESS) {
5678 goto error;
5679 }
5680
5681 return PSA_SUCCESS;
5682
5683error:
David Horstmann81a0d572023-11-20 17:15:32 +00005684 mbedtls_free(local_input->buffer);
5685 local_input->buffer = NULL;
5686 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005687 return status;
5688}
5689
David Horstmann81a0d572023-11-20 17:15:32 +00005690void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005691{
David Horstmann81a0d572023-11-20 17:15:32 +00005692 mbedtls_free(local_input->buffer);
5693 local_input->buffer = NULL;
5694 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005695}
5696
David Horstmann1a76ab12023-11-20 12:54:09 +00005697psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5698 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005699{
David Horstmann0d52c712023-11-23 15:50:37 +00005700 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005701
David Horstmann726bf052023-11-15 18:11:26 +00005702 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005703 return PSA_SUCCESS;
5704 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005705 local_output->buffer = mbedtls_calloc(output_len, 1);
5706 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005707 /* Since we dealt with the zero-length case above, we know that
5708 * a NULL return value means a failure of allocation. */
5709 return PSA_ERROR_INSUFFICIENT_MEMORY;
5710 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005711 local_output->length = output_len;
5712 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005713
5714 return PSA_SUCCESS;
5715}
5716
David Horstmann1a76ab12023-11-20 12:54:09 +00005717psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005718{
David Horstmann761761f2023-11-15 15:57:32 +00005719 psa_status_t status;
5720
David Horstmann1a76ab12023-11-20 12:54:09 +00005721 if (local_output->buffer == NULL) {
5722 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005723 return PSA_SUCCESS;
5724 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005725 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005726 /* We have an internal copy but nothing to copy back to. */
5727 return PSA_ERROR_CORRUPTION_DETECTED;
5728 }
5729
David Horstmann1a76ab12023-11-20 12:54:09 +00005730 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5731 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005732 if (status != PSA_SUCCESS) {
5733 return status;
5734 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005735
David Horstmann1a76ab12023-11-20 12:54:09 +00005736 mbedtls_free(local_output->buffer);
5737 local_output->buffer = NULL;
5738 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005739
5740 return PSA_SUCCESS;
5741}
5742
Gilles Peskinee59236f2018-01-27 23:32:46 +01005743#endif /* MBEDTLS_PSA_CRYPTO_C */