blob: 9976d72fef14357790ed9802c9e3b2059f9b9385 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
30
Ronald Crond6d28882020-12-14 14:56:02 +010031#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010036#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010037#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010039#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020041#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010043#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010044/* Include internal declarations that are useful for implementing persistently
45 * stored keys. */
46#include "psa_crypto_storage.h"
47
Gilles Peskineb2b64d32020-12-14 16:43:58 +010048#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010049
Gilles Peskineb46bef22019-07-30 21:32:04 +020050#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010055#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020057#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000058#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020059#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/blowfish.h"
61#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
73#include "mbedtls/md2.h"
74#include "mbedtls/md4.h"
75#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010076#include "mbedtls/md.h"
77#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010079#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010080#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000081#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/sha1.h"
85#include "mbedtls/sha256.h"
86#include "mbedtls/sha512.h"
87#include "mbedtls/xtea.h"
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020090
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010091/****************************************************************/
92/* Global data, support functions and library management */
93/****************************************************************/
94
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020096{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098}
99
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100100/* Values for psa_global_data_t::rng_state */
101#define RNG_NOT_INITIALIZED 0
102#define RNG_INITIALIZED 1
103#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100107 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +0100108 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100109} psa_global_data_t;
110
111static psa_global_data_t global_data;
112
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100113#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
114mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
115 &global_data.rng.drbg;
116#endif
117
itayzafrir0adf0fc2018-09-06 16:24:41 +0300118#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 if (global_data.initialized == 0) \
120 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100124 /* Mbed TLS error codes can combine a high-level error code and a
125 * low-level error code. The low-level error usually reflects the
126 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 int low_level_ret = -(-ret & 0x007f);
128 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100131
132 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
133 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
134 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100136 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100138
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200139 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200141
Gilles Peskine9a944802018-06-21 09:35:35 +0200142 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
143 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
144 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
145 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
146 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200152
Jaeden Amero93e21112019-02-20 13:57:28 +0000153#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000154 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000155#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
156 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
157#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100158 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100160 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100162
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000164 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
166 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
167#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100170 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100172
173 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179
Gilles Peskine26869f22019-05-06 15:25:00 +0200180 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200182
183 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200187
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
205 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
209 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100210 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
211 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
215 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100219#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100240 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
241 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100242 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
245 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100247 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100249#endif
250
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200251 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
252 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
253 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200255
Gilles Peskinea5905292018-02-07 20:59:33 +0100256 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100258 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100260 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200264 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266
Gilles Peskinef76aa772018-10-29 19:24:33 +0100267 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100269 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100271 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100273 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100275 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100277 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100279 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100284 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
287 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
292 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
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_PASSWORD_REQUIRED:
297 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_INVALID_ALG:
302 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
303 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309
Gilles Peskineff2d2002019-05-06 15:26:23 +0200310 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200312 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200314
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200315 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200317
Gilles Peskinea5905292018-02-07 20:59:33 +0100318 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100326 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
327 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100329 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100331 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200340 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
341 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
342 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200344
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100349
itayzafrir5c753392018-05-08 11:18:38 +0300350 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
358 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300360 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100362 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100366
Janos Follatha13b9052019-11-22 12:48:59 +0000367 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300369
Gilles Peskinee59236f2018-01-27 23:32:46 +0100370 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100372 }
373}
374
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200376
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/****************************************************************/
379/* Key management */
380/****************************************************************/
381
John Durkop9814fa22020-11-04 12:28:15 -0800382#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800383 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100384 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
385 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
386 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
388 size_t bits,
389 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200390{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100392 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100394#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100397#endif
398#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
410#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 if (bits_is_sloppy) {
415 return MBEDTLS_ECP_DP_SECP521R1;
416 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100418#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 }
420 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100421
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100424#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100427#endif
428#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
432#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100436 }
437 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100438
Paul Elliott8ff510a2020-06-02 17:19:28 +0100439 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100442 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 if (bits_is_sloppy) {
446 return MBEDTLS_ECP_DP_CURVE25519;
447 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
450#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100454 }
455 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100456
Paul Elliott8ff510a2020-06-02 17:19:28 +0100457 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100466#endif
467#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100468 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100470#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100471 }
472 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200473 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100474
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100475 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200477}
John Durkop9814fa22020-11-04 12:28:15 -0800478#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100479 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
480 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
481 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
482 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
485 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486{
487 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200491 case PSA_KEY_TYPE_DERIVE:
492 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100493#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (bits != 128 && bits != 192 && bits != 256) {
496 return PSA_ERROR_INVALID_ARGUMENT;
497 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200498 break;
499#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200500#if defined(PSA_WANT_KEY_TYPE_ARIA)
501 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 if (bits != 128 && bits != 192 && bits != 256) {
503 return PSA_ERROR_INVALID_ARGUMENT;
504 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200505 break;
506#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100507#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 if (bits != 128 && bits != 192 && bits != 256) {
510 return PSA_ERROR_INVALID_ARGUMENT;
511 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 break;
513#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100514#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 if (bits != 64 && bits != 128 && bits != 192) {
517 return PSA_ERROR_INVALID_ARGUMENT;
518 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 if (bits < 8 || bits > 2048) {
524 return PSA_ERROR_INVALID_ARGUMENT;
525 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 break;
527#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100528#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200529 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 if (bits != 256) {
531 return PSA_ERROR_INVALID_ARGUMENT;
532 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200533 break;
534#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200537 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (bits % 8 != 0) {
539 return PSA_ERROR_INVALID_ARGUMENT;
540 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200543}
544
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100545/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100546 *
Steven Cooremancd640932021-03-08 12:00:27 +0100547 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
548 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100549 *
550 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 * \param[in] key_type The key type of the key to be used with the
552 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100553 *
554 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100555 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100557 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100558 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100559MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100560 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100562{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 if (PSA_ALG_IS_HMAC(algorithm)) {
564 if (key_type == PSA_KEY_TYPE_HMAC) {
565 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100567 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
570 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
571 * key. */
572 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
573 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
574 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
575 * the block length (larger than 1) for block ciphers. */
576 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
577 return PSA_SUCCESS;
578 }
579 }
580 }
581
582 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583}
584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
586 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200587{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 if (slot->key.data != NULL) {
589 return PSA_ERROR_ALREADY_EXISTS;
590 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592 slot->key.data = mbedtls_calloc(1, buffer_length);
593 if (slot->key.data == NULL) {
594 return PSA_ERROR_INSUFFICIENT_MEMORY;
595 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200596
Ronald Cronea0f8a62020-11-25 17:52:23 +0100597 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200599}
600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
602 const uint8_t *data,
603 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200604{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 psa_status_t status = psa_allocate_buffer_to_slot(slot,
606 data_length);
607 if (status != PSA_SUCCESS) {
608 return status;
609 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 memcpy(slot->key.data, data, data_length);
612 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200613}
614
Ronald Crona0fe59f2020-11-29 11:14:53 +0100615psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100616 const psa_key_attributes_t *attributes,
617 const uint8_t *data, size_t data_length,
618 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
622 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625 if (data_length == 0) {
626 return PSA_ERROR_NOT_SUPPORTED;
627 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 if (key_type_is_raw_bytes(type)) {
630 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200631
Steven Cooreman75b74362020-07-28 14:30:13 +0200632 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100633 if (data_length > SIZE_MAX / 8) {
634 return PSA_ERROR_NOT_SUPPORTED;
635 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200636
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200637 /* Enforce a size limit, and in particular ensure that the bit
638 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 if ((*bits) > PSA_MAX_KEY_BITS) {
640 return PSA_ERROR_NOT_SUPPORTED;
641 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 status = validate_unstructured_key_bit_size(type, *bits);
644 if (status != PSA_SUCCESS) {
645 return status;
646 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200647
Ronald Crondd04d422020-11-28 15:14:42 +0100648 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100650 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 return PSA_SUCCESS;
654 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800655#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
657 if (PSA_KEY_TYPE_IS_ECC(type)) {
658 return mbedtls_psa_ecp_import_key(attributes,
659 data, data_length,
660 key_buffer, key_buffer_size,
661 key_buffer_length,
662 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100663 }
John Durkop9814fa22020-11-04 12:28:15 -0800664#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
665 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
668 if (PSA_KEY_TYPE_IS_RSA(type)) {
669 return mbedtls_psa_rsa_import_key(attributes,
670 data, data_length,
671 key_buffer, key_buffer_size,
672 key_buffer_length,
673 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200674 }
John Durkop9814fa22020-11-04 12:28:15 -0800675#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
676 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100677 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100680}
681
Gilles Peskinef603c712019-01-19 13:40:11 +0100682/** Calculate the intersection of two algorithm usage policies.
683 *
684 * Return 0 (which allows no operation) on incompatibility.
685 */
686static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100687 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100688 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100690{
Gilles Peskine549ea862019-05-22 11:45:59 +0200691 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 if (alg1 == alg2) {
693 return alg1;
694 }
Steven Cooreman03488022021-02-18 12:07:20 +0100695 /* If the policies are from the same hash-and-sign family, check
696 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
698 PSA_ALG_IS_SIGN_HASH(alg2) &&
699 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
700 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
701 return alg2;
702 }
703 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
704 return alg1;
705 }
Steven Cooreman03488022021-02-18 12:07:20 +0100706 }
707 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100708 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100709 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
711 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
712 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
713 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
714 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100715 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100716
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100717 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100718 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
719 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
720 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
721 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100722 }
723 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
725 (alg1_len <= alg2_len)) {
726 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100727 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
729 (alg2_len <= alg1_len)) {
730 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100731 }
732 }
733 /* If the policies are from the same MAC family, check whether one
734 * of them is a minimum-MAC-length policy. Calculate the most
735 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100736 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
737 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
738 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100739 /* Validate the combination of key type and algorithm. Since the base
740 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
742 return 0;
743 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100744
Steven Cooreman31a876d2021-03-03 20:47:40 +0100745 /* Get the (exact or at-least) output lengths for both sides of the
746 * requested intersection. None of the currently supported algorithms
747 * have an output length dependent on the actual key size, so setting it
748 * to a bogus value of 0 is currently OK.
749 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100750 * Note that for at-least-this-length wildcard algorithms, the output
751 * length is set to the shortest allowed length, which allows us to
752 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
754 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100755 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756
Steven Cooremana1d83222021-02-25 10:20:29 +0100757 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
759 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
760 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100761 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100762
763 /* If only one is an at-least-this-length policy, the intersection would
764 * be the other (fixed-length) policy as long as said fixed length is
765 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100766 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
767 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100768 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100769 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
770 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100771 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100772
Steven Cooremancd640932021-03-08 12:00:27 +0100773 /* If none of them are wildcards, check whether they define the same tag
774 * length. This is still possible here when one is default-length and
775 * the other specific-length. Ensure to always return the
776 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 if (alg1_len == alg2_len) {
778 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
779 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100780 }
781 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100783}
784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785static int psa_key_algorithm_permits(psa_key_type_t key_type,
786 psa_algorithm_t policy_alg,
787 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if (requested_alg == policy_alg) {
791 return 1;
792 }
Steven Cooreman03488022021-02-18 12:07:20 +0100793 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
794 * and requested_alg is the same hash-and-sign family with any hash,
795 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
797 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
798 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
799 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100800 }
801 /* If policy_alg is a wildcard AEAD algorithm of the same base as
802 * the requested algorithm, check the requested tag length to be
803 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100804 if (PSA_ALG_IS_AEAD(policy_alg) &&
805 PSA_ALG_IS_AEAD(requested_alg) &&
806 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
807 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
808 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
809 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
810 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100811 }
Steven Cooremancd640932021-03-08 12:00:27 +0100812 /* If policy_alg is a MAC algorithm of the same base as the requested
813 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 if (PSA_ALG_IS_MAC(policy_alg) &&
815 PSA_ALG_IS_MAC(requested_alg) &&
816 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
817 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100818 /* Validate the combination of key type and algorithm. Since the policy
819 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100820 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
821 return 0;
822 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100823
Steven Cooreman31a876d2021-03-03 20:47:40 +0100824 /* Get both the requested output length for the algorithm which is to be
825 * verified, and the default output length for the base algorithm.
826 * Note that none of the currently supported algorithms have an output
827 * length dependent on actual key size, so setting it to a bogus value
828 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100829 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100830 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100831 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 key_type, 0,
833 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100834
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100835 /* If the policy is default-length, only allow an algorithm with
836 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
838 return requested_output_length == default_output_length;
839 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100840
841 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100842 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
844 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
845 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100846 }
847
Steven Cooremancd640932021-03-08 12:00:27 +0100848 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
849 * check for the requested MAC length to be equal to or longer than the
850 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
852 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
853 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 }
Steven Cooremance48e852020-10-05 16:02:45 +0200856 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200857 * a key derivation with that key agreement should also be allowed. This
858 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
860 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
861 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
862 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200863 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100864 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200866}
867
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100868/** Test whether a policy permits an algorithm.
869 *
870 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100871 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100872 * \note This function requires providing the key type for which the policy is
873 * being validated, since some algorithm policy definitions (e.g. MAC)
874 * have different properties depending on what kind of cipher it is
875 * combined with.
876 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100877 * \retval PSA_SUCCESS When \p alg is a specific algorithm
878 * allowed by the \p policy.
879 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
880 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
881 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100882 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
884 psa_key_type_t key_type,
885 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100886{
Steven Cooremand788fab2021-02-25 11:29:17 +0100887 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100888 if (alg == 0) {
889 return PSA_ERROR_INVALID_ARGUMENT;
890 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100891
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 if (PSA_ALG_IS_WILDCARD(alg)) {
894 return PSA_ERROR_INVALID_ARGUMENT;
895 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100897 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
898 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
899 return PSA_SUCCESS;
900 } else {
901 return PSA_ERROR_NOT_PERMITTED;
902 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100903}
904
Gilles Peskinef603c712019-01-19 13:40:11 +0100905/** Restrict a key policy based on a constraint.
906 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100907 * \note This function requires providing the key type for which the policy is
908 * being restricted, since some algorithm policy definitions (e.g. MAC)
909 * have different properties depending on what kind of cipher it is
910 * combined with.
911 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100912 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 * \param[in,out] policy The policy to restrict.
914 * \param[in] constraint The policy constraint to apply.
915 *
916 * \retval #PSA_SUCCESS
917 * \c *policy contains the intersection of the original value of
918 * \c *policy and \c *constraint.
919 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 * \c *policy is unchanged.
922 */
923static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100924 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100925 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100926 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100927{
928 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 psa_key_policy_algorithm_intersection(key_type, policy->alg,
930 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200931 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
933 constraint->alg2);
934 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
935 return PSA_ERROR_INVALID_ARGUMENT;
936 }
937 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
938 return PSA_ERROR_INVALID_ARGUMENT;
939 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 policy->usage &= constraint->usage;
941 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100943 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100944}
945
Ronald Cron5c522922020-11-14 16:35:34 +0100946/** Get the description of a key given its identifier and policy constraints
947 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 *
Ronald Cron5c522922020-11-14 16:35:34 +0100949 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100950 * nonzero, the key must allow operations with this algorithm. If \p alg is
951 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * In case of a persistent key, the function loads the description of the key
954 * into a key slot if not already done.
955 *
956 * On success, the returned key slot is locked. It is the responsibility of
957 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958 */
Ronald Cron5c522922020-11-14 16:35:34 +0100959static psa_status_t psa_get_and_lock_key_slot_with_policy(
960 mbedtls_svc_key_id_t key,
961 psa_key_slot_t **p_slot,
962 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100964{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200965 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
966 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100968 status = psa_get_and_lock_key_slot(key, p_slot);
969 if (status != PSA_SUCCESS) {
970 return status;
971 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100973
974 /* Enforce that usage policy for the key slot contains all the flags
975 * required by the usage parameter. There is one exception: public
976 * keys can always be exported, so we treat public key objects as
977 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100979 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100980 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100982 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100983 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200984 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100985 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100986
Shaun Case0e7791f2021-12-20 21:14:10 -0800987 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 if (alg != 0) {
989 status = psa_key_policy_permits(&slot->attr.policy,
990 slot->attr.type,
991 alg);
992 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100993 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100995 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998
999error:
1000 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001004}
Darryl Green940d72c2018-07-13 13:18:51 +01001005
Ronald Cron5c522922020-11-14 16:35:34 +01001006/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 *
1008 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001009 * available, as opposed to a key in a secure element and/or to be used
1010 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001012 * This is a temporary function that may be used instead of
1013 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1014 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015 *
Ronald Cron5c522922020-11-14 16:35:34 +01001016 * On success, the returned key slot is locked. It is the responsibility of the
1017 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001018 */
Ronald Cron5c522922020-11-14 16:35:34 +01001019static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1020 mbedtls_svc_key_id_t key,
1021 psa_key_slot_t **p_slot,
1022 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001025 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1026 usage, alg);
1027 if (status != PSA_SUCCESS) {
1028 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001031 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1032 psa_unlock_key_slot(*p_slot);
1033 *p_slot = NULL;
1034 return PSA_ERROR_NOT_SUPPORTED;
1035 }
1036
1037 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001038}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001041{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001042 /* Data pointer will always be either a valid pointer or NULL in an
1043 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 if (slot->key.data != NULL) {
1045 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1046 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001049 slot->key.data = NULL;
1050 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001053}
1054
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055/** Completely wipe a slot in memory, including its policy.
1056 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001058{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001060
1061 /*
1062 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001063 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001064 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1065 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001066 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001067 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001068 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001069#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001070 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001071#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001072 status = PSA_ERROR_CORRUPTION_DETECTED;
1073 }
1074
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001075 /* Multipart operations may still be using the key. This is safe
1076 * because all multipart operation objects are independent from
1077 * the key slot: if they need to access the key after the setup
1078 * phase, they have a copy of the key. Note that this means that
1079 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080 /* At this point, key material and other type-specific content has
1081 * been wiped. Clear remaining metadata. We can call memset and not
1082 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083 memset(slot, 0, sizeof(*slot));
1084 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001085}
1086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001087psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001089 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090 psa_status_t status; /* status of the last operation */
1091 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001092#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1093 psa_se_drv_table_entry_t *driver;
1094#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001096 if (mbedtls_svc_key_id_is_null(key)) {
1097 return PSA_SUCCESS;
1098 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001099
Ronald Cronf2911112020-10-29 17:51:10 +01001100 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001101 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001102 * key, this will load the key description from persistent memory if not
1103 * done yet. We cannot avoid this loading as without it we don't know if
1104 * the key is operated by an SE or not and this information is needed by
1105 * the current implementation.
1106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 status = psa_get_and_lock_key_slot(key, &slot);
1108 if (status != PSA_SUCCESS) {
1109 return status;
1110 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001111
Ronald Cronf2911112020-10-29 17:51:10 +01001112 /*
1113 * If the key slot containing the key description is under access by the
1114 * library (apart from the present access), the key cannot be destroyed
1115 * yet. For the time being, just return in error. Eventually (to be
1116 * implemented), the key should be destroyed when all accesses have
1117 * stopped.
1118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 if (slot->lock_count > 1) {
1120 psa_unlock_key_slot(slot);
1121 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001122 }
1123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001125 /* Refuse the destruction of a read-only key (which may or may not work
1126 * if we attempt it, depending on whether the key is merely read-only
1127 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001128 * Just do the best we can, which is to wipe the copy in memory
1129 * (done in this function's cleanup code). */
1130 overall_status = PSA_ERROR_NOT_PERMITTED;
1131 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001132 }
1133
Gilles Peskine354f7672019-07-12 23:46:38 +02001134#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1136 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001137 /* For a key in a secure element, we need to do three things:
1138 * remove the key file in internal storage, destroy the
1139 * key inside the secure element, and update the driver's
1140 * persistent data. Start a transaction that will encompass these
1141 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001142 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001143 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001145 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001146 status = psa_crypto_save_transaction();
1147 if (status != PSA_SUCCESS) {
1148 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149 /* We should still try to destroy the key in the secure
1150 * element and the key metadata in storage. This is especially
1151 * important if the error is that the storage is full.
1152 * But how to do it exactly without risking an inconsistent
1153 * state after a reset?
1154 * https://github.com/ARMmbed/mbed-crypto/issues/215
1155 */
1156 overall_status = status;
1157 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001158 }
1159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001160 status = psa_destroy_se_key(driver,
1161 psa_key_slot_get_slot_number(slot));
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 Peskinefc762652019-07-22 19:30:34 +02001165 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1167
Darryl Greend49a4992018-06-18 17:27:26 +01001168#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1170 status = psa_destroy_persistent_key(slot->attr.id);
1171 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001173 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001175 /* TODO: other slots may have a copy of the same key. We should
1176 * invalidate them.
1177 * https://github.com/ARMmbed/mbed-crypto/issues/214
1178 */
Darryl Greend49a4992018-06-18 17:27:26 +01001179 }
1180#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001181
Gilles Peskinefc762652019-07-22 19:30:34 +02001182#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 if (driver != NULL) {
1184 status = psa_save_se_persistent_data(driver);
1185 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001186 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001187 }
1188 status = psa_crypto_stop_transaction();
1189 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001192 }
1193#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1194
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001195exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001197 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001198 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 }
1201 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202}
1203
John Durkop07cc04a2020-11-16 22:08:34 -08001204#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001205 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001206static psa_status_t psa_get_rsa_public_exponent(
1207 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001209{
1210 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 uint8_t *buffer = NULL;
1213 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1217 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 }
1220 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001221 /* It's the default value, which is reported as an empty string,
1222 * so there's nothing to do. */
1223 goto exit;
1224 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 buflen = mbedtls_mpi_size(&mpi);
1227 buffer = mbedtls_calloc(1, buflen);
1228 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001229 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1230 goto exit;
1231 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001232 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1233 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001236 attributes->domain_parameters = buffer;
1237 attributes->domain_parameters_size = buflen;
1238
1239exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001240 mbedtls_mpi_free(&mpi);
1241 if (ret != 0) {
1242 mbedtls_free(buffer);
1243 }
1244 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001245}
John Durkop07cc04a2020-11-16 22:08:34 -08001246#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001247 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001248
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001249/** Retrieve all the publicly-accessible attributes of a key.
1250 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1252 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001255 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001256 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001260 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1261 if (status != PSA_SUCCESS) {
1262 return status;
1263 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001264
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001265 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1267 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001268
1269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1271 psa_set_key_slot_number(attributes,
1272 psa_key_slot_get_slot_number(slot));
1273 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001274#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001277#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001278 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001279 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001281 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001282 * is not yet implemented.
1283 * https://github.com/ARMmbed/mbed-crypto/issues/216
1284 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001286 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001287
Ronald Cron90857082020-11-25 14:58:33 +01001288 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 slot->attr.type,
1290 slot->key.data,
1291 slot->key.bytes,
1292 &rsa);
1293 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001294 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001295 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 status = psa_get_rsa_public_exponent(rsa,
1298 attributes);
1299 mbedtls_rsa_free(rsa);
1300 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001301 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001303#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001304 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001305 default:
1306 /* Nothing else to do. */
1307 break;
1308 }
1309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310 if (status != PSA_SUCCESS) {
1311 psa_reset_key_attributes(attributes);
1312 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001316 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317}
1318
Gilles Peskinec8000c02019-08-02 20:15:51 +02001319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1320psa_status_t psa_get_key_slot_number(
1321 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001323{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001324 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 return PSA_SUCCESS;
1327 } else {
1328 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001329 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001330}
1331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1334 size_t key_buffer_size,
1335 uint8_t *data,
1336 size_t data_size,
1337 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001338{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 if (key_buffer_size > data_size) {
1340 return PSA_ERROR_BUFFER_TOO_SMALL;
1341 }
1342 memcpy(data, key_buffer, key_buffer_size);
1343 memset(data + key_buffer_size, 0,
1344 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001345 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001347}
1348
Ronald Cron7285cda2020-11-26 14:46:37 +01001349psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001350 const psa_key_attributes_t *attributes,
1351 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353{
Ronald Crond18b5f82020-11-25 16:46:05 +01001354 psa_key_type_t type = attributes->core.type;
1355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 if (key_type_is_raw_bytes(type) ||
1357 PSA_KEY_TYPE_IS_RSA(type) ||
1358 PSA_KEY_TYPE_IS_ECC(type)) {
1359 return psa_export_key_buffer_internal(
1360 key_buffer, key_buffer_size,
1361 data, data_size, data_length);
1362 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001363 /* This shouldn't happen in the reference implementation, but
1364 it is valid for a special-purpose implementation to omit
1365 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001367 }
1368}
1369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1371 uint8_t *data,
1372 size_t data_size,
1373 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001374{
1375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1376 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1377 psa_key_slot_t *slot;
1378
Ronald Crone907e552021-01-18 13:22:38 +01001379 /* Reject a zero-length output buffer now, since this can never be a
1380 * valid key representation. This way we know that data must be a valid
1381 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 if (data_size == 0) {
1383 return PSA_ERROR_BUFFER_TOO_SMALL;
1384 }
Ronald Crone907e552021-01-18 13:22:38 +01001385
Ronald Cron9486f9d2020-11-26 13:36:23 +01001386 /* Set the key to empty now, so that even when there are errors, we always
1387 * set data_length to a value between 0 and data_size. On error, setting
1388 * the key to empty is a good choice because an empty key representation is
1389 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001390 *data_length = 0;
1391
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392 /* Export requires the EXPORT flag. There is an exception for public keys,
1393 * which don't require any flag, but
1394 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1395 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1397 PSA_KEY_USAGE_EXPORT, 0);
1398 if (status != PSA_SUCCESS) {
1399 return status;
1400 }
mohammad160306e79202018-03-28 13:17:44 +03001401
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 psa_key_attributes_t attributes = {
1403 .core = slot->attr
1404 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405 status = psa_driver_wrapper_export_key(&attributes,
1406 slot->key.data, slot->key.bytes,
1407 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001412}
1413
Ronald Cron7285cda2020-11-26 14:46:37 +01001414psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001415 const psa_key_attributes_t *attributes,
1416 const uint8_t *key_buffer,
1417 size_t key_buffer_size,
1418 uint8_t *data,
1419 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001420 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421{
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1425 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001426 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 return psa_export_key_buffer_internal(
1428 key_buffer, key_buffer_size,
1429 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001430 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001432 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001433#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001434 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1435 return mbedtls_psa_rsa_export_public_key(attributes,
1436 key_buffer,
1437 key_buffer_size,
1438 data,
1439 data_size,
1440 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001441#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001444#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1445 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001446 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001447#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1449 return mbedtls_psa_ecp_export_public_key(attributes,
1450 key_buffer,
1451 key_buffer_size,
1452 data,
1453 data_size,
1454 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001455#else
1456 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001458#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1459 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001460 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001462 /* This shouldn't happen in the reference implementation, but
1463 it is valid for a special-purpose implementation to omit
1464 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001466 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467}
Ronald Crond18b5f82020-11-25 16:46:05 +01001468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1470 uint8_t *data,
1471 size_t data_size,
1472 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001473{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001475 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001476 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001477
Ronald Crone907e552021-01-18 13:22:38 +01001478 /* Reject a zero-length output buffer now, since this can never be a
1479 * valid key representation. This way we know that data must be a valid
1480 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 if (data_size == 0) {
1482 return PSA_ERROR_BUFFER_TOO_SMALL;
1483 }
Ronald Crone907e552021-01-18 13:22:38 +01001484
Darryl Greendd8fb772018-11-07 16:00:44 +00001485 /* Set the key to empty now, so that even when there are errors, we always
1486 * set data_length to a value between 0 and data_size. On error, setting
1487 * the key to empty is a good choice because an empty key representation is
1488 * unlikely to be accepted anywhere. */
1489 *data_length = 0;
1490
1491 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1493 if (status != PSA_SUCCESS) {
1494 return status;
1495 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1498 status = PSA_ERROR_INVALID_ARGUMENT;
1499 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500 }
1501
Ronald Crond18b5f82020-11-25 16:46:05 +01001502 psa_key_attributes_t attributes = {
1503 .core = slot->attr
1504 };
Ronald Cron67227982020-11-26 15:16:05 +01001505 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001506 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
1509exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001510 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001513}
1514
Gilles Peskine91e8c332019-08-02 19:19:39 +02001515#if defined(static_assert)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516static_assert((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1517 "One or more key attribute flag is listed as both external-only and dual-use");
1518static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1519 "One or more key attribute flag is listed as both internal-only and dual-use");
1520static_assert((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1521 "One or more key attribute flag is listed as both internal-only and external-only");
Gilles Peskine91e8c332019-08-02 19:19:39 +02001522#endif
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524/** Validate that a key policy is internally well-formed.
1525 *
1526 * This function only rejects invalid policies. It does not validate the
1527 * consistency of the policy with respect to other attributes of the key
1528 * such as the key type.
1529 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001531{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001532 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1533 PSA_KEY_USAGE_COPY |
1534 PSA_KEY_USAGE_ENCRYPT |
1535 PSA_KEY_USAGE_DECRYPT |
1536 PSA_KEY_USAGE_SIGN_MESSAGE |
1537 PSA_KEY_USAGE_VERIFY_MESSAGE |
1538 PSA_KEY_USAGE_SIGN_HASH |
1539 PSA_KEY_USAGE_VERIFY_HASH |
1540 PSA_KEY_USAGE_DERIVE)) != 0) {
1541 return PSA_ERROR_INVALID_ARGUMENT;
1542 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001544 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001545}
1546
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001547/** Validate the internal consistency of key attributes.
1548 *
1549 * This function only rejects invalid attribute values. If does not
1550 * validate the consistency of the attributes with any key data that may
1551 * be involved in the creation of the key.
1552 *
1553 * Call this function early in the key creation process.
1554 *
1555 * \param[in] attributes Key attributes for the new key.
1556 * \param[out] p_drv On any return, the driver for the key, if any.
1557 * NULL for a transparent key.
1558 *
1559 */
1560static psa_status_t psa_validate_key_attributes(
1561 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001563{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001564 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1566 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 status = psa_validate_key_location(lifetime, p_drv);
1569 if (status != PSA_SUCCESS) {
1570 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001571 }
1572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 status = psa_validate_key_persistence(lifetime);
1574 if (status != PSA_SUCCESS) {
1575 return status;
1576 }
1577
1578 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1579 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1580 return PSA_ERROR_INVALID_ARGUMENT;
1581 }
1582 } else {
1583 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1584 return PSA_ERROR_INVALID_ARGUMENT;
1585 }
1586 }
1587
1588 status = psa_validate_key_policy(&attributes->core.policy);
1589 if (status != PSA_SUCCESS) {
1590 return status;
1591 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592
1593 /* Refuse to create overly large keys.
1594 * Note that this doesn't trigger on import if the attributes don't
1595 * explicitly specify a size (so psa_get_key_bits returns 0), so
1596 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1598 return PSA_ERROR_NOT_SUPPORTED;
1599 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001600
Gilles Peskine91e8c332019-08-02 19:19:39 +02001601 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1603 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1604 return PSA_ERROR_INVALID_ARGUMENT;
1605 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608}
1609
Gilles Peskine4747d192019-04-17 15:05:45 +02001610/** Prepare a key slot to receive key material.
1611 *
1612 * This function allocates a key slot and sets its metadata.
1613 *
1614 * If this function fails, call psa_fail_key_creation().
1615 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001616 * This function is intended to be used as follows:
1617 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001618 * it with the specified attributes, and in case of a volatile key assign it
1619 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001620 * -# Populate the slot with the key material.
1621 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1622 * In case of failure at any step, stop the sequence and call
1623 * psa_fail_key_creation().
1624 *
Ronald Cron5c522922020-11-14 16:35:34 +01001625 * On success, the key slot is locked. It is the responsibility of the caller
1626 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001627 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001628 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001629 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 * \param[out] p_slot On success, a pointer to the prepared slot.
1631 * \param[out] p_drv On any return, the driver for the key, if any.
1632 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001633 *
1634 * \retval #PSA_SUCCESS
1635 * The key slot is ready to receive key material.
1636 * \return If this function fails, the key slot is an invalid state.
1637 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 */
1639static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001640 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001642 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001643 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001644{
1645 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001646 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001647 psa_key_slot_t *slot;
1648
Gilles Peskinedf179142019-07-15 22:02:14 +02001649 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001650 *p_drv = NULL;
1651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001652 status = psa_validate_key_attributes(attributes, p_drv);
1653 if (status != PSA_SUCCESS) {
1654 return status;
1655 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1658 if (status != PSA_SUCCESS) {
1659 return status;
1660 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 slot = *p_slot;
1662
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001663 /* We're storing the declared bit-size of the key. It's up to each
1664 * creation mechanism to verify that this information is correct.
1665 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001666 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667 * is optional (import, copy). In case of a volatile key, assign it the
1668 * volatile key identifier associated to the slot returned to contain its
1669 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001670
1671 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001673#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1674 slot->attr.id = volatile_key_id;
1675#else
1676 slot->attr.id.key_id = volatile_key_id;
1677#endif
1678 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001679
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001681 * external-only flags, query `attributes`. Thanks to the check
1682 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001683 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001684 * may have set. */
1685 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001686
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001688 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001690 * create the key file in internal storage, create the
1691 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 * persistent data. This is done by starting a transaction that will
1693 * encompass these three actions.
1694 * For registering a volatile key, we just need to find an appropriate
1695 * slot number inside the SE. Since the key is designated volatile, creating
1696 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001697 /* The first thing to do is to find a slot number for the new key.
1698 * We save the slot number in persistent storage as part of the
1699 * transaction data. It will be needed to recover if the power
1700 * fails during the key creation process, to clean up on the secure
1701 * element side after restarting. Obtaining a slot number from the
1702 * secure element driver updates its persistent state, but we do not yet
1703 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001704 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001705 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001706 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001707 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1708 &slot_number);
1709 if (status != PSA_SUCCESS) {
1710 return status;
1711 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001713 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1714 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001715 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001716 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001717 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 status = psa_crypto_save_transaction();
1719 if (status != PSA_SUCCESS) {
1720 (void) psa_crypto_stop_transaction();
1721 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001723 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001724
1725 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001727 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001730 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001731 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001732 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001733#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001735 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001736}
Gilles Peskine4747d192019-04-17 15:05:45 +02001737
1738/** Finalize the creation of a key once its key material has been set.
1739 *
1740 * This entails writing the key to persistent storage.
1741 *
1742 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001743 * See the documentation of psa_start_key_creation() for the intended use
1744 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 *
Ronald Cron5c522922020-11-14 16:35:34 +01001746 * If the finalization succeeds, the function unlocks the key slot (it was
1747 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1748 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001749 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001750 * \param[in,out] slot Pointer to the slot with key material.
1751 * \param[in] driver The secure element driver for the key,
1752 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001753 * \param[out] key On success, identifier of the key. Note that the
1754 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001755 *
1756 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001757 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1759 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1760 * \retval #PSA_ERROR_ALREADY_EXISTS
1761 * \retval #PSA_ERROR_DATA_INVALID
1762 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001763 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001764 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001765 * \return If this function fails, the key slot is an invalid state.
1766 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001767 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001768static psa_status_t psa_finish_key_creation(
1769 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001770 psa_se_drv_table_entry_t *driver,
1771 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001772{
1773 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001774 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001775 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001776
1777#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001781 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001782 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001784
Gilles Peskineb46bef22019-07-30 21:32:04 +02001785#if defined(static_assert)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 static_assert(sizeof(slot_number) ==
1787 sizeof(data.slot_number),
1788 "Slot number size does not match psa_se_key_data_storage_t");
Gilles Peskineb46bef22019-07-30 21:32:04 +02001789#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001790 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1791 status = psa_save_persistent_key(&slot->attr,
1792 (uint8_t *) &data,
1793 sizeof(data));
1794 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001795#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1796 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001797 /* Key material is saved in export representation in the slot, so
1798 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799 status = psa_save_persistent_key(&slot->attr,
1800 slot->key.data,
1801 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001802 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001803 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001804#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1805
Gilles Peskinecbaff462019-07-12 23:46:04 +02001806#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001807 /* Finish the transaction for a key creation. This does not
1808 * happen when registering an existing key. Detect this case
1809 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001810 * creation of a persistent key in a secure element requires a transaction,
1811 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 if (driver != NULL &&
1813 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1814 status = psa_save_se_persistent_data(driver);
1815 if (status != PSA_SUCCESS) {
1816 psa_destroy_persistent_key(slot->attr.id);
1817 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001820 }
1821#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1822
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001824 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 status = psa_unlock_key_slot(slot);
1826 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001827 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001829 }
Ronald Cron50972942020-11-14 11:28:25 +01001830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001832}
1833
1834/** Abort the creation of a key.
1835 *
1836 * You may call this function after calling psa_start_key_creation(),
1837 * or after psa_finish_key_creation() fails. In other circumstances, this
1838 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001839 * See the documentation of psa_start_key_creation() for the intended use
1840 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 * \param[in,out] slot Pointer to the slot with key material.
1843 * \param[in] driver The secure element driver for the key,
1844 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846static void psa_fail_key_creation(psa_key_slot_t *slot,
1847 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001848{
Gilles Peskine011e4282019-06-26 18:34:38 +02001849 (void) driver;
1850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001851 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001854
1855#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001856 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 * element, and the failure happened later (when saving metadata
1858 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001859 * element.
1860 * https://github.com/ARMmbed/mbed-crypto/issues/217
1861 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001862
Gilles Peskined7729582019-08-05 15:55:54 +02001863 /* Abort the ongoing transaction if any (there may not be one if
1864 * the creation process failed before starting one, or if the
1865 * key creation is a registration of a key in a secure element).
1866 * Earlier functions must already have done what it takes to undo any
1867 * partial creation. All that's left is to update the transaction data
1868 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001872 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001873}
1874
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001875/** Validate optional attributes during key creation.
1876 *
1877 * Some key attributes are optional during key creation. If they are
1878 * specified in the attributes structure, check that they are consistent
1879 * with the data in the slot.
1880 *
1881 * This function should be called near the end of key creation, after
1882 * the slot in memory is fully populated but before saving persistent data.
1883 */
1884static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001885 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001886 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001887{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 if (attributes->core.type != 0) {
1889 if (attributes->core.type != slot->attr.type) {
1890 return PSA_ERROR_INVALID_ARGUMENT;
1891 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 }
1893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001894 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001895#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001896 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1897 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001898 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001899 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001901
Ronald Cron90857082020-11-25 14:58:33 +01001902 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903 slot->attr.type,
1904 slot->key.data,
1905 slot->key.bytes,
1906 &rsa);
1907 if (status != PSA_SUCCESS) {
1908 return status;
1909 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 mbedtls_mpi_init(&actual);
1912 mbedtls_mpi_init(&required);
1913 ret = mbedtls_rsa_export(rsa,
1914 NULL, NULL, NULL, NULL, &actual);
1915 mbedtls_rsa_free(rsa);
1916 mbedtls_free(rsa);
1917 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001918 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 }
1920 ret = mbedtls_mpi_read_binary(&required,
1921 attributes->domain_parameters,
1922 attributes->domain_parameters_size);
1923 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 }
1926 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 }
1929rsa_exit:
1930 mbedtls_mpi_free(&actual);
1931 mbedtls_mpi_free(&required);
1932 if (ret != 0) {
1933 return mbedtls_to_psa_error(ret);
1934 }
1935 } else
John Durkop9814fa22020-11-04 12:28:15 -08001936#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1937 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001940 }
1941 }
1942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001943 if (attributes->core.bits != 0) {
1944 if (attributes->core.bits != slot->attr.bits) {
1945 return PSA_ERROR_INVALID_ARGUMENT;
1946 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 }
1948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950}
1951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001952psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1953 const uint8_t *data,
1954 size_t data_length,
1955 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001956{
1957 psa_status_t status;
1958 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001959 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001960 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961
Ronald Cron81709fc2020-11-14 12:10:32 +01001962 *key = MBEDTLS_SVC_KEY_ID_INIT;
1963
Gilles Peskine0f84d622019-09-12 19:03:13 +02001964 /* Reject zero-length symmetric keys (including raw data key objects).
1965 * This also rejects any key which might be encoded as an empty string,
1966 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967 if (data_length == 0) {
1968 return PSA_ERROR_INVALID_ARGUMENT;
1969 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1972 &slot, &driver);
1973 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001977 /* In the case of a transparent key or an opaque key stored in local
1978 * storage (thus not in the case of generating a key in a secure element
1979 * or cryptoprocessor with storage), we have to allocate a buffer to
1980 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 if (slot->key.data == NULL) {
1982 status = psa_allocate_buffer_to_slot(slot, data_length);
1983 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001984 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001986 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001987
1988 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 status = psa_driver_wrapper_import_key(attributes,
1990 data, data_length,
1991 slot->key.data,
1992 slot->key.bytes,
1993 &slot->key.bytes, &bits);
1994 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001995 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001999 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002001 status = PSA_ERROR_INVALID_ARGUMENT;
2002 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002003 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 status = psa_validate_optional_attributes(slot, attributes);
2006 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002007 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 if (status != PSA_SUCCESS) {
2013 psa_fail_key_creation(slot, driver);
2014 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002016 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002017}
2018
Gilles Peskined7729582019-08-05 15:55:54 +02002019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2020psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002022{
2023 psa_status_t status;
2024 psa_key_slot_t *slot = NULL;
2025 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002027
2028 /* Leaving attributes unspecified is not currently supported.
2029 * It could make sense to query the key type and size from the
2030 * secure element, but not all secure elements support this
2031 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002032 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2033 return PSA_ERROR_NOT_SUPPORTED;
2034 }
2035 if (psa_get_key_bits(attributes) == 0) {
2036 return PSA_ERROR_NOT_SUPPORTED;
2037 }
Gilles Peskined7729582019-08-05 15:55:54 +02002038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2040 &slot, &driver);
2041 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002042 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 }
Gilles Peskined7729582019-08-05 15:55:54 +02002044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002046
2047exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002048 if (status != PSA_SUCCESS) {
2049 psa_fail_key_creation(slot, driver);
2050 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002051
Gilles Peskined7729582019-08-05 15:55:54 +02002052 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 psa_close_key(key);
2054 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002055}
2056#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2059 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002060{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002061 psa_status_t status = psa_copy_key_material_into_slot(target,
2062 source->key.data,
2063 source->key.bytes);
2064 if (status != PSA_SUCCESS) {
2065 return status;
2066 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002067
Steven Cooreman398aee52020-10-13 14:35:45 +02002068 target->attr.type = source->attr.type;
2069 target->attr.bits = source->attr.bits;
2070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002072}
2073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2075 const psa_key_attributes_t *specified_attributes,
2076 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002077{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002079 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002080 psa_key_slot_t *source_slot = NULL;
2081 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002082 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002083 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002084
Ronald Cron81709fc2020-11-14 12:10:32 +01002085 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2086
Ronald Cron5c522922020-11-14 16:35:34 +01002087 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2089 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002090 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002091 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 status = psa_validate_optional_attributes(source_slot,
2094 specified_attributes);
2095 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 status = psa_restrict_key_policy(source_slot->attr.type,
2100 &actual_attributes.core.policy,
2101 &source_slot->attr.policy);
2102 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2107 &target_slot, &driver);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002112#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002114 /* Copying to a secure element is not implemented yet. */
2115 status = PSA_ERROR_NOT_SUPPORTED;
2116 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002117 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002118#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002121 /*
2122 * Copying through an opaque driver is not implemented yet, consider
2123 * a lifetime with an external location as an invalid parameter for
2124 * now.
2125 */
2126 status = PSA_ERROR_INVALID_ARGUMENT;
2127 goto exit;
2128 }
2129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 status = psa_copy_key_material(source_slot, target_slot);
2131 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002132 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002136exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 if (status != PSA_SUCCESS) {
2138 psa_fail_key_creation(target_slot, driver);
2139 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002144}
2145
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002146
2147
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002148/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002149/* Message digests */
2150/****************************************************************/
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002155 if (operation->id == 0) {
2156 return PSA_SUCCESS;
2157 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002160 operation->id = 0;
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163}
2164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002165psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2166 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002169
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002171 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002172 status = PSA_ERROR_BAD_STATE;
2173 goto exit;
2174 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002177 status = PSA_ERROR_INVALID_ARGUMENT;
2178 goto exit;
2179 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002180
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002181 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2182 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002186
2187exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 if (status != PSA_SUCCESS) {
2189 psa_hash_abort(operation);
2190 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191
2192 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193}
2194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2196 const uint8_t *input,
2197 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002202 status = PSA_ERROR_BAD_STATE;
2203 goto exit;
2204 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002205
Steven Cooremanc8288352021-03-04 14:02:19 +01002206 /* Don't require hash implementations to behave correctly on a
2207 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 if (input_length == 0) {
2209 return PSA_SUCCESS;
2210 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002213
2214exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 if (status != PSA_SUCCESS) {
2216 psa_hash_abort(operation);
2217 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220}
2221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2223 uint8_t *hash,
2224 size_t hash_size,
2225 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002227 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 if (operation->id == 0) {
2229 return PSA_ERROR_BAD_STATE;
2230 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231
Steven Cooreman1e582352021-02-18 17:24:37 +01002232 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 operation, hash, hash_size, hash_length);
2234 psa_hash_abort(operation);
2235 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236}
2237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002238psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2239 const uint8_t *hash,
2240 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002242 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002244 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 operation,
2246 actual_hash, sizeof(actual_hash),
2247 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002250 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002254 status = PSA_ERROR_INVALID_SIGNATURE;
2255 goto exit;
2256 }
2257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002259 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002261
2262exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2264 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002265 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269}
2270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002271psa_status_t psa_hash_compute(psa_algorithm_t alg,
2272 const uint8_t *input, size_t input_length,
2273 uint8_t *hash, size_t hash_size,
2274 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002275{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002276 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 if (!PSA_ALG_IS_HASH(alg)) {
2278 return PSA_ERROR_INVALID_ARGUMENT;
2279 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2282 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002283}
2284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002285psa_status_t psa_hash_compare(psa_algorithm_t alg,
2286 const uint8_t *input, size_t input_length,
2287 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002288{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002289 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002290 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 if (!PSA_ALG_IS_HASH(alg)) {
2293 return PSA_ERROR_INVALID_ARGUMENT;
2294 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002295
Steven Cooreman1e582352021-02-18 17:24:37 +01002296 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002297 alg, input, input_length,
2298 actual_hash, sizeof(actual_hash),
2299 &actual_hash_length);
2300 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002301 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002302 }
2303 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
2305 goto exit;
2306 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002308 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002310
2311exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2313 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002314}
2315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2317 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002318{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 if (source_operation->id == 0 ||
2320 target_operation->id != 0) {
2321 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002324 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2325 target_operation);
2326 if (status != PSA_SUCCESS) {
2327 psa_hash_abort(target_operation);
2328 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002330 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002331}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332
2333
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002335/* MAC */
2336/****************************************************************/
2337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002339{
Steven Cooreman07897832021-03-19 18:28:56 +01002340 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 if (operation->id == 0) {
2342 return PSA_SUCCESS;
2343 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002345 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002346 operation->mac_size = 0;
2347 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002348 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002350 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351}
2352
Ronald Cron1c650a12021-06-17 16:33:22 +02002353static psa_status_t psa_mac_finalize_alg_and_key_validation(
2354 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002355 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002357{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 psa_key_type_t key_type = psa_get_key_type(attributes);
2360 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002362 if (!PSA_ALG_IS_MAC(alg)) {
2363 return PSA_ERROR_INVALID_ARGUMENT;
2364 }
Steven Cooreman07897832021-03-19 18:28:56 +01002365
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002366 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 status = psa_mac_key_can_do(alg, key_type);
2368 if (status != PSA_SUCCESS) {
2369 return status;
2370 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002371
Steven Cooremana6474de2021-05-10 11:13:41 +02002372 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002373 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002376 /* A very short MAC is too short for security since it can be
2377 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2378 * so we make this our minimum, even though 32 bits is still
2379 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002381 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2384 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002385 /* It's impossible to "truncate" to a larger length than the full length
2386 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002388 }
2389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002391 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2392 * that is disabled in the compile-time configuration. The result can
2393 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2394 * configuration into account. In this case, force a return of
2395 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2396 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2397 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2398 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2399 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002401 }
2402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002404}
2405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2407 mbedtls_svc_key_id_t key,
2408 psa_algorithm_t alg,
2409 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002410{
2411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2412 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002413 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002414
2415 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002417 status = PSA_ERROR_BAD_STATE;
2418 goto exit;
2419 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002420
2421 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002422 key,
2423 &slot,
2424 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2425 alg);
2426 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002427 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002428 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002429
2430 psa_key_attributes_t attributes = {
2431 .core = slot->attr
2432 };
2433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2435 &operation->mac_size);
2436 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002437 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002438 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002439
2440 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002441 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002442 if (is_sign) {
2443 status = psa_driver_wrapper_mac_sign_setup(operation,
2444 &attributes,
2445 slot->key.data,
2446 slot->key.bytes,
2447 alg);
2448 } else {
2449 status = psa_driver_wrapper_mac_verify_setup(operation,
2450 &attributes,
2451 slot->key.data,
2452 slot->key.bytes,
2453 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002454 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002455
Gilles Peskinefbfac682018-07-08 20:51:54 +02002456exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 if (status != PSA_SUCCESS) {
2458 psa_mac_abort(operation);
2459 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002463 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002464}
2465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2467 mbedtls_svc_key_id_t key,
2468 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002469{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002471}
2472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2474 mbedtls_svc_key_id_t key,
2475 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002476{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002477 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002478}
2479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2481 const uint8_t *input,
2482 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002484 if (operation->id == 0) {
2485 return PSA_ERROR_BAD_STATE;
2486 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487
Steven Cooreman11743f92021-03-19 18:38:46 +01002488 /* Don't require hash implementations to behave correctly on a
2489 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 if (input_length == 0) {
2491 return PSA_SUCCESS;
2492 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002494 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2495 input, input_length);
2496 if (status != PSA_SUCCESS) {
2497 psa_mac_abort(operation);
2498 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501}
2502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002503psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2504 uint8_t *mac,
2505 size_t mac_size,
2506 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002507{
Steven Cooreman87885df2021-03-19 19:04:39 +01002508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2509 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002511 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002512 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002513 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002514 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002517 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002518 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002519 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002520
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002521 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2522 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002524 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002525 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002526 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002528 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002529 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002530 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002531 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002532
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533 status = psa_driver_wrapper_mac_sign_finish(operation,
2534 mac, operation->mac_size,
2535 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002536
Dave Rodgman478ab542021-06-25 09:09:02 +01002537exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002538 /* In case of success, set the potential excess room in the output buffer
2539 * to an invalid value, to avoid potentially leaking a longer MAC.
2540 * In case of error, set the output length and content to a safe default,
2541 * such that in case the caller misses an error check, the output would be
2542 * an unachievable MAC.
2543 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002544 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002545 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002546 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002547 }
mohammad16036df908f2018-04-02 08:34:15 -07002548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002549 if (mac_size > operation->mac_size) {
2550 memset(&mac[operation->mac_size], '!',
2551 mac_size - operation->mac_size);
2552 }
Ronald Cron882eb782021-06-18 13:05:48 +02002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002556 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002557}
2558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2560 const uint8_t *mac,
2561 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562{
Steven Cooreman87885df2021-03-19 19:04:39 +01002563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2564 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002567 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002568 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002569 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002571 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002572 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002573 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002574 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002577 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002578 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002579 }
2580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 status = psa_driver_wrapper_mac_verify_finish(operation,
2582 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002583
Dave Rodgman478ab542021-06-25 09:09:02 +01002584exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002586
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002587 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588}
2589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002590static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2591 psa_algorithm_t alg,
2592 const uint8_t *input,
2593 size_t input_length,
2594 uint8_t *mac,
2595 size_t mac_size,
2596 size_t *mac_length,
2597 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002598{
2599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002600 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2601 psa_key_slot_t *slot;
2602 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603
Ronald Crondbb86462021-06-17 17:17:20 +02002604 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002605 key,
2606 &slot,
2607 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2608 alg);
2609 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002610 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002612
Ronald Crondbb86462021-06-17 17:17:20 +02002613 psa_key_attributes_t attributes = {
2614 .core = slot->attr
2615 };
2616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2618 &operation_mac_size);
2619 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002620 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002624 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002625 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002626 }
2627
2628 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002629 &attributes,
2630 slot->key.data, slot->key.bytes,
2631 alg,
2632 input, input_length,
2633 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002634
2635exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002636 /* In case of success, set the potential excess room in the output buffer
2637 * to an invalid value, to avoid potentially leaking a longer MAC.
2638 * In case of error, set the output length and content to a safe default,
2639 * such that in case the caller misses an error check, the output would be
2640 * an unachievable MAC.
2641 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002643 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002644 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002645 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 if (mac_size > operation_mac_size) {
2647 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2648 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002650 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002653}
2654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002655psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002656 psa_algorithm_t alg,
2657 const uint8_t *input,
2658 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002659 uint8_t *mac,
2660 size_t mac_size,
2661 size_t *mac_length)
2662{
2663 return psa_mac_compute_internal(key, alg,
2664 input, input_length,
2665 mac, mac_size, mac_length, 1);
2666}
2667
2668psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2669 psa_algorithm_t alg,
2670 const uint8_t *input,
2671 size_t input_length,
2672 const uint8_t *mac,
2673 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674{
2675 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002676 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2677 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 status = psa_mac_compute_internal(key, alg,
2680 input, input_length,
2681 actual_mac, sizeof(actual_mac),
2682 &actual_mac_length, 0);
2683 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002684 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002685 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002688 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002689 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002690 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002691 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002692 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002693 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002694 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002695
2696exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002697 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002699 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002700}
Gilles Peskine20035e32018-02-03 22:44:14 +01002701
Gilles Peskine20035e32018-02-03 22:44:14 +01002702/****************************************************************/
2703/* Asymmetric cryptography */
2704/****************************************************************/
2705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2707 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002708{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002709 if (input_is_message) {
2710 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2711 return PSA_ERROR_INVALID_ARGUMENT;
2712 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2715 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2716 return PSA_ERROR_INVALID_ARGUMENT;
2717 }
2718 }
2719 } else {
2720 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2721 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002722 }
2723 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002725 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002726}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002728static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2729 int input_is_message,
2730 psa_algorithm_t alg,
2731 const uint8_t *input,
2732 size_t input_length,
2733 uint8_t *signature,
2734 size_t signature_size,
2735 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002736{
2737 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2738 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2739 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002740
2741 *signature_length = 0;
2742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 status = psa_sign_verify_check_alg(input_is_message, alg);
2744 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002745 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002747
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002748 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002749 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002750 * buffer can in principle be empty since it doesn't actually have
2751 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 if (signature_size == 0) {
2753 return PSA_ERROR_BUFFER_TOO_SMALL;
2754 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002755
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002756 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 key, &slot,
2758 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2759 PSA_KEY_USAGE_SIGN_HASH,
2760 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002762 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002767 status = PSA_ERROR_INVALID_ARGUMENT;
2768 goto exit;
2769 }
2770
2771 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002773 };
2774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002776 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002777 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002778 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 signature, signature_size, signature_length);
2780 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002781
2782 status = psa_driver_wrapper_sign_hash(
2783 &attributes, slot->key.data, slot->key.bytes,
2784 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002786 }
2787
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002788
2789exit:
2790 /* Fill the unused part of the output buffer (the whole buffer on error,
2791 * the trailing part on success) with something that isn't a valid signature
2792 * (barring an attack on the signature and deliberately-crafted input),
2793 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 if (status == PSA_SUCCESS) {
2795 memset(signature + *signature_length, '!',
2796 signature_size - *signature_length);
2797 } else {
2798 memset(signature, '!', signature_size);
2799 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002800 /* If signature_size is 0 then we have nothing to do. We must not call
2801 * memset because signature may be NULL in this case. */
2802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806}
2807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2809 int input_is_message,
2810 psa_algorithm_t alg,
2811 const uint8_t *input,
2812 size_t input_length,
2813 const uint8_t *signature,
2814 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002815{
2816 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2817 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2818 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 status = psa_sign_verify_check_alg(input_is_message, alg);
2821 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002822 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002824
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002825 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826 key, &slot,
2827 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2828 PSA_KEY_USAGE_VERIFY_HASH,
2829 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 if (status != PSA_SUCCESS) {
2832 return status;
2833 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002834
2835 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002837 };
2838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002840 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002842 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 signature, signature_length);
2844 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002845 status = psa_driver_wrapper_verify_hash(
2846 &attributes, slot->key.data, slot->key.bytes,
2847 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002849 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002854
2855}
2856
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002857psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002858 const psa_key_attributes_t *attributes,
2859 const uint8_t *key_buffer,
2860 size_t key_buffer_size,
2861 psa_algorithm_t alg,
2862 const uint8_t *input,
2863 size_t input_length,
2864 uint8_t *signature,
2865 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002867{
2868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002871 size_t hash_length;
2872 uint8_t hash[PSA_HASH_MAX_SIZE];
2873
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002874 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 PSA_ALG_SIGN_GET_HASH(alg),
2876 input, input_length,
2877 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002880 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002882
2883 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 attributes, key_buffer, key_buffer_size,
2885 alg, hash, hash_length,
2886 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002887 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002890}
2891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2893 psa_algorithm_t alg,
2894 const uint8_t *input,
2895 size_t input_length,
2896 uint8_t *signature,
2897 size_t signature_size,
2898 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002899{
2900 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002901 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002903}
2904
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002905psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002906 const psa_key_attributes_t *attributes,
2907 const uint8_t *key_buffer,
2908 size_t key_buffer_size,
2909 psa_algorithm_t alg,
2910 const uint8_t *input,
2911 size_t input_length,
2912 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002914{
2915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002918 size_t hash_length;
2919 uint8_t hash[PSA_HASH_MAX_SIZE];
2920
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002921 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 PSA_ALG_SIGN_GET_HASH(alg),
2923 input, input_length,
2924 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002927 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002929
2930 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 attributes, key_buffer, key_buffer_size,
2932 alg, hash, hash_length,
2933 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002934 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002937}
2938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2940 psa_algorithm_t alg,
2941 const uint8_t *input,
2942 size_t input_length,
2943 const uint8_t *signature,
2944 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002945{
2946 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002947 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002949}
2950
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002951psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002952 const psa_key_attributes_t *attributes,
2953 const uint8_t *key_buffer, size_t key_buffer_size,
2954 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002956{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2958 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2959 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002960#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2962 return mbedtls_psa_rsa_sign_hash(
2963 attributes,
2964 key_buffer, key_buffer_size,
2965 alg, hash, hash_length,
2966 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002967#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2968 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 } else {
2970 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002971 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2973 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002974#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2976 return mbedtls_psa_ecdsa_sign_hash(
2977 attributes,
2978 key_buffer, key_buffer_size,
2979 alg, hash, hash_length,
2980 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002981#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2982 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 } else {
2984 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002985 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002986 }
Ronald Cron4501c982021-03-19 20:09:32 +01002987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 (void) key_buffer;
2989 (void) key_buffer_size;
2990 (void) hash;
2991 (void) hash_length;
2992 (void) signature;
2993 (void) signature_size;
2994 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002997}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3000 psa_algorithm_t alg,
3001 const uint8_t *hash,
3002 size_t hash_length,
3003 uint8_t *signature,
3004 size_t signature_size,
3005 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003006{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003007 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003008 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003010}
3011
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003012psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003013 const psa_key_attributes_t *attributes,
3014 const uint8_t *key_buffer, size_t key_buffer_size,
3015 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003017{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3019 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3020 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003021#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3023 return mbedtls_psa_rsa_verify_hash(
3024 attributes,
3025 key_buffer, key_buffer_size,
3026 alg, hash, hash_length,
3027 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003028#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3029 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 } else {
3031 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003032 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3034 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003035#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003036 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3037 return mbedtls_psa_ecdsa_verify_hash(
3038 attributes,
3039 key_buffer, key_buffer_size,
3040 alg, hash, hash_length,
3041 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003042#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3043 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 } else {
3045 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003046 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003047 }
Ronald Cron4501c982021-03-19 20:09:32 +01003048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 (void) key_buffer;
3050 (void) key_buffer_size;
3051 (void) hash;
3052 (void) hash_length;
3053 (void) signature;
3054 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003057}
3058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3060 psa_algorithm_t alg,
3061 const uint8_t *hash,
3062 size_t hash_length,
3063 const uint8_t *signature,
3064 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003065{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003066 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003067 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003069}
3070
John Durkop0e005192020-10-31 22:06:54 -07003071#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3073 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3076 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3077 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3078 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003079}
John Durkop0e005192020-10-31 22:06:54 -07003080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3083 psa_algorithm_t alg,
3084 const uint8_t *input,
3085 size_t input_length,
3086 const uint8_t *salt,
3087 size_t salt_length,
3088 uint8_t *output,
3089 size_t output_size,
3090 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003094 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003095
Darryl Green5cc689a2018-07-24 15:34:10 +01003096 (void) input;
3097 (void) input_length;
3098 (void) salt;
3099 (void) output;
3100 (void) output_size;
3101
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003102 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3105 return PSA_ERROR_INVALID_ARGUMENT;
3106 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003107
Ronald Cron5c522922020-11-14 16:35:34 +01003108 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3110 if (status != PSA_SUCCESS) {
3111 return status;
3112 }
3113 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3114 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115 status = PSA_ERROR_INVALID_ARGUMENT;
3116 goto exit;
3117 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003120#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003121 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003122 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3124 slot->key.data,
3125 slot->key.bytes,
3126 &rsa);
3127 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003132 status = PSA_ERROR_BUFFER_TOO_SMALL;
3133 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003134 }
Ronald Cron7207d572021-09-08 14:28:35 +02003135#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3136 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003138#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003139 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 mbedtls_rsa_pkcs1_encrypt(rsa,
3141 mbedtls_psa_get_random,
3142 MBEDTLS_PSA_RANDOM_STATE,
3143 MBEDTLS_RSA_PUBLIC,
3144 input_length,
3145 input,
3146 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003147#else
3148 status = PSA_ERROR_NOT_SUPPORTED;
3149#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 } else
3151 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003152#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003154 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3156 mbedtls_psa_get_random,
3157 MBEDTLS_PSA_RANDOM_STATE,
3158 MBEDTLS_RSA_PUBLIC,
3159 salt, salt_length,
3160 input_length,
3161 input,
3162 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003163#else
3164 status = PSA_ERROR_NOT_SUPPORTED;
3165#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 }
Ronald Cron7207d572021-09-08 14:28:35 +02003169#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003171rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 if (status == PSA_SUCCESS) {
3173 *output_length = mbedtls_rsa_get_len(rsa);
3174 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 mbedtls_rsa_free(rsa);
3177 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003178#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003179 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003181 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003183
3184exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003188}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3191 psa_algorithm_t alg,
3192 const uint8_t *input,
3193 size_t input_length,
3194 const uint8_t *salt,
3195 size_t salt_length,
3196 uint8_t *output,
3197 size_t output_size,
3198 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003201 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003202 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003203
Darryl Green5cc689a2018-07-24 15:34:10 +01003204 (void) input;
3205 (void) input_length;
3206 (void) salt;
3207 (void) output;
3208 (void) output_size;
3209
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003210 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3213 return PSA_ERROR_INVALID_ARGUMENT;
3214 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003215
Ronald Cron5c522922020-11-14 16:35:34 +01003216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3218 if (status != PSA_SUCCESS) {
3219 return status;
3220 }
3221 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003222 status = PSA_ERROR_INVALID_ARGUMENT;
3223 goto exit;
3224 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003227#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003229 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3231 slot->key.data,
3232 slot->key.bytes,
3233 &rsa);
3234 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003239 status = PSA_ERROR_INVALID_ARGUMENT;
3240 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003241 }
Ronald Cron7207d572021-09-08 14:28:35 +02003242#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3243 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003246#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003247 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248 mbedtls_rsa_pkcs1_decrypt(rsa,
3249 mbedtls_psa_get_random,
3250 MBEDTLS_PSA_RANDOM_STATE,
3251 MBEDTLS_RSA_PRIVATE,
3252 output_length,
3253 input,
3254 output,
3255 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003256#else
3257 status = PSA_ERROR_NOT_SUPPORTED;
3258#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 } else
3260 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003261#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003263 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3265 mbedtls_psa_get_random,
3266 MBEDTLS_PSA_RANDOM_STATE,
3267 MBEDTLS_RSA_PRIVATE,
3268 salt, salt_length,
3269 output_length,
3270 input,
3271 output,
3272 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003273#else
3274 status = PSA_ERROR_NOT_SUPPORTED;
3275#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003277 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003279
Ronald Cron7207d572021-09-08 14:28:35 +02003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003282rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 mbedtls_rsa_free(rsa);
3284 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003285#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3286 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003288 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003290
3291exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003295}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003296
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003297
3298
mohammad1603503973b2018-03-12 15:59:30 +02003299/****************************************************************/
3300/* Symmetric cryptography */
3301/****************************************************************/
3302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3304 mbedtls_svc_key_id_t key,
3305 psa_algorithm_t alg,
3306 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003307{
3308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3309 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003310 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3312 PSA_KEY_USAGE_ENCRYPT :
3313 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003314
3315 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003317 status = PSA_ERROR_BAD_STATE;
3318 goto exit;
3319 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003322 status = PSA_ERROR_INVALID_ARGUMENT;
3323 goto exit;
3324 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003326 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3327 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003328 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003329 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003330
Ronald Cron49fafa92021-03-10 08:34:23 +01003331 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003332 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003333 * so we only set it (in the driver wrapper) after resources have been
3334 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003335 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003337 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003339 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003340 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003341 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 }
3343 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003344
Ronald Crona4af55f2020-12-14 14:36:06 +01003345 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003347 };
3348
Ronald Cronab99ac22020-10-01 16:38:28 +02003349 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003350 if (cipher_operation == MBEDTLS_ENCRYPT) {
3351 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3352 &attributes,
3353 slot->key.data,
3354 slot->key.bytes,
3355 alg);
3356 } else {
3357 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3358 &attributes,
3359 slot->key.data,
3360 slot->key.bytes,
3361 alg);
3362 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003363
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003364exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 if (status != PSA_SUCCESS) {
3366 psa_cipher_abort(operation);
3367 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003371 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003372}
3373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003374psa_status_t psa_cipher_encrypt_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_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003379}
3380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3382 mbedtls_svc_key_id_t key,
3383 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003384{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003386}
3387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003388psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3389 uint8_t *iv,
3390 size_t iv_size,
3391 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003392{
Ronald Cron6d051732020-10-01 14:10:20 +02003393 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003394 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3395 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003398 status = PSA_ERROR_BAD_STATE;
3399 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003400 }
3401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003403 status = PSA_ERROR_BAD_STATE;
3404 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003405 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003406
Ronald Cronc423acb2021-07-05 12:31:44 +02003407 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003408 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003409 status = PSA_ERROR_BUFFER_TOO_SMALL;
3410 goto exit;
3411 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003413 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003414 status = PSA_ERROR_GENERIC_ERROR;
3415 goto exit;
3416 }
3417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003418 status = psa_generate_random(local_iv, default_iv_length);
3419 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003420 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 }
Ronald Cron5618a392021-03-26 09:52:26 +01003422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 status = psa_driver_wrapper_cipher_set_iv(operation,
3424 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003425
3426exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 if (status == PSA_SUCCESS) {
3428 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003429 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003430 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003432 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003434 }
Ronald Cron6d051732020-10-01 14:10:20 +02003435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003436 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003437}
3438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3440 const uint8_t *iv,
3441 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003442{
Ronald Cron6d051732020-10-01 14:10:20 +02003443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003445 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003446 status = PSA_ERROR_BAD_STATE;
3447 goto exit;
3448 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003450 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003451 status = PSA_ERROR_BAD_STATE;
3452 goto exit;
3453 }
Ronald Crona0d68172021-03-26 10:15:08 +01003454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003455 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003456 status = PSA_ERROR_INVALID_ARGUMENT;
3457 goto exit;
3458 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 status = psa_driver_wrapper_cipher_set_iv(operation,
3461 iv,
3462 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003463
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003464exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003465 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003466 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 } else {
3468 psa_cipher_abort(operation);
3469 }
3470 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003471}
3472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3474 const uint8_t *input,
3475 size_t input_length,
3476 uint8_t *output,
3477 size_t output_size,
3478 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003479{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003482 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003483 status = PSA_ERROR_BAD_STATE;
3484 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003485 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003487 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003488 status = PSA_ERROR_BAD_STATE;
3489 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003490 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 status = psa_driver_wrapper_cipher_update(operation,
3493 input,
3494 input_length,
3495 output,
3496 output_size,
3497 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003498
3499exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 if (status != PSA_SUCCESS) {
3501 psa_cipher_abort(operation);
3502 }
Ronald Cron6d051732020-10-01 14:10:20 +02003503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003504 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003505}
3506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3508 uint8_t *output,
3509 size_t output_size,
3510 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003511{
David Saadab4ecc272019-02-14 13:48:10 +02003512 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003514 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003515 status = PSA_ERROR_BAD_STATE;
3516 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003517 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003520 status = PSA_ERROR_BAD_STATE;
3521 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003522 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003524 status = psa_driver_wrapper_cipher_finish(operation,
3525 output,
3526 output_size,
3527 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003528
3529exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530 if (status == PSA_SUCCESS) {
3531 return psa_cipher_abort(operation);
3532 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003533 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003537 }
mohammad1603503973b2018-03-12 15:59:30 +02003538}
3539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003540psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003541{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003542 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003543 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003544 * in use. It's ok to call abort on such an object, and there's
3545 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003547 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003550
Ronald Cron49fafa92021-03-10 08:34:23 +01003551 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003552 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003553 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003555 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003556}
3557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003558psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3559 psa_algorithm_t alg,
3560 const uint8_t *input,
3561 size_t input_length,
3562 uint8_t *output,
3563 size_t output_size,
3564 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003565{
3566 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003567 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003568 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003569 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3570 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003572 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003573 status = PSA_ERROR_INVALID_ARGUMENT;
3574 goto exit;
3575 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003577 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3578 PSA_KEY_USAGE_ENCRYPT,
3579 alg);
3580 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003581 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003582 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003583
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003584 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003585 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003586 };
3587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003588 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3589 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003590 status = PSA_ERROR_GENERIC_ERROR;
3591 goto exit;
3592 }
3593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003594 if (default_iv_length > 0) {
3595 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003596 status = PSA_ERROR_BUFFER_TOO_SMALL;
3597 goto exit;
3598 }
3599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 status = psa_generate_random(local_iv, default_iv_length);
3601 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003602 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003604 }
3605
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003606 status = psa_driver_wrapper_cipher_encrypt(
3607 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003608 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609 mbedtls_buffer_offset(output, default_iv_length),
3610 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003611
3612exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 unlock_status = psa_unlock_key_slot(slot);
3614 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003615 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003616 }
Ronald Cron16377072021-07-08 19:00:07 +02003617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003618 if (status == PSA_SUCCESS) {
3619 if (default_iv_length > 0) {
3620 memcpy(output, local_iv, default_iv_length);
3621 }
3622 *output_length += default_iv_length;
3623 } else {
3624 *output_length = 0;
3625 }
3626
3627 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003628}
3629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003630psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3631 psa_algorithm_t alg,
3632 const uint8_t *input,
3633 size_t input_length,
3634 uint8_t *output,
3635 size_t output_size,
3636 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003637{
3638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003639 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003640 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003642 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003643 status = PSA_ERROR_INVALID_ARGUMENT;
3644 goto exit;
3645 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003647 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3648 PSA_KEY_USAGE_DECRYPT,
3649 alg);
3650 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003651 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003652 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003653
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003654 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003656 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003659 status = PSA_ERROR_INVALID_ARGUMENT;
3660 goto exit;
3661 }
3662
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003663 status = psa_driver_wrapper_cipher_decrypt(
3664 &attributes, slot->key.data, slot->key.bytes,
3665 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003667
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003668exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 unlock_status = psa_unlock_key_slot(slot);
3670 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003671 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003675 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003676 }
Ronald Cron16377072021-07-08 19:00:07 +02003677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003679}
3680
3681
mohammad16035955c982018-04-26 00:53:03 +03003682/****************************************************************/
3683/* AEAD */
3684/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003686psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3687 psa_algorithm_t alg,
3688 const uint8_t *nonce,
3689 size_t nonce_length,
3690 const uint8_t *additional_data,
3691 size_t additional_data_length,
3692 const uint8_t *plaintext,
3693 size_t plaintext_length,
3694 uint8_t *ciphertext,
3695 size_t ciphertext_size,
3696 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003697{
Ronald Cron215633c2021-03-16 17:15:37 +01003698 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3699 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003700
mohammad1603f08a5502018-06-03 15:05:47 +03003701 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003703 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3704 return PSA_ERROR_NOT_SUPPORTED;
3705 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003706
Ronald Cron9a986162021-03-26 12:40:07 +01003707 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003708 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3709 if (status != PSA_SUCCESS) {
3710 return status;
3711 }
mohammad16035955c982018-04-26 00:53:03 +03003712
Ronald Cron215633c2021-03-16 17:15:37 +01003713 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003715 };
mohammad16035955c982018-04-26 00:53:03 +03003716
Ronald Cronde822812021-03-17 16:08:20 +01003717 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003718 &attributes, slot->key.data, slot->key.bytes,
3719 alg,
3720 nonce, nonce_length,
3721 additional_data, additional_data_length,
3722 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3726 memset(ciphertext, 0, ciphertext_size);
3727 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003732}
3733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3735 psa_algorithm_t alg,
3736 const uint8_t *nonce,
3737 size_t nonce_length,
3738 const uint8_t *additional_data,
3739 size_t additional_data_length,
3740 const uint8_t *ciphertext,
3741 size_t ciphertext_length,
3742 uint8_t *plaintext,
3743 size_t plaintext_size,
3744 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003745{
Ronald Cron215633c2021-03-16 17:15:37 +01003746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3747 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003748
Gilles Peskineee652a32018-06-01 19:23:52 +02003749 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003751 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3752 return PSA_ERROR_NOT_SUPPORTED;
3753 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003754
Ronald Cron9a986162021-03-26 12:40:07 +01003755 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3757 if (status != PSA_SUCCESS) {
3758 return status;
3759 }
mohammad16035955c982018-04-26 00:53:03 +03003760
Ronald Cron215633c2021-03-16 17:15:37 +01003761 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003763 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003764
Ronald Cronde822812021-03-17 16:08:20 +01003765 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003766 &attributes, slot->key.data, slot->key.bytes,
3767 alg,
3768 nonce, nonce_length,
3769 additional_data, additional_data_length,
3770 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 if (status != PSA_SUCCESS && plaintext_size != 0) {
3774 memset(plaintext, 0, plaintext_size);
3775 }
mohammad160360a64d02018-06-03 17:20:42 +03003776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003777 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 return status;
mohammad16035955c982018-04-26 00:53:03 +03003780}
3781
Gilles Peskinee59236f2018-01-27 23:32:46 +01003782/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003783/* Generators */
3784/****************************************************************/
3785
John Durkop07cc04a2020-11-16 22:08:34 -08003786#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3787 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3788 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3789#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003790#endif /* At least one builtin KDF */
3791
3792#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3793 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3794 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3795static psa_status_t psa_key_derivation_start_hmac(
3796 psa_mac_operation_t *operation,
3797 psa_algorithm_t hash_alg,
3798 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003800{
3801 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3804 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3805 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003806
Steven Cooreman15f0d922021-05-07 14:14:37 +02003807 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003810 status = psa_driver_wrapper_mac_sign_setup(operation,
3811 &attributes,
3812 hmac_key, hmac_key_length,
3813 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 psa_reset_key_attributes(&attributes);
3816 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003817}
3818#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003819
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003820#define HKDF_STATE_INIT 0 /* no input yet */
3821#define HKDF_STATE_STARTED 1 /* got salt */
3822#define HKDF_STATE_KEYED 2 /* got key */
3823#define HKDF_STATE_OUTPUT 3 /* output started */
3824
Gilles Peskinecbe66502019-05-16 16:59:18 +02003825static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003826 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003827{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3829 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3830 } else {
3831 return operation->alg;
3832 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003833}
3834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003835psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003836{
3837 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3839 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003840 /* The object has (apparently) been initialized but it is not
3841 * in use. It's ok to call abort on such an object, and there's
3842 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 } else
John Durkopd0321952020-10-29 21:37:36 -07003844#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3846 mbedtls_free(operation->ctx.hkdf.info);
3847 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3848 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003849#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3850#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3851 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003852 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3853 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3854 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3855 if (operation->ctx.tls12_prf.secret != NULL) {
3856 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3857 operation->ctx.tls12_prf.secret_length);
3858 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003859 }
3860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 if (operation->ctx.tls12_prf.seed != NULL) {
3862 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3863 operation->ctx.tls12_prf.seed_length);
3864 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003865 }
3866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867 if (operation->ctx.tls12_prf.label != NULL) {
3868 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3869 operation->ctx.tls12_prf.label_length);
3870 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003871 }
3872
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003873 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003874
3875 /* We leave the fields Ai and output_block to be erased safely by the
3876 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003878#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3879 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003880 {
3881 status = PSA_ERROR_BAD_STATE;
3882 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 mbedtls_platform_zeroize(operation, sizeof(*operation));
3884 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003885}
3886
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003887psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003890 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003891 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003893 }
3894
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003895 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003896 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003897}
3898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3900 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003901{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902 if (operation->alg == 0) {
3903 return PSA_ERROR_BAD_STATE;
3904 }
3905 if (capacity > operation->capacity) {
3906 return PSA_ERROR_INVALID_ARGUMENT;
3907 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003908 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003910}
3911
John Durkopd0321952020-10-29 21:37:36 -07003912#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003913/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003914 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3916 psa_algorithm_t hash_alg,
3917 uint8_t *output,
3918 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003919{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003921 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003922 psa_status_t status;
3923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003924 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3925 return PSA_ERROR_BAD_STATE;
3926 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003927 hkdf->state = HKDF_STATE_OUTPUT;
3928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003930 /* Copy what remains of the current block */
3931 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003933 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003934 }
3935 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 output += n;
3937 output_length -= n;
3938 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003939 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003940 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003942 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003943 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003944 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003945 * object was corrupted or if this function is called directly
3946 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003947 if (hkdf->block_number == 0xff) {
3948 return PSA_ERROR_BAD_STATE;
3949 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003950
3951 /* We need a new block */
3952 ++hkdf->block_number;
3953 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003954
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003955 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3956 hash_alg,
3957 hkdf->prk,
3958 hash_length);
3959 if (status != PSA_SUCCESS) {
3960 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003961 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003962
3963 if (hkdf->block_number != 1) {
3964 status = psa_mac_update(&hkdf->hmac,
3965 hkdf->output_block,
3966 hash_length);
3967 if (status != PSA_SUCCESS) {
3968 return status;
3969 }
3970 }
3971 status = psa_mac_update(&hkdf->hmac,
3972 hkdf->info,
3973 hkdf->info_length);
3974 if (status != PSA_SUCCESS) {
3975 return status;
3976 }
3977 status = psa_mac_update(&hkdf->hmac,
3978 &hkdf->block_number, 1);
3979 if (status != PSA_SUCCESS) {
3980 return status;
3981 }
3982 status = psa_mac_sign_finish(&hkdf->hmac,
3983 hkdf->output_block,
3984 sizeof(hkdf->output_block),
3985 &hmac_output_length);
3986 if (status != PSA_SUCCESS) {
3987 return status;
3988 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003989 }
3990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003991 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003992}
John Durkop07cc04a2020-11-16 22:08:34 -08003993#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003994
John Durkop07cc04a2020-11-16 22:08:34 -08003995#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3996 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003997static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3998 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004000{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004001 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4002 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004003 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4004 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004005 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004006
Janos Follath7742fee2019-06-17 12:58:10 +01004007 /* We can't be wanting more output after block 0xff, otherwise
4008 * the capacity check in psa_key_derivation_output_bytes() would have
4009 * prevented this call. It could happen only if the operation
4010 * object was corrupted or if this function is called directly
4011 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004012 if (tls12_prf->block_number == 0xff) {
4013 return PSA_ERROR_CORRUPTION_DETECTED;
4014 }
Janos Follath7742fee2019-06-17 12:58:10 +01004015
4016 /* We need a new block */
4017 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004018 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004019
4020 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4021 *
4022 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4023 *
4024 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4025 * HMAC_hash(secret, A(2) + seed) +
4026 * HMAC_hash(secret, A(3) + seed) + ...
4027 *
4028 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004029 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004030 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004031 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004032 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004033 * is currently extracted as `output_block` and where i is
4034 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004035 */
4036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 status = psa_key_derivation_start_hmac(&hmac,
4038 hash_alg,
4039 tls12_prf->secret,
4040 tls12_prf->secret_length);
4041 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004042 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004044
4045 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004047 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4048 * the variable seed and in this instance means it in the context of the
4049 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004050 status = psa_mac_update(&hmac,
4051 tls12_prf->label,
4052 tls12_prf->label_length);
4053 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004054 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004055 }
4056 status = psa_mac_update(&hmac,
4057 tls12_prf->seed,
4058 tls12_prf->seed_length);
4059 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004060 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 }
4062 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004063 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4065 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004066 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004068 }
4069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 status = psa_mac_sign_finish(&hmac,
4071 tls12_prf->Ai, hash_length,
4072 &hmac_output_length);
4073 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004074 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004075 }
4076 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004077 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004078 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004079
4080 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 status = psa_key_derivation_start_hmac(&hmac,
4082 hash_alg,
4083 tls12_prf->secret,
4084 tls12_prf->secret_length);
4085 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004086 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 }
4088 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4089 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004090 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 }
4092 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4093 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004094 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004095 }
4096 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4097 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004098 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004099 }
4100 status = psa_mac_sign_finish(&hmac,
4101 tls12_prf->output_block, hash_length,
4102 &hmac_output_length);
4103 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004104 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004106
Janos Follath7742fee2019-06-17 12:58:10 +01004107
4108cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 cleanup_status = psa_mac_abort(&hmac);
4110 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004111 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004114 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004115}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004116
Janos Follath844eb0e2019-06-19 12:10:49 +01004117static psa_status_t psa_key_derivation_tls12_prf_read(
4118 psa_tls12_prf_key_derivation_t *tls12_prf,
4119 psa_algorithm_t alg,
4120 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004121 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004122{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4124 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004125 psa_status_t status;
4126 uint8_t offset, length;
4127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004128 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004129 case PSA_TLS12_PRF_STATE_LABEL_SET:
4130 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4131 break;
4132 case PSA_TLS12_PRF_STATE_OUTPUT:
4133 break;
4134 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004135 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004136 }
4137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004139 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 if (tls12_prf->left_in_block == 0) {
4141 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4142 alg);
4143 if (status != PSA_SUCCESS) {
4144 return status;
4145 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004146
4147 continue;
4148 }
4149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004151 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004152 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004153 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004155
4156 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004158 output += length;
4159 output_length -= length;
4160 tls12_prf->left_in_block -= length;
4161 }
4162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004164}
John Durkop07cc04a2020-11-16 22:08:34 -08004165#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4166 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004167
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004168psa_status_t psa_key_derivation_output_bytes(
4169 psa_key_derivation_operation_t *operation,
4170 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004171 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004172{
4173 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004178 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004179 }
4180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004182 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004185 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 goto exit;
4187 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004188 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004190 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4192 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004195 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004196 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004198
John Durkopd0321952020-10-29 21:37:36 -07004199#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4201 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4202 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4203 output, output_length);
4204 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004205#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4206#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4207 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004208 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4209 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4210 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4211 kdf_alg, output,
4212 output_length);
4213 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004214#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4215 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004217 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219 }
4220
4221exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004222 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004223 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 * This allows us to differentiate between exhausted operations and
4225 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4226 * operations. */
4227 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004229 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004230 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004231 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004233}
4234
David Brown7807bf72021-02-09 16:28:23 -07004235#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004236static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004237{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 if (data_size >= 8) {
4239 mbedtls_des_key_set_parity(data);
4240 }
4241 if (data_size >= 16) {
4242 mbedtls_des_key_set_parity(data + 8);
4243 }
4244 if (data_size >= 24) {
4245 mbedtls_des_key_set_parity(data + 16);
4246 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004247}
David Brown7807bf72021-02-09 16:28:23 -07004248#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004249
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004250static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004251 psa_key_slot_t *slot,
4252 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004253 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004254{
4255 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004257 psa_status_t status;
4258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 if (!key_type_is_raw_bytes(slot->attr.type)) {
4260 return PSA_ERROR_INVALID_ARGUMENT;
4261 }
4262 if (bits % 8 != 0) {
4263 return PSA_ERROR_INVALID_ARGUMENT;
4264 }
4265 data = mbedtls_calloc(1, bytes);
4266 if (data == NULL) {
4267 return PSA_ERROR_INSUFFICIENT_MEMORY;
4268 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 status = psa_key_derivation_output_bytes(operation, data, bytes);
4271 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004272 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273 }
David Brown12ca5032021-02-11 11:02:00 -07004274#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004275 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4276 psa_des_set_key_parity(data, bytes);
4277 }
David Brown8107e312021-02-12 08:08:46 -07004278#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004280 status = psa_allocate_buffer_to_slot(slot, bytes);
4281 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004282 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 }
Ronald Crondd04d422020-11-28 15:14:42 +01004284
Ronald Cronbf33c932020-11-28 18:06:53 +01004285 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004286 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004288 };
4289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 status = psa_driver_wrapper_import_key(&attributes,
4291 data, bytes,
4292 slot->key.data,
4293 slot->key.bytes,
4294 &slot->key.bytes, &bits);
4295 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004296 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004298
4299exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300 mbedtls_free(data);
4301 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004302}
4303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4305 psa_key_derivation_operation_t *operation,
4306 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004307{
4308 psa_status_t status;
4309 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004310 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004311
Ronald Cron81709fc2020-11-14 12:10:32 +01004312 *key = MBEDTLS_SVC_KEY_ID_INIT;
4313
Gilles Peskine0f84d622019-09-12 19:03:13 +02004314 /* Reject any attempt to create a zero-length key so that we don't
4315 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 if (psa_get_key_bits(attributes) == 0) {
4317 return PSA_ERROR_INVALID_ARGUMENT;
4318 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 if (operation->alg == PSA_ALG_NONE) {
4321 return PSA_ERROR_BAD_STATE;
4322 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 if (!operation->can_output_key) {
4325 return PSA_ERROR_NOT_PERMITTED;
4326 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4329 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004331 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004332 /* Deriving a key in a secure element is not implemented yet. */
4333 status = PSA_ERROR_NOT_SUPPORTED;
4334 }
4335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004336 if (status == PSA_SUCCESS) {
4337 status = psa_generate_derived_key_internal(slot,
4338 attributes->core.bits,
4339 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004340 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (status == PSA_SUCCESS) {
4342 status = psa_finish_key_creation(slot, driver, key);
4343 }
4344 if (status != PSA_SUCCESS) {
4345 psa_fail_key_creation(slot, driver);
4346 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004349}
4350
Gilles Peskineeab56e42018-07-12 17:12:33 +02004351
4352
4353/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004354/* Key derivation */
4355/****************************************************************/
4356
Steven Cooreman932ffb72021-02-15 12:14:32 +01004357#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004359{
4360#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4362 return 1;
4363 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004364#endif
4365#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4367 return 1;
4368 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004369#endif
4370#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4372 return 1;
4373 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004374#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004375 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004376}
4377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004379{
4380 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 psa_status_t status = psa_hash_setup(&operation, alg);
4382 psa_hash_abort(&operation);
4383 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004384}
4385
Gilles Peskine969c5d62019-01-16 15:53:06 +01004386static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004387 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004389{
Janos Follath5fe19732019-06-20 15:09:30 +01004390 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4391 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004392 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004393
Gilles Peskine969c5d62019-01-16 15:53:06 +01004394 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004395 if (!is_kdf_alg_supported(kdf_alg)) {
4396 return PSA_ERROR_NOT_SUPPORTED;
4397 }
John Durkop07cc04a2020-11-16 22:08:34 -08004398
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004399 /* All currently supported key derivation algorithms are based on a
4400 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4402 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4403 if (hash_size == 0) {
4404 return PSA_ERROR_NOT_SUPPORTED;
4405 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004406
4407 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4408 * we might fail later, which is somewhat unfriendly and potentially
4409 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004410 psa_status_t status = psa_hash_try_support(hash_alg);
4411 if (status != PSA_SUCCESS) {
4412 return status;
4413 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004415 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4416 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4417 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4418 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004419 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004420
Gilles Peskinecdacf042021-04-29 21:10:00 +02004421 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004423}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004425static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004426{
4427#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004428 if (alg == PSA_ALG_ECDH) {
4429 return PSA_SUCCESS;
4430 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004431#endif
4432 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004434}
John Durkop07cc04a2020-11-16 22:08:34 -08004435#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4438 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004439{
4440 psa_status_t status;
4441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 if (operation->alg != 0) {
4443 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004444 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4447 return PSA_ERROR_INVALID_ARGUMENT;
4448 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4449#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4450 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4451 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4452 status = psa_key_agreement_try_support(ka_alg);
4453 if (status != PSA_SUCCESS) {
4454 return status;
4455 }
4456 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4457#else
4458 return PSA_ERROR_NOT_SUPPORTED;
4459#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4460 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4461#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4462 status = psa_key_derivation_setup_kdf(operation, alg);
4463#else
4464 return PSA_ERROR_NOT_SUPPORTED;
4465#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4466 } else {
4467 return PSA_ERROR_INVALID_ARGUMENT;
4468 }
4469
4470 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004472 }
4473 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004474}
4475
John Durkopd0321952020-10-29 21:37:36 -07004476#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4478 psa_algorithm_t hash_alg,
4479 psa_key_derivation_step_t step,
4480 const uint8_t *data,
4481 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004482{
4483 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004485 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004486 if (hkdf->state != HKDF_STATE_INIT) {
4487 return PSA_ERROR_BAD_STATE;
4488 } else {
4489 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4490 hash_alg,
4491 data, data_length);
4492 if (status != PSA_SUCCESS) {
4493 return status;
4494 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004495 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004497 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004498 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004500 if (hkdf->state == HKDF_STATE_INIT) {
4501 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4502 hash_alg,
4503 NULL, 0);
4504 if (status != PSA_SUCCESS) {
4505 return status;
4506 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004507 hkdf->state = HKDF_STATE_STARTED;
4508 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004509 if (hkdf->state != HKDF_STATE_STARTED) {
4510 return PSA_ERROR_BAD_STATE;
4511 }
4512 status = psa_mac_update(&hkdf->hmac,
4513 data, data_length);
4514 if (status != PSA_SUCCESS) {
4515 return status;
4516 }
4517 status = psa_mac_sign_finish(&hkdf->hmac,
4518 hkdf->prk,
4519 sizeof(hkdf->prk),
4520 &data_length);
4521 if (status != PSA_SUCCESS) {
4522 return status;
4523 }
4524 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004525 hkdf->block_number = 0;
4526 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004527 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004528 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 if (hkdf->state == HKDF_STATE_OUTPUT) {
4530 return PSA_ERROR_BAD_STATE;
4531 }
4532 if (hkdf->info_set) {
4533 return PSA_ERROR_BAD_STATE;
4534 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004535 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004536 if (data_length != 0) {
4537 hkdf->info = mbedtls_calloc(1, data_length);
4538 if (hkdf->info == NULL) {
4539 return PSA_ERROR_INSUFFICIENT_MEMORY;
4540 }
4541 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542 }
4543 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004544 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004545 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004546 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004547 }
4548}
John Durkop07cc04a2020-11-16 22:08:34 -08004549#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004550
John Durkop07cc04a2020-11-16 22:08:34 -08004551#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4552 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4554 const uint8_t *data,
4555 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004556{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4558 return PSA_ERROR_BAD_STATE;
4559 }
Janos Follathf08e2652019-06-13 09:05:41 +01004560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 if (data_length != 0) {
4562 prf->seed = mbedtls_calloc(1, data_length);
4563 if (prf->seed == NULL) {
4564 return PSA_ERROR_INSUFFICIENT_MEMORY;
4565 }
Janos Follathf08e2652019-06-13 09:05:41 +01004566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004568 prf->seed_length = data_length;
4569 }
Janos Follathf08e2652019-06-13 09:05:41 +01004570
Gilles Peskine43f958b2020-12-13 14:55:14 +01004571 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004574}
4575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4577 const uint8_t *data,
4578 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004579{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4581 return PSA_ERROR_BAD_STATE;
4582 }
Janos Follath81550542019-06-13 14:26:34 +01004583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 if (data_length != 0) {
4585 prf->secret = mbedtls_calloc(1, data_length);
4586 if (prf->secret == NULL) {
4587 return PSA_ERROR_INSUFFICIENT_MEMORY;
4588 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004590 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004591 prf->secret_length = data_length;
4592 }
Janos Follath81550542019-06-13 14:26:34 +01004593
Gilles Peskine43f958b2020-12-13 14:55:14 +01004594 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004597}
4598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4600 const uint8_t *data,
4601 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004602{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4604 return PSA_ERROR_BAD_STATE;
4605 }
Janos Follath63028dd2019-06-13 09:15:47 +01004606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 if (data_length != 0) {
4608 prf->label = mbedtls_calloc(1, data_length);
4609 if (prf->label == NULL) {
4610 return PSA_ERROR_INSUFFICIENT_MEMORY;
4611 }
Janos Follath63028dd2019-06-13 09:15:47 +01004612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004613 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004614 prf->label_length = data_length;
4615 }
Janos Follath63028dd2019-06-13 09:15:47 +01004616
Gilles Peskine43f958b2020-12-13 14:55:14 +01004617 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004619 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004620}
4621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004622static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4623 psa_key_derivation_step_t step,
4624 const uint8_t *data,
4625 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004626{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004628 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004630 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004632 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004634 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004636 }
4637}
John Durkop07cc04a2020-11-16 22:08:34 -08004638#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4639 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4640
4641#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4642static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4643 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004644 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004645 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004646{
4647 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004649 uint8_t *cur = pms;
4650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004651 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4652 return PSA_ERROR_INVALID_ARGUMENT;
4653 }
John Durkop07cc04a2020-11-16 22:08:34 -08004654
4655 /* Quoting RFC 4279, Section 2:
4656 *
4657 * The premaster secret is formed as follows: if the PSK is N octets
4658 * long, concatenate a uint16 with the value N, N zero octets, a second
4659 * uint16 with the value N, and the PSK itself.
4660 */
4661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004662 *cur++ = MBEDTLS_BYTE_1(data_length);
4663 *cur++ = MBEDTLS_BYTE_0(data_length);
4664 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004665 cur += data_length;
4666 *cur++ = pms[0];
4667 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004668 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004669 cur += data_length;
4670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 mbedtls_platform_zeroize(pms, sizeof(pms));
4674 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004675}
Janos Follath6660f0e2019-06-17 08:44:03 +01004676
4677static psa_status_t psa_tls12_prf_psk_to_ms_input(
4678 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004679 psa_key_derivation_step_t step,
4680 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004682{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004683 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4684 return psa_tls12_prf_psk_to_ms_set_key(prf,
4685 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004686 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004689}
John Durkop07cc04a2020-11-16 22:08:34 -08004690#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004691
Gilles Peskineb8965192019-09-24 16:21:10 +02004692/** Check whether the given key type is acceptable for the given
4693 * input step of a key derivation.
4694 *
4695 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4696 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4697 * Both secret and non-secret inputs can alternatively have the type
4698 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4699 * that the input was passed as a buffer rather than via a key object.
4700 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004701static int psa_key_derivation_check_input_type(
4702 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004704{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004705 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004706 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004707 if (key_type == PSA_KEY_TYPE_DERIVE) {
4708 return PSA_SUCCESS;
4709 }
4710 if (key_type == PSA_KEY_TYPE_NONE) {
4711 return PSA_SUCCESS;
4712 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004713 break;
4714 case PSA_KEY_DERIVATION_INPUT_LABEL:
4715 case PSA_KEY_DERIVATION_INPUT_SALT:
4716 case PSA_KEY_DERIVATION_INPUT_INFO:
4717 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4719 return PSA_SUCCESS;
4720 }
4721 if (key_type == PSA_KEY_TYPE_NONE) {
4722 return PSA_SUCCESS;
4723 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004724 break;
4725 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004727}
4728
Janos Follathb80a94e2019-06-12 15:54:46 +01004729static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004731 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004732 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004733 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004734 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004735{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004736 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004739 status = psa_key_derivation_check_input_type(step, key_type);
4740 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004741 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004742 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004743
John Durkopd0321952020-10-29 21:37:36 -07004744#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004745 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4746 status = psa_hkdf_input(&operation->ctx.hkdf,
4747 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4748 step, data, data_length);
4749 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004750#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4751#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004752 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4753 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4754 step, data, data_length);
4755 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004756#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4757#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4759 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4760 step, data, data_length);
4761 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004762#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004765 (void) data;
4766 (void) data_length;
4767 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769 }
4770
Gilles Peskine224b0d62019-09-23 18:13:17 +02004771exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 if (status != PSA_SUCCESS) {
4773 psa_key_derivation_abort(operation);
4774 }
4775 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776}
4777
Janos Follath51f4a0f2019-06-14 11:35:55 +01004778psa_status_t psa_key_derivation_input_bytes(
4779 psa_key_derivation_operation_t *operation,
4780 psa_key_derivation_step_t step,
4781 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004783{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004784 return psa_key_derivation_input_internal(operation, step,
4785 PSA_KEY_TYPE_NONE,
4786 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004787}
4788
Janos Follath51f4a0f2019-06-14 11:35:55 +01004789psa_status_t psa_key_derivation_input_key(
4790 psa_key_derivation_operation_t *operation,
4791 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004793{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004794 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004795 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004796 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004797
Ronald Cron5c522922020-11-14 16:35:34 +01004798 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4800 if (status != PSA_SUCCESS) {
4801 psa_key_derivation_abort(operation);
4802 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004803 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004804
4805 /* Passing a key object as a SECRET input unlocks the permission
4806 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004808 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 status = psa_key_derivation_input_internal(operation,
4812 step, slot->attr.type,
4813 slot->key.data,
4814 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004816 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004819}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004820
4821
4822
4823/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004824/* Key agreement */
4825/****************************************************************/
4826
John Durkop6ba40d12020-11-10 08:50:04 -08004827#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4829 size_t peer_key_length,
4830 const mbedtls_ecp_keypair *our_key,
4831 uint8_t *shared_secret,
4832 size_t shared_secret_size,
4833 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004834{
Steven Cooremand4867872020-08-05 16:31:39 +02004835 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004836 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004837 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004838 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4840 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004841
Ronald Cron90857082020-11-25 14:58:33 +01004842 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4844 bits,
4845 peer_key,
4846 peer_key_length,
4847 &their_key);
4848 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004849 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004851
Jaeden Amero97271b32019-01-10 19:38:51 +00004852 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4854 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004855 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004857 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004858 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4859 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004860 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004862
Jaeden Amero97271b32019-01-10 19:38:51 +00004863 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 mbedtls_ecdh_calc_secret(&ecdh,
4865 shared_secret_length,
4866 shared_secret, shared_secret_size,
4867 mbedtls_psa_get_random,
4868 MBEDTLS_PSA_RANDOM_STATE));
4869 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004870 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871 }
4872 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004873 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004875
4876exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004877 if (status != PSA_SUCCESS) {
4878 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4879 }
4880 mbedtls_ecdh_free(&ecdh);
4881 mbedtls_ecp_keypair_free(their_key);
4882 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004884 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004885}
John Durkop6ba40d12020-11-10 08:50:04 -08004886#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004887
Gilles Peskine01d718c2018-09-18 12:01:02 +02004888#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004890static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4891 psa_key_slot_t *private_key,
4892 const uint8_t *peer_key,
4893 size_t peer_key_length,
4894 uint8_t *shared_secret,
4895 size_t shared_secret_size,
4896 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004897{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004898 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004899#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004900 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004901 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4902 return PSA_ERROR_INVALID_ARGUMENT;
4903 }
Steven Cooremana2371e52020-07-28 14:30:39 +02004904 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004905 psa_status_t status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906 private_key->attr.type,
4907 private_key->attr.bits,
4908 private_key->key.data,
4909 private_key->key.bytes,
4910 &ecp);
4911 if (status != PSA_SUCCESS) {
4912 return status;
4913 }
4914 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4915 ecp,
4916 shared_secret, shared_secret_size,
4917 shared_secret_length);
4918 mbedtls_ecp_keypair_free(ecp);
4919 mbedtls_free(ecp);
4920 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004921#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004922 default:
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;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005020 status = PSA_ERROR_INVALID_ARGUMENT;
5021 goto exit;
5022 }
Ronald Cron5c522922020-11-14 16:35:34 +01005023 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005024 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5025 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005026 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005027 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005028
Gilles Peskine42313fb2022-04-14 00:17:15 +02005029 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5030 * for the output size. The PSA specification only guarantees that this
5031 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5032 * but it might be nice to allow smaller buffers if the output fits.
5033 * At the time of writing this comment, with only ECDH implemented,
5034 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5035 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5036 * be exact for it as well. */
5037 size_t expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005038 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5039 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005040 status = PSA_ERROR_BUFFER_TOO_SMALL;
5041 goto exit;
5042 }
5043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005044 status = psa_key_agreement_raw_internal(alg, slot,
5045 peer_key, peer_key_length,
5046 output, output_size,
5047 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005048
5049exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005051 /* If an error happens and is not handled properly, the output
5052 * may be used as a key to protect sensitive data. Arrange for such
5053 * a key to be random, which is likely to result in decryption or
5054 * verification errors. This is better than filling the buffer with
5055 * some constant data such as zeros, which would result in the data
5056 * being protected with a reproducible, easily knowable key.
5057 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005059 *output_length = output_size;
5060 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005064 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005065}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005066
5067
Gilles Peskine30524eb2020-11-13 17:02:26 +01005068
Gilles Peskine86a440b2018-11-12 18:39:40 +01005069/****************************************************************/
5070/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005071/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005072
Gilles Peskine30524eb2020-11-13 17:02:26 +01005073/** Initialize the PSA random generator.
5074 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005076{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005077#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005078 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005079#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5080
Gilles Peskine30524eb2020-11-13 17:02:26 +01005081 /* Set default configuration if
5082 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005084 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 }
5086 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005087 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005090 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005091#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5092 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5093 /* The PSA entropy injection feature depends on using NV seed as an entropy
5094 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 mbedtls_entropy_add_source(&rng->entropy,
5096 mbedtls_nv_seed_poll, NULL,
5097 MBEDTLS_ENTROPY_BLOCK_SIZE,
5098 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005099#endif
5100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005102#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005103}
5104
5105/** Deinitialize the PSA random generator.
5106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005107static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005108{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005109#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005110 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005111#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005112 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5113 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005114#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005115}
5116
5117/** Seed the PSA random generator.
5118 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005120{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005121#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5122 /* Do nothing: the external RNG seeds itself. */
5123 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005125#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005126 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005127 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5128 drbg_seed, sizeof(drbg_seed) - 1);
5129 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005130#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005131}
5132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133psa_status_t psa_generate_random(uint8_t *output,
5134 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005135{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005136 GUARD_MODULE_INITIALIZED;
5137
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005138#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5139
5140 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005141 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5142 output, output_size,
5143 &output_length);
5144 if (status != PSA_SUCCESS) {
5145 return status;
5146 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005147 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005148 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 if (output_length != output_size) {
5150 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5151 }
5152 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005153
5154#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005156 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005157 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5159 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5160 output_size);
5161 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5162 output, request_size);
5163 if (ret != 0) {
5164 return mbedtls_to_psa_error(ret);
5165 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005166 output_size -= request_size;
5167 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005168 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005170#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005171}
5172
Gilles Peskine8814fc42020-12-14 15:33:44 +01005173/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005174 *
5175 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5176 * `psa_generate_random(...)`. The state parameter is ignored since the
5177 * PSA API doesn't support passing an explicit state.
5178 *
5179 * In the non-external case, psa_generate_random() calls an
5180 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5181 * and semantics as mbedtls_psa_get_random(). As an optimization,
5182 * instead of doing this back-and-forth between the PSA API and the
5183 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5184 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005186#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5187int mbedtls_psa_get_random(void *p_rng,
5188 unsigned char *output,
5189 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005190{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005191 /* This function takes a pointer to the RNG state because that's what
5192 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5193 * its own state internally and doesn't let the caller access that state.
5194 * So we just ignore the state parameter, and in practice we'll pass
5195 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005196 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005197 psa_status_t status = psa_generate_random(output, output_size);
5198 if (status == PSA_SUCCESS) {
5199 return 0;
5200 } else {
5201 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5202 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005203}
5204#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5205
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005206#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5207#include "mbedtls/entropy_poll.h"
5208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005209psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5210 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005211{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212 if (global_data.initialized) {
5213 return PSA_ERROR_NOT_PERMITTED;
5214 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005216 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5217 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5218 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5219 return PSA_ERROR_INVALID_ARGUMENT;
5220 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005222 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005223}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005224#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005225
Ronald Cron3772afe2021-02-08 16:10:05 +01005226/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005227 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005228 * \param type The key type
5229 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005230 *
5231 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005232 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005233 * \retval #PSA_ERROR_INVALID_ARGUMENT
5234 * The size in bits of the key is not valid.
5235 * \retval #PSA_ERROR_NOT_SUPPORTED
5236 * The type and/or the size in bits of the key or the combination of
5237 * the two is not supported.
5238 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005239static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005240 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005241{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005244 if (key_type_is_raw_bytes(type)) {
5245 status = validate_unstructured_key_bit_size(type, bits);
5246 if (status != PSA_SUCCESS) {
5247 return status;
5248 }
5249 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005250#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5252 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5253 return PSA_ERROR_NOT_SUPPORTED;
5254 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005255
Ronald Cron01b2aba2020-10-05 09:42:02 +02005256 /* Accept only byte-aligned keys, for the same reasons as
5257 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005258 if (bits % 8 != 0) {
5259 return PSA_ERROR_NOT_SUPPORTED;
5260 }
5261 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005262#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005263
Ronald Cron5c4d3862020-12-07 11:07:24 +01005264#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005266 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 return PSA_SUCCESS;
5268 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005269#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005270 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005272 }
5273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005274 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005275}
5276
Ronald Cron55ed0592020-10-05 10:30:40 +02005277psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005278 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005279 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005280{
5281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005282 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005284 if ((attributes->domain_parameters == NULL) &&
5285 (attributes->domain_parameters_size != 0)) {
5286 return PSA_ERROR_INVALID_ARGUMENT;
5287 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 if (key_type_is_raw_bytes(type)) {
5290 status = psa_generate_random(key_buffer, key_buffer_size);
5291 if (status != PSA_SUCCESS) {
5292 return status;
5293 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005294
David Brown12ca5032021-02-11 11:02:00 -07005295#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005296 if (type == PSA_KEY_TYPE_DES) {
5297 psa_des_set_key_parity(key_buffer, key_buffer_size);
5298 }
David Brown8107e312021-02-12 08:08:46 -07005299#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005300 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005301
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005302#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5303 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005304 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5305 return mbedtls_psa_rsa_generate_key(attributes,
5306 key_buffer,
5307 key_buffer_size,
5308 key_buffer_length);
5309 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5311 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005312
John Durkop9814fa22020-11-04 12:28:15 -08005313#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5315 return mbedtls_psa_ecp_generate_key(attributes,
5316 key_buffer,
5317 key_buffer_size,
5318 key_buffer_length);
5319 } else
John Durkop9814fa22020-11-04 12:28:15 -08005320#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005321 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005322 (void) key_buffer_length;
5323 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005324 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005326 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005327}
Darryl Green0c6575a2018-11-07 16:05:30 +00005328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5330 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005331{
5332 psa_status_t status;
5333 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005334 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005335 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005336
Ronald Cron81709fc2020-11-14 12:10:32 +01005337 *key = MBEDTLS_SVC_KEY_ID_INIT;
5338
Gilles Peskine0f84d622019-09-12 19:03:13 +02005339 /* Reject any attempt to create a zero-length key so that we don't
5340 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005341 if (psa_get_key_bits(attributes) == 0) {
5342 return PSA_ERROR_INVALID_ARGUMENT;
5343 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005344
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005345 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5347 return PSA_ERROR_INVALID_ARGUMENT;
5348 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005350 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5351 &slot, &driver);
5352 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005353 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005354 }
Gilles Peskine11792082019-08-06 18:36:36 +02005355
Ronald Cron977c2472020-10-13 08:32:21 +02005356 /* In the case of a transparent key or an opaque key stored in local
5357 * storage (thus not in the case of generating a key in a secure element
5358 * or cryptoprocessor with storage), we have to allocate a buffer to
5359 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005360 if (slot->key.data == NULL) {
5361 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5362 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005363 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005364 attributes->core.type, attributes->core.bits);
5365 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005366 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005367 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005368
5369 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005370 attributes->core.type,
5371 attributes->core.bits);
5372 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005373 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005374 attributes, &key_buffer_size);
5375 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005376 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005377 }
Ronald Cron977c2472020-10-13 08:32:21 +02005378 }
Ronald Cron977c2472020-10-13 08:32:21 +02005379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005380 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5381 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005382 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005383 }
Ronald Cron977c2472020-10-13 08:32:21 +02005384 }
5385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005386 status = psa_driver_wrapper_generate_key(attributes,
5387 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005389 if (status != PSA_SUCCESS) {
5390 psa_remove_key_data_from_memory(slot);
5391 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005392
Gilles Peskine11792082019-08-06 18:36:36 +02005393exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005394 if (status == PSA_SUCCESS) {
5395 status = psa_finish_key_creation(slot, driver, key);
5396 }
5397 if (status != PSA_SUCCESS) {
5398 psa_fail_key_creation(slot, driver);
5399 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005401 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005402}
5403
Gilles Peskinee59236f2018-01-27 23:32:46 +01005404/****************************************************************/
5405/* Module setup */
5406/****************************************************************/
5407
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005408#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005409psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 void (* entropy_init)(mbedtls_entropy_context *ctx),
5411 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005412{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005413 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5414 return PSA_ERROR_BAD_STATE;
5415 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005416 global_data.rng.entropy_init = entropy_init;
5417 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005418 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005419}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005420#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005423{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 psa_wipe_all_key_slots();
5425 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5426 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005427 }
5428 /* Wipe all remaining data, including configuration.
5429 * In particular, this sets all state indicator to the value
5430 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005431 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005432
5433 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005434 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005435}
5436
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005437#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5438/** Recover a transaction that was interrupted by a power failure.
5439 *
5440 * This function is called during initialization, before psa_crypto_init()
5441 * returns. If this function returns a failure status, the initialization
5442 * fails.
5443 */
5444static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005446{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005448 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5449 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 /* TODO - fall through to the failure case until this
5451 * is implemented.
5452 * https://github.com/ARMmbed/mbed-crypto/issues/218
5453 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005454 default:
5455 /* We found an unsupported transaction in the storage.
5456 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005457 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005458 }
5459}
5460#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005462psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005464 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465
Gilles Peskinec6b69072018-11-20 21:42:52 +01005466 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 if (global_data.initialized != 0) {
5468 return PSA_SUCCESS;
5469 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470
Gilles Peskine30524eb2020-11-13 17:02:26 +01005471 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005473 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 status = mbedtls_psa_random_seed(&global_data.rng);
5475 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005476 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005477 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005478 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 status = psa_initialize_key_slots();
5481 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005482 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005484
Ronald Cron088d5d02021-04-10 16:57:30 +02005485 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005486 status = psa_driver_wrapper_init();
5487 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005488 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005490
Gilles Peskine4b734222019-07-24 15:56:31 +02005491#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 status = psa_crypto_load_transaction();
5493 if (status == PSA_SUCCESS) {
5494 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5495 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005496 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497 }
5498 status = psa_crypto_stop_transaction();
5499 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005500 /* There's no transaction to complete. It's all good. */
5501 status = PSA_SUCCESS;
5502 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005503#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005504
Gilles Peskinec6b69072018-11-20 21:42:52 +01005505 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506 global_data.initialized = 1;
5507
5508exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 if (status != PSA_SUCCESS) {
5510 mbedtls_psa_crypto_free();
5511 }
5512 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005513}
5514
5515#endif /* MBEDTLS_PSA_CRYPTO_C */