blob: 1531c70eda7465720ee5f51f1bd306a7628f3d39 [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 Horstmann34980bd2023-11-29 17:07:13 +0000109/* Substitute an input buffer for a local copy of itself.
110 * Assumptions:
111 * - psa_status_t status exists
112 * - An exit label is declared
113 * - The name <buffer>_copy is not used for the given value of <buffer>
114 */
115#define SWAP_FOR_LOCAL_INPUT(input, length) \
116 psa_crypto_local_input_t input ## _copy = PSA_CRYPTO_LOCAL_INPUT_INIT; \
117 status = psa_crypto_local_input_alloc(input, length, &input ## _copy); \
118 if (status != PSA_SUCCESS) { \
119 goto exit; \
120 } \
121 input = input ## _copy.buffer;
122
123#define FREE_LOCAL_INPUT(input) \
124 psa_crypto_local_input_free(&input ## _copy);
125
126/* Substitute an output buffer for a local copy of itself.
127 * Assumptions:
128 * - psa_status_t status exists
129 * - An exit label is declared
130 * - The name <buffer>_copy is not used for the given value of <buffer>
131 */
132#define SWAP_FOR_LOCAL_OUTPUT(output, length) \
133 psa_crypto_local_output_t output ## _copy = PSA_CRYPTO_LOCAL_OUTPUT_INIT; \
134 status = psa_crypto_local_output_alloc(output, length, &output ## _copy); \
135 if (status != PSA_SUCCESS) { \
136 goto exit; \
137 } \
138 output = output ## _copy.buffer;
139
140#define FREE_LOCAL_OUTPUT(output) \
141 psa_status_t local_output_free_status; \
142 local_output_free_status = psa_crypto_local_output_free(&output ## _copy); \
143 if (local_output_free_status != PSA_SUCCESS) { \
144 status = local_output_free_status; \
145 }
146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100149 /* Mbed TLS error codes can combine a high-level error code and a
150 * low-level error code. The low-level error usually reflects the
151 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 int low_level_ret = -(-ret & 0x007f);
153 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100154 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100156
157 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
158 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
159 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200164 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200166
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
168 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
169 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
170 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
171 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200173 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200175 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187
Jaeden Amero93e21112019-02-20 13:57:28 +0000188#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000189 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000190#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
198 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
Gilles Peskine26869f22019-05-06 15:25:00 +0200205 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200207
208 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200210 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200212
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
234 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100235 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
236 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100237 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
240 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100250
Gilles Peskinee59236f2018-01-27 23:32:46 +0100251 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
252 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
253 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100255
256 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262
Gilles Peskine82e57d12020-11-13 21:31:17 +0100263#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100265 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
266 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100267 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100269 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
270 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100272 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100274#endif
275
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200276 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
277 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
278 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200280
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100283 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100285 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100287 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200289 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100291
Gilles Peskinef76aa772018-10-29 19:24:33 +0100292 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100296 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100298 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100300 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100302 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100304 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100306 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100308
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
312 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
317 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
322 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
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_INVALID_ALG:
327 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
328 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334
Gilles Peskineff2d2002019-05-06 15:26:23 +0200335 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200337 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200339
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200340 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100341 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200342
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100349 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
352 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100356 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100358 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100360 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100362 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100364
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200365 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
366 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
367 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200369
Gilles Peskinea5905292018-02-07 20:59:33 +0100370 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100372 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100374
itayzafrir5c753392018-05-08 11:18:38 +0300375 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300376 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300378 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300380 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300382 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
383 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300385 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100387 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300389 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100391
Janos Follatha13b9052019-11-22 12:48:59 +0000392 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300394
Gilles Peskinee59236f2018-01-27 23:32:46 +0100395 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100397 }
398}
399
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200400
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200401
402
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403/****************************************************************/
404/* Key management */
405/****************************************************************/
406
John Durkop9814fa22020-11-04 12:28:15 -0800407#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800408 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100409 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
410 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
411 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
413 size_t bits,
414 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200415{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100417 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
423#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
427#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100428 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100430#endif
431#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100434#endif
435#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100436 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100438 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 if (bits_is_sloppy) {
440 return MBEDTLS_ECP_DP_SECP521R1;
441 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100443#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 }
445 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100446
Paul Elliott8ff510a2020-06-02 17:19:28 +0100447 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100450 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100452#endif
453#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#endif
457#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100458 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100460#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 if (bits_is_sloppy) {
471 return MBEDTLS_ECP_DP_CURVE25519;
472 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100474#endif
475#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100476 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100479 }
480 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100481
Paul Elliott8ff510a2020-06-02 17:19:28 +0100482 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100484#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100485 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100487#endif
488#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100489 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100491#endif
492#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100493 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100496 }
497 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200498 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100499
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100500 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200502}
John Durkop9814fa22020-11-04 12:28:15 -0800503#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100504 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
505 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
506 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
507 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
510 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511{
512 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200516 case PSA_KEY_TYPE_DERIVE:
517 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100518#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 if (bits != 128 && bits != 192 && bits != 256) {
521 return PSA_ERROR_INVALID_ARGUMENT;
522 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200523 break;
524#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200525#if defined(PSA_WANT_KEY_TYPE_ARIA)
526 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 if (bits != 128 && bits != 192 && bits != 256) {
528 return PSA_ERROR_INVALID_ARGUMENT;
529 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200530 break;
531#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100532#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if (bits != 128 && bits != 192 && bits != 256) {
535 return PSA_ERROR_INVALID_ARGUMENT;
536 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200537 break;
538#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100539#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (bits != 64 && bits != 128 && bits != 192) {
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_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200547 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 if (bits < 8 || bits > 2048) {
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_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200554 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 if (bits != 256) {
556 return PSA_ERROR_INVALID_ARGUMENT;
557 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200558 break;
559#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200562 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if (bits % 8 != 0) {
564 return PSA_ERROR_INVALID_ARGUMENT;
565 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200568}
569
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100570/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100571 *
Steven Cooremancd640932021-03-08 12:00:27 +0100572 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
573 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 *
575 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100576 * \param[in] key_type The key type of the key to be used with the
577 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100578 *
579 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100580 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100581 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100582 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100584MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100585 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100587{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 if (PSA_ALG_IS_HMAC(algorithm)) {
589 if (key_type == PSA_KEY_TYPE_HMAC) {
590 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100591 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100592 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
595 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
596 * key. */
597 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
598 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
599 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
600 * the block length (larger than 1) for block ciphers. */
601 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
602 return PSA_SUCCESS;
603 }
604 }
605 }
606
607 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100608}
609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
611 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200612{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 if (slot->key.data != NULL) {
614 return PSA_ERROR_ALREADY_EXISTS;
615 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 slot->key.data = mbedtls_calloc(1, buffer_length);
618 if (slot->key.data == NULL) {
619 return PSA_ERROR_INSUFFICIENT_MEMORY;
620 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200621
Ronald Cronea0f8a62020-11-25 17:52:23 +0100622 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200624}
625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100626psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
627 const uint8_t *data,
628 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200629{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 psa_status_t status = psa_allocate_buffer_to_slot(slot,
631 data_length);
632 if (status != PSA_SUCCESS) {
633 return status;
634 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 memcpy(slot->key.data, data, data_length);
637 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200638}
639
Ronald Crona0fe59f2020-11-29 11:14:53 +0100640psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100641 const psa_key_attributes_t *attributes,
642 const uint8_t *data, size_t data_length,
643 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
647 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200649 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100650 if (data_length == 0) {
651 return PSA_ERROR_NOT_SUPPORTED;
652 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 if (key_type_is_raw_bytes(type)) {
655 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200656
Steven Cooreman75b74362020-07-28 14:30:13 +0200657 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 if (data_length > SIZE_MAX / 8) {
659 return PSA_ERROR_NOT_SUPPORTED;
660 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200661
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200662 /* Enforce a size limit, and in particular ensure that the bit
663 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100664 if ((*bits) > PSA_MAX_KEY_BITS) {
665 return PSA_ERROR_NOT_SUPPORTED;
666 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 status = validate_unstructured_key_bit_size(type, *bits);
669 if (status != PSA_SUCCESS) {
670 return status;
671 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200672
Ronald Crondd04d422020-11-28 15:14:42 +0100673 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100674 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100675 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 return PSA_SUCCESS;
679 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800680#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
682 if (PSA_KEY_TYPE_IS_ECC(type)) {
683 return mbedtls_psa_ecp_import_key(attributes,
684 data, data_length,
685 key_buffer, key_buffer_size,
686 key_buffer_length,
687 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100688 }
John Durkop9814fa22020-11-04 12:28:15 -0800689#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
690 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700691#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
693 if (PSA_KEY_TYPE_IS_RSA(type)) {
694 return mbedtls_psa_rsa_import_key(attributes,
695 data, data_length,
696 key_buffer, key_buffer_size,
697 key_buffer_length,
698 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200699 }
John Durkop9814fa22020-11-04 12:28:15 -0800700#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
701 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100702 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100705}
706
Gilles Peskinef603c712019-01-19 13:40:11 +0100707/** Calculate the intersection of two algorithm usage policies.
708 *
709 * Return 0 (which allows no operation) on incompatibility.
710 */
711static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100712 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100713 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100715{
Gilles Peskine549ea862019-05-22 11:45:59 +0200716 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 if (alg1 == alg2) {
718 return alg1;
719 }
Steven Cooreman03488022021-02-18 12:07:20 +0100720 /* If the policies are from the same hash-and-sign family, check
721 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
723 PSA_ALG_IS_SIGN_HASH(alg2) &&
724 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
725 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
726 return alg2;
727 }
728 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
729 return alg1;
730 }
Steven Cooreman03488022021-02-18 12:07:20 +0100731 }
732 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100733 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100734 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
736 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
737 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
738 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
739 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100740 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100741
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100742 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100743 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
744 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
745 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
746 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100747 }
748 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
750 (alg1_len <= alg2_len)) {
751 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100752 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
754 (alg2_len <= alg1_len)) {
755 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100756 }
757 }
758 /* If the policies are from the same MAC family, check whether one
759 * of them is a minimum-MAC-length policy. Calculate the most
760 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
762 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
763 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 /* Validate the combination of key type and algorithm. Since the base
765 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
767 return 0;
768 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100769
Steven Cooreman31a876d2021-03-03 20:47:40 +0100770 /* Get the (exact or at-least) output lengths for both sides of the
771 * requested intersection. None of the currently supported algorithms
772 * have an output length dependent on the actual key size, so setting it
773 * to a bogus value of 0 is currently OK.
774 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100775 * Note that for at-least-this-length wildcard algorithms, the output
776 * length is set to the shortest allowed length, which allows us to
777 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
779 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100780 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100781
Steven Cooremana1d83222021-02-25 10:20:29 +0100782 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
784 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
785 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100786 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100787
788 /* If only one is an at-least-this-length policy, the intersection would
789 * be the other (fixed-length) policy as long as said fixed length is
790 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
792 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100793 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
795 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100796 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100797
Steven Cooremancd640932021-03-08 12:00:27 +0100798 /* If none of them are wildcards, check whether they define the same tag
799 * length. This is still possible here when one is default-length and
800 * the other specific-length. Ensure to always return the
801 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 if (alg1_len == alg2_len) {
803 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
804 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 }
806 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100808}
809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810static int psa_key_algorithm_permits(psa_key_type_t key_type,
811 psa_algorithm_t policy_alg,
812 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200813{
Gilles Peskine549ea862019-05-22 11:45:59 +0200814 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 if (requested_alg == policy_alg) {
816 return 1;
817 }
Steven Cooreman03488022021-02-18 12:07:20 +0100818 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
819 * and requested_alg is the same hash-and-sign family with any hash,
820 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
822 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
823 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
824 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100825 }
826 /* If policy_alg is a wildcard AEAD algorithm of the same base as
827 * the requested algorithm, check the requested tag length to be
828 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829 if (PSA_ALG_IS_AEAD(policy_alg) &&
830 PSA_ALG_IS_AEAD(requested_alg) &&
831 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
832 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
833 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
834 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
835 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100836 }
Steven Cooremancd640932021-03-08 12:00:27 +0100837 /* If policy_alg is a MAC algorithm of the same base as the requested
838 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (PSA_ALG_IS_MAC(policy_alg) &&
840 PSA_ALG_IS_MAC(requested_alg) &&
841 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
842 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100843 /* Validate the combination of key type and algorithm. Since the policy
844 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
846 return 0;
847 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100848
Steven Cooreman31a876d2021-03-03 20:47:40 +0100849 /* Get both the requested output length for the algorithm which is to be
850 * verified, and the default output length for the base algorithm.
851 * Note that none of the currently supported algorithms have an output
852 * length dependent on actual key size, so setting it to a bogus value
853 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100854 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100856 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 key_type, 0,
858 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100859
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100860 /* If the policy is default-length, only allow an algorithm with
861 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100862 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
863 return requested_output_length == default_output_length;
864 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100865
866 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100867 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
869 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
870 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100871 }
872
Steven Cooremancd640932021-03-08 12:00:27 +0100873 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
874 * check for the requested MAC length to be equal to or longer than the
875 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100876 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
877 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
878 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100879 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200880 }
Steven Cooremance48e852020-10-05 16:02:45 +0200881 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200882 * a key derivation with that key agreement should also be allowed. This
883 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
885 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
886 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
887 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200888 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100889 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891}
892
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100893/** Test whether a policy permits an algorithm.
894 *
895 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100896 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100897 * \note This function requires providing the key type for which the policy is
898 * being validated, since some algorithm policy definitions (e.g. MAC)
899 * have different properties depending on what kind of cipher it is
900 * combined with.
901 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100902 * \retval PSA_SUCCESS When \p alg is a specific algorithm
903 * allowed by the \p policy.
904 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
905 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
906 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100907 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
909 psa_key_type_t key_type,
910 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100911{
Steven Cooremand788fab2021-02-25 11:29:17 +0100912 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100913 if (alg == 0) {
914 return PSA_ERROR_INVALID_ARGUMENT;
915 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100916
Steven Cooreman7e39f052021-02-23 14:18:32 +0100917 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 if (PSA_ALG_IS_WILDCARD(alg)) {
919 return PSA_ERROR_INVALID_ARGUMENT;
920 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100922 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
923 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
924 return PSA_SUCCESS;
925 } else {
926 return PSA_ERROR_NOT_PERMITTED;
927 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100928}
929
Gilles Peskinef603c712019-01-19 13:40:11 +0100930/** Restrict a key policy based on a constraint.
931 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100932 * \note This function requires providing the key type for which the policy is
933 * being restricted, since some algorithm policy definitions (e.g. MAC)
934 * have different properties depending on what kind of cipher it is
935 * combined with.
936 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100937 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100938 * \param[in,out] policy The policy to restrict.
939 * \param[in] constraint The policy constraint to apply.
940 *
941 * \retval #PSA_SUCCESS
942 * \c *policy contains the intersection of the original value of
943 * \c *policy and \c *constraint.
944 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100945 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100946 * \c *policy is unchanged.
947 */
948static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100949 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100950 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100952{
953 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 psa_key_policy_algorithm_intersection(key_type, policy->alg,
955 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200956 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
958 constraint->alg2);
959 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
960 return PSA_ERROR_INVALID_ARGUMENT;
961 }
962 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
963 return PSA_ERROR_INVALID_ARGUMENT;
964 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100965 policy->usage &= constraint->usage;
966 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200967 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100969}
970
Ronald Cron5c522922020-11-14 16:35:34 +0100971/** Get the description of a key given its identifier and policy constraints
972 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200973 *
Ronald Cron5c522922020-11-14 16:35:34 +0100974 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100975 * nonzero, the key must allow operations with this algorithm. If \p alg is
976 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100977 *
Ronald Cron5c522922020-11-14 16:35:34 +0100978 * In case of a persistent key, the function loads the description of the key
979 * into a key slot if not already done.
980 *
981 * On success, the returned key slot is locked. It is the responsibility of
982 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 */
Ronald Cron5c522922020-11-14 16:35:34 +0100984static psa_status_t psa_get_and_lock_key_slot_with_policy(
985 mbedtls_svc_key_id_t key,
986 psa_key_slot_t **p_slot,
987 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100989{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
991 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 status = psa_get_and_lock_key_slot(key, p_slot);
994 if (status != PSA_SUCCESS) {
995 return status;
996 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100998
999 /* Enforce that usage policy for the key slot contains all the flags
1000 * required by the usage parameter. There is one exception: public
1001 * keys can always be exported, so we treat public key objects as
1002 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001004 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001008 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001009 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001010 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001011
Shaun Case0e7791f2021-12-20 21:14:10 -08001012 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 if (alg != 0) {
1014 status = psa_key_policy_permits(&slot->attr.policy,
1015 slot->attr.type,
1016 alg);
1017 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001018 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001020 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001023
1024error:
1025 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001026 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001028 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001029}
Darryl Green940d72c2018-07-13 13:18:51 +01001030
Ronald Cron5c522922020-11-14 16:35:34 +01001031/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001032 *
1033 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001034 * available, as opposed to a key in a secure element and/or to be used
1035 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001036 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001037 * This is a temporary function that may be used instead of
1038 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1039 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001040 *
Ronald Cron5c522922020-11-14 16:35:34 +01001041 * On success, the returned key slot is locked. It is the responsibility of the
1042 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001043 */
Ronald Cron5c522922020-11-14 16:35:34 +01001044static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1045 mbedtls_svc_key_id_t key,
1046 psa_key_slot_t **p_slot,
1047 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001049{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1051 usage, alg);
1052 if (status != PSA_SUCCESS) {
1053 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001054 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1057 psa_unlock_key_slot(*p_slot);
1058 *p_slot = NULL;
1059 return PSA_ERROR_NOT_SUPPORTED;
1060 }
1061
1062 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001063}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001065psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001066{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001067 /* Data pointer will always be either a valid pointer or NULL in an
1068 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 if (slot->key.data != NULL) {
1070 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1071 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001074 slot->key.data = NULL;
1075 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001078}
1079
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080/** Completely wipe a slot in memory, including its policy.
1081 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001085
1086 /*
1087 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001088 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001089 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1090 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001091 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001092 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001093 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001094#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001096#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001097 status = PSA_ERROR_CORRUPTION_DETECTED;
1098 }
1099
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001100 /* Multipart operations may still be using the key. This is safe
1101 * because all multipart operation objects are independent from
1102 * the key slot: if they need to access the key after the setup
1103 * phase, they have a copy of the key. Note that this means that
1104 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001105 /* At this point, key material and other type-specific content has
1106 * been wiped. Clear remaining metadata. We can call memset and not
1107 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001108 memset(slot, 0, sizeof(*slot));
1109 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001110}
1111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001112psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001114 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001115 psa_status_t status; /* status of the last operation */
1116 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001117#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1118 psa_se_drv_table_entry_t *driver;
1119#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001121 if (mbedtls_svc_key_id_is_null(key)) {
1122 return PSA_SUCCESS;
1123 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001124
Ronald Cronf2911112020-10-29 17:51:10 +01001125 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001126 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001127 * key, this will load the key description from persistent memory if not
1128 * done yet. We cannot avoid this loading as without it we don't know if
1129 * the key is operated by an SE or not and this information is needed by
1130 * the current implementation.
1131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 status = psa_get_and_lock_key_slot(key, &slot);
1133 if (status != PSA_SUCCESS) {
1134 return status;
1135 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001136
Ronald Cronf2911112020-10-29 17:51:10 +01001137 /*
1138 * If the key slot containing the key description is under access by the
1139 * library (apart from the present access), the key cannot be destroyed
1140 * yet. For the time being, just return in error. Eventually (to be
1141 * implemented), the key should be destroyed when all accesses have
1142 * stopped.
1143 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 if (slot->lock_count > 1) {
1145 psa_unlock_key_slot(slot);
1146 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001147 }
1148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001150 /* Refuse the destruction of a read-only key (which may or may not work
1151 * if we attempt it, depending on whether the key is merely read-only
1152 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001153 * Just do the best we can, which is to wipe the copy in memory
1154 * (done in this function's cleanup code). */
1155 overall_status = PSA_ERROR_NOT_PERMITTED;
1156 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001157 }
1158
Gilles Peskine354f7672019-07-12 23:46:38 +02001159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1161 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001162 /* For a key in a secure element, we need to do three things:
1163 * remove the key file in internal storage, destroy the
1164 * key inside the secure element, and update the driver's
1165 * persistent data. Start a transaction that will encompass these
1166 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001168 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001170 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 status = psa_crypto_save_transaction();
1172 if (status != PSA_SUCCESS) {
1173 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 /* We should still try to destroy the key in the secure
1175 * element and the key metadata in storage. This is especially
1176 * important if the error is that the storage is full.
1177 * But how to do it exactly without risking an inconsistent
1178 * state after a reset?
1179 * https://github.com/ARMmbed/mbed-crypto/issues/215
1180 */
1181 overall_status = status;
1182 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001183 }
1184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 status = psa_destroy_se_key(driver,
1186 psa_key_slot_get_slot_number(slot));
1187 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001190 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1192
Darryl Greend49a4992018-06-18 17:27:26 +01001193#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1195 status = psa_destroy_persistent_key(slot->attr.id);
1196 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001200 /* TODO: other slots may have a copy of the same key. We should
1201 * invalidate them.
1202 * https://github.com/ARMmbed/mbed-crypto/issues/214
1203 */
Darryl Greend49a4992018-06-18 17:27:26 +01001204 }
1205#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001206
Gilles Peskinefc762652019-07-22 19:30:34 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 if (driver != NULL) {
1209 status = psa_save_se_persistent_data(driver);
1210 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001211 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001212 }
1213 status = psa_crypto_stop_transaction();
1214 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001215 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001217 }
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1219
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001220exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001221 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 }
1226 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001227}
1228
John Durkop07cc04a2020-11-16 22:08:34 -08001229#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001230 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001231static psa_status_t psa_get_rsa_public_exponent(
1232 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001234{
1235 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001237 uint8_t *buffer = NULL;
1238 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1242 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001243 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 }
1245 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001246 /* It's the default value, which is reported as an empty string,
1247 * so there's nothing to do. */
1248 goto exit;
1249 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251 buflen = mbedtls_mpi_size(&mpi);
1252 buffer = mbedtls_calloc(1, buflen);
1253 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001254 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1255 goto exit;
1256 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1258 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001259 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 attributes->domain_parameters = buffer;
1262 attributes->domain_parameters_size = buflen;
1263
1264exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 mbedtls_mpi_free(&mpi);
1266 if (ret != 0) {
1267 mbedtls_free(buffer);
1268 }
1269 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001270}
John Durkop07cc04a2020-11-16 22:08:34 -08001271#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001272 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001273
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001274/** Retrieve all the publicly-accessible attributes of a key.
1275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1277 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001280 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001281 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1286 if (status != PSA_SUCCESS) {
1287 return status;
1288 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001289
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001290 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1292 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293
1294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1296 psa_set_key_slot_number(attributes,
1297 psa_key_slot_get_slot_number(slot));
1298 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001303 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001304 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001305 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001306 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001307 * is not yet implemented.
1308 * https://github.com/ARMmbed/mbed-crypto/issues/216
1309 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001311 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001312
Ronald Cron90857082020-11-25 14:58:33 +01001313 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 slot->attr.type,
1315 slot->key.data,
1316 slot->key.bytes,
1317 &rsa);
1318 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001319 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001320 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 status = psa_get_rsa_public_exponent(rsa,
1323 attributes);
1324 mbedtls_rsa_free(rsa);
1325 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001326 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001327 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001328#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001329 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001330 default:
1331 /* Nothing else to do. */
1332 break;
1333 }
1334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 if (status != PSA_SUCCESS) {
1336 psa_reset_key_attributes(attributes);
1337 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342}
1343
Gilles Peskinec8000c02019-08-02 20:15:51 +02001344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1345psa_status_t psa_get_key_slot_number(
1346 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001348{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001350 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 return PSA_SUCCESS;
1352 } else {
1353 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001354 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001355}
1356#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1359 size_t key_buffer_size,
1360 uint8_t *data,
1361 size_t data_size,
1362 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001363{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 if (key_buffer_size > data_size) {
1365 return PSA_ERROR_BUFFER_TOO_SMALL;
1366 }
1367 memcpy(data, key_buffer, key_buffer_size);
1368 memset(data + key_buffer_size, 0,
1369 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001370 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001371 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001372}
1373
Ronald Cron7285cda2020-11-26 14:46:37 +01001374psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001375 const psa_key_attributes_t *attributes,
1376 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378{
Ronald Crond18b5f82020-11-25 16:46:05 +01001379 psa_key_type_t type = attributes->core.type;
1380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 if (key_type_is_raw_bytes(type) ||
1382 PSA_KEY_TYPE_IS_RSA(type) ||
1383 PSA_KEY_TYPE_IS_ECC(type)) {
1384 return psa_export_key_buffer_internal(
1385 key_buffer, key_buffer_size,
1386 data, data_size, data_length);
1387 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 /* This shouldn't happen in the reference implementation, but
1389 it is valid for a special-purpose implementation to omit
1390 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001391 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392 }
1393}
1394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1396 uint8_t *data,
1397 size_t data_size,
1398 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399{
1400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1401 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1402 psa_key_slot_t *slot;
1403
Ronald Crone907e552021-01-18 13:22:38 +01001404 /* Reject a zero-length output buffer now, since this can never be a
1405 * valid key representation. This way we know that data must be a valid
1406 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407 if (data_size == 0) {
1408 return PSA_ERROR_BUFFER_TOO_SMALL;
1409 }
Ronald Crone907e552021-01-18 13:22:38 +01001410
Ronald Cron9486f9d2020-11-26 13:36:23 +01001411 /* Set the key to empty now, so that even when there are errors, we always
1412 * set data_length to a value between 0 and data_size. On error, setting
1413 * the key to empty is a good choice because an empty key representation is
1414 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001415 *data_length = 0;
1416
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417 /* Export requires the EXPORT flag. There is an exception for public keys,
1418 * which don't require any flag, but
1419 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1420 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001421 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1422 PSA_KEY_USAGE_EXPORT, 0);
1423 if (status != PSA_SUCCESS) {
1424 return status;
1425 }
mohammad160306e79202018-03-28 13:17:44 +03001426
Ronald Crond18b5f82020-11-25 16:46:05 +01001427 psa_key_attributes_t attributes = {
1428 .core = slot->attr
1429 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 status = psa_driver_wrapper_export_key(&attributes,
1431 slot->key.data, slot->key.bytes,
1432 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001436 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001437}
1438
Ronald Cron7285cda2020-11-26 14:46:37 +01001439psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001440 const psa_key_attributes_t *attributes,
1441 const uint8_t *key_buffer,
1442 size_t key_buffer_size,
1443 uint8_t *data,
1444 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001446{
Ronald Crond18b5f82020-11-25 16:46:05 +01001447 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1450 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001451 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 return psa_export_key_buffer_internal(
1453 key_buffer, key_buffer_size,
1454 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001455 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001458#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001459 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1460 return mbedtls_psa_rsa_export_public_key(attributes,
1461 key_buffer,
1462 key_buffer_size,
1463 data,
1464 data_size,
1465 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001466#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001469#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1470 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001471 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001472#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1474 return mbedtls_psa_ecp_export_public_key(attributes,
1475 key_buffer,
1476 key_buffer_size,
1477 data,
1478 data_size,
1479 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001480#else
1481 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001485 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001486 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001487 /* This shouldn't happen in the reference implementation, but
1488 it is valid for a special-purpose implementation to omit
1489 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001491 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492}
Ronald Crond18b5f82020-11-25 16:46:05 +01001493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1495 uint8_t *data,
1496 size_t data_size,
1497 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001498{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001499 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001500 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001501 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001502 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001503
Ronald Crone907e552021-01-18 13:22:38 +01001504 /* Reject a zero-length output buffer now, since this can never be a
1505 * valid key representation. This way we know that data must be a valid
1506 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 if (data_size == 0) {
1508 return PSA_ERROR_BUFFER_TOO_SMALL;
1509 }
Ronald Crone907e552021-01-18 13:22:38 +01001510
Darryl Greendd8fb772018-11-07 16:00:44 +00001511 /* Set the key to empty now, so that even when there are errors, we always
1512 * set data_length to a value between 0 and data_size. On error, setting
1513 * the key to empty is a good choice because an empty key representation is
1514 * unlikely to be accepted anywhere. */
1515 *data_length = 0;
1516
1517 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1519 if (status != PSA_SUCCESS) {
1520 return status;
1521 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001523 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1524 status = PSA_ERROR_INVALID_ARGUMENT;
1525 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526 }
1527
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001528 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001529 .core = slot->attr
1530 };
Ronald Cron67227982020-11-26 15:16:05 +01001531 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001532 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001534
1535exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001536 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001539}
1540
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001541MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1542 "One or more key attribute flag is listed as both external-only and dual-use")
1543MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1544 "One or more key attribute flag is listed as both internal-only and dual-use")
1545MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1546 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001547
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001548/** Validate that a key policy is internally well-formed.
1549 *
1550 * This function only rejects invalid policies. It does not validate the
1551 * consistency of the policy with respect to other attributes of the key
1552 * such as the key type.
1553 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001555{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1557 PSA_KEY_USAGE_COPY |
1558 PSA_KEY_USAGE_ENCRYPT |
1559 PSA_KEY_USAGE_DECRYPT |
1560 PSA_KEY_USAGE_SIGN_MESSAGE |
1561 PSA_KEY_USAGE_VERIFY_MESSAGE |
1562 PSA_KEY_USAGE_SIGN_HASH |
1563 PSA_KEY_USAGE_VERIFY_HASH |
1564 PSA_KEY_USAGE_DERIVE)) != 0) {
1565 return PSA_ERROR_INVALID_ARGUMENT;
1566 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001569}
1570
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571/** Validate the internal consistency of key attributes.
1572 *
1573 * This function only rejects invalid attribute values. If does not
1574 * validate the consistency of the attributes with any key data that may
1575 * be involved in the creation of the key.
1576 *
1577 * Call this function early in the key creation process.
1578 *
1579 * \param[in] attributes Key attributes for the new key.
1580 * \param[out] p_drv On any return, the driver for the key, if any.
1581 * NULL for a transparent key.
1582 *
1583 */
1584static psa_status_t psa_validate_key_attributes(
1585 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001586 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001587{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001588 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1590 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 status = psa_validate_key_location(lifetime, p_drv);
1593 if (status != PSA_SUCCESS) {
1594 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001595 }
1596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 status = psa_validate_key_persistence(lifetime);
1598 if (status != PSA_SUCCESS) {
1599 return status;
1600 }
1601
1602 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1603 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1604 return PSA_ERROR_INVALID_ARGUMENT;
1605 }
1606 } else {
1607 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1608 return PSA_ERROR_INVALID_ARGUMENT;
1609 }
1610 }
1611
1612 status = psa_validate_key_policy(&attributes->core.policy);
1613 if (status != PSA_SUCCESS) {
1614 return status;
1615 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001616
1617 /* Refuse to create overly large keys.
1618 * Note that this doesn't trigger on import if the attributes don't
1619 * explicitly specify a size (so psa_get_key_bits returns 0), so
1620 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1622 return PSA_ERROR_NOT_SUPPORTED;
1623 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001624
Gilles Peskine91e8c332019-08-02 19:19:39 +02001625 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001626 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1627 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1628 return PSA_ERROR_INVALID_ARGUMENT;
1629 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001632}
1633
Gilles Peskine4747d192019-04-17 15:05:45 +02001634/** Prepare a key slot to receive key material.
1635 *
1636 * This function allocates a key slot and sets its metadata.
1637 *
1638 * If this function fails, call psa_fail_key_creation().
1639 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001640 * This function is intended to be used as follows:
1641 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001642 * it with the specified attributes, and in case of a volatile key assign it
1643 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001644 * -# Populate the slot with the key material.
1645 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1646 * In case of failure at any step, stop the sequence and call
1647 * psa_fail_key_creation().
1648 *
Ronald Cron5c522922020-11-14 16:35:34 +01001649 * On success, the key slot is locked. It is the responsibility of the caller
1650 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001651 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001652 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001653 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001654 * \param[out] p_slot On success, a pointer to the prepared slot.
1655 * \param[out] p_drv On any return, the driver for the key, if any.
1656 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001657 *
1658 * \retval #PSA_SUCCESS
1659 * The key slot is ready to receive key material.
1660 * \return If this function fails, the key slot is an invalid state.
1661 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 */
1663static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001664 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001667 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001668{
1669 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001670 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 psa_key_slot_t *slot;
1672
Gilles Peskinedf179142019-07-15 22:02:14 +02001673 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001674 *p_drv = NULL;
1675
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 status = psa_validate_key_attributes(attributes, p_drv);
1677 if (status != PSA_SUCCESS) {
1678 return status;
1679 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1682 if (status != PSA_SUCCESS) {
1683 return status;
1684 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001685 slot = *p_slot;
1686
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687 /* We're storing the declared bit-size of the key. It's up to each
1688 * creation mechanism to verify that this information is correct.
1689 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001690 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001691 * is optional (import, copy). In case of a volatile key, assign it the
1692 * volatile key identifier associated to the slot returned to contain its
1693 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001694
1695 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001697#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1698 slot->attr.id = volatile_key_id;
1699#else
1700 slot->attr.id.key_id = volatile_key_id;
1701#endif
1702 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001703
Gilles Peskine91e8c332019-08-02 19:19:39 +02001704 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001705 * external-only flags, query `attributes`. Thanks to the check
1706 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001707 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001708 * may have set. */
1709 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001710
Gilles Peskinecbaff462019-07-12 23:46:04 +02001711#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001712 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001713 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001714 * create the key file in internal storage, create the
1715 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001716 * persistent data. This is done by starting a transaction that will
1717 * encompass these three actions.
1718 * For registering a volatile key, we just need to find an appropriate
1719 * slot number inside the SE. Since the key is designated volatile, creating
1720 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001721 /* The first thing to do is to find a slot number for the new key.
1722 * We save the slot number in persistent storage as part of the
1723 * transaction data. It will be needed to recover if the power
1724 * fails during the key creation process, to clean up on the secure
1725 * element side after restarting. Obtaining a slot number from the
1726 * secure element driver updates its persistent state, but we do not yet
1727 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001728 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001731 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1732 &slot_number);
1733 if (status != PSA_SUCCESS) {
1734 return status;
1735 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1738 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001739 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001740 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001741 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001742 status = psa_crypto_save_transaction();
1743 if (status != PSA_SUCCESS) {
1744 (void) psa_crypto_stop_transaction();
1745 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001746 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001747 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001748
1749 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001751 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001753 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001754 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001755 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001756 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001757#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001760}
Gilles Peskine4747d192019-04-17 15:05:45 +02001761
1762/** Finalize the creation of a key once its key material has been set.
1763 *
1764 * This entails writing the key to persistent storage.
1765 *
1766 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001767 * See the documentation of psa_start_key_creation() for the intended use
1768 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 *
Ronald Cron5c522922020-11-14 16:35:34 +01001770 * If the finalization succeeds, the function unlocks the key slot (it was
1771 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1772 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001773 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 * \param[in,out] slot Pointer to the slot with key material.
1775 * \param[in] driver The secure element driver for the key,
1776 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001777 * \param[out] key On success, identifier of the key. Note that the
1778 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001779 *
1780 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001781 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001782 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1783 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1784 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1785 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1786 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1787 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001788 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001789 * \return If this function fails, the key slot is an invalid state.
1790 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001792static psa_status_t psa_finish_key_creation(
1793 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001794 psa_se_drv_table_entry_t *driver,
1795 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001796{
1797 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001798 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001799 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001800
1801#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001803#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001805 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001806 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001808
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001809 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1810 sizeof(data.slot_number),
1811 "Slot number size does not match psa_se_key_data_storage_t");
1812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001813 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1814 status = psa_save_persistent_key(&slot->attr,
1815 (uint8_t *) &data,
1816 sizeof(data));
1817 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001818#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1819 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001820 /* Key material is saved in export representation in the slot, so
1821 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 status = psa_save_persistent_key(&slot->attr,
1823 slot->key.data,
1824 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001825 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001826 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001827#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1828
Gilles Peskinecbaff462019-07-12 23:46:04 +02001829#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001830 /* Finish the transaction for a key creation. This does not
1831 * happen when registering an existing key. Detect this case
1832 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833 * creation of a persistent key in a secure element requires a transaction,
1834 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 if (driver != NULL &&
1836 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1837 status = psa_save_se_persistent_data(driver);
1838 if (status != PSA_SUCCESS) {
1839 psa_destroy_persistent_key(slot->attr.id);
1840 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001841 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001843 }
1844#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001847 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001848 status = psa_unlock_key_slot(slot);
1849 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001850 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001851 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001852 }
Ronald Cron50972942020-11-14 11:28:25 +01001853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001855}
1856
1857/** Abort the creation of a key.
1858 *
1859 * You may call this function after calling psa_start_key_creation(),
1860 * or after psa_finish_key_creation() fails. In other circumstances, this
1861 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001862 * See the documentation of psa_start_key_creation() for the intended use
1863 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001865 * \param[in,out] slot Pointer to the slot with key material.
1866 * \param[in] driver The secure element driver for the key,
1867 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001868 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869static void psa_fail_key_creation(psa_key_slot_t *slot,
1870 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001871{
Gilles Peskine011e4282019-06-26 18:34:38 +02001872 (void) driver;
1873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001876 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001877
1878#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001879 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 * element, and the failure happened later (when saving metadata
1881 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001882 * element.
1883 * https://github.com/ARMmbed/mbed-crypto/issues/217
1884 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001885
Gilles Peskined7729582019-08-05 15:55:54 +02001886 /* Abort the ongoing transaction if any (there may not be one if
1887 * the creation process failed before starting one, or if the
1888 * key creation is a registration of a key in a secure element).
1889 * Earlier functions must already have done what it takes to undo any
1890 * partial creation. All that's left is to update the transaction data
1891 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001893#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001896}
1897
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001898/** Validate optional attributes during key creation.
1899 *
1900 * Some key attributes are optional during key creation. If they are
1901 * specified in the attributes structure, check that they are consistent
1902 * with the data in the slot.
1903 *
1904 * This function should be called near the end of key creation, after
1905 * the slot in memory is fully populated but before saving persistent data.
1906 */
1907static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001908 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 if (attributes->core.type != 0) {
1912 if (attributes->core.type != slot->attr.type) {
1913 return PSA_ERROR_INVALID_ARGUMENT;
1914 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 }
1916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001918#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1920 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001921 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001922 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001923 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001924
Ronald Cron90857082020-11-25 14:58:33 +01001925 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 slot->attr.type,
1927 slot->key.data,
1928 slot->key.bytes,
1929 &rsa);
1930 if (status != PSA_SUCCESS) {
1931 return status;
1932 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 mbedtls_mpi_init(&actual);
1935 mbedtls_mpi_init(&required);
1936 ret = mbedtls_rsa_export(rsa,
1937 NULL, NULL, NULL, NULL, &actual);
1938 mbedtls_rsa_free(rsa);
1939 mbedtls_free(rsa);
1940 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 }
1943 ret = mbedtls_mpi_read_binary(&required,
1944 attributes->domain_parameters,
1945 attributes->domain_parameters_size);
1946 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948 }
1949 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 }
1952rsa_exit:
1953 mbedtls_mpi_free(&actual);
1954 mbedtls_mpi_free(&required);
1955 if (ret != 0) {
1956 return mbedtls_to_psa_error(ret);
1957 }
1958 } else
John Durkop9814fa22020-11-04 12:28:15 -08001959#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1960 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 }
1964 }
1965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001966 if (attributes->core.bits != 0) {
1967 if (attributes->core.bits != slot->attr.bits) {
1968 return PSA_ERROR_INVALID_ARGUMENT;
1969 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 }
1971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973}
1974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1976 const uint8_t *data,
1977 size_t data_length,
1978 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001979{
1980 psa_status_t status;
1981 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001982 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001984
Ronald Cron81709fc2020-11-14 12:10:32 +01001985 *key = MBEDTLS_SVC_KEY_ID_INIT;
1986
Gilles Peskine0f84d622019-09-12 19:03:13 +02001987 /* Reject zero-length symmetric keys (including raw data key objects).
1988 * This also rejects any key which might be encoded as an empty string,
1989 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001990 if (data_length == 0) {
1991 return PSA_ERROR_INVALID_ARGUMENT;
1992 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1995 &slot, &driver);
1996 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001997 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002000 /* In the case of a transparent key or an opaque key stored in local
2001 * storage (thus not in the case of generating a key in a secure element
2002 * or cryptoprocessor with storage), we have to allocate a buffer to
2003 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 if (slot->key.data == NULL) {
2005 status = psa_allocate_buffer_to_slot(slot, data_length);
2006 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002007 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002009 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002010
2011 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 status = psa_driver_wrapper_import_key(attributes,
2013 data, data_length,
2014 slot->key.data,
2015 slot->key.bytes,
2016 &slot->key.bytes, &bits);
2017 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002018 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002022 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002023 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002024 status = PSA_ERROR_INVALID_ARGUMENT;
2025 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002026 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 status = psa_validate_optional_attributes(slot, attributes);
2029 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002030 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002034exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 if (status != PSA_SUCCESS) {
2036 psa_fail_key_creation(slot, driver);
2037 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002040}
2041
Gilles Peskined7729582019-08-05 15:55:54 +02002042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2043psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002045{
2046 psa_status_t status;
2047 psa_key_slot_t *slot = NULL;
2048 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002050
2051 /* Leaving attributes unspecified is not currently supported.
2052 * It could make sense to query the key type and size from the
2053 * secure element, but not all secure elements support this
2054 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2056 return PSA_ERROR_NOT_SUPPORTED;
2057 }
2058 if (psa_get_key_bits(attributes) == 0) {
2059 return PSA_ERROR_NOT_SUPPORTED;
2060 }
Gilles Peskined7729582019-08-05 15:55:54 +02002061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002062 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2063 &slot, &driver);
2064 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002065 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 }
Gilles Peskined7729582019-08-05 15:55:54 +02002067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002069
2070exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 if (status != PSA_SUCCESS) {
2072 psa_fail_key_creation(slot, driver);
2073 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074
Gilles Peskined7729582019-08-05 15:55:54 +02002075 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002076 psa_close_key(key);
2077 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002078}
2079#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002081static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2082 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002083{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 psa_status_t status = psa_copy_key_material_into_slot(target,
2085 source->key.data,
2086 source->key.bytes);
2087 if (status != PSA_SUCCESS) {
2088 return status;
2089 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002090
Steven Cooreman398aee52020-10-13 14:35:45 +02002091 target->attr.type = source->attr.type;
2092 target->attr.bits = source->attr.bits;
2093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002095}
2096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2098 const psa_key_attributes_t *specified_attributes,
2099 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002100{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002102 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002103 psa_key_slot_t *source_slot = NULL;
2104 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002106 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002107
Ronald Cron81709fc2020-11-14 12:10:32 +01002108 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2109
Ronald Cron5c522922020-11-14 16:35:34 +01002110 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002111 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2112 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002113 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002114 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 status = psa_validate_optional_attributes(source_slot,
2117 specified_attributes);
2118 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002119 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 status = psa_restrict_key_policy(source_slot->attr.type,
2123 &actual_attributes.core.policy,
2124 &source_slot->attr.policy);
2125 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2130 &target_slot, &driver);
2131 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002135#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002137 /* Copying to a secure element is not implemented yet. */
2138 status = PSA_ERROR_NOT_SUPPORTED;
2139 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002144 /*
2145 * Copying through an opaque driver is not implemented yet, consider
2146 * a lifetime with an external location as an invalid parameter for
2147 * now.
2148 */
2149 status = PSA_ERROR_INVALID_ARGUMENT;
2150 goto exit;
2151 }
2152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002153 status = psa_copy_key_material(source_slot, target_slot);
2154 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002155 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002159exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 if (status != PSA_SUCCESS) {
2161 psa_fail_key_creation(target_slot, driver);
2162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002164 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002166 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002167}
2168
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002169
2170
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002171/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002172/* Message digests */
2173/****************************************************************/
2174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002177 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 if (operation->id == 0) {
2179 return PSA_SUCCESS;
2180 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002183 operation->id = 0;
2184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186}
2187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2189 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002192
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002193 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002194 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002195 status = PSA_ERROR_BAD_STATE;
2196 goto exit;
2197 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002200 status = PSA_ERROR_INVALID_ARGUMENT;
2201 goto exit;
2202 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002204 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2205 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002209
2210exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 if (status != PSA_SUCCESS) {
2212 psa_hash_abort(operation);
2213 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002214
2215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2219 const uint8_t *input,
2220 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002225 status = PSA_ERROR_BAD_STATE;
2226 goto exit;
2227 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002228
Steven Cooremanc8288352021-03-04 14:02:19 +01002229 /* Don't require hash implementations to behave correctly on a
2230 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 if (input_length == 0) {
2232 return PSA_SUCCESS;
2233 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002236
2237exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 if (status != PSA_SUCCESS) {
2239 psa_hash_abort(operation);
2240 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243}
2244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2246 uint8_t *hash,
2247 size_t hash_size,
2248 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002250 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 if (operation->id == 0) {
2252 return PSA_ERROR_BAD_STATE;
2253 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254
Steven Cooreman1e582352021-02-18 17:24:37 +01002255 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 operation, hash, hash_size, hash_length);
2257 psa_hash_abort(operation);
2258 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259}
2260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2262 const uint8_t *hash,
2263 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002265 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002267 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 operation,
2269 actual_hash, sizeof(actual_hash),
2270 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002273 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002277 status = PSA_ERROR_INVALID_SIGNATURE;
2278 goto exit;
2279 }
2280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002282 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002284
2285exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2287 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002288 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292}
2293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294psa_status_t psa_hash_compute(psa_algorithm_t alg,
2295 const uint8_t *input, size_t input_length,
2296 uint8_t *hash, size_t hash_size,
2297 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002298{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002299 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002300 if (!PSA_ALG_IS_HASH(alg)) {
2301 return PSA_ERROR_INVALID_ARGUMENT;
2302 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2305 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002306}
2307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308psa_status_t psa_hash_compare(psa_algorithm_t alg,
2309 const uint8_t *input, size_t input_length,
2310 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002311{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002312 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002313 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (!PSA_ALG_IS_HASH(alg)) {
2316 return PSA_ERROR_INVALID_ARGUMENT;
2317 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002318
Steven Cooreman1e582352021-02-18 17:24:37 +01002319 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 alg, input, input_length,
2321 actual_hash, sizeof(actual_hash),
2322 &actual_hash_length);
2323 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002324 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 }
2326 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002327 status = PSA_ERROR_INVALID_SIGNATURE;
2328 goto exit;
2329 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002331 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002332 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002333
2334exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2336 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002337}
2338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2340 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002341{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 if (source_operation->id == 0 ||
2343 target_operation->id != 0) {
2344 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002345 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2348 target_operation);
2349 if (status != PSA_SUCCESS) {
2350 psa_hash_abort(target_operation);
2351 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002354}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355
2356
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358/* MAC */
2359/****************************************************************/
2360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002361psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002362{
Steven Cooreman07897832021-03-19 18:28:56 +01002363 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (operation->id == 0) {
2365 return PSA_SUCCESS;
2366 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002368 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002369 operation->mac_size = 0;
2370 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002371 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002374}
2375
Ronald Cron1c650a12021-06-17 16:33:22 +02002376static psa_status_t psa_mac_finalize_alg_and_key_validation(
2377 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002378 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002380{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 psa_key_type_t key_type = psa_get_key_type(attributes);
2383 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002385 if (!PSA_ALG_IS_MAC(alg)) {
2386 return PSA_ERROR_INVALID_ARGUMENT;
2387 }
Steven Cooreman07897832021-03-19 18:28:56 +01002388
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002389 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 status = psa_mac_key_can_do(alg, key_type);
2391 if (status != PSA_SUCCESS) {
2392 return status;
2393 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002394
Steven Cooremana6474de2021-05-10 11:13:41 +02002395 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002399 /* A very short MAC is too short for security since it can be
2400 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2401 * so we make this our minimum, even though 32 bits is still
2402 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002404 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2407 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002408 /* It's impossible to "truncate" to a larger length than the full length
2409 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002411 }
2412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002414 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2415 * that is disabled in the compile-time configuration. The result can
2416 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2417 * configuration into account. In this case, force a return of
2418 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2419 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2420 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2421 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2422 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002424 }
2425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002427}
2428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002429static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2430 mbedtls_svc_key_id_t key,
2431 psa_algorithm_t alg,
2432 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002433{
2434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2435 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002436 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002437 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002438
2439 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002441 status = PSA_ERROR_BAD_STATE;
2442 goto exit;
2443 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002444
2445 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002446 key,
2447 &slot,
2448 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2449 alg);
2450 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002451 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002452 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002453
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002454 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002455 .core = slot->attr
2456 };
2457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2459 &operation->mac_size);
2460 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002461 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002463
2464 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002465 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (is_sign) {
2467 status = psa_driver_wrapper_mac_sign_setup(operation,
2468 &attributes,
2469 slot->key.data,
2470 slot->key.bytes,
2471 alg);
2472 } else {
2473 status = psa_driver_wrapper_mac_verify_setup(operation,
2474 &attributes,
2475 slot->key.data,
2476 slot->key.bytes,
2477 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002478 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002479
Gilles Peskinefbfac682018-07-08 20:51:54 +02002480exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 if (status != PSA_SUCCESS) {
2482 psa_mac_abort(operation);
2483 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488}
2489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2491 mbedtls_svc_key_id_t key,
2492 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002493{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002495}
2496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002497psa_status_t psa_mac_verify_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, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002502}
2503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2505 const uint8_t *input,
2506 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 if (operation->id == 0) {
2509 return PSA_ERROR_BAD_STATE;
2510 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511
Steven Cooreman11743f92021-03-19 18:38:46 +01002512 /* Don't require hash implementations to behave correctly on a
2513 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 if (input_length == 0) {
2515 return PSA_SUCCESS;
2516 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2519 input, input_length);
2520 if (status != PSA_SUCCESS) {
2521 psa_mac_abort(operation);
2522 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525}
2526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002527psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2528 uint8_t *mac,
2529 size_t mac_size,
2530 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002531{
Steven Cooreman87885df2021-03-19 19:04:39 +01002532 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2533 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002535 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002536 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002537 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002538 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002541 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002542 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002543 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002544
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002545 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2546 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 if (operation->mac_size == 0) {
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 Cooremanf8ad2122021-05-11 11:09:13 +02002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002553 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002554 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002555 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 status = psa_driver_wrapper_mac_sign_finish(operation,
2558 mac, operation->mac_size,
2559 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002560
Dave Rodgman478ab542021-06-25 09:09:02 +01002561exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002562 /* In case of success, set the potential excess room in the output buffer
2563 * to an invalid value, to avoid potentially leaking a longer MAC.
2564 * In case of error, set the output length and content to a safe default,
2565 * such that in case the caller misses an error check, the output would be
2566 * an unachievable MAC.
2567 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002569 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002570 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002571 }
mohammad16036df908f2018-04-02 08:34:15 -07002572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002573 if (mac_size > operation->mac_size) {
2574 memset(&mac[operation->mac_size], '!',
2575 mac_size - operation->mac_size);
2576 }
Ronald Cron882eb782021-06-18 13:05:48 +02002577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002578 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002581}
2582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2584 const uint8_t *mac,
2585 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586{
Steven Cooreman87885df2021-03-19 19:04:39 +01002587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2588 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002591 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002592 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002593 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002595 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002596 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002597 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002598 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002601 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002602 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002603 }
2604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 status = psa_driver_wrapper_mac_verify_finish(operation,
2606 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002607
Dave Rodgman478ab542021-06-25 09:09:02 +01002608exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612}
2613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002614static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2615 psa_algorithm_t alg,
2616 const uint8_t *input,
2617 size_t input_length,
2618 uint8_t *mac,
2619 size_t mac_size,
2620 size_t *mac_length,
2621 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002622{
2623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002624 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002625 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002626 psa_key_slot_t *slot;
2627 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628
Ronald Crondbb86462021-06-17 17:17:20 +02002629 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 key,
2631 &slot,
2632 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2633 alg);
2634 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002635 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002637
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002638 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002639 .core = slot->attr
2640 };
2641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2643 &operation_mac_size);
2644 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002645 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002649 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002650 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002651 }
2652
2653 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654 &attributes,
2655 slot->key.data, slot->key.bytes,
2656 alg,
2657 input, input_length,
2658 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002659
2660exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002661 /* In case of success, set the potential excess room in the output buffer
2662 * to an invalid value, to avoid potentially leaking a longer MAC.
2663 * In case of error, set the output length and content to a safe default,
2664 * such that in case the caller misses an error check, the output would be
2665 * an unachievable MAC.
2666 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002668 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002669 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002670 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002671 if (mac_size > operation_mac_size) {
2672 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2673 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002677 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002678}
2679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002680psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002681 psa_algorithm_t alg,
2682 const uint8_t *input,
2683 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 uint8_t *mac,
2685 size_t mac_size,
2686 size_t *mac_length)
2687{
2688 return psa_mac_compute_internal(key, alg,
2689 input, input_length,
2690 mac, mac_size, mac_length, 1);
2691}
2692
2693psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2694 psa_algorithm_t alg,
2695 const uint8_t *input,
2696 size_t input_length,
2697 const uint8_t *mac,
2698 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002699{
2700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002701 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2702 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 status = psa_mac_compute_internal(key, alg,
2705 input, input_length,
2706 actual_mac, sizeof(actual_mac),
2707 &actual_mac_length, 0);
2708 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002709 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002713 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002714 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002715 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002717 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002718 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002719 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002720
2721exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002725}
Gilles Peskine20035e32018-02-03 22:44:14 +01002726
Gilles Peskine20035e32018-02-03 22:44:14 +01002727/****************************************************************/
2728/* Asymmetric cryptography */
2729/****************************************************************/
2730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002731static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2732 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002733{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002734 if (input_is_message) {
2735 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2736 return PSA_ERROR_INVALID_ARGUMENT;
2737 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002739 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2740 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2741 return PSA_ERROR_INVALID_ARGUMENT;
2742 }
2743 }
2744 } else {
2745 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2746 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002747 }
2748 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002751}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2754 int input_is_message,
2755 psa_algorithm_t alg,
2756 const uint8_t *input,
2757 size_t input_length,
2758 uint8_t *signature,
2759 size_t signature_size,
2760 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002761{
2762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2763 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002764 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002765 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002766
2767 *signature_length = 0;
2768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 status = psa_sign_verify_check_alg(input_is_message, alg);
2770 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002771 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002773
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002774 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002775 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002776 * buffer can in principle be empty since it doesn't actually have
2777 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002778 if (signature_size == 0) {
2779 return PSA_ERROR_BUFFER_TOO_SMALL;
2780 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002781
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002782 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 key, &slot,
2784 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2785 PSA_KEY_USAGE_SIGN_HASH,
2786 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002789 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002793 status = PSA_ERROR_INVALID_ARGUMENT;
2794 goto exit;
2795 }
2796
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002797 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002798 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002799 };
2800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002802 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002803 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002804 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 signature, signature_size, signature_length);
2806 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002807
2808 status = psa_driver_wrapper_sign_hash(
2809 &attributes, slot->key.data, slot->key.bytes,
2810 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002812 }
2813
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002814
2815exit:
2816 /* Fill the unused part of the output buffer (the whole buffer on error,
2817 * the trailing part on success) with something that isn't a valid signature
2818 * (barring an attack on the signature and deliberately-crafted input),
2819 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 if (status == PSA_SUCCESS) {
2821 memset(signature + *signature_length, '!',
2822 signature_size - *signature_length);
2823 } else {
2824 memset(signature, '!', signature_size);
2825 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002826 /* If signature_size is 0 then we have nothing to do. We must not call
2827 * memset because signature may be NULL in this case. */
2828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002829 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002832}
2833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2835 int input_is_message,
2836 psa_algorithm_t alg,
2837 const uint8_t *input,
2838 size_t input_length,
2839 const uint8_t *signature,
2840 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002841{
2842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2843 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2844 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 status = psa_sign_verify_check_alg(input_is_message, alg);
2847 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002848 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002850
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002851 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 key, &slot,
2853 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2854 PSA_KEY_USAGE_VERIFY_HASH,
2855 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002857 if (status != PSA_SUCCESS) {
2858 return status;
2859 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002860
2861 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002863 };
2864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002866 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002867 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002868 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 signature, signature_length);
2870 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002871 status = psa_driver_wrapper_verify_hash(
2872 &attributes, slot->key.data, slot->key.bytes,
2873 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002875 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002876
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002877 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002880
2881}
2882
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002883psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002884 const psa_key_attributes_t *attributes,
2885 const uint8_t *key_buffer,
2886 size_t key_buffer_size,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 uint8_t *signature,
2891 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002893{
2894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002897 size_t hash_length;
2898 uint8_t hash[PSA_HASH_MAX_SIZE];
2899
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002900 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 PSA_ALG_SIGN_GET_HASH(alg),
2902 input, input_length,
2903 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002906 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002908
2909 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 attributes, key_buffer, key_buffer_size,
2911 alg, hash, hash_length,
2912 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002913 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916}
2917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2919 psa_algorithm_t alg,
2920 const uint8_t *input,
2921 size_t input_length,
2922 uint8_t *signature,
2923 size_t signature_size,
2924 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002925{
2926 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002927 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002929}
2930
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002931psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002932 const psa_key_attributes_t *attributes,
2933 const uint8_t *key_buffer,
2934 size_t key_buffer_size,
2935 psa_algorithm_t alg,
2936 const uint8_t *input,
2937 size_t input_length,
2938 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002940{
2941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002943 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002944 size_t hash_length;
2945 uint8_t hash[PSA_HASH_MAX_SIZE];
2946
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002947 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 PSA_ALG_SIGN_GET_HASH(alg),
2949 input, input_length,
2950 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002953 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002954 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002955
2956 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 attributes, key_buffer, key_buffer_size,
2958 alg, hash, hash_length,
2959 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002960 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002963}
2964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2966 psa_algorithm_t alg,
2967 const uint8_t *input,
2968 size_t input_length,
2969 const uint8_t *signature,
2970 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002971{
2972 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002973 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002975}
2976
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002977psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002978 const psa_key_attributes_t *attributes,
2979 const uint8_t *key_buffer, size_t key_buffer_size,
2980 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002982{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2984 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2985 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002986#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2988 return mbedtls_psa_rsa_sign_hash(
2989 attributes,
2990 key_buffer, key_buffer_size,
2991 alg, hash, hash_length,
2992 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002993#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2994 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 } else {
2996 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002997 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002998 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2999 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3002 return mbedtls_psa_ecdsa_sign_hash(
3003 attributes,
3004 key_buffer, key_buffer_size,
3005 alg, hash, hash_length,
3006 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003007#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3008 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 } else {
3010 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003011 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003012 }
Ronald Cron4501c982021-03-19 20:09:32 +01003013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014 (void) key_buffer;
3015 (void) key_buffer_size;
3016 (void) hash;
3017 (void) hash_length;
3018 (void) signature;
3019 (void) signature_size;
3020 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003023}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003024
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3026 psa_algorithm_t alg,
3027 const uint8_t *hash,
3028 size_t hash_length,
3029 uint8_t *signature,
3030 size_t signature_size,
3031 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003032{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003033 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003034 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003036}
3037
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003038psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003039 const psa_key_attributes_t *attributes,
3040 const uint8_t *key_buffer, size_t key_buffer_size,
3041 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003043{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3045 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3046 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003047#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3049 return mbedtls_psa_rsa_verify_hash(
3050 attributes,
3051 key_buffer, key_buffer_size,
3052 alg, hash, hash_length,
3053 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003054#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3055 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 } else {
3057 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003058 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3060 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003061#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3063 return mbedtls_psa_ecdsa_verify_hash(
3064 attributes,
3065 key_buffer, key_buffer_size,
3066 alg, hash, hash_length,
3067 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3069 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003070 } else {
3071 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003072 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003073 }
Ronald Cron4501c982021-03-19 20:09:32 +01003074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 (void) key_buffer;
3076 (void) key_buffer_size;
3077 (void) hash;
3078 (void) hash_length;
3079 (void) signature;
3080 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003083}
3084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3086 psa_algorithm_t alg,
3087 const uint8_t *hash,
3088 size_t hash_length,
3089 const uint8_t *signature,
3090 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003091{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003092 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003093 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003095}
3096
John Durkop0e005192020-10-31 22:06:54 -07003097#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003098static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3099 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003100{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3102 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3103 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3104 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003105}
John Durkop0e005192020-10-31 22:06:54 -07003106#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3109 psa_algorithm_t alg,
3110 const uint8_t *input,
3111 size_t input_length,
3112 const uint8_t *salt,
3113 size_t salt_length,
3114 uint8_t *output,
3115 size_t output_size,
3116 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003119 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003120 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003121
Darryl Green5cc689a2018-07-24 15:34:10 +01003122 (void) input;
3123 (void) input_length;
3124 (void) salt;
3125 (void) output;
3126 (void) output_size;
3127
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003128 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003130 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3131 return PSA_ERROR_INVALID_ARGUMENT;
3132 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003133
Ronald Cron5c522922020-11-14 16:35:34 +01003134 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3136 if (status != PSA_SUCCESS) {
3137 return status;
3138 }
3139 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3140 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003141 status = PSA_ERROR_INVALID_ARGUMENT;
3142 goto exit;
3143 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003146#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003147 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003148 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3150 slot->key.data,
3151 slot->key.bytes,
3152 &rsa);
3153 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003154 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003157 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003158 status = PSA_ERROR_BUFFER_TOO_SMALL;
3159 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003160 }
Ronald Cron7207d572021-09-08 14:28:35 +02003161#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3162 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003163 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003164#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003165 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 mbedtls_rsa_pkcs1_encrypt(rsa,
3167 mbedtls_psa_get_random,
3168 MBEDTLS_PSA_RANDOM_STATE,
3169 MBEDTLS_RSA_PUBLIC,
3170 input_length,
3171 input,
3172 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003173#else
3174 status = PSA_ERROR_NOT_SUPPORTED;
3175#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 } else
3177 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003180 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3182 mbedtls_psa_get_random,
3183 MBEDTLS_PSA_RANDOM_STATE,
3184 MBEDTLS_RSA_PUBLIC,
3185 salt, salt_length,
3186 input_length,
3187 input,
3188 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003189#else
3190 status = PSA_ERROR_NOT_SUPPORTED;
3191#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003193 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194 }
Ronald Cron7207d572021-09-08 14:28:35 +02003195#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003197rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 if (status == PSA_SUCCESS) {
3199 *output_length = mbedtls_rsa_get_len(rsa);
3200 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 mbedtls_rsa_free(rsa);
3203 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003204#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003205 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003206 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003207 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003209
3210exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003214}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003216psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *input,
3219 size_t input_length,
3220 const uint8_t *salt,
3221 size_t salt_length,
3222 uint8_t *output,
3223 size_t output_size,
3224 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003228 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003229
Darryl Green5cc689a2018-07-24 15:34:10 +01003230 (void) input;
3231 (void) input_length;
3232 (void) salt;
3233 (void) output;
3234 (void) output_size;
3235
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003236 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3239 return PSA_ERROR_INVALID_ARGUMENT;
3240 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003241
Ronald Cron5c522922020-11-14 16:35:34 +01003242 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3244 if (status != PSA_SUCCESS) {
3245 return status;
3246 }
3247 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003248 status = PSA_ERROR_INVALID_ARGUMENT;
3249 goto exit;
3250 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003253#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003255 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3257 slot->key.data,
3258 slot->key.bytes,
3259 &rsa);
3260 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003261 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003265 status = PSA_ERROR_INVALID_ARGUMENT;
3266 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003267 }
Ronald Cron7207d572021-09-08 14:28:35 +02003268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3269 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003272#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003273 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003274 mbedtls_rsa_pkcs1_decrypt(rsa,
3275 mbedtls_psa_get_random,
3276 MBEDTLS_PSA_RANDOM_STATE,
3277 MBEDTLS_RSA_PRIVATE,
3278 output_length,
3279 input,
3280 output,
3281 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003282#else
3283 status = PSA_ERROR_NOT_SUPPORTED;
3284#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003285 } else
3286 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003287#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003289 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3291 mbedtls_psa_get_random,
3292 MBEDTLS_PSA_RANDOM_STATE,
3293 MBEDTLS_RSA_PRIVATE,
3294 salt, salt_length,
3295 output_length,
3296 input,
3297 output,
3298 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003299#else
3300 status = PSA_ERROR_NOT_SUPPORTED;
3301#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003302 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003303 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003305
Ronald Cron7207d572021-09-08 14:28:35 +02003306#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003308rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 mbedtls_rsa_free(rsa);
3310 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003311#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3312 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003314 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003316
3317exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003318 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003321}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003322
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003323
3324
mohammad1603503973b2018-03-12 15:59:30 +02003325/****************************************************************/
3326/* Symmetric cryptography */
3327/****************************************************************/
3328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3330 mbedtls_svc_key_id_t key,
3331 psa_algorithm_t alg,
3332 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003333{
3334 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3335 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003336 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003337 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3339 PSA_KEY_USAGE_ENCRYPT :
3340 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003341
3342 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003344 status = PSA_ERROR_BAD_STATE;
3345 goto exit;
3346 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003349 status = PSA_ERROR_INVALID_ARGUMENT;
3350 goto exit;
3351 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3354 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003355 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003357
Ronald Cron49fafa92021-03-10 08:34:23 +01003358 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003359 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003360 * so we only set it (in the driver wrapper) after resources have been
3361 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003362 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003363 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003364 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003366 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003367 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003368 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 }
3370 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003371
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003372 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003374 };
3375
Ronald Cronab99ac22020-10-01 16:38:28 +02003376 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377 if (cipher_operation == MBEDTLS_ENCRYPT) {
3378 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3379 &attributes,
3380 slot->key.data,
3381 slot->key.bytes,
3382 alg);
3383 } else {
3384 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3385 &attributes,
3386 slot->key.data,
3387 slot->key.bytes,
3388 alg);
3389 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003390
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003391exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003392 if (status != PSA_SUCCESS) {
3393 psa_cipher_abort(operation);
3394 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003399}
3400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3402 mbedtls_svc_key_id_t key,
3403 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003404{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003406}
3407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408psa_status_t psa_cipher_decrypt_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_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003413}
3414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003415psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3416 uint8_t *iv,
3417 size_t iv_size,
3418 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003419{
Ronald Cron6d051732020-10-01 14:10:20 +02003420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003421 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003422 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003425 status = PSA_ERROR_BAD_STATE;
3426 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003427 }
3428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003430 status = PSA_ERROR_BAD_STATE;
3431 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003432 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003433
Ronald Cronc423acb2021-07-05 12:31:44 +02003434 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003436 status = PSA_ERROR_BUFFER_TOO_SMALL;
3437 goto exit;
3438 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003441 status = PSA_ERROR_GENERIC_ERROR;
3442 goto exit;
3443 }
3444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 status = psa_generate_random(local_iv, default_iv_length);
3446 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003447 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 }
Ronald Cron5618a392021-03-26 09:52:26 +01003449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 status = psa_driver_wrapper_cipher_set_iv(operation,
3451 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003452
3453exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 if (status == PSA_SUCCESS) {
3455 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003456 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003457 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003459 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003461 }
Ronald Cron6d051732020-10-01 14:10:20 +02003462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003464}
3465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3467 const uint8_t *iv,
3468 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003469{
Ronald Cron6d051732020-10-01 14:10:20 +02003470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003472 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003473 status = PSA_ERROR_BAD_STATE;
3474 goto exit;
3475 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003477 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003478 status = PSA_ERROR_BAD_STATE;
3479 goto exit;
3480 }
Ronald Crona0d68172021-03-26 10:15:08 +01003481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003483 status = PSA_ERROR_INVALID_ARGUMENT;
3484 goto exit;
3485 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 status = psa_driver_wrapper_cipher_set_iv(operation,
3488 iv,
3489 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003490
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003491exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003493 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 } else {
3495 psa_cipher_abort(operation);
3496 }
3497 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003498}
3499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3501 const uint8_t *input,
3502 size_t input_length,
3503 uint8_t *output,
3504 size_t output_size,
3505 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003506{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003507 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003509 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003510 status = PSA_ERROR_BAD_STATE;
3511 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003512 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003515 status = PSA_ERROR_BAD_STATE;
3516 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003517 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 status = psa_driver_wrapper_cipher_update(operation,
3520 input,
3521 input_length,
3522 output,
3523 output_size,
3524 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003525
3526exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 if (status != PSA_SUCCESS) {
3528 psa_cipher_abort(operation);
3529 }
Ronald Cron6d051732020-10-01 14:10:20 +02003530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003532}
3533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3535 uint8_t *output,
3536 size_t output_size,
3537 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003538{
David Saadab4ecc272019-02-14 13:48:10 +02003539 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003542 status = PSA_ERROR_BAD_STATE;
3543 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003544 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003547 status = PSA_ERROR_BAD_STATE;
3548 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003549 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 status = psa_driver_wrapper_cipher_finish(operation,
3552 output,
3553 output_size,
3554 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003555
3556exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 if (status == PSA_SUCCESS) {
3558 return psa_cipher_abort(operation);
3559 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003560 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003561 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003564 }
mohammad1603503973b2018-03-12 15:59:30 +02003565}
3566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003568{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003569 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003570 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003571 * in use. It's ok to call abort on such an object, and there's
3572 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003574 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003577
Ronald Cron49fafa92021-03-10 08:34:23 +01003578 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003579 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003580 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003583}
3584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3586 psa_algorithm_t alg,
3587 const uint8_t *input,
3588 size_t input_length,
3589 uint8_t *output,
3590 size_t output_size,
3591 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003592{
3593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003594 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003595 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003596 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003597 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3598 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003599
David Horstmann34980bd2023-11-29 17:07:13 +00003600 SWAP_FOR_LOCAL_INPUT(input, input_length);
3601 SWAP_FOR_LOCAL_OUTPUT(output, output_size);
David Horstmannd2ad8862023-11-27 17:43:05 +00003602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003604 status = PSA_ERROR_INVALID_ARGUMENT;
3605 goto exit;
3606 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3609 PSA_KEY_USAGE_ENCRYPT,
3610 alg);
3611 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003612 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003614
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003615 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003617 };
3618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3620 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003621 status = PSA_ERROR_GENERIC_ERROR;
3622 goto exit;
3623 }
3624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 if (default_iv_length > 0) {
3626 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003627 status = PSA_ERROR_BUFFER_TOO_SMALL;
3628 goto exit;
3629 }
3630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003631 status = psa_generate_random(local_iv, default_iv_length);
3632 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003633 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003634 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003635 }
3636
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003637 status = psa_driver_wrapper_cipher_encrypt(
3638 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003639 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640 mbedtls_buffer_offset(output, default_iv_length),
3641 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003642
3643exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 unlock_status = psa_unlock_key_slot(slot);
3645 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003646 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003647 }
Ronald Cron16377072021-07-08 19:00:07 +02003648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 if (status == PSA_SUCCESS) {
3650 if (default_iv_length > 0) {
3651 memcpy(output, local_iv, default_iv_length);
3652 }
3653 *output_length += default_iv_length;
3654 } else {
3655 *output_length = 0;
3656 }
3657
David Horstmann34980bd2023-11-29 17:07:13 +00003658 FREE_LOCAL_INPUT(input);
3659 FREE_LOCAL_OUTPUT(output);
David Horstmannd2ad8862023-11-27 17:43:05 +00003660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003662}
3663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3665 psa_algorithm_t alg,
3666 const uint8_t *input,
3667 size_t input_length,
3668 uint8_t *output,
3669 size_t output_size,
3670 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003671{
3672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003673 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003674 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003675 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003677 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003678 status = PSA_ERROR_INVALID_ARGUMENT;
3679 goto exit;
3680 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3683 PSA_KEY_USAGE_DECRYPT,
3684 alg);
3685 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003686 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003688
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003689 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003690 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003691 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003694 status = PSA_ERROR_INVALID_ARGUMENT;
3695 goto exit;
3696 }
3697
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003698 status = psa_driver_wrapper_cipher_decrypt(
3699 &attributes, slot->key.data, slot->key.bytes,
3700 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003702
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003703exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 unlock_status = psa_unlock_key_slot(slot);
3705 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003706 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003710 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 }
Ronald Cron16377072021-07-08 19:00:07 +02003712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003713 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003714}
3715
3716
mohammad16035955c982018-04-26 00:53:03 +03003717/****************************************************************/
3718/* AEAD */
3719/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3722 psa_algorithm_t alg,
3723 const uint8_t *nonce,
3724 size_t nonce_length,
3725 const uint8_t *additional_data,
3726 size_t additional_data_length,
3727 const uint8_t *plaintext,
3728 size_t plaintext_length,
3729 uint8_t *ciphertext,
3730 size_t ciphertext_size,
3731 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003732{
Ronald Cron215633c2021-03-16 17:15:37 +01003733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3734 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003735
mohammad1603f08a5502018-06-03 15:05:47 +03003736 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3739 return PSA_ERROR_NOT_SUPPORTED;
3740 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003741
Ronald Cron9a986162021-03-26 12:40:07 +01003742 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3744 if (status != PSA_SUCCESS) {
3745 return status;
3746 }
mohammad16035955c982018-04-26 00:53:03 +03003747
Ronald Cron215633c2021-03-16 17:15:37 +01003748 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003749 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003750 };
mohammad16035955c982018-04-26 00:53:03 +03003751
Ronald Cronde822812021-03-17 16:08:20 +01003752 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003753 &attributes, slot->key.data, slot->key.bytes,
3754 alg,
3755 nonce, nonce_length,
3756 additional_data, additional_data_length,
3757 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003760 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3761 memset(ciphertext, 0, ciphertext_size);
3762 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003764 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003767}
3768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3770 psa_algorithm_t alg,
3771 const uint8_t *nonce,
3772 size_t nonce_length,
3773 const uint8_t *additional_data,
3774 size_t additional_data_length,
3775 const uint8_t *ciphertext,
3776 size_t ciphertext_length,
3777 uint8_t *plaintext,
3778 size_t plaintext_size,
3779 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003780{
Ronald Cron215633c2021-03-16 17:15:37 +01003781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3782 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003783
Gilles Peskineee652a32018-06-01 19:23:52 +02003784 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3787 return PSA_ERROR_NOT_SUPPORTED;
3788 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003789
Ronald Cron9a986162021-03-26 12:40:07 +01003790 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003791 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3792 if (status != PSA_SUCCESS) {
3793 return status;
3794 }
mohammad16035955c982018-04-26 00:53:03 +03003795
Ronald Cron215633c2021-03-16 17:15:37 +01003796 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003797 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003798 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003799
Ronald Cronde822812021-03-17 16:08:20 +01003800 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003801 &attributes, slot->key.data, slot->key.bytes,
3802 alg,
3803 nonce, nonce_length,
3804 additional_data, additional_data_length,
3805 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003806 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 if (status != PSA_SUCCESS && plaintext_size != 0) {
3809 memset(plaintext, 0, plaintext_size);
3810 }
mohammad160360a64d02018-06-03 17:20:42 +03003811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003814 return status;
mohammad16035955c982018-04-26 00:53:03 +03003815}
3816
Gilles Peskinee59236f2018-01-27 23:32:46 +01003817/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003818/* Generators */
3819/****************************************************************/
3820
John Durkop07cc04a2020-11-16 22:08:34 -08003821#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3822 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3823 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3824#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003825#endif /* At least one builtin KDF */
3826
3827#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3828 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3829 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3830static psa_status_t psa_key_derivation_start_hmac(
3831 psa_mac_operation_t *operation,
3832 psa_algorithm_t hash_alg,
3833 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003835{
3836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3839 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3840 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003841
Steven Cooreman15f0d922021-05-07 14:14:37 +02003842 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 status = psa_driver_wrapper_mac_sign_setup(operation,
3846 &attributes,
3847 hmac_key, hmac_key_length,
3848 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 psa_reset_key_attributes(&attributes);
3851 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003852}
3853#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003854
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003855#define HKDF_STATE_INIT 0 /* no input yet */
3856#define HKDF_STATE_STARTED 1 /* got salt */
3857#define HKDF_STATE_KEYED 2 /* got key */
3858#define HKDF_STATE_OUTPUT 3 /* output started */
3859
Gilles Peskinecbe66502019-05-16 16:59:18 +02003860static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003862{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3864 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3865 } else {
3866 return operation->alg;
3867 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003868}
3869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003871{
3872 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3874 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003875 /* The object has (apparently) been initialized but it is not
3876 * in use. It's ok to call abort on such an object, and there's
3877 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 } else
John Durkopd0321952020-10-29 21:37:36 -07003879#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3881 mbedtls_free(operation->ctx.hkdf.info);
3882 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3883 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003884#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3885#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3886 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3888 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3889 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3890 if (operation->ctx.tls12_prf.secret != NULL) {
3891 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3892 operation->ctx.tls12_prf.secret_length);
3893 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003894 }
3895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 if (operation->ctx.tls12_prf.seed != NULL) {
3897 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3898 operation->ctx.tls12_prf.seed_length);
3899 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003900 }
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 if (operation->ctx.tls12_prf.label != NULL) {
3903 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3904 operation->ctx.tls12_prf.label_length);
3905 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003906 }
3907
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003908 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003909
3910 /* We leave the fields Ai and output_block to be erased safely by the
3911 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003913#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3914 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003915 {
3916 status = PSA_ERROR_BAD_STATE;
3917 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918 mbedtls_platform_zeroize(operation, sizeof(*operation));
3919 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003920}
3921
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003922psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003924{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003926 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003928 }
3929
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003930 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003932}
3933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3935 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003936{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 if (operation->alg == 0) {
3938 return PSA_ERROR_BAD_STATE;
3939 }
3940 if (capacity > operation->capacity) {
3941 return PSA_ERROR_INVALID_ARGUMENT;
3942 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003943 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003945}
3946
John Durkopd0321952020-10-29 21:37:36 -07003947#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003948/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003949 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3951 psa_algorithm_t hash_alg,
3952 uint8_t *output,
3953 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003954{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003955 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003956 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003957 psa_status_t status;
3958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3960 return PSA_ERROR_BAD_STATE;
3961 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003962 hkdf->state = HKDF_STATE_OUTPUT;
3963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003964 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003965 /* Copy what remains of the current block */
3966 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003967 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003968 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003969 }
3970 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003971 output += n;
3972 output_length -= n;
3973 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003975 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003977 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003978 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003979 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003980 * object was corrupted or if this function is called directly
3981 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003982 if (hkdf->block_number == 0xff) {
3983 return PSA_ERROR_BAD_STATE;
3984 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003985
3986 /* We need a new block */
3987 ++hkdf->block_number;
3988 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003990 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3991 hash_alg,
3992 hkdf->prk,
3993 hash_length);
3994 if (status != PSA_SUCCESS) {
3995 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003996 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997
3998 if (hkdf->block_number != 1) {
3999 status = psa_mac_update(&hkdf->hmac,
4000 hkdf->output_block,
4001 hash_length);
4002 if (status != PSA_SUCCESS) {
4003 return status;
4004 }
4005 }
4006 status = psa_mac_update(&hkdf->hmac,
4007 hkdf->info,
4008 hkdf->info_length);
4009 if (status != PSA_SUCCESS) {
4010 return status;
4011 }
4012 status = psa_mac_update(&hkdf->hmac,
4013 &hkdf->block_number, 1);
4014 if (status != PSA_SUCCESS) {
4015 return status;
4016 }
4017 status = psa_mac_sign_finish(&hkdf->hmac,
4018 hkdf->output_block,
4019 sizeof(hkdf->output_block),
4020 &hmac_output_length);
4021 if (status != PSA_SUCCESS) {
4022 return status;
4023 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004024 }
4025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004027}
John Durkop07cc04a2020-11-16 22:08:34 -08004028#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004029
John Durkop07cc04a2020-11-16 22:08:34 -08004030#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4031 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004032static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4033 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004035{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004036 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4037 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004038 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4039 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004040 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004041
Janos Follath7742fee2019-06-17 12:58:10 +01004042 /* We can't be wanting more output after block 0xff, otherwise
4043 * the capacity check in psa_key_derivation_output_bytes() would have
4044 * prevented this call. It could happen only if the operation
4045 * object was corrupted or if this function is called directly
4046 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004047 if (tls12_prf->block_number == 0xff) {
4048 return PSA_ERROR_CORRUPTION_DETECTED;
4049 }
Janos Follath7742fee2019-06-17 12:58:10 +01004050
4051 /* We need a new block */
4052 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004053 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004054
4055 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4056 *
4057 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4058 *
4059 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4060 * HMAC_hash(secret, A(2) + seed) +
4061 * HMAC_hash(secret, A(3) + seed) + ...
4062 *
4063 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004064 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004065 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004066 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004067 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004068 * is currently extracted as `output_block` and where i is
4069 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004070 */
4071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 status = psa_key_derivation_start_hmac(&hmac,
4073 hash_alg,
4074 tls12_prf->secret,
4075 tls12_prf->secret_length);
4076 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004077 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004078 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004079
4080 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004082 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4083 * the variable seed and in this instance means it in the context of the
4084 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004085 status = psa_mac_update(&hmac,
4086 tls12_prf->label,
4087 tls12_prf->label_length);
4088 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004089 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 }
4091 status = psa_mac_update(&hmac,
4092 tls12_prf->seed,
4093 tls12_prf->seed_length);
4094 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004095 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004096 }
4097 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004098 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4100 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004101 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004103 }
4104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105 status = psa_mac_sign_finish(&hmac,
4106 tls12_prf->Ai, hash_length,
4107 &hmac_output_length);
4108 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004109 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 }
4111 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004112 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004114
4115 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004116 status = psa_key_derivation_start_hmac(&hmac,
4117 hash_alg,
4118 tls12_prf->secret,
4119 tls12_prf->secret_length);
4120 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004121 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004122 }
4123 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4124 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004125 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004126 }
4127 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4128 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004129 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 }
4131 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4132 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004133 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 }
4135 status = psa_mac_sign_finish(&hmac,
4136 tls12_prf->output_block, hash_length,
4137 &hmac_output_length);
4138 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004139 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004141
Janos Follath7742fee2019-06-17 12:58:10 +01004142
4143cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 cleanup_status = psa_mac_abort(&hmac);
4145 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004146 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004150}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004151
Janos Follath844eb0e2019-06-19 12:10:49 +01004152static psa_status_t psa_key_derivation_tls12_prf_read(
4153 psa_tls12_prf_key_derivation_t *tls12_prf,
4154 psa_algorithm_t alg,
4155 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004157{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4159 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004160 psa_status_t status;
4161 uint8_t offset, length;
4162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004164 case PSA_TLS12_PRF_STATE_LABEL_SET:
4165 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4166 break;
4167 case PSA_TLS12_PRF_STATE_OUTPUT:
4168 break;
4169 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004171 }
4172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004174 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 if (tls12_prf->left_in_block == 0) {
4176 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4177 alg);
4178 if (status != PSA_SUCCESS) {
4179 return status;
4180 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004181
4182 continue;
4183 }
4184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004185 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004186 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004187 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004188 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004190
4191 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004193 output += length;
4194 output_length -= length;
4195 tls12_prf->left_in_block -= length;
4196 }
4197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004198 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004199}
John Durkop07cc04a2020-11-16 22:08:34 -08004200#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4201 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004202
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004203psa_status_t psa_key_derivation_output_bytes(
4204 psa_key_derivation_operation_t *operation,
4205 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004206 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004207{
4208 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004214 }
4215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004216 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004217 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004218 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004220 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004221 goto exit;
4222 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004223 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004225 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004226 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4227 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004228 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004229 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004231 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004233
John Durkopd0321952020-10-29 21:37:36 -07004234#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4236 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4237 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4238 output, output_length);
4239 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004240#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4241#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4242 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004243 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4244 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4245 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4246 kdf_alg, output,
4247 output_length);
4248 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004249#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4250 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004251 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004252 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004254 }
4255
4256exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004257 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004258 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004259 * This allows us to differentiate between exhausted operations and
4260 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4261 * operations. */
4262 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004266 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004268}
4269
David Brown7807bf72021-02-09 16:28:23 -07004270#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004272{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 if (data_size >= 8) {
4274 mbedtls_des_key_set_parity(data);
4275 }
4276 if (data_size >= 16) {
4277 mbedtls_des_key_set_parity(data + 8);
4278 }
4279 if (data_size >= 24) {
4280 mbedtls_des_key_set_parity(data + 16);
4281 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004282}
David Brown7807bf72021-02-09 16:28:23 -07004283#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004284
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004285static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004286 psa_key_slot_t *slot,
4287 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004288 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004289{
4290 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004292 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004293 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004295 if (!key_type_is_raw_bytes(slot->attr.type)) {
4296 return PSA_ERROR_INVALID_ARGUMENT;
4297 }
4298 if (bits % 8 != 0) {
4299 return PSA_ERROR_INVALID_ARGUMENT;
4300 }
4301 data = mbedtls_calloc(1, bytes);
4302 if (data == NULL) {
4303 return PSA_ERROR_INSUFFICIENT_MEMORY;
4304 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 status = psa_key_derivation_output_bytes(operation, data, bytes);
4307 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004308 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 }
David Brown12ca5032021-02-11 11:02:00 -07004310#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004311 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4312 psa_des_set_key_parity(data, bytes);
4313 }
David Brown8107e312021-02-12 08:08:46 -07004314#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 status = psa_allocate_buffer_to_slot(slot, bytes);
4317 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004318 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319 }
Ronald Crondd04d422020-11-28 15:14:42 +01004320
Ronald Cronbf33c932020-11-28 18:06:53 +01004321 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004322 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004323 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004324 };
4325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004326 status = psa_driver_wrapper_import_key(&attributes,
4327 data, bytes,
4328 slot->key.data,
4329 slot->key.bytes,
4330 &slot->key.bytes, &bits);
4331 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004332 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004334
4335exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 mbedtls_free(data);
4337 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004338}
4339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4341 psa_key_derivation_operation_t *operation,
4342 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004343{
4344 psa_status_t status;
4345 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004346 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004347
Ronald Cron81709fc2020-11-14 12:10:32 +01004348 *key = MBEDTLS_SVC_KEY_ID_INIT;
4349
Gilles Peskine0f84d622019-09-12 19:03:13 +02004350 /* Reject any attempt to create a zero-length key so that we don't
4351 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 if (psa_get_key_bits(attributes) == 0) {
4353 return PSA_ERROR_INVALID_ARGUMENT;
4354 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004356 if (operation->alg == PSA_ALG_NONE) {
4357 return PSA_ERROR_BAD_STATE;
4358 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004360 if (!operation->can_output_key) {
4361 return PSA_ERROR_NOT_PERMITTED;
4362 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004364 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4365 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004368 /* Deriving a key in a secure element is not implemented yet. */
4369 status = PSA_ERROR_NOT_SUPPORTED;
4370 }
4371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004372 if (status == PSA_SUCCESS) {
4373 status = psa_generate_derived_key_internal(slot,
4374 attributes->core.bits,
4375 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004376 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004377 if (status == PSA_SUCCESS) {
4378 status = psa_finish_key_creation(slot, driver, key);
4379 }
4380 if (status != PSA_SUCCESS) {
4381 psa_fail_key_creation(slot, driver);
4382 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004385}
4386
Gilles Peskineeab56e42018-07-12 17:12:33 +02004387
4388
4389/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004390/* Key derivation */
4391/****************************************************************/
4392
Steven Cooreman932ffb72021-02-15 12:14:32 +01004393#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004394static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004395{
4396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004397 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4398 return 1;
4399 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004400#endif
4401#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4403 return 1;
4404 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004405#endif
4406#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004407 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4408 return 1;
4409 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004410#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004412}
4413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004415{
4416 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417 psa_status_t status = psa_hash_setup(&operation, alg);
4418 psa_hash_abort(&operation);
4419 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004420}
4421
Gilles Peskine969c5d62019-01-16 15:53:06 +01004422static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004425{
Janos Follath5fe19732019-06-20 15:09:30 +01004426 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4427 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004429
Gilles Peskine969c5d62019-01-16 15:53:06 +01004430 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004431 if (!is_kdf_alg_supported(kdf_alg)) {
4432 return PSA_ERROR_NOT_SUPPORTED;
4433 }
John Durkop07cc04a2020-11-16 22:08:34 -08004434
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004435 /* All currently supported key derivation algorithms are based on a
4436 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4438 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4439 if (hash_size == 0) {
4440 return PSA_ERROR_NOT_SUPPORTED;
4441 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004442
4443 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4444 * we might fail later, which is somewhat unfriendly and potentially
4445 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 psa_status_t status = psa_hash_try_support(hash_alg);
4447 if (status != PSA_SUCCESS) {
4448 return status;
4449 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4452 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4453 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4454 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004455 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004456
Gilles Peskinecdacf042021-04-29 21:10:00 +02004457 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004458 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004459}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004461static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004462{
4463#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004464 if (alg == PSA_ALG_ECDH) {
4465 return PSA_SUCCESS;
4466 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004467#endif
4468 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004470}
John Durkop07cc04a2020-11-16 22:08:34 -08004471#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4474 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004475{
4476 psa_status_t status;
4477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004478 if (operation->alg != 0) {
4479 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004480 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4483 return PSA_ERROR_INVALID_ARGUMENT;
4484 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4485#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4486 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4487 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4488 status = psa_key_agreement_try_support(ka_alg);
4489 if (status != PSA_SUCCESS) {
4490 return status;
4491 }
4492 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4493#else
4494 return PSA_ERROR_NOT_SUPPORTED;
4495#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4496 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4497#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4498 status = psa_key_derivation_setup_kdf(operation, alg);
4499#else
4500 return PSA_ERROR_NOT_SUPPORTED;
4501#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4502 } else {
4503 return PSA_ERROR_INVALID_ARGUMENT;
4504 }
4505
4506 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004508 }
4509 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004510}
4511
John Durkopd0321952020-10-29 21:37:36 -07004512#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004513static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4514 psa_algorithm_t hash_alg,
4515 psa_key_derivation_step_t step,
4516 const uint8_t *data,
4517 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004518{
4519 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004521 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004522 if (hkdf->state != HKDF_STATE_INIT) {
4523 return PSA_ERROR_BAD_STATE;
4524 } else {
4525 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4526 hash_alg,
4527 data, data_length);
4528 if (status != PSA_SUCCESS) {
4529 return status;
4530 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004531 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004533 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004534 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004535 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 if (hkdf->state == HKDF_STATE_INIT) {
4537 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4538 hash_alg,
4539 NULL, 0);
4540 if (status != PSA_SUCCESS) {
4541 return status;
4542 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004543 hkdf->state = HKDF_STATE_STARTED;
4544 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004545 if (hkdf->state != HKDF_STATE_STARTED) {
4546 return PSA_ERROR_BAD_STATE;
4547 }
4548 status = psa_mac_update(&hkdf->hmac,
4549 data, data_length);
4550 if (status != PSA_SUCCESS) {
4551 return status;
4552 }
4553 status = psa_mac_sign_finish(&hkdf->hmac,
4554 hkdf->prk,
4555 sizeof(hkdf->prk),
4556 &data_length);
4557 if (status != PSA_SUCCESS) {
4558 return status;
4559 }
4560 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004561 hkdf->block_number = 0;
4562 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004564 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565 if (hkdf->state == HKDF_STATE_OUTPUT) {
4566 return PSA_ERROR_BAD_STATE;
4567 }
4568 if (hkdf->info_set) {
4569 return PSA_ERROR_BAD_STATE;
4570 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004571 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 if (data_length != 0) {
4573 hkdf->info = mbedtls_calloc(1, data_length);
4574 if (hkdf->info == NULL) {
4575 return PSA_ERROR_INSUFFICIENT_MEMORY;
4576 }
4577 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004578 }
4579 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004581 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004582 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004583 }
4584}
John Durkop07cc04a2020-11-16 22:08:34 -08004585#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004586
John Durkop07cc04a2020-11-16 22:08:34 -08004587#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4588 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4590 const uint8_t *data,
4591 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004592{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4594 return PSA_ERROR_BAD_STATE;
4595 }
Janos Follathf08e2652019-06-13 09:05:41 +01004596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004597 if (data_length != 0) {
4598 prf->seed = mbedtls_calloc(1, data_length);
4599 if (prf->seed == NULL) {
4600 return PSA_ERROR_INSUFFICIENT_MEMORY;
4601 }
Janos Follathf08e2652019-06-13 09:05:41 +01004602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004604 prf->seed_length = data_length;
4605 }
Janos Follathf08e2652019-06-13 09:05:41 +01004606
Gilles Peskine43f958b2020-12-13 14:55:14 +01004607 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004610}
4611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4613 const uint8_t *data,
4614 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004615{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004616 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4617 return PSA_ERROR_BAD_STATE;
4618 }
Janos Follath81550542019-06-13 14:26:34 +01004619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 if (data_length != 0) {
4621 prf->secret = mbedtls_calloc(1, data_length);
4622 if (prf->secret == NULL) {
4623 return PSA_ERROR_INSUFFICIENT_MEMORY;
4624 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004627 prf->secret_length = data_length;
4628 }
Janos Follath81550542019-06-13 14:26:34 +01004629
Gilles Peskine43f958b2020-12-13 14:55:14 +01004630 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004632 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004633}
4634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4636 const uint8_t *data,
4637 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004638{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4640 return PSA_ERROR_BAD_STATE;
4641 }
Janos Follath63028dd2019-06-13 09:15:47 +01004642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 if (data_length != 0) {
4644 prf->label = mbedtls_calloc(1, data_length);
4645 if (prf->label == NULL) {
4646 return PSA_ERROR_INSUFFICIENT_MEMORY;
4647 }
Janos Follath63028dd2019-06-13 09:15:47 +01004648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004649 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004650 prf->label_length = data_length;
4651 }
Janos Follath63028dd2019-06-13 09:15:47 +01004652
Gilles Peskine43f958b2020-12-13 14:55:14 +01004653 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004655 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004656}
4657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4659 psa_key_derivation_step_t step,
4660 const uint8_t *data,
4661 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004662{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004664 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004666 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004668 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004670 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004672 }
4673}
John Durkop07cc04a2020-11-16 22:08:34 -08004674#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4675 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4676
4677#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4678static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4679 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004680 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004682{
4683 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004685 uint8_t *cur = pms;
4686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4688 return PSA_ERROR_INVALID_ARGUMENT;
4689 }
John Durkop07cc04a2020-11-16 22:08:34 -08004690
4691 /* Quoting RFC 4279, Section 2:
4692 *
4693 * The premaster secret is formed as follows: if the PSK is N octets
4694 * long, concatenate a uint16 with the value N, N zero octets, a second
4695 * uint16 with the value N, and the PSK itself.
4696 */
4697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 *cur++ = MBEDTLS_BYTE_1(data_length);
4699 *cur++ = MBEDTLS_BYTE_0(data_length);
4700 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004701 cur += data_length;
4702 *cur++ = pms[0];
4703 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004705 cur += data_length;
4706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004707 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 mbedtls_platform_zeroize(pms, sizeof(pms));
4710 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004711}
Janos Follath6660f0e2019-06-17 08:44:03 +01004712
4713static psa_status_t psa_tls12_prf_psk_to_ms_input(
4714 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004715 psa_key_derivation_step_t step,
4716 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004717 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004718{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004719 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4720 return psa_tls12_prf_psk_to_ms_set_key(prf,
4721 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004722 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004724 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004725}
John Durkop07cc04a2020-11-16 22:08:34 -08004726#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004727
Gilles Peskineb8965192019-09-24 16:21:10 +02004728/** Check whether the given key type is acceptable for the given
4729 * input step of a key derivation.
4730 *
4731 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4732 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4733 * Both secret and non-secret inputs can alternatively have the type
4734 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4735 * that the input was passed as a buffer rather than via a key object.
4736 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004737static int psa_key_derivation_check_input_type(
4738 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004740{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004742 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 if (key_type == PSA_KEY_TYPE_DERIVE) {
4744 return PSA_SUCCESS;
4745 }
4746 if (key_type == PSA_KEY_TYPE_NONE) {
4747 return PSA_SUCCESS;
4748 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004749 break;
4750 case PSA_KEY_DERIVATION_INPUT_LABEL:
4751 case PSA_KEY_DERIVATION_INPUT_SALT:
4752 case PSA_KEY_DERIVATION_INPUT_INFO:
4753 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4755 return PSA_SUCCESS;
4756 }
4757 if (key_type == PSA_KEY_TYPE_NONE) {
4758 return PSA_SUCCESS;
4759 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004760 break;
4761 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004763}
4764
Janos Follathb80a94e2019-06-12 15:54:46 +01004765static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004767 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004768 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004769 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004770 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004772 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004775 status = psa_key_derivation_check_input_type(step, key_type);
4776 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004777 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004779
John Durkopd0321952020-10-29 21:37:36 -07004780#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4782 status = psa_hkdf_input(&operation->ctx.hkdf,
4783 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4784 step, data, data_length);
4785 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004786#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4787#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4789 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4790 step, data, data_length);
4791 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004792#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4793#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4795 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4796 step, data, data_length);
4797 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004798#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004799 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004801 (void) data;
4802 (void) data_length;
4803 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004805 }
4806
Gilles Peskine224b0d62019-09-23 18:13:17 +02004807exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004808 if (status != PSA_SUCCESS) {
4809 psa_key_derivation_abort(operation);
4810 }
4811 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812}
4813
Janos Follath51f4a0f2019-06-14 11:35:55 +01004814psa_status_t psa_key_derivation_input_bytes(
4815 psa_key_derivation_operation_t *operation,
4816 psa_key_derivation_step_t step,
4817 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004819{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 return psa_key_derivation_input_internal(operation, step,
4821 PSA_KEY_TYPE_NONE,
4822 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004823}
4824
Janos Follath51f4a0f2019-06-14 11:35:55 +01004825psa_status_t psa_key_derivation_input_key(
4826 psa_key_derivation_operation_t *operation,
4827 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004829{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004831 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004832 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004833
Ronald Cron5c522922020-11-14 16:35:34 +01004834 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4836 if (status != PSA_SUCCESS) {
4837 psa_key_derivation_abort(operation);
4838 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004839 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004840
4841 /* Passing a key object as a SECRET input unlocks the permission
4842 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004844 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004845 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 status = psa_key_derivation_input_internal(operation,
4848 step, slot->attr.type,
4849 slot->key.data,
4850 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004855}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004856
4857
4858
4859/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004860/* Key agreement */
4861/****************************************************************/
4862
John Durkop6ba40d12020-11-10 08:50:04 -08004863#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4865 size_t peer_key_length,
4866 const mbedtls_ecp_keypair *our_key,
4867 uint8_t *shared_secret,
4868 size_t shared_secret_size,
4869 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004870{
Steven Cooremand4867872020-08-05 16:31:39 +02004871 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004872 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004873 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004874 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4876 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004877
Ronald Cron90857082020-11-25 14:58:33 +01004878 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004879 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4880 bits,
4881 peer_key,
4882 peer_key_length,
4883 &their_key);
4884 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004885 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004887
Jaeden Amero97271b32019-01-10 19:38:51 +00004888 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004889 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4890 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004891 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004892 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004893 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4895 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004896 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004898
Jaeden Amero97271b32019-01-10 19:38:51 +00004899 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900 mbedtls_ecdh_calc_secret(&ecdh,
4901 shared_secret_length,
4902 shared_secret, shared_secret_size,
4903 mbedtls_psa_get_random,
4904 MBEDTLS_PSA_RANDOM_STATE));
4905 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004906 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004907 }
4908 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004909 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004910 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004911
4912exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004913 if (status != PSA_SUCCESS) {
4914 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4915 }
4916 mbedtls_ecdh_free(&ecdh);
4917 mbedtls_ecp_keypair_free(their_key);
4918 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004920 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004921}
John Durkop6ba40d12020-11-10 08:50:04 -08004922#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004923
Gilles Peskine01d718c2018-09-18 12:01:02 +02004924#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004926static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4927 psa_key_slot_t *private_key,
4928 const uint8_t *peer_key,
4929 size_t peer_key_length,
4930 uint8_t *shared_secret,
4931 size_t shared_secret_size,
4932 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004933{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004934 mbedtls_ecp_keypair *ecp = NULL;
4935 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004936
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004938#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004939 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004940 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4941 return PSA_ERROR_INVALID_ARGUMENT;
4942 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004943 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944 private_key->attr.type,
4945 private_key->attr.bits,
4946 private_key->key.data,
4947 private_key->key.bytes,
4948 &ecp);
4949 if (status != PSA_SUCCESS) {
4950 return status;
4951 }
4952 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4953 ecp,
4954 shared_secret, shared_secret_size,
4955 shared_secret_length);
4956 mbedtls_ecp_keypair_free(ecp);
4957 mbedtls_free(ecp);
4958 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004959#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004960 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004961 (void) ecp;
4962 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004963 (void) private_key;
4964 (void) peer_key;
4965 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004966 (void) shared_secret;
4967 (void) shared_secret_size;
4968 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004969 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004970 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004971}
4972
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004973/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004974 * to potentially free embedded data structures and wipe confidential data.
4975 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4977 psa_key_derivation_step_t step,
4978 psa_key_slot_t *private_key,
4979 const uint8_t *peer_key,
4980 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004981{
4982 psa_status_t status;
4983 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4984 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004985 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004986
4987 /* Step 1: run the secret agreement algorithm to generate the shared
4988 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004989 status = psa_key_agreement_raw_internal(ka_alg,
4990 private_key,
4991 peer_key, peer_key_length,
4992 shared_secret,
4993 sizeof(shared_secret),
4994 &shared_secret_length);
4995 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02004996 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02004998
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004999 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005000 * the shared secret. A shared secret is permitted wherever a key
5001 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002 status = psa_key_derivation_input_internal(operation, step,
5003 PSA_KEY_TYPE_DERIVE,
5004 shared_secret,
5005 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02005006exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
5008 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005009}
5010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
5012 psa_key_derivation_step_t step,
5013 mbedtls_svc_key_id_t private_key,
5014 const uint8_t *peer_key,
5015 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02005016{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005018 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005019 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005021 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5022 return PSA_ERROR_INVALID_ARGUMENT;
5023 }
Ronald Cron5c522922020-11-14 16:35:34 +01005024 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005025 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
5026 if (status != PSA_SUCCESS) {
5027 return status;
5028 }
5029 status = psa_key_agreement_internal(operation, step,
5030 slot,
5031 peer_key, peer_key_length);
5032 if (status != PSA_SUCCESS) {
5033 psa_key_derivation_abort(operation);
5034 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005035 /* If a private key has been added as SECRET, we allow the derived
5036 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005037 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005038 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005040 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005042 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005045}
5046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5048 mbedtls_svc_key_id_t private_key,
5049 const uint8_t *peer_key,
5050 size_t peer_key_length,
5051 uint8_t *output,
5052 size_t output_size,
5053 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005054{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005056 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005057 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005058 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005061 status = PSA_ERROR_INVALID_ARGUMENT;
5062 goto exit;
5063 }
Ronald Cron5c522922020-11-14 16:35:34 +01005064 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005065 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5066 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005067 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005068 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005069
Gilles Peskine42313fb2022-04-14 00:17:15 +02005070 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5071 * for the output size. The PSA specification only guarantees that this
5072 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5073 * but it might be nice to allow smaller buffers if the output fits.
5074 * At the time of writing this comment, with only ECDH implemented,
5075 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5076 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5077 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005078 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5080 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005081 status = PSA_ERROR_BUFFER_TOO_SMALL;
5082 goto exit;
5083 }
5084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 status = psa_key_agreement_raw_internal(alg, slot,
5086 peer_key, peer_key_length,
5087 output, output_size,
5088 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005089
5090exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005092 /* If an error happens and is not handled properly, the output
5093 * may be used as a key to protect sensitive data. Arrange for such
5094 * a key to be random, which is likely to result in decryption or
5095 * verification errors. This is better than filling the buffer with
5096 * some constant data such as zeros, which would result in the data
5097 * being protected with a reproducible, easily knowable key.
5098 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005100 *output_length = output_size;
5101 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005106}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005107
5108
Gilles Peskine30524eb2020-11-13 17:02:26 +01005109
Gilles Peskine86a440b2018-11-12 18:39:40 +01005110/****************************************************************/
5111/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005112/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005113
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005114#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5115#include "mbedtls/entropy_poll.h"
5116#endif
5117
Gilles Peskine30524eb2020-11-13 17:02:26 +01005118/** Initialize the PSA random generator.
5119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005121{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005122#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005124#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5125
Gilles Peskine30524eb2020-11-13 17:02:26 +01005126 /* Set default configuration if
5127 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005128 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005129 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 }
5131 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005132 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005136#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5137 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5138 /* The PSA entropy injection feature depends on using NV seed as an entropy
5139 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 mbedtls_entropy_add_source(&rng->entropy,
5141 mbedtls_nv_seed_poll, NULL,
5142 MBEDTLS_ENTROPY_BLOCK_SIZE,
5143 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005144#endif
5145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005147#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005148}
5149
5150/** Deinitialize the PSA random generator.
5151 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005153{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005154#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005156#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5158 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005159#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005160}
5161
5162/** Seed the PSA random generator.
5163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005165{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005166#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5167 /* Do nothing: the external RNG seeds itself. */
5168 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005170#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005171 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5173 drbg_seed, sizeof(drbg_seed) - 1);
5174 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005175#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005176}
5177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178psa_status_t psa_generate_random(uint8_t *output,
5179 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005180{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005181 GUARD_MODULE_INITIALIZED;
5182
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005183#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5184
5185 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5187 output, output_size,
5188 &output_length);
5189 if (status != PSA_SUCCESS) {
5190 return status;
5191 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005192 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005193 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 if (output_length != output_size) {
5195 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5196 }
5197 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005198
5199#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005202 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5204 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5205 output_size);
5206 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5207 output, request_size);
5208 if (ret != 0) {
5209 return mbedtls_to_psa_error(ret);
5210 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005211 output_size -= request_size;
5212 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005213 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005215#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005216}
5217
Gilles Peskine8814fc42020-12-14 15:33:44 +01005218/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005219 *
5220 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5221 * `psa_generate_random(...)`. The state parameter is ignored since the
5222 * PSA API doesn't support passing an explicit state.
5223 *
5224 * In the non-external case, psa_generate_random() calls an
5225 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5226 * and semantics as mbedtls_psa_get_random(). As an optimization,
5227 * instead of doing this back-and-forth between the PSA API and the
5228 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5229 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005230 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5232int mbedtls_psa_get_random(void *p_rng,
5233 unsigned char *output,
5234 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005235{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005236 /* This function takes a pointer to the RNG state because that's what
5237 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5238 * its own state internally and doesn't let the caller access that state.
5239 * So we just ignore the state parameter, and in practice we'll pass
5240 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005241 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 psa_status_t status = psa_generate_random(output, output_size);
5243 if (status == PSA_SUCCESS) {
5244 return 0;
5245 } else {
5246 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5247 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005248}
5249#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5250
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005251#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5253 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005254{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 if (global_data.initialized) {
5256 return PSA_ERROR_NOT_PERMITTED;
5257 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5260 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5261 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5262 return PSA_ERROR_INVALID_ARGUMENT;
5263 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005266}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005267#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005268
Ronald Cron3772afe2021-02-08 16:10:05 +01005269/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005270 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005271 * \param type The key type
5272 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005273 *
5274 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005275 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005276 * \retval #PSA_ERROR_INVALID_ARGUMENT
5277 * The size in bits of the key is not valid.
5278 * \retval #PSA_ERROR_NOT_SUPPORTED
5279 * The type and/or the size in bits of the key or the combination of
5280 * the two is not supported.
5281 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005282static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005283 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005284{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if (key_type_is_raw_bytes(type)) {
5288 status = validate_unstructured_key_bit_size(type, bits);
5289 if (status != PSA_SUCCESS) {
5290 return status;
5291 }
5292 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005293#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005294 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5295 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5296 return PSA_ERROR_NOT_SUPPORTED;
5297 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005298
Ronald Cron01b2aba2020-10-05 09:42:02 +02005299 /* Accept only byte-aligned keys, for the same reasons as
5300 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005301 if (bits % 8 != 0) {
5302 return PSA_ERROR_NOT_SUPPORTED;
5303 }
5304 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005305#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005306
Ronald Cron5c4d3862020-12-07 11:07:24 +01005307#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005308 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005309 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005310 return PSA_SUCCESS;
5311 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005312#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005313 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005315 }
5316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005318}
5319
Ronald Cron55ed0592020-10-05 10:30:40 +02005320psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005321 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005322 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005323{
5324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005325 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327 if ((attributes->domain_parameters == NULL) &&
5328 (attributes->domain_parameters_size != 0)) {
5329 return PSA_ERROR_INVALID_ARGUMENT;
5330 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005332 if (key_type_is_raw_bytes(type)) {
5333 status = psa_generate_random(key_buffer, key_buffer_size);
5334 if (status != PSA_SUCCESS) {
5335 return status;
5336 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005337
David Brown12ca5032021-02-11 11:02:00 -07005338#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339 if (type == PSA_KEY_TYPE_DES) {
5340 psa_des_set_key_parity(key_buffer, key_buffer_size);
5341 }
David Brown8107e312021-02-12 08:08:46 -07005342#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005344
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005345#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5346 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005347 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5348 return mbedtls_psa_rsa_generate_key(attributes,
5349 key_buffer,
5350 key_buffer_size,
5351 key_buffer_length);
5352 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005353#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5354 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005355
John Durkop9814fa22020-11-04 12:28:15 -08005356#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5358 return mbedtls_psa_ecp_generate_key(attributes,
5359 key_buffer,
5360 key_buffer_size,
5361 key_buffer_length);
5362 } else
John Durkop9814fa22020-11-04 12:28:15 -08005363#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005364 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 (void) key_buffer_length;
5366 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005367 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005369 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005370}
Darryl Green0c6575a2018-11-07 16:05:30 +00005371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5373 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005374{
5375 psa_status_t status;
5376 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005377 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005378 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005379
Ronald Cron81709fc2020-11-14 12:10:32 +01005380 *key = MBEDTLS_SVC_KEY_ID_INIT;
5381
Gilles Peskine0f84d622019-09-12 19:03:13 +02005382 /* Reject any attempt to create a zero-length key so that we don't
5383 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005384 if (psa_get_key_bits(attributes) == 0) {
5385 return PSA_ERROR_INVALID_ARGUMENT;
5386 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005387
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005388 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5390 return PSA_ERROR_INVALID_ARGUMENT;
5391 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5394 &slot, &driver);
5395 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005396 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 }
Gilles Peskine11792082019-08-06 18:36:36 +02005398
Ronald Cron977c2472020-10-13 08:32:21 +02005399 /* In the case of a transparent key or an opaque key stored in local
5400 * storage (thus not in the case of generating a key in a secure element
5401 * or cryptoprocessor with storage), we have to allocate a buffer to
5402 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005403 if (slot->key.data == NULL) {
5404 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5405 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005406 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005407 attributes->core.type, attributes->core.bits);
5408 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005409 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005411
5412 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 attributes->core.type,
5414 attributes->core.bits);
5415 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005416 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 attributes, &key_buffer_size);
5418 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005419 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420 }
Ronald Cron977c2472020-10-13 08:32:21 +02005421 }
Ronald Cron977c2472020-10-13 08:32:21 +02005422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005423 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5424 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005425 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005426 }
Ronald Cron977c2472020-10-13 08:32:21 +02005427 }
5428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 status = psa_driver_wrapper_generate_key(attributes,
5430 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432 if (status != PSA_SUCCESS) {
5433 psa_remove_key_data_from_memory(slot);
5434 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005435
Gilles Peskine11792082019-08-06 18:36:36 +02005436exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 if (status == PSA_SUCCESS) {
5438 status = psa_finish_key_creation(slot, driver, key);
5439 }
5440 if (status != PSA_SUCCESS) {
5441 psa_fail_key_creation(slot, driver);
5442 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005444 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005445}
5446
Gilles Peskinee59236f2018-01-27 23:32:46 +01005447/****************************************************************/
5448/* Module setup */
5449/****************************************************************/
5450
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005451#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005452psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 void (* entropy_init)(mbedtls_entropy_context *ctx),
5454 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005455{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5457 return PSA_ERROR_BAD_STATE;
5458 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005459 global_data.rng.entropy_init = entropy_init;
5460 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005461 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005462}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005463#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005466{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 psa_wipe_all_key_slots();
5468 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5469 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005470 }
5471 /* Wipe all remaining data, including configuration.
5472 * In particular, this sets all state indicator to the value
5473 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005475
5476 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478}
5479
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005480#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5481/** Recover a transaction that was interrupted by a power failure.
5482 *
5483 * This function is called during initialization, before psa_crypto_init()
5484 * returns. If this function returns a failure status, the initialization
5485 * fails.
5486 */
5487static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005489{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005491 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5492 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005493 /* TODO - fall through to the failure case until this
5494 * is implemented.
5495 * https://github.com/ARMmbed/mbed-crypto/issues/218
5496 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005497 default:
5498 /* We found an unsupported transaction in the storage.
5499 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005501 }
5502}
5503#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005505psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005507 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508
Gilles Peskinec6b69072018-11-20 21:42:52 +01005509 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005510 if (global_data.initialized != 0) {
5511 return PSA_SUCCESS;
5512 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005513
Gilles Peskine30524eb2020-11-13 17:02:26 +01005514 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005515 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005516 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005517 status = mbedtls_psa_random_seed(&global_data.rng);
5518 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005521 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005523 status = psa_initialize_key_slots();
5524 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005525 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005527
Ronald Cron088d5d02021-04-10 16:57:30 +02005528 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005529 status = psa_driver_wrapper_init();
5530 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005531 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005533
Gilles Peskine4b734222019-07-24 15:56:31 +02005534#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 status = psa_crypto_load_transaction();
5536 if (status == PSA_SUCCESS) {
5537 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5538 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005539 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005540 }
5541 status = psa_crypto_stop_transaction();
5542 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005543 /* There's no transaction to complete. It's all good. */
5544 status = PSA_SUCCESS;
5545 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005546#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005547
Gilles Peskinec6b69072018-11-20 21:42:52 +01005548 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005549 global_data.initialized = 1;
5550
5551exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005552 if (status != PSA_SUCCESS) {
5553 mbedtls_psa_crypto_free();
5554 }
5555 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556}
5557
David Horstmann63f82f72023-11-28 15:21:56 +00005558/* Memory copying test hooks */
5559#if defined(MBEDTLS_TEST_HOOKS)
5560void (*psa_input_pre_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5561void (*psa_input_post_copy_hook)(const uint8_t *input, size_t input_len) = NULL;
5562void (*psa_output_pre_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5563void (*psa_output_post_copy_hook)(const uint8_t *output, size_t output_len) = NULL;
5564#endif
5565
David Horstmanndf493552023-11-15 15:18:30 +00005566/** Copy from an input buffer to a local copy.
5567 *
5568 * \param[in] input Pointer to input buffer.
5569 * \param[in] input_len Length of the input buffer.
5570 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5571 * \param[out] input_copy_len Length of the local copy buffer.
5572 * \return #PSA_SUCCESS, if the buffer was successfully
5573 * copied.
5574 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5575 * copy is too small to hold contents of the
5576 * input buffer.
5577 */
5578MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005579psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5580 uint8_t *input_copy, size_t input_copy_len)
5581{
5582 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005583 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005584 }
5585
David Horstmann0760b152023-11-24 16:21:04 +00005586#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005587 if (psa_input_pre_copy_hook != NULL) {
5588 psa_input_pre_copy_hook(input, input_len);
5589 }
David Horstmann0760b152023-11-24 16:21:04 +00005590#endif
5591
David Horstmann660027f2023-11-15 17:33:47 +00005592 if (input_len > 0) {
5593 memcpy(input_copy, input, input_len);
5594 }
David Horstmann957f9802023-10-30 20:29:43 +00005595
David Horstmann0760b152023-11-24 16:21:04 +00005596#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005597 if (psa_input_post_copy_hook != NULL) {
5598 psa_input_post_copy_hook(input, input_len);
5599 }
David Horstmann0760b152023-11-24 16:21:04 +00005600#endif
5601
David Horstmann957f9802023-10-30 20:29:43 +00005602 return PSA_SUCCESS;
5603}
5604
David Horstmanndf493552023-11-15 15:18:30 +00005605/** Copy from a local output buffer into a user-supplied one.
5606 *
5607 * \param[in] output_copy Pointer to a local buffer containing the output.
5608 * \param[in] output_copy_len Length of the local buffer.
5609 * \param[out] output Pointer to user-supplied output buffer.
5610 * \param[out] output_len Length of the user-supplied output buffer.
5611 * \return #PSA_SUCCESS, if the buffer was successfully
5612 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005613 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005614 * user-supplied output buffer is too small to
5615 * hold the contents of the local buffer.
5616 */
5617MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005618psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5619 uint8_t *output, size_t output_len)
5620{
5621 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005622 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005623 }
David Horstmann660027f2023-11-15 17:33:47 +00005624
David Horstmann0760b152023-11-24 16:21:04 +00005625#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005626 if (psa_output_pre_copy_hook != NULL) {
5627 psa_output_pre_copy_hook(output, output_len);
5628 }
David Horstmann0760b152023-11-24 16:21:04 +00005629#endif
5630
David Horstmann660027f2023-11-15 17:33:47 +00005631 if (output_copy_len > 0) {
5632 memcpy(output, output_copy, output_copy_len);
5633 }
5634
David Horstmann0760b152023-11-24 16:21:04 +00005635#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann63f82f72023-11-28 15:21:56 +00005636 if (psa_output_post_copy_hook != NULL) {
5637 psa_output_post_copy_hook(output, output_len);
5638 }
David Horstmann0760b152023-11-24 16:21:04 +00005639#endif
5640
David Horstmann2bd296e2023-10-30 20:37:12 +00005641 return PSA_SUCCESS;
5642}
5643
David Horstmann81a0d572023-11-20 17:15:32 +00005644psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5645 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005646{
5647 psa_status_t status;
5648
David Horstmann0d52c712023-11-23 15:50:37 +00005649 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005650
David Horstmann726bf052023-11-15 18:11:26 +00005651 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005652 return PSA_SUCCESS;
5653 }
5654
David Horstmann81a0d572023-11-20 17:15:32 +00005655 local_input->buffer = mbedtls_calloc(input_len, 1);
5656 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005657 /* Since we dealt with the zero-length case above, we know that
5658 * a NULL return value means a failure of allocation. */
5659 return PSA_ERROR_INSUFFICIENT_MEMORY;
5660 }
David Horstmann81a0d572023-11-20 17:15:32 +00005661 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005662
David Horstmann81a0d572023-11-20 17:15:32 +00005663 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005664
5665 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005666 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005667 if (status != PSA_SUCCESS) {
5668 goto error;
5669 }
5670
5671 return PSA_SUCCESS;
5672
5673error:
David Horstmann81a0d572023-11-20 17:15:32 +00005674 mbedtls_free(local_input->buffer);
5675 local_input->buffer = NULL;
5676 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005677 return status;
5678}
5679
David Horstmann81a0d572023-11-20 17:15:32 +00005680void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005681{
David Horstmann81a0d572023-11-20 17:15:32 +00005682 mbedtls_free(local_input->buffer);
5683 local_input->buffer = NULL;
5684 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005685}
5686
David Horstmann1a76ab12023-11-20 12:54:09 +00005687psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5688 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005689{
David Horstmann0d52c712023-11-23 15:50:37 +00005690 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005691
David Horstmann726bf052023-11-15 18:11:26 +00005692 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005693 return PSA_SUCCESS;
5694 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005695 local_output->buffer = mbedtls_calloc(output_len, 1);
5696 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005697 /* Since we dealt with the zero-length case above, we know that
5698 * a NULL return value means a failure of allocation. */
5699 return PSA_ERROR_INSUFFICIENT_MEMORY;
5700 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005701 local_output->length = output_len;
5702 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005703
5704 return PSA_SUCCESS;
5705}
5706
David Horstmann1a76ab12023-11-20 12:54:09 +00005707psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005708{
David Horstmann761761f2023-11-15 15:57:32 +00005709 psa_status_t status;
5710
David Horstmann1a76ab12023-11-20 12:54:09 +00005711 if (local_output->buffer == NULL) {
5712 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005713 return PSA_SUCCESS;
5714 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005715 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005716 /* We have an internal copy but nothing to copy back to. */
5717 return PSA_ERROR_CORRUPTION_DETECTED;
5718 }
5719
David Horstmann1a76ab12023-11-20 12:54:09 +00005720 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5721 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005722 if (status != PSA_SUCCESS) {
5723 return status;
5724 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005725
David Horstmann1a76ab12023-11-20 12:54:09 +00005726 mbedtls_free(local_output->buffer);
5727 local_output->buffer = NULL;
5728 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005729
5730 return PSA_SUCCESS;
5731}
5732
Gilles Peskinee59236f2018-01-27 23:32:46 +01005733#endif /* MBEDTLS_PSA_CRYPTO_C */