blob: 60c81a572d2c91700bcc9367df36d07f348894c0 [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
David Horstmann0760b152023-11-24 16:21:04 +000076#if defined(MBEDTLS_TEST_HOOKS)
77#include "test/memory.h"
78#endif
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020081
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082/****************************************************************/
83/* Global data, support functions and library management */
84/****************************************************************/
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020087{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020089}
90
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010091/* Values for psa_global_data_t::rng_state */
92#define RNG_NOT_INITIALIZED 0
93#define RNG_INITIALIZED 1
94#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +010095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +010097 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010098 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +010099 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100} psa_global_data_t;
101
102static psa_global_data_t global_data;
103
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100104#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
105mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
106 &global_data.rng.drbg;
107#endif
108
itayzafrir0adf0fc2018-09-06 16:24:41 +0300109#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (global_data.initialized == 0) \
111 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100115 /* Mbed TLS error codes can combine a high-level error code and a
116 * low-level error code. The low-level error usually reflects the
117 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 int low_level_ret = -(-ret & 0x007f);
119 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100122
123 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
124 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
125 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100127 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100129
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200130 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200132
Gilles Peskine9a944802018-06-21 09:35:35 +0200133 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
134 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
135 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
136 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
137 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200139 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200141 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200143
Jaeden Amero93e21112019-02-20 13:57:28 +0000144#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000145 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000146#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
147 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
148#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100149 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100151 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
Jaeden Amero93e21112019-02-20 13:57:28 +0000154#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000155 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000156#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
157 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
158#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100161 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Gilles Peskine26869f22019-05-06 15:25:00 +0200171 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200173
174 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200176 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200178
Gilles Peskinea5905292018-02-07 20:59:33 +0100179 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195
196 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
200 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100201 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
202 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
206 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100210#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100211
212 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
Gilles Peskinee59236f2018-01-27 23:32:46 +0100217 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
218 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
219 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
222 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100231 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
232 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100233 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100235 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
236 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240#endif
241
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200246
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200305
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200308
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100330
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200335
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100353 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100357
Janos Follatha13b9052019-11-22 12:48:59 +0000358 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300360
Gilles Peskinee59236f2018-01-27 23:32:46 +0100361 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100363 }
364}
365
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200366
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200367
368
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369/****************************************************************/
370/* Key management */
371/****************************************************************/
372
John Durkop9814fa22020-11-04 12:28:15 -0800373#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800374 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100375 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
376 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
377 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
379 size_t bits,
380 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200381{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100383 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100385#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100386 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100388#endif
389#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100390 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100392#endif
393#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100400#endif
401#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 if (bits_is_sloppy) {
406 return MBEDTLS_ECP_DP_SECP521R1;
407 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100408 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 }
411 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100412
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
419#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100420 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100422#endif
423#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 }
428 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100429
Paul Elliott8ff510a2020-06-02 17:19:28 +0100430 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 if (bits_is_sloppy) {
437 return MBEDTLS_ECP_DP_CURVE25519;
438 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#endif
441#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100444#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 }
446 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100447
Paul Elliott8ff510a2020-06-02 17:19:28 +0100448 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
454#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 }
463 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100466 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200468}
John Durkop9814fa22020-11-04 12:28:15 -0800469#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100470 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
471 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
472 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
473 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
476 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477{
478 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200482 case PSA_KEY_TYPE_DERIVE:
483 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100484#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 if (bits != 128 && bits != 192 && bits != 256) {
487 return PSA_ERROR_INVALID_ARGUMENT;
488 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 break;
490#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200491#if defined(PSA_WANT_KEY_TYPE_ARIA)
492 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 if (bits != 128 && bits != 192 && bits != 256) {
494 return PSA_ERROR_INVALID_ARGUMENT;
495 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200496 break;
497#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100498#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (bits != 128 && bits != 192 && bits != 256) {
501 return PSA_ERROR_INVALID_ARGUMENT;
502 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503 break;
504#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100505#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (bits != 64 && bits != 128 && bits != 192) {
508 return PSA_ERROR_INVALID_ARGUMENT;
509 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510 break;
511#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100512#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 if (bits < 8 || bits > 2048) {
515 return PSA_ERROR_INVALID_ARGUMENT;
516 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 break;
518#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100519#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200520 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 if (bits != 256) {
522 return PSA_ERROR_INVALID_ARGUMENT;
523 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200524 break;
525#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 if (bits % 8 != 0) {
530 return PSA_ERROR_INVALID_ARGUMENT;
531 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534}
535
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100536/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537 *
Steven Cooremancd640932021-03-08 12:00:27 +0100538 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
539 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100540 *
541 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100542 * \param[in] key_type The key type of the key to be used with the
543 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100544 *
545 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100551 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100553{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100554 if (PSA_ALG_IS_HMAC(algorithm)) {
555 if (key_type == PSA_KEY_TYPE_HMAC) {
556 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
561 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
562 * key. */
563 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
564 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
565 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
566 * the block length (larger than 1) for block ciphers. */
567 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
568 return PSA_SUCCESS;
569 }
570 }
571 }
572
573 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100574}
575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
577 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200578{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 if (slot->key.data != NULL) {
580 return PSA_ERROR_ALREADY_EXISTS;
581 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 slot->key.data = mbedtls_calloc(1, buffer_length);
584 if (slot->key.data == NULL) {
585 return PSA_ERROR_INSUFFICIENT_MEMORY;
586 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200587
Ronald Cronea0f8a62020-11-25 17:52:23 +0100588 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200590}
591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
593 const uint8_t *data,
594 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200595{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 psa_status_t status = psa_allocate_buffer_to_slot(slot,
597 data_length);
598 if (status != PSA_SUCCESS) {
599 return status;
600 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 memcpy(slot->key.data, data, data_length);
603 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200604}
605
Ronald Crona0fe59f2020-11-29 11:14:53 +0100606psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100607 const psa_key_attributes_t *attributes,
608 const uint8_t *data, size_t data_length,
609 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
613 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200615 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100616 if (data_length == 0) {
617 return PSA_ERROR_NOT_SUPPORTED;
618 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (key_type_is_raw_bytes(type)) {
621 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200622
Steven Cooreman75b74362020-07-28 14:30:13 +0200623 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (data_length > SIZE_MAX / 8) {
625 return PSA_ERROR_NOT_SUPPORTED;
626 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200627
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200628 /* Enforce a size limit, and in particular ensure that the bit
629 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 if ((*bits) > PSA_MAX_KEY_BITS) {
631 return PSA_ERROR_NOT_SUPPORTED;
632 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 status = validate_unstructured_key_bit_size(type, *bits);
635 if (status != PSA_SUCCESS) {
636 return status;
637 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Ronald Crondd04d422020-11-28 15:14:42 +0100639 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100641 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 return PSA_SUCCESS;
645 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800646#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
648 if (PSA_KEY_TYPE_IS_ECC(type)) {
649 return mbedtls_psa_ecp_import_key(attributes,
650 data, data_length,
651 key_buffer, key_buffer_size,
652 key_buffer_length,
653 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100654 }
John Durkop9814fa22020-11-04 12:28:15 -0800655#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
656 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700657#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
659 if (PSA_KEY_TYPE_IS_RSA(type)) {
660 return mbedtls_psa_rsa_import_key(attributes,
661 data, data_length,
662 key_buffer, key_buffer_size,
663 key_buffer_length,
664 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200665 }
John Durkop9814fa22020-11-04 12:28:15 -0800666#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
667 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100668 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100671}
672
Gilles Peskinef603c712019-01-19 13:40:11 +0100673/** Calculate the intersection of two algorithm usage policies.
674 *
675 * Return 0 (which allows no operation) on incompatibility.
676 */
677static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100678 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100679 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100680 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100681{
Gilles Peskine549ea862019-05-22 11:45:59 +0200682 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100683 if (alg1 == alg2) {
684 return alg1;
685 }
Steven Cooreman03488022021-02-18 12:07:20 +0100686 /* If the policies are from the same hash-and-sign family, check
687 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
689 PSA_ALG_IS_SIGN_HASH(alg2) &&
690 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
691 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
692 return alg2;
693 }
694 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
695 return alg1;
696 }
Steven Cooreman03488022021-02-18 12:07:20 +0100697 }
698 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100699 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100700 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
702 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
703 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
704 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
705 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100706 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100707
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100708 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
710 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
711 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
712 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100713 }
714 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
716 (alg1_len <= alg2_len)) {
717 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
720 (alg2_len <= alg1_len)) {
721 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100722 }
723 }
724 /* If the policies are from the same MAC family, check whether one
725 * of them is a minimum-MAC-length policy. Calculate the most
726 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
728 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
729 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100730 /* Validate the combination of key type and algorithm. Since the base
731 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
733 return 0;
734 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100735
Steven Cooreman31a876d2021-03-03 20:47:40 +0100736 /* Get the (exact or at-least) output lengths for both sides of the
737 * requested intersection. None of the currently supported algorithms
738 * have an output length dependent on the actual key size, so setting it
739 * to a bogus value of 0 is currently OK.
740 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100741 * Note that for at-least-this-length wildcard algorithms, the output
742 * length is set to the shortest allowed length, which allows us to
743 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
745 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100746 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100747
Steven Cooremana1d83222021-02-25 10:20:29 +0100748 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
750 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
751 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100752 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100753
754 /* If only one is an at-least-this-length policy, the intersection would
755 * be the other (fixed-length) policy as long as said fixed length is
756 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
758 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100759 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
761 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100762 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100763
Steven Cooremancd640932021-03-08 12:00:27 +0100764 /* If none of them are wildcards, check whether they define the same tag
765 * length. This is still possible here when one is default-length and
766 * the other specific-length. Ensure to always return the
767 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 if (alg1_len == alg2_len) {
769 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
770 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100771 }
772 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100774}
775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776static int psa_key_algorithm_permits(psa_key_type_t key_type,
777 psa_algorithm_t policy_alg,
778 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200779{
Gilles Peskine549ea862019-05-22 11:45:59 +0200780 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 if (requested_alg == policy_alg) {
782 return 1;
783 }
Steven Cooreman03488022021-02-18 12:07:20 +0100784 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
785 * and requested_alg is the same hash-and-sign family with any hash,
786 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
788 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
789 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
790 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100791 }
792 /* If policy_alg is a wildcard AEAD algorithm of the same base as
793 * the requested algorithm, check the requested tag length to be
794 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_AEAD(policy_alg) &&
796 PSA_ALG_IS_AEAD(requested_alg) &&
797 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
798 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
799 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
800 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
801 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100802 }
Steven Cooremancd640932021-03-08 12:00:27 +0100803 /* If policy_alg is a MAC algorithm of the same base as the requested
804 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 if (PSA_ALG_IS_MAC(policy_alg) &&
806 PSA_ALG_IS_MAC(requested_alg) &&
807 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
808 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100809 /* Validate the combination of key type and algorithm. Since the policy
810 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
812 return 0;
813 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100814
Steven Cooreman31a876d2021-03-03 20:47:40 +0100815 /* Get both the requested output length for the algorithm which is to be
816 * verified, and the default output length for the base algorithm.
817 * Note that none of the currently supported algorithms have an output
818 * length dependent on actual key size, so setting it to a bogus value
819 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100820 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100822 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100823 key_type, 0,
824 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100825
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100826 /* If the policy is default-length, only allow an algorithm with
827 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
829 return requested_output_length == default_output_length;
830 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831
832 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100833 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100834 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
835 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
836 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100837 }
838
Steven Cooremancd640932021-03-08 12:00:27 +0100839 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
840 * check for the requested MAC length to be equal to or longer than the
841 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
843 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
844 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100845 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200846 }
Steven Cooremance48e852020-10-05 16:02:45 +0200847 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200848 * a key derivation with that key agreement should also be allowed. This
849 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
851 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
852 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
853 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200854 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100855 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100856 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200857}
858
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100859/** Test whether a policy permits an algorithm.
860 *
861 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100862 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100863 * \note This function requires providing the key type for which the policy is
864 * being validated, since some algorithm policy definitions (e.g. MAC)
865 * have different properties depending on what kind of cipher it is
866 * combined with.
867 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868 * \retval PSA_SUCCESS When \p alg is a specific algorithm
869 * allowed by the \p policy.
870 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
871 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
872 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100873 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100874static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
875 psa_key_type_t key_type,
876 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100877{
Steven Cooremand788fab2021-02-25 11:29:17 +0100878 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 if (alg == 0) {
880 return PSA_ERROR_INVALID_ARGUMENT;
881 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100882
Steven Cooreman7e39f052021-02-23 14:18:32 +0100883 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 if (PSA_ALG_IS_WILDCARD(alg)) {
885 return PSA_ERROR_INVALID_ARGUMENT;
886 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
889 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
890 return PSA_SUCCESS;
891 } else {
892 return PSA_ERROR_NOT_PERMITTED;
893 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100894}
895
Gilles Peskinef603c712019-01-19 13:40:11 +0100896/** Restrict a key policy based on a constraint.
897 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100898 * \note This function requires providing the key type for which the policy is
899 * being restricted, since some algorithm policy definitions (e.g. MAC)
900 * have different properties depending on what kind of cipher it is
901 * combined with.
902 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100903 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100904 * \param[in,out] policy The policy to restrict.
905 * \param[in] constraint The policy constraint to apply.
906 *
907 * \retval #PSA_SUCCESS
908 * \c *policy contains the intersection of the original value of
909 * \c *policy and \c *constraint.
910 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100911 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 * \c *policy is unchanged.
913 */
914static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100915 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100916 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100918{
919 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920 psa_key_policy_algorithm_intersection(key_type, policy->alg,
921 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200922 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100923 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
924 constraint->alg2);
925 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
926 return PSA_ERROR_INVALID_ARGUMENT;
927 }
928 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
929 return PSA_ERROR_INVALID_ARGUMENT;
930 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100931 policy->usage &= constraint->usage;
932 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200933 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100934 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100935}
936
Ronald Cron5c522922020-11-14 16:35:34 +0100937/** Get the description of a key given its identifier and policy constraints
938 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200939 *
Ronald Cron5c522922020-11-14 16:35:34 +0100940 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100941 * nonzero, the key must allow operations with this algorithm. If \p alg is
942 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100943 *
Ronald Cron5c522922020-11-14 16:35:34 +0100944 * In case of a persistent key, the function loads the description of the key
945 * into a key slot if not already done.
946 *
947 * On success, the returned key slot is locked. It is the responsibility of
948 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200949 */
Ronald Cron5c522922020-11-14 16:35:34 +0100950static psa_status_t psa_get_and_lock_key_slot_with_policy(
951 mbedtls_svc_key_id_t key,
952 psa_key_slot_t **p_slot,
953 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100955{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
957 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100959 status = psa_get_and_lock_key_slot(key, p_slot);
960 if (status != PSA_SUCCESS) {
961 return status;
962 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100964
965 /* Enforce that usage policy for the key slot contains all the flags
966 * required by the usage parameter. There is one exception: public
967 * keys can always be exported, so we treat public key objects as
968 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100970 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100974 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100976 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100977
Shaun Case0e7791f2021-12-20 21:14:10 -0800978 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 if (alg != 0) {
980 status = psa_key_policy_permits(&slot->attr.policy,
981 slot->attr.type,
982 alg);
983 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100984 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100986 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989
990error:
991 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +0200993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +0100995}
Darryl Green940d72c2018-07-13 13:18:51 +0100996
Ronald Cron5c522922020-11-14 16:35:34 +0100997/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998 *
999 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001000 * available, as opposed to a key in a secure element and/or to be used
1001 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001002 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001003 * This is a temporary function that may be used instead of
1004 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1005 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006 *
Ronald Cron5c522922020-11-14 16:35:34 +01001007 * On success, the returned key slot is locked. It is the responsibility of the
1008 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001009 */
Ronald Cron5c522922020-11-14 16:35:34 +01001010static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1011 mbedtls_svc_key_id_t key,
1012 psa_key_slot_t **p_slot,
1013 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001016 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1017 usage, alg);
1018 if (status != PSA_SUCCESS) {
1019 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1023 psa_unlock_key_slot(*p_slot);
1024 *p_slot = NULL;
1025 return PSA_ERROR_NOT_SUPPORTED;
1026 }
1027
1028 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001029}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001032{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001033 /* Data pointer will always be either a valid pointer or NULL in an
1034 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 if (slot->key.data != NULL) {
1036 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1037 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001040 slot->key.data = NULL;
1041 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001042
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001044}
1045
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001046/** Completely wipe a slot in memory, including its policy.
1047 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001049{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001051
1052 /*
1053 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001054 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001055 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1056 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001057 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001058 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001060#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001061 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001062#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001063 status = PSA_ERROR_CORRUPTION_DETECTED;
1064 }
1065
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001066 /* Multipart operations may still be using the key. This is safe
1067 * because all multipart operation objects are independent from
1068 * the key slot: if they need to access the key after the setup
1069 * phase, they have a copy of the key. Note that this means that
1070 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001071 /* At this point, key material and other type-specific content has
1072 * been wiped. Clear remaining metadata. We can call memset and not
1073 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074 memset(slot, 0, sizeof(*slot));
1075 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001076}
1077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001080 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001081 psa_status_t status; /* status of the last operation */
1082 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084 psa_se_drv_table_entry_t *driver;
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001087 if (mbedtls_svc_key_id_is_null(key)) {
1088 return PSA_SUCCESS;
1089 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001090
Ronald Cronf2911112020-10-29 17:51:10 +01001091 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001092 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001093 * key, this will load the key description from persistent memory if not
1094 * done yet. We cannot avoid this loading as without it we don't know if
1095 * the key is operated by an SE or not and this information is needed by
1096 * the current implementation.
1097 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098 status = psa_get_and_lock_key_slot(key, &slot);
1099 if (status != PSA_SUCCESS) {
1100 return status;
1101 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001102
Ronald Cronf2911112020-10-29 17:51:10 +01001103 /*
1104 * If the key slot containing the key description is under access by the
1105 * library (apart from the present access), the key cannot be destroyed
1106 * yet. For the time being, just return in error. Eventually (to be
1107 * implemented), the key should be destroyed when all accesses have
1108 * stopped.
1109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110 if (slot->lock_count > 1) {
1111 psa_unlock_key_slot(slot);
1112 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001113 }
1114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001116 /* Refuse the destruction of a read-only key (which may or may not work
1117 * if we attempt it, depending on whether the key is merely read-only
1118 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001119 * Just do the best we can, which is to wipe the copy in memory
1120 * (done in this function's cleanup code). */
1121 overall_status = PSA_ERROR_NOT_PERMITTED;
1122 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001123 }
1124
Gilles Peskine354f7672019-07-12 23:46:38 +02001125#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1127 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001128 /* For a key in a secure element, we need to do three things:
1129 * remove the key file in internal storage, destroy the
1130 * key inside the secure element, and update the driver's
1131 * persistent data. Start a transaction that will encompass these
1132 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001133 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001134 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001136 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 status = psa_crypto_save_transaction();
1138 if (status != PSA_SUCCESS) {
1139 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001140 /* We should still try to destroy the key in the secure
1141 * element and the key metadata in storage. This is especially
1142 * important if the error is that the storage is full.
1143 * But how to do it exactly without risking an inconsistent
1144 * state after a reset?
1145 * https://github.com/ARMmbed/mbed-crypto/issues/215
1146 */
1147 overall_status = status;
1148 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001149 }
1150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 status = psa_destroy_se_key(driver,
1152 psa_key_slot_get_slot_number(slot));
1153 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001154 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001156 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001157#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1158
Darryl Greend49a4992018-06-18 17:27:26 +01001159#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1161 status = psa_destroy_persistent_key(slot->attr.id);
1162 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001163 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001165
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001166 /* TODO: other slots may have a copy of the same key. We should
1167 * invalidate them.
1168 * https://github.com/ARMmbed/mbed-crypto/issues/214
1169 */
Darryl Greend49a4992018-06-18 17:27:26 +01001170 }
1171#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001172
Gilles Peskinefc762652019-07-22 19:30:34 +02001173#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 if (driver != NULL) {
1175 status = psa_save_se_persistent_data(driver);
1176 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 }
1179 status = psa_crypto_stop_transaction();
1180 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001181 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001183 }
1184#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1185
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001186exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 }
1192 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001193}
1194
John Durkop07cc04a2020-11-16 22:08:34 -08001195#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001196 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001197static psa_status_t psa_get_rsa_public_exponent(
1198 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001200{
1201 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001203 uint8_t *buffer = NULL;
1204 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1208 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001210 }
1211 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001212 /* It's the default value, which is reported as an empty string,
1213 * so there's nothing to do. */
1214 goto exit;
1215 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 buflen = mbedtls_mpi_size(&mpi);
1218 buffer = mbedtls_calloc(1, buflen);
1219 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1221 goto exit;
1222 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1224 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001225 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 attributes->domain_parameters = buffer;
1228 attributes->domain_parameters_size = buflen;
1229
1230exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 mbedtls_mpi_free(&mpi);
1232 if (ret != 0) {
1233 mbedtls_free(buffer);
1234 }
1235 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001236}
John Durkop07cc04a2020-11-16 22:08:34 -08001237#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001238 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001239
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001240/** Retrieve all the publicly-accessible attributes of a key.
1241 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1243 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001246 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001247 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1252 if (status != PSA_SUCCESS) {
1253 return status;
1254 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001255
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001256 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1258 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001259
1260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1262 psa_set_key_slot_number(attributes,
1263 psa_key_slot_get_slot_number(slot));
1264 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001267 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001268#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001269 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001270 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001271 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001272 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001273 * is not yet implemented.
1274 * https://github.com/ARMmbed/mbed-crypto/issues/216
1275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001277 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001278
Ronald Cron90857082020-11-25 14:58:33 +01001279 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 slot->attr.type,
1281 slot->key.data,
1282 slot->key.bytes,
1283 &rsa);
1284 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001285 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 status = psa_get_rsa_public_exponent(rsa,
1289 attributes);
1290 mbedtls_rsa_free(rsa);
1291 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001292 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001293 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001294#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001295 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001296 default:
1297 /* Nothing else to do. */
1298 break;
1299 }
1300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 if (status != PSA_SUCCESS) {
1302 psa_reset_key_attributes(attributes);
1303 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001305 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001307 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308}
1309
Gilles Peskinec8000c02019-08-02 20:15:51 +02001310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1311psa_status_t psa_get_key_slot_number(
1312 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001316 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 return PSA_SUCCESS;
1318 } else {
1319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001320 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001321}
1322#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1325 size_t key_buffer_size,
1326 uint8_t *data,
1327 size_t data_size,
1328 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001329{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 if (key_buffer_size > data_size) {
1331 return PSA_ERROR_BUFFER_TOO_SMALL;
1332 }
1333 memcpy(data, key_buffer, key_buffer_size);
1334 memset(data + key_buffer_size, 0,
1335 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001336 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001337 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001338}
1339
Ronald Cron7285cda2020-11-26 14:46:37 +01001340psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 const psa_key_attributes_t *attributes,
1342 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344{
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 psa_key_type_t type = attributes->core.type;
1346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 if (key_type_is_raw_bytes(type) ||
1348 PSA_KEY_TYPE_IS_RSA(type) ||
1349 PSA_KEY_TYPE_IS_ECC(type)) {
1350 return psa_export_key_buffer_internal(
1351 key_buffer, key_buffer_size,
1352 data, data_size, data_length);
1353 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001354 /* This shouldn't happen in the reference implementation, but
1355 it is valid for a special-purpose implementation to omit
1356 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 }
1359}
1360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1362 uint8_t *data,
1363 size_t data_size,
1364 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001365{
1366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1368 psa_key_slot_t *slot;
1369
Ronald Crone907e552021-01-18 13:22:38 +01001370 /* Reject a zero-length output buffer now, since this can never be a
1371 * valid key representation. This way we know that data must be a valid
1372 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 if (data_size == 0) {
1374 return PSA_ERROR_BUFFER_TOO_SMALL;
1375 }
Ronald Crone907e552021-01-18 13:22:38 +01001376
Ronald Cron9486f9d2020-11-26 13:36:23 +01001377 /* Set the key to empty now, so that even when there are errors, we always
1378 * set data_length to a value between 0 and data_size. On error, setting
1379 * the key to empty is a good choice because an empty key representation is
1380 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001381 *data_length = 0;
1382
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383 /* Export requires the EXPORT flag. There is an exception for public keys,
1384 * which don't require any flag, but
1385 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1386 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1388 PSA_KEY_USAGE_EXPORT, 0);
1389 if (status != PSA_SUCCESS) {
1390 return status;
1391 }
mohammad160306e79202018-03-28 13:17:44 +03001392
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 psa_key_attributes_t attributes = {
1394 .core = slot->attr
1395 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 status = psa_driver_wrapper_export_key(&attributes,
1397 slot->key.data, slot->key.bytes,
1398 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001403}
1404
Ronald Cron7285cda2020-11-26 14:46:37 +01001405psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 const psa_key_attributes_t *attributes,
1407 const uint8_t *key_buffer,
1408 size_t key_buffer_size,
1409 uint8_t *data,
1410 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412{
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1416 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001417 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 return psa_export_key_buffer_internal(
1419 key_buffer, key_buffer_size,
1420 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001421 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001424#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1426 return mbedtls_psa_rsa_export_public_key(attributes,
1427 key_buffer,
1428 key_buffer_size,
1429 data,
1430 data_size,
1431 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001432#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001433 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001435#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1436 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001438#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001439 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1440 return mbedtls_psa_ecp_export_public_key(attributes,
1441 key_buffer,
1442 key_buffer_size,
1443 data,
1444 data_size,
1445 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001446#else
1447 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001449#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1450 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001451 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001453 /* This shouldn't happen in the reference implementation, but
1454 it is valid for a special-purpose implementation to omit
1455 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001457 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001458}
Ronald Crond18b5f82020-11-25 16:46:05 +01001459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1461 uint8_t *data,
1462 size_t data_size,
1463 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001464{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001466 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001467 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001468 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001469
Ronald Crone907e552021-01-18 13:22:38 +01001470 /* Reject a zero-length output buffer now, since this can never be a
1471 * valid key representation. This way we know that data must be a valid
1472 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001473 if (data_size == 0) {
1474 return PSA_ERROR_BUFFER_TOO_SMALL;
1475 }
Ronald Crone907e552021-01-18 13:22:38 +01001476
Darryl Greendd8fb772018-11-07 16:00:44 +00001477 /* Set the key to empty now, so that even when there are errors, we always
1478 * set data_length to a value between 0 and data_size. On error, setting
1479 * the key to empty is a good choice because an empty key representation is
1480 * unlikely to be accepted anywhere. */
1481 *data_length = 0;
1482
1483 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001484 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1485 if (status != PSA_SUCCESS) {
1486 return status;
1487 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1490 status = PSA_ERROR_INVALID_ARGUMENT;
1491 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001492 }
1493
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001494 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001495 .core = slot->attr
1496 };
Ronald Cron67227982020-11-26 15:16:05 +01001497 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001498 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001499 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500
1501exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001505}
1506
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001507MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1508 "One or more key attribute flag is listed as both external-only and dual-use")
1509MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1510 "One or more key attribute flag is listed as both internal-only and dual-use")
1511MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1512 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514/** Validate that a key policy is internally well-formed.
1515 *
1516 * This function only rejects invalid policies. It does not validate the
1517 * consistency of the policy with respect to other attributes of the key
1518 * such as the key type.
1519 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001520static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001521{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1523 PSA_KEY_USAGE_COPY |
1524 PSA_KEY_USAGE_ENCRYPT |
1525 PSA_KEY_USAGE_DECRYPT |
1526 PSA_KEY_USAGE_SIGN_MESSAGE |
1527 PSA_KEY_USAGE_VERIFY_MESSAGE |
1528 PSA_KEY_USAGE_SIGN_HASH |
1529 PSA_KEY_USAGE_VERIFY_HASH |
1530 PSA_KEY_USAGE_DERIVE)) != 0) {
1531 return PSA_ERROR_INVALID_ARGUMENT;
1532 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001535}
1536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate the internal consistency of key attributes.
1538 *
1539 * This function only rejects invalid attribute values. If does not
1540 * validate the consistency of the attributes with any key data that may
1541 * be involved in the creation of the key.
1542 *
1543 * Call this function early in the key creation process.
1544 *
1545 * \param[in] attributes Key attributes for the new key.
1546 * \param[out] p_drv On any return, the driver for the key, if any.
1547 * NULL for a transparent key.
1548 *
1549 */
1550static psa_status_t psa_validate_key_attributes(
1551 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001553{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001554 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001555 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1556 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001558 status = psa_validate_key_location(lifetime, p_drv);
1559 if (status != PSA_SUCCESS) {
1560 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001561 }
1562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 status = psa_validate_key_persistence(lifetime);
1564 if (status != PSA_SUCCESS) {
1565 return status;
1566 }
1567
1568 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1569 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1570 return PSA_ERROR_INVALID_ARGUMENT;
1571 }
1572 } else {
1573 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1574 return PSA_ERROR_INVALID_ARGUMENT;
1575 }
1576 }
1577
1578 status = psa_validate_key_policy(&attributes->core.policy);
1579 if (status != PSA_SUCCESS) {
1580 return status;
1581 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
1583 /* Refuse to create overly large keys.
1584 * Note that this doesn't trigger on import if the attributes don't
1585 * explicitly specify a size (so psa_get_key_bits returns 0), so
1586 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1588 return PSA_ERROR_NOT_SUPPORTED;
1589 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Gilles Peskine91e8c332019-08-02 19:19:39 +02001591 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1593 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1594 return PSA_ERROR_INVALID_ARGUMENT;
1595 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598}
1599
Gilles Peskine4747d192019-04-17 15:05:45 +02001600/** Prepare a key slot to receive key material.
1601 *
1602 * This function allocates a key slot and sets its metadata.
1603 *
1604 * If this function fails, call psa_fail_key_creation().
1605 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001606 * This function is intended to be used as follows:
1607 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001608 * it with the specified attributes, and in case of a volatile key assign it
1609 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 * -# Populate the slot with the key material.
1611 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1612 * In case of failure at any step, stop the sequence and call
1613 * psa_fail_key_creation().
1614 *
Ronald Cron5c522922020-11-14 16:35:34 +01001615 * On success, the key slot is locked. It is the responsibility of the caller
1616 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001617 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001618 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001619 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001620 * \param[out] p_slot On success, a pointer to the prepared slot.
1621 * \param[out] p_drv On any return, the driver for the key, if any.
1622 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001623 *
1624 * \retval #PSA_SUCCESS
1625 * The key slot is ready to receive key material.
1626 * \return If this function fails, the key slot is an invalid state.
1627 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001628 */
1629static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001630 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001632 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001634{
1635 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001636 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 psa_key_slot_t *slot;
1638
Gilles Peskinedf179142019-07-15 22:02:14 +02001639 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 *p_drv = NULL;
1641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642 status = psa_validate_key_attributes(attributes, p_drv);
1643 if (status != PSA_SUCCESS) {
1644 return status;
1645 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001647 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1648 if (status != PSA_SUCCESS) {
1649 return status;
1650 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 slot = *p_slot;
1652
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001653 /* We're storing the declared bit-size of the key. It's up to each
1654 * creation mechanism to verify that this information is correct.
1655 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001656 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001657 * is optional (import, copy). In case of a volatile key, assign it the
1658 * volatile key identifier associated to the slot returned to contain its
1659 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001660
1661 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001663#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1664 slot->attr.id = volatile_key_id;
1665#else
1666 slot->attr.id.key_id = volatile_key_id;
1667#endif
1668 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001669
Gilles Peskine91e8c332019-08-02 19:19:39 +02001670 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001671 * external-only flags, query `attributes`. Thanks to the check
1672 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001673 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001674 * may have set. */
1675 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001676
Gilles Peskinecbaff462019-07-12 23:46:04 +02001677#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001678 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001679 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001680 * create the key file in internal storage, create the
1681 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001682 * persistent data. This is done by starting a transaction that will
1683 * encompass these three actions.
1684 * For registering a volatile key, we just need to find an appropriate
1685 * slot number inside the SE. Since the key is designated volatile, creating
1686 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001687 /* The first thing to do is to find a slot number for the new key.
1688 * We save the slot number in persistent storage as part of the
1689 * transaction data. It will be needed to recover if the power
1690 * fails during the key creation process, to clean up on the secure
1691 * element side after restarting. Obtaining a slot number from the
1692 * secure element driver updates its persistent state, but we do not yet
1693 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001694 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001695 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001696 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1698 &slot_number);
1699 if (status != PSA_SUCCESS) {
1700 return status;
1701 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1704 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001705 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001706 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001707 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001708 status = psa_crypto_save_transaction();
1709 if (status != PSA_SUCCESS) {
1710 (void) psa_crypto_stop_transaction();
1711 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001713 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001714
1715 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001716 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001717 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001720 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001721 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001722 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001723#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001726}
Gilles Peskine4747d192019-04-17 15:05:45 +02001727
1728/** Finalize the creation of a key once its key material has been set.
1729 *
1730 * This entails writing the key to persistent storage.
1731 *
1732 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001733 * See the documentation of psa_start_key_creation() for the intended use
1734 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001735 *
Ronald Cron5c522922020-11-14 16:35:34 +01001736 * If the finalization succeeds, the function unlocks the key slot (it was
1737 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1738 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001739 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001740 * \param[in,out] slot Pointer to the slot with key material.
1741 * \param[in] driver The secure element driver for the key,
1742 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001743 * \param[out] key On success, identifier of the key. Note that the
1744 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 *
1746 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001748 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1749 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1750 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1751 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1752 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1753 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001754 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001755 * \return If this function fails, the key slot is an invalid state.
1756 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001758static psa_status_t psa_finish_key_creation(
1759 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001760 psa_se_drv_table_entry_t *driver,
1761 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001762{
1763 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001764 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001765 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001766
1767#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001768 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001769#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001771 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001772 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001774
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001775 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1776 sizeof(data.slot_number),
1777 "Slot number size does not match psa_se_key_data_storage_t");
1778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1780 status = psa_save_persistent_key(&slot->attr,
1781 (uint8_t *) &data,
1782 sizeof(data));
1783 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001784#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1785 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001786 /* Key material is saved in export representation in the slot, so
1787 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001788 status = psa_save_persistent_key(&slot->attr,
1789 slot->key.data,
1790 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001791 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001793#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1794
Gilles Peskinecbaff462019-07-12 23:46:04 +02001795#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001796 /* Finish the transaction for a key creation. This does not
1797 * happen when registering an existing key. Detect this case
1798 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799 * creation of a persistent key in a secure element requires a transaction,
1800 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801 if (driver != NULL &&
1802 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1803 status = psa_save_se_persistent_data(driver);
1804 if (status != PSA_SUCCESS) {
1805 psa_destroy_persistent_key(slot->attr.id);
1806 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001807 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001808 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001809 }
1810#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001813 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 status = psa_unlock_key_slot(slot);
1815 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001816 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001818 }
Ronald Cron50972942020-11-14 11:28:25 +01001819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001821}
1822
1823/** Abort the creation of a key.
1824 *
1825 * You may call this function after calling psa_start_key_creation(),
1826 * or after psa_finish_key_creation() fails. In other circumstances, this
1827 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001828 * See the documentation of psa_start_key_creation() for the intended use
1829 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001831 * \param[in,out] slot Pointer to the slot with key material.
1832 * \param[in] driver The secure element driver for the key,
1833 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835static void psa_fail_key_creation(psa_key_slot_t *slot,
1836 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001837{
Gilles Peskine011e4282019-06-26 18:34:38 +02001838 (void) driver;
1839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001840 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001843
1844#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001845 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 * element, and the failure happened later (when saving metadata
1847 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001848 * element.
1849 * https://github.com/ARMmbed/mbed-crypto/issues/217
1850 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001851
Gilles Peskined7729582019-08-05 15:55:54 +02001852 /* Abort the ongoing transaction if any (there may not be one if
1853 * the creation process failed before starting one, or if the
1854 * key creation is a registration of a key in a secure element).
1855 * Earlier functions must already have done what it takes to undo any
1856 * partial creation. All that's left is to update the transaction data
1857 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001862}
1863
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001864/** Validate optional attributes during key creation.
1865 *
1866 * Some key attributes are optional during key creation. If they are
1867 * specified in the attributes structure, check that they are consistent
1868 * with the data in the slot.
1869 *
1870 * This function should be called near the end of key creation, after
1871 * the slot in memory is fully populated but before saving persistent data.
1872 */
1873static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001874 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001875 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001877 if (attributes->core.type != 0) {
1878 if (attributes->core.type != slot->attr.type) {
1879 return PSA_ERROR_INVALID_ARGUMENT;
1880 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 }
1882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001884#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1886 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001887 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001888 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001890
Ronald Cron90857082020-11-25 14:58:33 +01001891 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 slot->attr.type,
1893 slot->key.data,
1894 slot->key.bytes,
1895 &rsa);
1896 if (status != PSA_SUCCESS) {
1897 return status;
1898 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 mbedtls_mpi_init(&actual);
1901 mbedtls_mpi_init(&required);
1902 ret = mbedtls_rsa_export(rsa,
1903 NULL, NULL, NULL, NULL, &actual);
1904 mbedtls_rsa_free(rsa);
1905 mbedtls_free(rsa);
1906 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001907 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 }
1909 ret = mbedtls_mpi_read_binary(&required,
1910 attributes->domain_parameters,
1911 attributes->domain_parameters_size);
1912 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001913 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001914 }
1915 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 }
1918rsa_exit:
1919 mbedtls_mpi_free(&actual);
1920 mbedtls_mpi_free(&required);
1921 if (ret != 0) {
1922 return mbedtls_to_psa_error(ret);
1923 }
1924 } else
John Durkop9814fa22020-11-04 12:28:15 -08001925#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1926 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 }
1930 }
1931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 if (attributes->core.bits != 0) {
1933 if (attributes->core.bits != slot->attr.bits) {
1934 return PSA_ERROR_INVALID_ARGUMENT;
1935 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 }
1937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001938 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001939}
1940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001941psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1942 const uint8_t *data,
1943 size_t data_length,
1944 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001945{
1946 psa_status_t status;
1947 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001948 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001949 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950
Ronald Cron81709fc2020-11-14 12:10:32 +01001951 *key = MBEDTLS_SVC_KEY_ID_INIT;
1952
Gilles Peskine0f84d622019-09-12 19:03:13 +02001953 /* Reject zero-length symmetric keys (including raw data key objects).
1954 * This also rejects any key which might be encoded as an empty string,
1955 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956 if (data_length == 0) {
1957 return PSA_ERROR_INVALID_ARGUMENT;
1958 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1961 &slot, &driver);
1962 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001966 /* In the case of a transparent key or an opaque key stored in local
1967 * storage (thus not in the case of generating a key in a secure element
1968 * or cryptoprocessor with storage), we have to allocate a buffer to
1969 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001970 if (slot->key.data == NULL) {
1971 status = psa_allocate_buffer_to_slot(slot, data_length);
1972 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001973 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001975 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001976
1977 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 status = psa_driver_wrapper_import_key(attributes,
1979 data, data_length,
1980 slot->key.data,
1981 slot->key.bytes,
1982 &slot->key.bytes, &bits);
1983 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001987 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001988 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001990 status = PSA_ERROR_INVALID_ARGUMENT;
1991 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001992 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 status = psa_validate_optional_attributes(slot, attributes);
1995 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02001996 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 if (status != PSA_SUCCESS) {
2002 psa_fail_key_creation(slot, driver);
2003 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002006}
2007
Gilles Peskined7729582019-08-05 15:55:54 +02002008#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2009psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002011{
2012 psa_status_t status;
2013 psa_key_slot_t *slot = NULL;
2014 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002015 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002016
2017 /* Leaving attributes unspecified is not currently supported.
2018 * It could make sense to query the key type and size from the
2019 * secure element, but not all secure elements support this
2020 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2022 return PSA_ERROR_NOT_SUPPORTED;
2023 }
2024 if (psa_get_key_bits(attributes) == 0) {
2025 return PSA_ERROR_NOT_SUPPORTED;
2026 }
Gilles Peskined7729582019-08-05 15:55:54 +02002027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2029 &slot, &driver);
2030 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002031 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002032 }
Gilles Peskined7729582019-08-05 15:55:54 +02002033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002035
2036exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 if (status != PSA_SUCCESS) {
2038 psa_fail_key_creation(slot, driver);
2039 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002040
Gilles Peskined7729582019-08-05 15:55:54 +02002041 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 psa_close_key(key);
2043 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002044}
2045#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002047static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2048 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002049{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 psa_status_t status = psa_copy_key_material_into_slot(target,
2051 source->key.data,
2052 source->key.bytes);
2053 if (status != PSA_SUCCESS) {
2054 return status;
2055 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002056
Steven Cooreman398aee52020-10-13 14:35:45 +02002057 target->attr.type = source->attr.type;
2058 target->attr.bits = source->attr.bits;
2059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002061}
2062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2064 const psa_key_attributes_t *specified_attributes,
2065 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002066{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002068 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002069 psa_key_slot_t *source_slot = NULL;
2070 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002072 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002073
Ronald Cron81709fc2020-11-14 12:10:32 +01002074 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2075
Ronald Cron5c522922020-11-14 16:35:34 +01002076 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2078 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 status = psa_validate_optional_attributes(source_slot,
2083 specified_attributes);
2084 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002085 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002086 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 status = psa_restrict_key_policy(source_slot->attr.type,
2089 &actual_attributes.core.policy,
2090 &source_slot->attr.policy);
2091 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2096 &target_slot, &driver);
2097 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002100
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002101#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002103 /* Copying to a secure element is not implemented yet. */
2104 status = PSA_ERROR_NOT_SUPPORTED;
2105 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002106 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002107#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002110 /*
2111 * Copying through an opaque driver is not implemented yet, consider
2112 * a lifetime with an external location as an invalid parameter for
2113 * now.
2114 */
2115 status = PSA_ERROR_INVALID_ARGUMENT;
2116 goto exit;
2117 }
2118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 status = psa_copy_key_material(source_slot, target_slot);
2120 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 if (status != PSA_SUCCESS) {
2127 psa_fail_key_creation(target_slot, driver);
2128 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002133}
2134
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002135
2136
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002137/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002138/* Message digests */
2139/****************************************************************/
2140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002143 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 if (operation->id == 0) {
2145 return PSA_SUCCESS;
2146 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002149 operation->id = 0;
2150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002151 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002152}
2153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2155 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002156{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002157 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002158
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002159 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002160 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002161 status = PSA_ERROR_BAD_STATE;
2162 goto exit;
2163 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002166 status = PSA_ERROR_INVALID_ARGUMENT;
2167 goto exit;
2168 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002169
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002170 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2171 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002175
2176exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002177 if (status != PSA_SUCCESS) {
2178 psa_hash_abort(operation);
2179 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002180
2181 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182}
2183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2185 const uint8_t *input,
2186 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191 status = PSA_ERROR_BAD_STATE;
2192 goto exit;
2193 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002194
Steven Cooremanc8288352021-03-04 14:02:19 +01002195 /* Don't require hash implementations to behave correctly on a
2196 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002197 if (input_length == 0) {
2198 return PSA_SUCCESS;
2199 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002202
2203exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 if (status != PSA_SUCCESS) {
2205 psa_hash_abort(operation);
2206 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209}
2210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2212 uint8_t *hash,
2213 size_t hash_size,
2214 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002216 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 if (operation->id == 0) {
2218 return PSA_ERROR_BAD_STATE;
2219 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220
Steven Cooreman1e582352021-02-18 17:24:37 +01002221 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222 operation, hash, hash_size, hash_length);
2223 psa_hash_abort(operation);
2224 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225}
2226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002227psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2228 const uint8_t *hash,
2229 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002231 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002233 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 operation,
2235 actual_hash, sizeof(actual_hash),
2236 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002239 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002243 status = PSA_ERROR_INVALID_SIGNATURE;
2244 goto exit;
2245 }
2246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002247 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002248 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002250
2251exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002252 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2253 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002254 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258}
2259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260psa_status_t psa_hash_compute(psa_algorithm_t alg,
2261 const uint8_t *input, size_t input_length,
2262 uint8_t *hash, size_t hash_size,
2263 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002264{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002265 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 if (!PSA_ALG_IS_HASH(alg)) {
2267 return PSA_ERROR_INVALID_ARGUMENT;
2268 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2271 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002272}
2273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274psa_status_t psa_hash_compare(psa_algorithm_t alg,
2275 const uint8_t *input, size_t input_length,
2276 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002277{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002278 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002279 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 if (!PSA_ALG_IS_HASH(alg)) {
2282 return PSA_ERROR_INVALID_ARGUMENT;
2283 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002284
Steven Cooreman1e582352021-02-18 17:24:37 +01002285 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 alg, input, input_length,
2287 actual_hash, sizeof(actual_hash),
2288 &actual_hash_length);
2289 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002290 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002291 }
2292 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002293 status = PSA_ERROR_INVALID_SIGNATURE;
2294 goto exit;
2295 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002297 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002299
2300exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002301 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2302 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002303}
2304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2306 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002307{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 if (source_operation->id == 0 ||
2309 target_operation->id != 0) {
2310 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002311 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2314 target_operation);
2315 if (status != PSA_SUCCESS) {
2316 psa_hash_abort(target_operation);
2317 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002320}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321
2322
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324/* MAC */
2325/****************************************************************/
2326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002328{
Steven Cooreman07897832021-03-19 18:28:56 +01002329 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 if (operation->id == 0) {
2331 return PSA_SUCCESS;
2332 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002335 operation->mac_size = 0;
2336 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002337 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340}
2341
Ronald Cron1c650a12021-06-17 16:33:22 +02002342static psa_status_t psa_mac_finalize_alg_and_key_validation(
2343 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002344 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002346{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348 psa_key_type_t key_type = psa_get_key_type(attributes);
2349 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351 if (!PSA_ALG_IS_MAC(alg)) {
2352 return PSA_ERROR_INVALID_ARGUMENT;
2353 }
Steven Cooreman07897832021-03-19 18:28:56 +01002354
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002355 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 status = psa_mac_key_can_do(alg, key_type);
2357 if (status != PSA_SUCCESS) {
2358 return status;
2359 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002360
Steven Cooremana6474de2021-05-10 11:13:41 +02002361 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002365 /* A very short MAC is too short for security since it can be
2366 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2367 * so we make this our minimum, even though 32 bits is still
2368 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002370 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2373 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002374 /* It's impossible to "truncate" to a larger length than the full length
2375 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002377 }
2378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002380 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2381 * that is disabled in the compile-time configuration. The result can
2382 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2383 * configuration into account. In this case, force a return of
2384 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2385 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2386 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2387 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2388 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002390 }
2391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002393}
2394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002395static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2396 mbedtls_svc_key_id_t key,
2397 psa_algorithm_t alg,
2398 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002399{
2400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2401 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002402 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002403 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002404
2405 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002407 status = PSA_ERROR_BAD_STATE;
2408 goto exit;
2409 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002410
2411 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 key,
2413 &slot,
2414 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2415 alg);
2416 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002417 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002419
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002420 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002421 .core = slot->attr
2422 };
2423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2425 &operation->mac_size);
2426 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002427 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002429
2430 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002431 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (is_sign) {
2433 status = psa_driver_wrapper_mac_sign_setup(operation,
2434 &attributes,
2435 slot->key.data,
2436 slot->key.bytes,
2437 alg);
2438 } else {
2439 status = psa_driver_wrapper_mac_verify_setup(operation,
2440 &attributes,
2441 slot->key.data,
2442 slot->key.bytes,
2443 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002444 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002445
Gilles Peskinefbfac682018-07-08 20:51:54 +02002446exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447 if (status != PSA_SUCCESS) {
2448 psa_mac_abort(operation);
2449 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454}
2455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2457 mbedtls_svc_key_id_t key,
2458 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002459{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002461}
2462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2464 mbedtls_svc_key_id_t key,
2465 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002466{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002468}
2469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2471 const uint8_t *input,
2472 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002474 if (operation->id == 0) {
2475 return PSA_ERROR_BAD_STATE;
2476 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477
Steven Cooreman11743f92021-03-19 18:38:46 +01002478 /* Don't require hash implementations to behave correctly on a
2479 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 if (input_length == 0) {
2481 return PSA_SUCCESS;
2482 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2485 input, input_length);
2486 if (status != PSA_SUCCESS) {
2487 psa_mac_abort(operation);
2488 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491}
2492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2494 uint8_t *mac,
2495 size_t mac_size,
2496 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002497{
Steven Cooreman87885df2021-03-19 19:04:39 +01002498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2499 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002502 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002503 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002504 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002507 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002508 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002509 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002510
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002511 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2512 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002514 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002515 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002516 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002518 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002519 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002520 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002521 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 status = psa_driver_wrapper_mac_sign_finish(operation,
2524 mac, operation->mac_size,
2525 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002526
Dave Rodgman478ab542021-06-25 09:09:02 +01002527exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002528 /* In case of success, set the potential excess room in the output buffer
2529 * to an invalid value, to avoid potentially leaking a longer MAC.
2530 * In case of error, set the output length and content to a safe default,
2531 * such that in case the caller misses an error check, the output would be
2532 * an unachievable MAC.
2533 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002535 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002536 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002537 }
mohammad16036df908f2018-04-02 08:34:15 -07002538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002539 if (mac_size > operation->mac_size) {
2540 memset(&mac[operation->mac_size], '!',
2541 mac_size - operation->mac_size);
2542 }
Ronald Cron882eb782021-06-18 13:05:48 +02002543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002547}
2548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2550 const uint8_t *mac,
2551 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552{
Steven Cooreman87885df2021-03-19 19:04:39 +01002553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2554 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002557 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002558 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002559 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002561 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002562 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002563 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002564 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002567 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002568 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002569 }
2570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002571 status = psa_driver_wrapper_mac_verify_finish(operation,
2572 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002573
Dave Rodgman478ab542021-06-25 09:09:02 +01002574exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002575 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578}
2579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2581 psa_algorithm_t alg,
2582 const uint8_t *input,
2583 size_t input_length,
2584 uint8_t *mac,
2585 size_t mac_size,
2586 size_t *mac_length,
2587 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002588{
2589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002590 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002591 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002592 psa_key_slot_t *slot;
2593 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594
Ronald Crondbb86462021-06-17 17:17:20 +02002595 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596 key,
2597 &slot,
2598 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2599 alg);
2600 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002601 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002603
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002604 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002605 .core = slot->attr
2606 };
2607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002608 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2609 &operation_mac_size);
2610 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002611 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002614 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002615 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002616 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002617 }
2618
2619 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 &attributes,
2621 slot->key.data, slot->key.bytes,
2622 alg,
2623 input, input_length,
2624 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002625
2626exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002627 /* In case of success, set the potential excess room in the output buffer
2628 * to an invalid value, to avoid potentially leaking a longer MAC.
2629 * In case of error, set the output length and content to a safe default,
2630 * such that in case the caller misses an error check, the output would be
2631 * an unachievable MAC.
2632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002634 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002635 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002636 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 if (mac_size > operation_mac_size) {
2638 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2639 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002644}
2645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002650 uint8_t *mac,
2651 size_t mac_size,
2652 size_t *mac_length)
2653{
2654 return psa_mac_compute_internal(key, alg,
2655 input, input_length,
2656 mac, mac_size, mac_length, 1);
2657}
2658
2659psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2660 psa_algorithm_t alg,
2661 const uint8_t *input,
2662 size_t input_length,
2663 const uint8_t *mac,
2664 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002665{
2666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002667 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2668 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 status = psa_mac_compute_internal(key, alg,
2671 input, input_length,
2672 actual_mac, sizeof(actual_mac),
2673 &actual_mac_length, 0);
2674 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002675 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002676 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002679 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002680 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002681 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002683 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002684 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002685 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002686
2687exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002691}
Gilles Peskine20035e32018-02-03 22:44:14 +01002692
Gilles Peskine20035e32018-02-03 22:44:14 +01002693/****************************************************************/
2694/* Asymmetric cryptography */
2695/****************************************************************/
2696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2698 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002699{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002700 if (input_is_message) {
2701 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2702 return PSA_ERROR_INVALID_ARGUMENT;
2703 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2706 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2707 return PSA_ERROR_INVALID_ARGUMENT;
2708 }
2709 }
2710 } else {
2711 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2712 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002713 }
2714 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002717}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2720 int input_is_message,
2721 psa_algorithm_t alg,
2722 const uint8_t *input,
2723 size_t input_length,
2724 uint8_t *signature,
2725 size_t signature_size,
2726 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002727{
2728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2729 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002730 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002731 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002732
2733 *signature_length = 0;
2734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 status = psa_sign_verify_check_alg(input_is_message, alg);
2736 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002737 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002739
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002740 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002741 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002742 * buffer can in principle be empty since it doesn't actually have
2743 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 if (signature_size == 0) {
2745 return PSA_ERROR_BUFFER_TOO_SMALL;
2746 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002747
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002748 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749 key, &slot,
2750 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2751 PSA_KEY_USAGE_SIGN_HASH,
2752 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002754 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002755 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002759 status = PSA_ERROR_INVALID_ARGUMENT;
2760 goto exit;
2761 }
2762
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002763 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002765 };
2766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002768 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002769 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002770 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 signature, signature_size, signature_length);
2772 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002773
2774 status = psa_driver_wrapper_sign_hash(
2775 &attributes, slot->key.data, slot->key.bytes,
2776 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002778 }
2779
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002780
2781exit:
2782 /* Fill the unused part of the output buffer (the whole buffer on error,
2783 * the trailing part on success) with something that isn't a valid signature
2784 * (barring an attack on the signature and deliberately-crafted input),
2785 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002786 if (status == PSA_SUCCESS) {
2787 memset(signature + *signature_length, '!',
2788 signature_size - *signature_length);
2789 } else {
2790 memset(signature, '!', signature_size);
2791 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002792 /* If signature_size is 0 then we have nothing to do. We must not call
2793 * memset because signature may be NULL in this case. */
2794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002798}
2799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2801 int input_is_message,
2802 psa_algorithm_t alg,
2803 const uint8_t *input,
2804 size_t input_length,
2805 const uint8_t *signature,
2806 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002807{
2808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2809 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2810 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 status = psa_sign_verify_check_alg(input_is_message, alg);
2813 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002814 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002816
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002817 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 key, &slot,
2819 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2820 PSA_KEY_USAGE_VERIFY_HASH,
2821 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 if (status != PSA_SUCCESS) {
2824 return status;
2825 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002826
2827 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002828 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002829 };
2830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002832 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002833 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002834 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 signature, signature_length);
2836 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002837 status = psa_driver_wrapper_verify_hash(
2838 &attributes, slot->key.data, slot->key.bytes,
2839 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002845 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002846
2847}
2848
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002849psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002850 const psa_key_attributes_t *attributes,
2851 const uint8_t *key_buffer,
2852 size_t key_buffer_size,
2853 psa_algorithm_t alg,
2854 const uint8_t *input,
2855 size_t input_length,
2856 uint8_t *signature,
2857 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002859{
2860 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002863 size_t hash_length;
2864 uint8_t hash[PSA_HASH_MAX_SIZE];
2865
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002866 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002867 PSA_ALG_SIGN_GET_HASH(alg),
2868 input, input_length,
2869 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002872 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002873 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002874
2875 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002876 attributes, key_buffer, key_buffer_size,
2877 alg, hash, hash_length,
2878 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002879 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002882}
2883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2885 psa_algorithm_t alg,
2886 const uint8_t *input,
2887 size_t input_length,
2888 uint8_t *signature,
2889 size_t signature_size,
2890 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002891{
2892 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002893 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002894 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002895}
2896
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002897psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002898 const psa_key_attributes_t *attributes,
2899 const uint8_t *key_buffer,
2900 size_t key_buffer_size,
2901 psa_algorithm_t alg,
2902 const uint8_t *input,
2903 size_t input_length,
2904 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002906{
2907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002910 size_t hash_length;
2911 uint8_t hash[PSA_HASH_MAX_SIZE];
2912
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002913 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914 PSA_ALG_SIGN_GET_HASH(alg),
2915 input, input_length,
2916 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002919 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002921
2922 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 attributes, key_buffer, key_buffer_size,
2924 alg, hash, hash_length,
2925 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002926 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002929}
2930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2932 psa_algorithm_t alg,
2933 const uint8_t *input,
2934 size_t input_length,
2935 const uint8_t *signature,
2936 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002937{
2938 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002939 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002940 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002941}
2942
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002943psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002944 const psa_key_attributes_t *attributes,
2945 const uint8_t *key_buffer, size_t key_buffer_size,
2946 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002948{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2950 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2951 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002952#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2954 return mbedtls_psa_rsa_sign_hash(
2955 attributes,
2956 key_buffer, key_buffer_size,
2957 alg, hash, hash_length,
2958 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002959#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2960 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 } else {
2962 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002963 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2965 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002966#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2968 return mbedtls_psa_ecdsa_sign_hash(
2969 attributes,
2970 key_buffer, key_buffer_size,
2971 alg, hash, hash_length,
2972 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002973#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2974 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 } else {
2976 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002977 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002978 }
Ronald Cron4501c982021-03-19 20:09:32 +01002979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002980 (void) key_buffer;
2981 (void) key_buffer_size;
2982 (void) hash;
2983 (void) hash_length;
2984 (void) signature;
2985 (void) signature_size;
2986 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002989}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002991psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2992 psa_algorithm_t alg,
2993 const uint8_t *hash,
2994 size_t hash_length,
2995 uint8_t *signature,
2996 size_t signature_size,
2997 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002998{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002999 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003000 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003002}
3003
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003004psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003005 const psa_key_attributes_t *attributes,
3006 const uint8_t *key_buffer, size_t key_buffer_size,
3007 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003009{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003010 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3011 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3012 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003013#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3015 return mbedtls_psa_rsa_verify_hash(
3016 attributes,
3017 key_buffer, key_buffer_size,
3018 alg, hash, hash_length,
3019 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003020#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3021 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 } else {
3023 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003024 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3026 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003027#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003028 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3029 return mbedtls_psa_ecdsa_verify_hash(
3030 attributes,
3031 key_buffer, key_buffer_size,
3032 alg, hash, hash_length,
3033 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003034#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3035 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003036 } else {
3037 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003038 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003039 }
Ronald Cron4501c982021-03-19 20:09:32 +01003040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041 (void) key_buffer;
3042 (void) key_buffer_size;
3043 (void) hash;
3044 (void) hash_length;
3045 (void) signature;
3046 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003049}
3050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3052 psa_algorithm_t alg,
3053 const uint8_t *hash,
3054 size_t hash_length,
3055 const uint8_t *signature,
3056 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003057{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003058 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003059 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003061}
3062
John Durkop0e005192020-10-31 22:06:54 -07003063#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3065 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003066{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003067 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3068 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3069 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3070 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003071}
John Durkop0e005192020-10-31 22:06:54 -07003072#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3075 psa_algorithm_t alg,
3076 const uint8_t *input,
3077 size_t input_length,
3078 const uint8_t *salt,
3079 size_t salt_length,
3080 uint8_t *output,
3081 size_t output_size,
3082 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003086 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087
Darryl Green5cc689a2018-07-24 15:34:10 +01003088 (void) input;
3089 (void) input_length;
3090 (void) salt;
3091 (void) output;
3092 (void) output_size;
3093
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003094 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3097 return PSA_ERROR_INVALID_ARGUMENT;
3098 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003099
Ronald Cron5c522922020-11-14 16:35:34 +01003100 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003101 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3102 if (status != PSA_SUCCESS) {
3103 return status;
3104 }
3105 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3106 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003107 status = PSA_ERROR_INVALID_ARGUMENT;
3108 goto exit;
3109 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003111 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003112#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003113 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003114 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3116 slot->key.data,
3117 slot->key.bytes,
3118 &rsa);
3119 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003120 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003121 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003124 status = PSA_ERROR_BUFFER_TOO_SMALL;
3125 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003126 }
Ronald Cron7207d572021-09-08 14:28:35 +02003127#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3128 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003130#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003131 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003132 mbedtls_rsa_pkcs1_encrypt(rsa,
3133 mbedtls_psa_get_random,
3134 MBEDTLS_PSA_RANDOM_STATE,
3135 MBEDTLS_RSA_PUBLIC,
3136 input_length,
3137 input,
3138 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003139#else
3140 status = PSA_ERROR_NOT_SUPPORTED;
3141#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003142 } else
3143 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003144#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003146 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003147 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3148 mbedtls_psa_get_random,
3149 MBEDTLS_PSA_RANDOM_STATE,
3150 MBEDTLS_RSA_PUBLIC,
3151 salt, salt_length,
3152 input_length,
3153 input,
3154 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003155#else
3156 status = PSA_ERROR_NOT_SUPPORTED;
3157#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003159 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003160 }
Ronald Cron7207d572021-09-08 14:28:35 +02003161#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003163rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 if (status == PSA_SUCCESS) {
3165 *output_length = mbedtls_rsa_get_len(rsa);
3166 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168 mbedtls_rsa_free(rsa);
3169 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003170#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003171 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003173 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003175
3176exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003177 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003180}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003182psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3183 psa_algorithm_t alg,
3184 const uint8_t *input,
3185 size_t input_length,
3186 const uint8_t *salt,
3187 size_t salt_length,
3188 uint8_t *output,
3189 size_t output_size,
3190 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003193 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003194 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003195
Darryl Green5cc689a2018-07-24 15:34:10 +01003196 (void) input;
3197 (void) input_length;
3198 (void) salt;
3199 (void) output;
3200 (void) output_size;
3201
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003202 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003204 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3205 return PSA_ERROR_INVALID_ARGUMENT;
3206 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003207
Ronald Cron5c522922020-11-14 16:35:34 +01003208 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003209 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3210 if (status != PSA_SUCCESS) {
3211 return status;
3212 }
3213 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003214 status = PSA_ERROR_INVALID_ARGUMENT;
3215 goto exit;
3216 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003221 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3223 slot->key.data,
3224 slot->key.bytes,
3225 &rsa);
3226 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003227 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003231 status = PSA_ERROR_INVALID_ARGUMENT;
3232 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003233 }
Ronald Cron7207d572021-09-08 14:28:35 +02003234#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3235 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003237 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003238#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003239 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003240 mbedtls_rsa_pkcs1_decrypt(rsa,
3241 mbedtls_psa_get_random,
3242 MBEDTLS_PSA_RANDOM_STATE,
3243 MBEDTLS_RSA_PRIVATE,
3244 output_length,
3245 input,
3246 output,
3247 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003248#else
3249 status = PSA_ERROR_NOT_SUPPORTED;
3250#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003251 } else
3252 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003253#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003255 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3257 mbedtls_psa_get_random,
3258 MBEDTLS_PSA_RANDOM_STATE,
3259 MBEDTLS_RSA_PRIVATE,
3260 salt, salt_length,
3261 output_length,
3262 input,
3263 output,
3264 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003265#else
3266 status = PSA_ERROR_NOT_SUPPORTED;
3267#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003268 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003269 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003271
Ronald Cron7207d572021-09-08 14:28:35 +02003272#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003273 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003274rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 mbedtls_rsa_free(rsa);
3276 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003277#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3278 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003280 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003282
3283exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003287}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003288
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003289
3290
mohammad1603503973b2018-03-12 15:59:30 +02003291/****************************************************************/
3292/* Symmetric cryptography */
3293/****************************************************************/
3294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3296 mbedtls_svc_key_id_t key,
3297 psa_algorithm_t alg,
3298 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003299{
3300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3301 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003302 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003303 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003304 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3305 PSA_KEY_USAGE_ENCRYPT :
3306 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003307
3308 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003310 status = PSA_ERROR_BAD_STATE;
3311 goto exit;
3312 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003315 status = PSA_ERROR_INVALID_ARGUMENT;
3316 goto exit;
3317 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3320 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003321 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003323
Ronald Cron49fafa92021-03-10 08:34:23 +01003324 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003325 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003326 * so we only set it (in the driver wrapper) after resources have been
3327 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003328 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003330 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003332 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003334 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 }
3336 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003337
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003338 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003340 };
3341
Ronald Cronab99ac22020-10-01 16:38:28 +02003342 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 if (cipher_operation == MBEDTLS_ENCRYPT) {
3344 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3345 &attributes,
3346 slot->key.data,
3347 slot->key.bytes,
3348 alg);
3349 } else {
3350 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3351 &attributes,
3352 slot->key.data,
3353 slot->key.bytes,
3354 alg);
3355 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003356
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003357exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 if (status != PSA_SUCCESS) {
3359 psa_cipher_abort(operation);
3360 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003364 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003365}
3366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003367psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3368 mbedtls_svc_key_id_t key,
3369 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003370{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003371 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003372}
3373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003374psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3375 mbedtls_svc_key_id_t key,
3376 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003377{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003378 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003379}
3380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3382 uint8_t *iv,
3383 size_t iv_size,
3384 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003385{
Ronald Cron6d051732020-10-01 14:10:20 +02003386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003387 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003388 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003390 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003391 status = PSA_ERROR_BAD_STATE;
3392 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003393 }
3394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003396 status = PSA_ERROR_BAD_STATE;
3397 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003398 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003399
Ronald Cronc423acb2021-07-05 12:31:44 +02003400 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003402 status = PSA_ERROR_BUFFER_TOO_SMALL;
3403 goto exit;
3404 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003406 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003407 status = PSA_ERROR_GENERIC_ERROR;
3408 goto exit;
3409 }
3410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 status = psa_generate_random(local_iv, default_iv_length);
3412 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003413 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 }
Ronald Cron5618a392021-03-26 09:52:26 +01003415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003416 status = psa_driver_wrapper_cipher_set_iv(operation,
3417 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003418
3419exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420 if (status == PSA_SUCCESS) {
3421 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003422 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003423 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003425 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003427 }
Ronald Cron6d051732020-10-01 14:10:20 +02003428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003430}
3431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3433 const uint8_t *iv,
3434 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003435{
Ronald Cron6d051732020-10-01 14:10:20 +02003436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003438 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003439 status = PSA_ERROR_BAD_STATE;
3440 goto exit;
3441 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003443 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003444 status = PSA_ERROR_BAD_STATE;
3445 goto exit;
3446 }
Ronald Crona0d68172021-03-26 10:15:08 +01003447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003449 status = PSA_ERROR_INVALID_ARGUMENT;
3450 goto exit;
3451 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003452
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003453 status = psa_driver_wrapper_cipher_set_iv(operation,
3454 iv,
3455 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003456
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003457exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003459 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 } else {
3461 psa_cipher_abort(operation);
3462 }
3463 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003464}
3465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3467 const uint8_t *input,
3468 size_t input_length,
3469 uint8_t *output,
3470 size_t output_size,
3471 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003472{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003475 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003476 status = PSA_ERROR_BAD_STATE;
3477 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003478 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003481 status = PSA_ERROR_BAD_STATE;
3482 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003483 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003485 status = psa_driver_wrapper_cipher_update(operation,
3486 input,
3487 input_length,
3488 output,
3489 output_size,
3490 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003491
3492exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 if (status != PSA_SUCCESS) {
3494 psa_cipher_abort(operation);
3495 }
Ronald Cron6d051732020-10-01 14:10:20 +02003496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003497 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003498}
3499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3501 uint8_t *output,
3502 size_t output_size,
3503 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003504{
David Saadab4ecc272019-02-14 13:48:10 +02003505 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003508 status = PSA_ERROR_BAD_STATE;
3509 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003510 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003512 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513 status = PSA_ERROR_BAD_STATE;
3514 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003515 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003517 status = psa_driver_wrapper_cipher_finish(operation,
3518 output,
3519 output_size,
3520 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003521
3522exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003523 if (status == PSA_SUCCESS) {
3524 return psa_cipher_abort(operation);
3525 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003526 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003530 }
mohammad1603503973b2018-03-12 15:59:30 +02003531}
3532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003534{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003536 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003537 * in use. It's ok to call abort on such an object, and there's
3538 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003540 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003542 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003543
Ronald Cron49fafa92021-03-10 08:34:23 +01003544 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003545 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003546 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003548 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003549}
3550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3552 psa_algorithm_t alg,
3553 const uint8_t *input,
3554 size_t input_length,
3555 uint8_t *output,
3556 size_t output_size,
3557 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003558{
3559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003560 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003561 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003562 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003563 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3564 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003566 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003567 status = PSA_ERROR_INVALID_ARGUMENT;
3568 goto exit;
3569 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003571 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3572 PSA_KEY_USAGE_ENCRYPT,
3573 alg);
3574 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003575 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003576 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003577
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003578 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003580 };
3581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3583 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003584 status = PSA_ERROR_GENERIC_ERROR;
3585 goto exit;
3586 }
3587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003588 if (default_iv_length > 0) {
3589 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003590 status = PSA_ERROR_BUFFER_TOO_SMALL;
3591 goto exit;
3592 }
3593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 status = psa_generate_random(local_iv, default_iv_length);
3595 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003596 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003598 }
3599
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003600 status = psa_driver_wrapper_cipher_encrypt(
3601 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003602 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 mbedtls_buffer_offset(output, default_iv_length),
3604 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003605
3606exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003607 unlock_status = psa_unlock_key_slot(slot);
3608 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003609 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003610 }
Ronald Cron16377072021-07-08 19:00:07 +02003611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612 if (status == PSA_SUCCESS) {
3613 if (default_iv_length > 0) {
3614 memcpy(output, local_iv, default_iv_length);
3615 }
3616 *output_length += default_iv_length;
3617 } else {
3618 *output_length = 0;
3619 }
3620
3621 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003622}
3623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003624psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3625 psa_algorithm_t alg,
3626 const uint8_t *input,
3627 size_t input_length,
3628 uint8_t *output,
3629 size_t output_size,
3630 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003631{
3632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003633 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003634 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003635 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003637 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003638 status = PSA_ERROR_INVALID_ARGUMENT;
3639 goto exit;
3640 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3643 PSA_KEY_USAGE_DECRYPT,
3644 alg);
3645 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003646 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003648
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003649 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003651 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003654 status = PSA_ERROR_INVALID_ARGUMENT;
3655 goto exit;
3656 }
3657
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003658 status = psa_driver_wrapper_cipher_decrypt(
3659 &attributes, slot->key.data, slot->key.bytes,
3660 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003662
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003663exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 unlock_status = psa_unlock_key_slot(slot);
3665 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003666 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003667 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003670 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003671 }
Ronald Cron16377072021-07-08 19:00:07 +02003672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003674}
3675
3676
mohammad16035955c982018-04-26 00:53:03 +03003677/****************************************************************/
3678/* AEAD */
3679/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003681psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3682 psa_algorithm_t alg,
3683 const uint8_t *nonce,
3684 size_t nonce_length,
3685 const uint8_t *additional_data,
3686 size_t additional_data_length,
3687 const uint8_t *plaintext,
3688 size_t plaintext_length,
3689 uint8_t *ciphertext,
3690 size_t ciphertext_size,
3691 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003692{
Ronald Cron215633c2021-03-16 17:15:37 +01003693 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3694 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003695
mohammad1603f08a5502018-06-03 15:05:47 +03003696 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3699 return PSA_ERROR_NOT_SUPPORTED;
3700 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003701
Ronald Cron9a986162021-03-26 12:40:07 +01003702 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003703 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3704 if (status != PSA_SUCCESS) {
3705 return status;
3706 }
mohammad16035955c982018-04-26 00:53:03 +03003707
Ronald Cron215633c2021-03-16 17:15:37 +01003708 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003710 };
mohammad16035955c982018-04-26 00:53:03 +03003711
Ronald Cronde822812021-03-17 16:08:20 +01003712 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003713 &attributes, slot->key.data, slot->key.bytes,
3714 alg,
3715 nonce, nonce_length,
3716 additional_data, additional_data_length,
3717 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3721 memset(ciphertext, 0, ciphertext_size);
3722 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003724 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003727}
3728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3730 psa_algorithm_t alg,
3731 const uint8_t *nonce,
3732 size_t nonce_length,
3733 const uint8_t *additional_data,
3734 size_t additional_data_length,
3735 const uint8_t *ciphertext,
3736 size_t ciphertext_length,
3737 uint8_t *plaintext,
3738 size_t plaintext_size,
3739 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003740{
Ronald Cron215633c2021-03-16 17:15:37 +01003741 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3742 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003743
Gilles Peskineee652a32018-06-01 19:23:52 +02003744 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003745
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3747 return PSA_ERROR_NOT_SUPPORTED;
3748 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003749
Ronald Cron9a986162021-03-26 12:40:07 +01003750 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003751 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3752 if (status != PSA_SUCCESS) {
3753 return status;
3754 }
mohammad16035955c982018-04-26 00:53:03 +03003755
Ronald Cron215633c2021-03-16 17:15:37 +01003756 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003757 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003758 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003759
Ronald Cronde822812021-03-17 16:08:20 +01003760 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003761 &attributes, slot->key.data, slot->key.bytes,
3762 alg,
3763 nonce, nonce_length,
3764 additional_data, additional_data_length,
3765 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003766 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003768 if (status != PSA_SUCCESS && plaintext_size != 0) {
3769 memset(plaintext, 0, plaintext_size);
3770 }
mohammad160360a64d02018-06-03 17:20:42 +03003771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003772 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 return status;
mohammad16035955c982018-04-26 00:53:03 +03003775}
3776
Gilles Peskinee59236f2018-01-27 23:32:46 +01003777/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003778/* Generators */
3779/****************************************************************/
3780
John Durkop07cc04a2020-11-16 22:08:34 -08003781#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3782 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3783 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3784#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003785#endif /* At least one builtin KDF */
3786
3787#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3788 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3789 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3790static psa_status_t psa_key_derivation_start_hmac(
3791 psa_mac_operation_t *operation,
3792 psa_algorithm_t hash_alg,
3793 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003795{
3796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3799 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003801
Steven Cooreman15f0d922021-05-07 14:14:37 +02003802 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 status = psa_driver_wrapper_mac_sign_setup(operation,
3806 &attributes,
3807 hmac_key, hmac_key_length,
3808 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 psa_reset_key_attributes(&attributes);
3811 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003812}
3813#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003814
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003815#define HKDF_STATE_INIT 0 /* no input yet */
3816#define HKDF_STATE_STARTED 1 /* got salt */
3817#define HKDF_STATE_KEYED 2 /* got key */
3818#define HKDF_STATE_OUTPUT 3 /* output started */
3819
Gilles Peskinecbe66502019-05-16 16:59:18 +02003820static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003822{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3824 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3825 } else {
3826 return operation->alg;
3827 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003828}
3829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003830psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003831{
3832 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3834 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003835 /* The object has (apparently) been initialized but it is not
3836 * in use. It's ok to call abort on such an object, and there's
3837 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 } else
John Durkopd0321952020-10-29 21:37:36 -07003839#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3841 mbedtls_free(operation->ctx.hkdf.info);
3842 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3843 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003844#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3846 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003847 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3848 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3849 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3850 if (operation->ctx.tls12_prf.secret != NULL) {
3851 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3852 operation->ctx.tls12_prf.secret_length);
3853 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003854 }
3855
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003856 if (operation->ctx.tls12_prf.seed != NULL) {
3857 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3858 operation->ctx.tls12_prf.seed_length);
3859 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003860 }
3861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 if (operation->ctx.tls12_prf.label != NULL) {
3863 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3864 operation->ctx.tls12_prf.label_length);
3865 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003866 }
3867
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003868 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003869
3870 /* We leave the fields Ai and output_block to be erased safely by the
3871 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003873#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3874 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003875 {
3876 status = PSA_ERROR_BAD_STATE;
3877 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878 mbedtls_platform_zeroize(operation, sizeof(*operation));
3879 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003880}
3881
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003882psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003884{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003885 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003886 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003888 }
3889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003890 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003892}
3893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3895 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003896{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003897 if (operation->alg == 0) {
3898 return PSA_ERROR_BAD_STATE;
3899 }
3900 if (capacity > operation->capacity) {
3901 return PSA_ERROR_INVALID_ARGUMENT;
3902 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003903 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003905}
3906
John Durkopd0321952020-10-29 21:37:36 -07003907#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003908/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003909 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3911 psa_algorithm_t hash_alg,
3912 uint8_t *output,
3913 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003914{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003916 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917 psa_status_t status;
3918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003919 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3920 return PSA_ERROR_BAD_STATE;
3921 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003922 hkdf->state = HKDF_STATE_OUTPUT;
3923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003925 /* Copy what remains of the current block */
3926 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003928 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 }
3930 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003931 output += n;
3932 output_length -= n;
3933 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003935 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003936 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003937 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003938 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003939 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003940 * object was corrupted or if this function is called directly
3941 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 if (hkdf->block_number == 0xff) {
3943 return PSA_ERROR_BAD_STATE;
3944 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003945
3946 /* We need a new block */
3947 ++hkdf->block_number;
3948 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3951 hash_alg,
3952 hkdf->prk,
3953 hash_length);
3954 if (status != PSA_SUCCESS) {
3955 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003956 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003957
3958 if (hkdf->block_number != 1) {
3959 status = psa_mac_update(&hkdf->hmac,
3960 hkdf->output_block,
3961 hash_length);
3962 if (status != PSA_SUCCESS) {
3963 return status;
3964 }
3965 }
3966 status = psa_mac_update(&hkdf->hmac,
3967 hkdf->info,
3968 hkdf->info_length);
3969 if (status != PSA_SUCCESS) {
3970 return status;
3971 }
3972 status = psa_mac_update(&hkdf->hmac,
3973 &hkdf->block_number, 1);
3974 if (status != PSA_SUCCESS) {
3975 return status;
3976 }
3977 status = psa_mac_sign_finish(&hkdf->hmac,
3978 hkdf->output_block,
3979 sizeof(hkdf->output_block),
3980 &hmac_output_length);
3981 if (status != PSA_SUCCESS) {
3982 return status;
3983 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003984 }
3985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003986 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003987}
John Durkop07cc04a2020-11-16 22:08:34 -08003988#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003989
John Durkop07cc04a2020-11-16 22:08:34 -08003990#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3991 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003992static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3993 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003994 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01003995{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003996 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
3997 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003998 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
3999 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004000 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004001
Janos Follath7742fee2019-06-17 12:58:10 +01004002 /* We can't be wanting more output after block 0xff, otherwise
4003 * the capacity check in psa_key_derivation_output_bytes() would have
4004 * prevented this call. It could happen only if the operation
4005 * object was corrupted or if this function is called directly
4006 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 if (tls12_prf->block_number == 0xff) {
4008 return PSA_ERROR_CORRUPTION_DETECTED;
4009 }
Janos Follath7742fee2019-06-17 12:58:10 +01004010
4011 /* We need a new block */
4012 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004013 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004014
4015 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4016 *
4017 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4018 *
4019 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4020 * HMAC_hash(secret, A(2) + seed) +
4021 * HMAC_hash(secret, A(3) + seed) + ...
4022 *
4023 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004024 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004025 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004026 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004027 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004028 * is currently extracted as `output_block` and where i is
4029 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004030 */
4031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004032 status = psa_key_derivation_start_hmac(&hmac,
4033 hash_alg,
4034 tls12_prf->secret,
4035 tls12_prf->secret_length);
4036 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004037 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004038 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004039
4040 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004042 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4043 * the variable seed and in this instance means it in the context of the
4044 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004045 status = psa_mac_update(&hmac,
4046 tls12_prf->label,
4047 tls12_prf->label_length);
4048 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004049 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 }
4051 status = psa_mac_update(&hmac,
4052 tls12_prf->seed,
4053 tls12_prf->seed_length);
4054 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004055 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056 }
4057 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004058 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004059 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4060 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004061 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004062 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004063 }
4064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065 status = psa_mac_sign_finish(&hmac,
4066 tls12_prf->Ai, hash_length,
4067 &hmac_output_length);
4068 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004069 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 }
4071 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004072 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004073 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004074
4075 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004076 status = psa_key_derivation_start_hmac(&hmac,
4077 hash_alg,
4078 tls12_prf->secret,
4079 tls12_prf->secret_length);
4080 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004081 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004082 }
4083 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4084 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004085 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004086 }
4087 status = psa_mac_update(&hmac, tls12_prf->label, 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, tls12_prf->seed, tls12_prf->seed_length);
4092 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004093 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 }
4095 status = psa_mac_sign_finish(&hmac,
4096 tls12_prf->output_block, hash_length,
4097 &hmac_output_length);
4098 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004099 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004100 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004101
Janos Follath7742fee2019-06-17 12:58:10 +01004102
4103cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 cleanup_status = psa_mac_abort(&hmac);
4105 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004106 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004107 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004110}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004111
Janos Follath844eb0e2019-06-19 12:10:49 +01004112static psa_status_t psa_key_derivation_tls12_prf_read(
4113 psa_tls12_prf_key_derivation_t *tls12_prf,
4114 psa_algorithm_t alg,
4115 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004116 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004118 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4119 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004120 psa_status_t status;
4121 uint8_t offset, length;
4122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004124 case PSA_TLS12_PRF_STATE_LABEL_SET:
4125 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4126 break;
4127 case PSA_TLS12_PRF_STATE_OUTPUT:
4128 break;
4129 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004130 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004131 }
4132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004134 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004135 if (tls12_prf->left_in_block == 0) {
4136 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4137 alg);
4138 if (status != PSA_SUCCESS) {
4139 return status;
4140 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004141
4142 continue;
4143 }
4144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004145 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004146 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004148 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004150
4151 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004153 output += length;
4154 output_length -= length;
4155 tls12_prf->left_in_block -= length;
4156 }
4157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004159}
John Durkop07cc04a2020-11-16 22:08:34 -08004160#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4161 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004162
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004163psa_status_t psa_key_derivation_output_bytes(
4164 psa_key_derivation_operation_t *operation,
4165 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004167{
4168 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004169 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004174 }
4175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004178 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004180 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004181 goto exit;
4182 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004185 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4187 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004188 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004193
John Durkopd0321952020-10-29 21:37:36 -07004194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004195 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4196 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4197 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4198 output, output_length);
4199 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004200#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4201#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4202 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4204 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4205 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4206 kdf_alg, output,
4207 output_length);
4208 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004209#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4210 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004211 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004212 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004213 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004214 }
4215
4216exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004217 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004218 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 * This allows us to differentiate between exhausted operations and
4220 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4221 * operations. */
4222 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004223 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004226 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004227 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004228}
4229
David Brown7807bf72021-02-09 16:28:23 -07004230#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004231static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004232{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233 if (data_size >= 8) {
4234 mbedtls_des_key_set_parity(data);
4235 }
4236 if (data_size >= 16) {
4237 mbedtls_des_key_set_parity(data + 8);
4238 }
4239 if (data_size >= 24) {
4240 mbedtls_des_key_set_parity(data + 16);
4241 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004242}
David Brown7807bf72021-02-09 16:28:23 -07004243#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004244
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004245static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004246 psa_key_slot_t *slot,
4247 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004248 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004249{
4250 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004251 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004252 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004253 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (!key_type_is_raw_bytes(slot->attr.type)) {
4256 return PSA_ERROR_INVALID_ARGUMENT;
4257 }
4258 if (bits % 8 != 0) {
4259 return PSA_ERROR_INVALID_ARGUMENT;
4260 }
4261 data = mbedtls_calloc(1, bytes);
4262 if (data == NULL) {
4263 return PSA_ERROR_INSUFFICIENT_MEMORY;
4264 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 status = psa_key_derivation_output_bytes(operation, data, bytes);
4267 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004268 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 }
David Brown12ca5032021-02-11 11:02:00 -07004270#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4272 psa_des_set_key_parity(data, bytes);
4273 }
David Brown8107e312021-02-12 08:08:46 -07004274#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004276 status = psa_allocate_buffer_to_slot(slot, bytes);
4277 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004278 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 }
Ronald Crondd04d422020-11-28 15:14:42 +01004280
Ronald Cronbf33c932020-11-28 18:06:53 +01004281 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004282 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004284 };
4285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 status = psa_driver_wrapper_import_key(&attributes,
4287 data, bytes,
4288 slot->key.data,
4289 slot->key.bytes,
4290 &slot->key.bytes, &bits);
4291 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004292 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004294
4295exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 mbedtls_free(data);
4297 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004298}
4299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4301 psa_key_derivation_operation_t *operation,
4302 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004303{
4304 psa_status_t status;
4305 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004306 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004307
Ronald Cron81709fc2020-11-14 12:10:32 +01004308 *key = MBEDTLS_SVC_KEY_ID_INIT;
4309
Gilles Peskine0f84d622019-09-12 19:03:13 +02004310 /* Reject any attempt to create a zero-length key so that we don't
4311 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 if (psa_get_key_bits(attributes) == 0) {
4313 return PSA_ERROR_INVALID_ARGUMENT;
4314 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 if (operation->alg == PSA_ALG_NONE) {
4317 return PSA_ERROR_BAD_STATE;
4318 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 if (!operation->can_output_key) {
4321 return PSA_ERROR_NOT_PERMITTED;
4322 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4325 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004328 /* Deriving a key in a secure element is not implemented yet. */
4329 status = PSA_ERROR_NOT_SUPPORTED;
4330 }
4331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 if (status == PSA_SUCCESS) {
4333 status = psa_generate_derived_key_internal(slot,
4334 attributes->core.bits,
4335 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004336 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337 if (status == PSA_SUCCESS) {
4338 status = psa_finish_key_creation(slot, driver, key);
4339 }
4340 if (status != PSA_SUCCESS) {
4341 psa_fail_key_creation(slot, driver);
4342 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004345}
4346
Gilles Peskineeab56e42018-07-12 17:12:33 +02004347
4348
4349/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004350/* Key derivation */
4351/****************************************************************/
4352
Steven Cooreman932ffb72021-02-15 12:14:32 +01004353#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004355{
4356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4358 return 1;
4359 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004360#endif
4361#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4363 return 1;
4364 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004365#endif
4366#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4368 return 1;
4369 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004370#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004372}
4373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004375{
4376 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004377 psa_status_t status = psa_hash_setup(&operation, alg);
4378 psa_hash_abort(&operation);
4379 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004380}
4381
Gilles Peskine969c5d62019-01-16 15:53:06 +01004382static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004385{
Janos Follath5fe19732019-06-20 15:09:30 +01004386 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4387 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004389
Gilles Peskine969c5d62019-01-16 15:53:06 +01004390 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 if (!is_kdf_alg_supported(kdf_alg)) {
4392 return PSA_ERROR_NOT_SUPPORTED;
4393 }
John Durkop07cc04a2020-11-16 22:08:34 -08004394
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004395 /* All currently supported key derivation algorithms are based on a
4396 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004397 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4398 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4399 if (hash_size == 0) {
4400 return PSA_ERROR_NOT_SUPPORTED;
4401 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004402
4403 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4404 * we might fail later, which is somewhat unfriendly and potentially
4405 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406 psa_status_t status = psa_hash_try_support(hash_alg);
4407 if (status != PSA_SUCCESS) {
4408 return status;
4409 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4412 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4413 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4414 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004416
Gilles Peskinecdacf042021-04-29 21:10:00 +02004417 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004419}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004422{
4423#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 if (alg == PSA_ALG_ECDH) {
4425 return PSA_SUCCESS;
4426 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004427#endif
4428 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004430}
John Durkop07cc04a2020-11-16 22:08:34 -08004431#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4434 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435{
4436 psa_status_t status;
4437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 if (operation->alg != 0) {
4439 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4443 return PSA_ERROR_INVALID_ARGUMENT;
4444 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4445#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4446 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4447 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4448 status = psa_key_agreement_try_support(ka_alg);
4449 if (status != PSA_SUCCESS) {
4450 return status;
4451 }
4452 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4453#else
4454 return PSA_ERROR_NOT_SUPPORTED;
4455#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4456 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4457#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4458 status = psa_key_derivation_setup_kdf(operation, alg);
4459#else
4460 return PSA_ERROR_NOT_SUPPORTED;
4461#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4462 } else {
4463 return PSA_ERROR_INVALID_ARGUMENT;
4464 }
4465
4466 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004468 }
4469 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004470}
4471
John Durkopd0321952020-10-29 21:37:36 -07004472#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4474 psa_algorithm_t hash_alg,
4475 psa_key_derivation_step_t step,
4476 const uint8_t *data,
4477 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478{
4479 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004481 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 if (hkdf->state != HKDF_STATE_INIT) {
4483 return PSA_ERROR_BAD_STATE;
4484 } else {
4485 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4486 hash_alg,
4487 data, data_length);
4488 if (status != PSA_SUCCESS) {
4489 return status;
4490 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004491 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004493 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004494 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 if (hkdf->state == HKDF_STATE_INIT) {
4497 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4498 hash_alg,
4499 NULL, 0);
4500 if (status != PSA_SUCCESS) {
4501 return status;
4502 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004503 hkdf->state = HKDF_STATE_STARTED;
4504 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (hkdf->state != HKDF_STATE_STARTED) {
4506 return PSA_ERROR_BAD_STATE;
4507 }
4508 status = psa_mac_update(&hkdf->hmac,
4509 data, data_length);
4510 if (status != PSA_SUCCESS) {
4511 return status;
4512 }
4513 status = psa_mac_sign_finish(&hkdf->hmac,
4514 hkdf->prk,
4515 sizeof(hkdf->prk),
4516 &data_length);
4517 if (status != PSA_SUCCESS) {
4518 return status;
4519 }
4520 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004521 hkdf->block_number = 0;
4522 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004524 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 if (hkdf->state == HKDF_STATE_OUTPUT) {
4526 return PSA_ERROR_BAD_STATE;
4527 }
4528 if (hkdf->info_set) {
4529 return PSA_ERROR_BAD_STATE;
4530 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004531 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 if (data_length != 0) {
4533 hkdf->info = mbedtls_calloc(1, data_length);
4534 if (hkdf->info == NULL) {
4535 return PSA_ERROR_INSUFFICIENT_MEMORY;
4536 }
4537 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004538 }
4539 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004541 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004543 }
4544}
John Durkop07cc04a2020-11-16 22:08:34 -08004545#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004546
John Durkop07cc04a2020-11-16 22:08:34 -08004547#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4548 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4550 const uint8_t *data,
4551 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004552{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4554 return PSA_ERROR_BAD_STATE;
4555 }
Janos Follathf08e2652019-06-13 09:05:41 +01004556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 if (data_length != 0) {
4558 prf->seed = mbedtls_calloc(1, data_length);
4559 if (prf->seed == NULL) {
4560 return PSA_ERROR_INSUFFICIENT_MEMORY;
4561 }
Janos Follathf08e2652019-06-13 09:05:41 +01004562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004564 prf->seed_length = data_length;
4565 }
Janos Follathf08e2652019-06-13 09:05:41 +01004566
Gilles Peskine43f958b2020-12-13 14:55:14 +01004567 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004569 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004570}
4571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4573 const uint8_t *data,
4574 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004575{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4577 return PSA_ERROR_BAD_STATE;
4578 }
Janos Follath81550542019-06-13 14:26:34 +01004579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (data_length != 0) {
4581 prf->secret = mbedtls_calloc(1, data_length);
4582 if (prf->secret == NULL) {
4583 return PSA_ERROR_INSUFFICIENT_MEMORY;
4584 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004586 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004587 prf->secret_length = data_length;
4588 }
Janos Follath81550542019-06-13 14:26:34 +01004589
Gilles Peskine43f958b2020-12-13 14:55:14 +01004590 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004593}
4594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4596 const uint8_t *data,
4597 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004598{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4600 return PSA_ERROR_BAD_STATE;
4601 }
Janos Follath63028dd2019-06-13 09:15:47 +01004602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 if (data_length != 0) {
4604 prf->label = mbedtls_calloc(1, data_length);
4605 if (prf->label == NULL) {
4606 return PSA_ERROR_INSUFFICIENT_MEMORY;
4607 }
Janos Follath63028dd2019-06-13 09:15:47 +01004608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004610 prf->label_length = data_length;
4611 }
Janos Follath63028dd2019-06-13 09:15:47 +01004612
Gilles Peskine43f958b2020-12-13 14:55:14 +01004613 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004616}
4617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4619 psa_key_derivation_step_t step,
4620 const uint8_t *data,
4621 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004624 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004626 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004628 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004630 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004632 }
4633}
John Durkop07cc04a2020-11-16 22:08:34 -08004634#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4635 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4636
4637#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4638static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4639 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004640 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004642{
4643 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004644 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004645 uint8_t *cur = pms;
4646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4648 return PSA_ERROR_INVALID_ARGUMENT;
4649 }
John Durkop07cc04a2020-11-16 22:08:34 -08004650
4651 /* Quoting RFC 4279, Section 2:
4652 *
4653 * The premaster secret is formed as follows: if the PSK is N octets
4654 * long, concatenate a uint16 with the value N, N zero octets, a second
4655 * uint16 with the value N, and the PSK itself.
4656 */
4657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 *cur++ = MBEDTLS_BYTE_1(data_length);
4659 *cur++ = MBEDTLS_BYTE_0(data_length);
4660 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004661 cur += data_length;
4662 *cur++ = pms[0];
4663 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004665 cur += data_length;
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 mbedtls_platform_zeroize(pms, sizeof(pms));
4670 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004671}
Janos Follath6660f0e2019-06-17 08:44:03 +01004672
4673static psa_status_t psa_tls12_prf_psk_to_ms_input(
4674 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004675 psa_key_derivation_step_t step,
4676 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004678{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004679 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4680 return psa_tls12_prf_psk_to_ms_set_key(prf,
4681 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004682 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004685}
John Durkop07cc04a2020-11-16 22:08:34 -08004686#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004687
Gilles Peskineb8965192019-09-24 16:21:10 +02004688/** Check whether the given key type is acceptable for the given
4689 * input step of a key derivation.
4690 *
4691 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4692 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4693 * Both secret and non-secret inputs can alternatively have the type
4694 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4695 * that the input was passed as a buffer rather than via a key object.
4696 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004697static int psa_key_derivation_check_input_type(
4698 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004700{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004702 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 if (key_type == PSA_KEY_TYPE_DERIVE) {
4704 return PSA_SUCCESS;
4705 }
4706 if (key_type == PSA_KEY_TYPE_NONE) {
4707 return PSA_SUCCESS;
4708 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004709 break;
4710 case PSA_KEY_DERIVATION_INPUT_LABEL:
4711 case PSA_KEY_DERIVATION_INPUT_SALT:
4712 case PSA_KEY_DERIVATION_INPUT_INFO:
4713 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4715 return PSA_SUCCESS;
4716 }
4717 if (key_type == PSA_KEY_TYPE_NONE) {
4718 return PSA_SUCCESS;
4719 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004720 break;
4721 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004722 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004723}
4724
Janos Follathb80a94e2019-06-12 15:54:46 +01004725static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004727 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004728 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004729 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004731{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004732 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004733 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735 status = psa_key_derivation_check_input_type(step, key_type);
4736 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004737 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004739
John Durkopd0321952020-10-29 21:37:36 -07004740#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4742 status = psa_hkdf_input(&operation->ctx.hkdf,
4743 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4744 step, data, data_length);
4745 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004746#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4747#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004748 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4749 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4750 step, data, data_length);
4751 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004752#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4753#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4755 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4756 step, data, data_length);
4757 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004758#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004759 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004761 (void) data;
4762 (void) data_length;
4763 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765 }
4766
Gilles Peskine224b0d62019-09-23 18:13:17 +02004767exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 if (status != PSA_SUCCESS) {
4769 psa_key_derivation_abort(operation);
4770 }
4771 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772}
4773
Janos Follath51f4a0f2019-06-14 11:35:55 +01004774psa_status_t psa_key_derivation_input_bytes(
4775 psa_key_derivation_operation_t *operation,
4776 psa_key_derivation_step_t step,
4777 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004779{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 return psa_key_derivation_input_internal(operation, step,
4781 PSA_KEY_TYPE_NONE,
4782 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004783}
4784
Janos Follath51f4a0f2019-06-14 11:35:55 +01004785psa_status_t psa_key_derivation_input_key(
4786 psa_key_derivation_operation_t *operation,
4787 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004789{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004791 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004793
Ronald Cron5c522922020-11-14 16:35:34 +01004794 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4796 if (status != PSA_SUCCESS) {
4797 psa_key_derivation_abort(operation);
4798 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004799 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004800
4801 /* Passing a key object as a SECRET input unlocks the permission
4802 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004804 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 status = psa_key_derivation_input_internal(operation,
4808 step, slot->attr.type,
4809 slot->key.data,
4810 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004815}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004816
4817
4818
4819/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004820/* Key agreement */
4821/****************************************************************/
4822
John Durkop6ba40d12020-11-10 08:50:04 -08004823#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4825 size_t peer_key_length,
4826 const mbedtls_ecp_keypair *our_key,
4827 uint8_t *shared_secret,
4828 size_t shared_secret_size,
4829 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004830{
Steven Cooremand4867872020-08-05 16:31:39 +02004831 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004832 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004833 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004834 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4836 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004837
Ronald Cron90857082020-11-25 14:58:33 +01004838 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4840 bits,
4841 peer_key,
4842 peer_key_length,
4843 &their_key);
4844 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004845 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004846 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004847
Jaeden Amero97271b32019-01-10 19:38:51 +00004848 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4850 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004851 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004853 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4855 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004856 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004858
Jaeden Amero97271b32019-01-10 19:38:51 +00004859 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 mbedtls_ecdh_calc_secret(&ecdh,
4861 shared_secret_length,
4862 shared_secret, shared_secret_size,
4863 mbedtls_psa_get_random,
4864 MBEDTLS_PSA_RANDOM_STATE));
4865 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004866 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 }
4868 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004869 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004871
4872exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 if (status != PSA_SUCCESS) {
4874 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4875 }
4876 mbedtls_ecdh_free(&ecdh);
4877 mbedtls_ecp_keypair_free(their_key);
4878 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004881}
John Durkop6ba40d12020-11-10 08:50:04 -08004882#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004883
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4887 psa_key_slot_t *private_key,
4888 const uint8_t *peer_key,
4889 size_t peer_key_length,
4890 uint8_t *shared_secret,
4891 size_t shared_secret_size,
4892 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004893{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004894 mbedtls_ecp_keypair *ecp = NULL;
4895 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004898#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004899 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4901 return PSA_ERROR_INVALID_ARGUMENT;
4902 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004903 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004904 private_key->attr.type,
4905 private_key->attr.bits,
4906 private_key->key.data,
4907 private_key->key.bytes,
4908 &ecp);
4909 if (status != PSA_SUCCESS) {
4910 return status;
4911 }
4912 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4913 ecp,
4914 shared_secret, shared_secret_size,
4915 shared_secret_length);
4916 mbedtls_ecp_keypair_free(ecp);
4917 mbedtls_free(ecp);
4918 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004919#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004920 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004921 (void) ecp;
4922 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004923 (void) private_key;
4924 (void) peer_key;
4925 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004926 (void) shared_secret;
4927 (void) shared_secret_size;
4928 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004931}
4932
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004933/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004934 * to potentially free embedded data structures and wipe confidential data.
4935 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004936static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4937 psa_key_derivation_step_t step,
4938 psa_key_slot_t *private_key,
4939 const uint8_t *peer_key,
4940 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004941{
4942 psa_status_t status;
4943 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4944 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004946
4947 /* Step 1: run the secret agreement algorithm to generate the shared
4948 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949 status = psa_key_agreement_raw_internal(ka_alg,
4950 private_key,
4951 peer_key, peer_key_length,
4952 shared_secret,
4953 sizeof(shared_secret),
4954 &shared_secret_length);
4955 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02004956 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02004958
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004959 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004960 * the shared secret. A shared secret is permitted wherever a key
4961 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962 status = psa_key_derivation_input_internal(operation, step,
4963 PSA_KEY_TYPE_DERIVE,
4964 shared_secret,
4965 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02004966exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
4968 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004969}
4970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004971psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
4972 psa_key_derivation_step_t step,
4973 mbedtls_svc_key_id_t private_key,
4974 const uint8_t *peer_key,
4975 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02004976{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004977 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004978 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004979 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4982 return PSA_ERROR_INVALID_ARGUMENT;
4983 }
Ronald Cron5c522922020-11-14 16:35:34 +01004984 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004985 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4986 if (status != PSA_SUCCESS) {
4987 return status;
4988 }
4989 status = psa_key_agreement_internal(operation, step,
4990 slot,
4991 peer_key, peer_key_length);
4992 if (status != PSA_SUCCESS) {
4993 psa_key_derivation_abort(operation);
4994 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004995 /* If a private key has been added as SECRET, we allow the derived
4996 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004997 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004998 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005000 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005004 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005005}
5006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5008 mbedtls_svc_key_id_t private_key,
5009 const uint8_t *peer_key,
5010 size_t peer_key_length,
5011 uint8_t *output,
5012 size_t output_size,
5013 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005014{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005016 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005017 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005018 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005021 status = PSA_ERROR_INVALID_ARGUMENT;
5022 goto exit;
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, alg);
5026 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005027 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005029
Gilles Peskine42313fb2022-04-14 00:17:15 +02005030 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5031 * for the output size. The PSA specification only guarantees that this
5032 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5033 * but it might be nice to allow smaller buffers if the output fits.
5034 * At the time of writing this comment, with only ECDH implemented,
5035 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5036 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5037 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005038 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5040 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005041 status = PSA_ERROR_BUFFER_TOO_SMALL;
5042 goto exit;
5043 }
5044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005045 status = psa_key_agreement_raw_internal(alg, slot,
5046 peer_key, peer_key_length,
5047 output, output_size,
5048 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005049
5050exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005052 /* If an error happens and is not handled properly, the output
5053 * may be used as a key to protect sensitive data. Arrange for such
5054 * a key to be random, which is likely to result in decryption or
5055 * verification errors. This is better than filling the buffer with
5056 * some constant data such as zeros, which would result in the data
5057 * being protected with a reproducible, easily knowable key.
5058 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005060 *output_length = output_size;
5061 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005063 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005065 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005066}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005067
5068
Gilles Peskine30524eb2020-11-13 17:02:26 +01005069
Gilles Peskine86a440b2018-11-12 18:39:40 +01005070/****************************************************************/
5071/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005072/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005073
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005074#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5075#include "mbedtls/entropy_poll.h"
5076#endif
5077
Gilles Peskine30524eb2020-11-13 17:02:26 +01005078/** Initialize the PSA random generator.
5079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005080static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005081{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005082#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005084#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5085
Gilles Peskine30524eb2020-11-13 17:02:26 +01005086 /* Set default configuration if
5087 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005089 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005090 }
5091 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005092 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005096#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5097 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5098 /* The PSA entropy injection feature depends on using NV seed as an entropy
5099 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 mbedtls_entropy_add_source(&rng->entropy,
5101 mbedtls_nv_seed_poll, NULL,
5102 MBEDTLS_ENTROPY_BLOCK_SIZE,
5103 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005104#endif
5105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005106 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005107#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005108}
5109
5110/** Deinitialize the PSA random generator.
5111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005113{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005114#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005115 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005116#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5118 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005119#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005120}
5121
5122/** Seed the PSA random generator.
5123 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005125{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005126#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5127 /* Do nothing: the external RNG seeds itself. */
5128 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005130#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005131 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5133 drbg_seed, sizeof(drbg_seed) - 1);
5134 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005135#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005136}
5137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005138psa_status_t psa_generate_random(uint8_t *output,
5139 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005140{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005141 GUARD_MODULE_INITIALIZED;
5142
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005143#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5144
5145 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5147 output, output_size,
5148 &output_length);
5149 if (status != PSA_SUCCESS) {
5150 return status;
5151 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005152 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005153 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 if (output_length != output_size) {
5155 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5156 }
5157 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005158
5159#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005162 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5164 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5165 output_size);
5166 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5167 output, request_size);
5168 if (ret != 0) {
5169 return mbedtls_to_psa_error(ret);
5170 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005171 output_size -= request_size;
5172 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005173 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005174 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005175#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005176}
5177
Gilles Peskine8814fc42020-12-14 15:33:44 +01005178/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005179 *
5180 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5181 * `psa_generate_random(...)`. The state parameter is ignored since the
5182 * PSA API doesn't support passing an explicit state.
5183 *
5184 * In the non-external case, psa_generate_random() calls an
5185 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5186 * and semantics as mbedtls_psa_get_random(). As an optimization,
5187 * instead of doing this back-and-forth between the PSA API and the
5188 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5189 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005191#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5192int mbedtls_psa_get_random(void *p_rng,
5193 unsigned char *output,
5194 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005195{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005196 /* This function takes a pointer to the RNG state because that's what
5197 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5198 * its own state internally and doesn't let the caller access that state.
5199 * So we just ignore the state parameter, and in practice we'll pass
5200 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005201 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005202 psa_status_t status = psa_generate_random(output, output_size);
5203 if (status == PSA_SUCCESS) {
5204 return 0;
5205 } else {
5206 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5207 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005208}
5209#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5210
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005211#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5213 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005214{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005215 if (global_data.initialized) {
5216 return PSA_ERROR_NOT_PERMITTED;
5217 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005219 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5220 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5221 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5222 return PSA_ERROR_INVALID_ARGUMENT;
5223 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005225 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005226}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005227#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005228
Ronald Cron3772afe2021-02-08 16:10:05 +01005229/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005230 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005231 * \param type The key type
5232 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005233 *
5234 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005235 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005236 * \retval #PSA_ERROR_INVALID_ARGUMENT
5237 * The size in bits of the key is not valid.
5238 * \retval #PSA_ERROR_NOT_SUPPORTED
5239 * The type and/or the size in bits of the key or the combination of
5240 * the two is not supported.
5241 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005242static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005243 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005244{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005247 if (key_type_is_raw_bytes(type)) {
5248 status = validate_unstructured_key_bit_size(type, bits);
5249 if (status != PSA_SUCCESS) {
5250 return status;
5251 }
5252 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005253#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005254 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5255 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5256 return PSA_ERROR_NOT_SUPPORTED;
5257 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005258
Ronald Cron01b2aba2020-10-05 09:42:02 +02005259 /* Accept only byte-aligned keys, for the same reasons as
5260 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261 if (bits % 8 != 0) {
5262 return PSA_ERROR_NOT_SUPPORTED;
5263 }
5264 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005265#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005266
Ronald Cron5c4d3862020-12-07 11:07:24 +01005267#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005268 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005269 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 return PSA_SUCCESS;
5271 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005272#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005273 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005275 }
5276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005278}
5279
Ronald Cron55ed0592020-10-05 10:30:40 +02005280psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005281 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005283{
5284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005285 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if ((attributes->domain_parameters == NULL) &&
5288 (attributes->domain_parameters_size != 0)) {
5289 return PSA_ERROR_INVALID_ARGUMENT;
5290 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292 if (key_type_is_raw_bytes(type)) {
5293 status = psa_generate_random(key_buffer, key_buffer_size);
5294 if (status != PSA_SUCCESS) {
5295 return status;
5296 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005297
David Brown12ca5032021-02-11 11:02:00 -07005298#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005299 if (type == PSA_KEY_TYPE_DES) {
5300 psa_des_set_key_parity(key_buffer, key_buffer_size);
5301 }
David Brown8107e312021-02-12 08:08:46 -07005302#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005303 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005304
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005305#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5306 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005307 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5308 return mbedtls_psa_rsa_generate_key(attributes,
5309 key_buffer,
5310 key_buffer_size,
5311 key_buffer_length);
5312 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005313#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5314 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005315
John Durkop9814fa22020-11-04 12:28:15 -08005316#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5318 return mbedtls_psa_ecp_generate_key(attributes,
5319 key_buffer,
5320 key_buffer_size,
5321 key_buffer_length);
5322 } else
John Durkop9814fa22020-11-04 12:28:15 -08005323#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005324 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 (void) key_buffer_length;
5326 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005327 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005330}
Darryl Green0c6575a2018-11-07 16:05:30 +00005331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005332psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5333 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005334{
5335 psa_status_t status;
5336 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005337 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005338 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005339
Ronald Cron81709fc2020-11-14 12:10:32 +01005340 *key = MBEDTLS_SVC_KEY_ID_INIT;
5341
Gilles Peskine0f84d622019-09-12 19:03:13 +02005342 /* Reject any attempt to create a zero-length key so that we don't
5343 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 if (psa_get_key_bits(attributes) == 0) {
5345 return PSA_ERROR_INVALID_ARGUMENT;
5346 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005347
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005348 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005349 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5350 return PSA_ERROR_INVALID_ARGUMENT;
5351 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005353 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5354 &slot, &driver);
5355 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005356 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 }
Gilles Peskine11792082019-08-06 18:36:36 +02005358
Ronald Cron977c2472020-10-13 08:32:21 +02005359 /* In the case of a transparent key or an opaque key stored in local
5360 * storage (thus not in the case of generating a key in a secure element
5361 * or cryptoprocessor with storage), we have to allocate a buffer to
5362 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005363 if (slot->key.data == NULL) {
5364 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5365 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005366 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005367 attributes->core.type, attributes->core.bits);
5368 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005369 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005371
5372 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 attributes->core.type,
5374 attributes->core.bits);
5375 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005376 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 attributes, &key_buffer_size);
5378 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005379 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 }
Ronald Cron977c2472020-10-13 08:32:21 +02005381 }
Ronald Cron977c2472020-10-13 08:32:21 +02005382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005383 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5384 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005385 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005386 }
Ronald Cron977c2472020-10-13 08:32:21 +02005387 }
5388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 status = psa_driver_wrapper_generate_key(attributes,
5390 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 if (status != PSA_SUCCESS) {
5393 psa_remove_key_data_from_memory(slot);
5394 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005395
Gilles Peskine11792082019-08-06 18:36:36 +02005396exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 if (status == PSA_SUCCESS) {
5398 status = psa_finish_key_creation(slot, driver, key);
5399 }
5400 if (status != PSA_SUCCESS) {
5401 psa_fail_key_creation(slot, driver);
5402 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005404 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005405}
5406
Gilles Peskinee59236f2018-01-27 23:32:46 +01005407/****************************************************************/
5408/* Module setup */
5409/****************************************************************/
5410
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005411#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005412psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 void (* entropy_init)(mbedtls_entropy_context *ctx),
5414 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005415{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5417 return PSA_ERROR_BAD_STATE;
5418 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005419 global_data.rng.entropy_init = entropy_init;
5420 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005422}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005423#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005425void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005426{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005427 psa_wipe_all_key_slots();
5428 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5429 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005430 }
5431 /* Wipe all remaining data, including configuration.
5432 * In particular, this sets all state indicator to the value
5433 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005435
5436 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005437 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005438}
5439
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005440#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5441/** Recover a transaction that was interrupted by a power failure.
5442 *
5443 * This function is called during initialization, before psa_crypto_init()
5444 * returns. If this function returns a failure status, the initialization
5445 * fails.
5446 */
5447static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005448 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005449{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005451 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5452 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005453 /* TODO - fall through to the failure case until this
5454 * is implemented.
5455 * https://github.com/ARMmbed/mbed-crypto/issues/218
5456 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005457 default:
5458 /* We found an unsupported transaction in the storage.
5459 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005461 }
5462}
5463#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005466{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005467 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005468
Gilles Peskinec6b69072018-11-20 21:42:52 +01005469 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 if (global_data.initialized != 0) {
5471 return PSA_SUCCESS;
5472 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005473
Gilles Peskine30524eb2020-11-13 17:02:26 +01005474 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005476 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 status = mbedtls_psa_random_seed(&global_data.rng);
5478 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005481 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 status = psa_initialize_key_slots();
5484 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005485 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005487
Ronald Cron088d5d02021-04-10 16:57:30 +02005488 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 status = psa_driver_wrapper_init();
5490 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005491 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005493
Gilles Peskine4b734222019-07-24 15:56:31 +02005494#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 status = psa_crypto_load_transaction();
5496 if (status == PSA_SUCCESS) {
5497 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5498 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005499 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 }
5501 status = psa_crypto_stop_transaction();
5502 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005503 /* There's no transaction to complete. It's all good. */
5504 status = PSA_SUCCESS;
5505 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005506#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005507
Gilles Peskinec6b69072018-11-20 21:42:52 +01005508 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005509 global_data.initialized = 1;
5510
5511exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005512 if (status != PSA_SUCCESS) {
5513 mbedtls_psa_crypto_free();
5514 }
5515 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005516}
5517
David Horstmanndf493552023-11-15 15:18:30 +00005518/** Copy from an input buffer to a local copy.
5519 *
5520 * \param[in] input Pointer to input buffer.
5521 * \param[in] input_len Length of the input buffer.
5522 * \param[out] input_copy Pointer to a local copy in which to store the input data.
5523 * \param[out] input_copy_len Length of the local copy buffer.
5524 * \return #PSA_SUCCESS, if the buffer was successfully
5525 * copied.
5526 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
5527 * copy is too small to hold contents of the
5528 * input buffer.
5529 */
5530MBEDTLS_STATIC_TESTABLE
David Horstmann957f9802023-10-30 20:29:43 +00005531psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5532 uint8_t *input_copy, size_t input_copy_len)
5533{
5534 if (input_len > input_copy_len) {
David Horstmann8684ad52023-11-03 19:51:40 +00005535 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmann957f9802023-10-30 20:29:43 +00005536 }
5537
David Horstmann0760b152023-11-24 16:21:04 +00005538#if defined(MBEDTLS_TEST_HOOKS)
5539 MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len);
5540#endif
5541
David Horstmann660027f2023-11-15 17:33:47 +00005542 if (input_len > 0) {
5543 memcpy(input_copy, input, input_len);
5544 }
David Horstmann957f9802023-10-30 20:29:43 +00005545
David Horstmann0760b152023-11-24 16:21:04 +00005546#if defined(MBEDTLS_TEST_HOOKS)
5547 MBEDTLS_TEST_MEMORY_POISON(input, input_len);
5548#endif
5549
David Horstmann957f9802023-10-30 20:29:43 +00005550 return PSA_SUCCESS;
5551}
5552
David Horstmanndf493552023-11-15 15:18:30 +00005553/** Copy from a local output buffer into a user-supplied one.
5554 *
5555 * \param[in] output_copy Pointer to a local buffer containing the output.
5556 * \param[in] output_copy_len Length of the local buffer.
5557 * \param[out] output Pointer to user-supplied output buffer.
5558 * \param[out] output_len Length of the user-supplied output buffer.
5559 * \return #PSA_SUCCESS, if the buffer was successfully
5560 * copied.
David Horstmann232ab252023-11-20 12:39:38 +00005561 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmanndf493552023-11-15 15:18:30 +00005562 * user-supplied output buffer is too small to
5563 * hold the contents of the local buffer.
5564 */
5565MBEDTLS_STATIC_TESTABLE
David Horstmann2bd296e2023-10-30 20:37:12 +00005566psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5567 uint8_t *output, size_t output_len)
5568{
5569 if (output_len < output_copy_len) {
David Horstmann232ab252023-11-20 12:39:38 +00005570 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann2bd296e2023-10-30 20:37:12 +00005571 }
David Horstmann660027f2023-11-15 17:33:47 +00005572
David Horstmann0760b152023-11-24 16:21:04 +00005573#if defined(MBEDTLS_TEST_HOOKS)
5574 MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len);
5575#endif
5576
David Horstmann660027f2023-11-15 17:33:47 +00005577 if (output_copy_len > 0) {
5578 memcpy(output, output_copy, output_copy_len);
5579 }
5580
David Horstmann0760b152023-11-24 16:21:04 +00005581#if defined(MBEDTLS_TEST_HOOKS)
5582 MBEDTLS_TEST_MEMORY_POISON(output, output_len);
5583#endif
5584
David Horstmann2bd296e2023-10-30 20:37:12 +00005585 return PSA_SUCCESS;
5586}
5587
David Horstmann81a0d572023-11-20 17:15:32 +00005588psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
5589 psa_crypto_local_input_t *local_input)
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005590{
5591 psa_status_t status;
5592
David Horstmann0d52c712023-11-23 15:50:37 +00005593 *local_input = PSA_CRYPTO_LOCAL_INPUT_INIT;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005594
David Horstmann726bf052023-11-15 18:11:26 +00005595 if (input_len == 0) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005596 return PSA_SUCCESS;
5597 }
5598
David Horstmann81a0d572023-11-20 17:15:32 +00005599 local_input->buffer = mbedtls_calloc(input_len, 1);
5600 if (local_input->buffer == NULL) {
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005601 /* Since we dealt with the zero-length case above, we know that
5602 * a NULL return value means a failure of allocation. */
5603 return PSA_ERROR_INSUFFICIENT_MEMORY;
5604 }
David Horstmann81a0d572023-11-20 17:15:32 +00005605 /* From now on, we must free local_input->buffer on error. */
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005606
David Horstmann81a0d572023-11-20 17:15:32 +00005607 local_input->length = input_len;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005608
5609 status = psa_crypto_copy_input(input, input_len,
David Horstmann81a0d572023-11-20 17:15:32 +00005610 local_input->buffer, local_input->length);
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005611 if (status != PSA_SUCCESS) {
5612 goto error;
5613 }
5614
5615 return PSA_SUCCESS;
5616
5617error:
David Horstmann81a0d572023-11-20 17:15:32 +00005618 mbedtls_free(local_input->buffer);
5619 local_input->buffer = NULL;
5620 local_input->length = 0;
David Horstmann8d9d4fe2023-11-07 16:36:48 +00005621 return status;
5622}
5623
David Horstmann81a0d572023-11-20 17:15:32 +00005624void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmannfa2d75d2023-11-07 18:00:41 +00005625{
David Horstmann81a0d572023-11-20 17:15:32 +00005626 mbedtls_free(local_input->buffer);
5627 local_input->buffer = NULL;
5628 local_input->length = 0;
David Horstmannfa2d75d2023-11-07 18:00:41 +00005629}
5630
David Horstmann1a76ab12023-11-20 12:54:09 +00005631psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
5632 psa_crypto_local_output_t *local_output)
David Horstmann953cd5b2023-11-08 15:19:43 +00005633{
David Horstmann0d52c712023-11-23 15:50:37 +00005634 *local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT;
David Horstmann953cd5b2023-11-08 15:19:43 +00005635
David Horstmann726bf052023-11-15 18:11:26 +00005636 if (output_len == 0) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005637 return PSA_SUCCESS;
5638 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005639 local_output->buffer = mbedtls_calloc(output_len, 1);
5640 if (local_output->buffer == NULL) {
David Horstmann953cd5b2023-11-08 15:19:43 +00005641 /* Since we dealt with the zero-length case above, we know that
5642 * a NULL return value means a failure of allocation. */
5643 return PSA_ERROR_INSUFFICIENT_MEMORY;
5644 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005645 local_output->length = output_len;
5646 local_output->original = output;
David Horstmann953cd5b2023-11-08 15:19:43 +00005647
5648 return PSA_SUCCESS;
5649}
5650
David Horstmann1a76ab12023-11-20 12:54:09 +00005651psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
David Horstmannaeeb2742023-11-08 17:44:18 +00005652{
David Horstmann761761f2023-11-15 15:57:32 +00005653 psa_status_t status;
5654
David Horstmann1a76ab12023-11-20 12:54:09 +00005655 if (local_output->buffer == NULL) {
5656 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005657 return PSA_SUCCESS;
5658 }
David Horstmann1a76ab12023-11-20 12:54:09 +00005659 if (local_output->original == NULL) {
David Horstmannaeeb2742023-11-08 17:44:18 +00005660 /* We have an internal copy but nothing to copy back to. */
5661 return PSA_ERROR_CORRUPTION_DETECTED;
5662 }
5663
David Horstmann1a76ab12023-11-20 12:54:09 +00005664 status = psa_crypto_copy_output(local_output->buffer, local_output->length,
5665 local_output->original, local_output->length);
David Horstmann761761f2023-11-15 15:57:32 +00005666 if (status != PSA_SUCCESS) {
5667 return status;
5668 }
David Horstmannaeeb2742023-11-08 17:44:18 +00005669
David Horstmann1a76ab12023-11-20 12:54:09 +00005670 mbedtls_free(local_output->buffer);
5671 local_output->buffer = NULL;
5672 local_output->length = 0;
David Horstmannaeeb2742023-11-08 17:44:18 +00005673
5674 return PSA_SUCCESS;
5675}
5676
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677#endif /* MBEDTLS_PSA_CRYPTO_C */