blob: fdcdd43d50cb605b111a9dba16e9e469ecccb291 [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 Peskine2f9c4dc2018-01-28 13:16:24 +010050#include <stdlib.h>
51#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskine996deb12018-08-01 15:45:45 +020089
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010090/****************************************************************/
91/* Global data, support functions and library management */
92/****************************************************************/
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020095{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020097}
98
Gilles Peskine5a3c50e2018-12-04 12:27:09 +010099/* Values for psa_global_data_t::rng_state */
100#define RNG_NOT_INITIALIZED 0
101#define RNG_INITIALIZED 1
102#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104typedef struct {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100105 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100106 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +0100107 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100108} psa_global_data_t;
109
110static psa_global_data_t global_data;
111
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100112#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
113mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
114 &global_data.rng.drbg;
115#endif
116
itayzafrir0adf0fc2018-09-06 16:24:41 +0300117#define GUARD_MODULE_INITIALIZED \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 if (global_data.initialized == 0) \
119 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100122{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100123 /* Mbed TLS error codes can combine a high-level error code and a
124 * low-level error code. The low-level error usually reflects the
125 * root cause better, so dispatch on that preferably. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 int low_level_ret = -(-ret & 0x007f);
127 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100130
131 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
132 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
133 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100135 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200138 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200140
Gilles Peskine9a944802018-06-21 09:35:35 +0200141 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
142 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
143 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
144 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
145 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200147 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200149 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200151
Jaeden Amero93e21112019-02-20 13:57:28 +0000152#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000153 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000154#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
155 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
156#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100159 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
172 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178
Gilles Peskine26869f22019-05-06 15:25:00 +0200179 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200181
182 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200184 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
204 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
208 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100209 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
210 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
214 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100218#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
220 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
Gilles Peskinee59236f2018-01-27 23:32:46 +0100225 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
226 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
227 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229
230 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100239 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
240 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100246 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248#endif
249
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200250 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
252 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200254
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100257 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100261 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return PSA_ERROR_STORAGE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200263 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100265
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100272 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100274 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100276 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
286 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
291 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100295 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
296 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_INVALID_ALG:
301 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
302 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308
Gilles Peskineff2d2002019-05-06 15:26:23 +0200309 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200311 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200313
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200314 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200316
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
326 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100330 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200339 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
340 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
341 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 return PSA_ERROR_HARDWARE_FAILURE;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200343
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348
itayzafrir5c753392018-05-08 11:18:38 +0300349 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300350 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300352 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300354 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300356 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
357 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100361 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 return PSA_ERROR_INSUFFICIENT_ENTROPY;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskine40d81602020-11-25 00:09:47 +0100365
Janos Follatha13b9052019-11-22 12:48:59 +0000366 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300368
Gilles Peskinee59236f2018-01-27 23:32:46 +0100369 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100371 }
372}
373
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200374
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200375
376
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100377/****************************************************************/
378/* Key management */
379/****************************************************************/
380
John Durkop9814fa22020-11-04 12:28:15 -0800381#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800382 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100383 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
384 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
385 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
387 size_t bits,
388 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200389{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100391 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100393#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100396#endif
397#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100398 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100400#endif
401#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100402 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100404#endif
405#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100406 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100408#endif
409#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 case 521:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100412 case 528:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 if (bits_is_sloppy) {
414 return MBEDTLS_ECP_DP_SECP521R1;
415 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100416 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100418 }
419 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100420
Paul Elliott8ff510a2020-06-02 17:19:28 +0100421 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#endif
427#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100428 case 384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100430#endif
431#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100434#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Paul Elliott8ff510a2020-06-02 17:19:28 +0100438 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100440#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100441 case 255:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 if (bits_is_sloppy) {
445 return MBEDTLS_ECP_DP_CURVE25519;
446 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#endif
449#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100450 case 448:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100452#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 }
454 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100455
Paul Elliott8ff510a2020-06-02 17:19:28 +0100456 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 192:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
462#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100463 case 224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100465#endif
466#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100469#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 }
471 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200472 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100474 (void) bits_is_sloppy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476}
John Durkop9814fa22020-11-04 12:28:15 -0800477#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100478 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
479 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
480 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
481 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483static psa_status_t validate_unstructured_key_bit_size(psa_key_type_t type,
484 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485{
486 /* Check that the bit size is acceptable for the key type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200488 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200490 case PSA_KEY_TYPE_DERIVE:
491 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100492#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493 case PSA_KEY_TYPE_AES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 if (bits != 128 && bits != 192 && bits != 256) {
495 return PSA_ERROR_INVALID_ARGUMENT;
496 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 break;
498#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200499#if defined(PSA_WANT_KEY_TYPE_ARIA)
500 case PSA_KEY_TYPE_ARIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if (bits != 128 && bits != 192 && bits != 256) {
502 return PSA_ERROR_INVALID_ARGUMENT;
503 }
Gilles Peskine8890f642021-09-21 11:59:39 +0200504 break;
505#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100506#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200507 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 if (bits != 128 && bits != 192 && bits != 256) {
509 return PSA_ERROR_INVALID_ARGUMENT;
510 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 break;
512#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100513#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514 case PSA_KEY_TYPE_DES:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 if (bits != 64 && bits != 128 && bits != 192) {
516 return PSA_ERROR_INVALID_ARGUMENT;
517 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200518 break;
519#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100520#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200521 case PSA_KEY_TYPE_ARC4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 if (bits < 8 || bits > 2048) {
523 return PSA_ERROR_INVALID_ARGUMENT;
524 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525 break;
526#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100527#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200528 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 if (bits != 256) {
530 return PSA_ERROR_INVALID_ARGUMENT;
531 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200532 break;
533#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 if (bits % 8 != 0) {
538 return PSA_ERROR_INVALID_ARGUMENT;
539 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542}
543
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100545 *
Steven Cooremancd640932021-03-08 12:00:27 +0100546 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
547 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548 *
549 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100550 * \param[in] key_type The key type of the key to be used with the
551 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 *
553 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100554 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100555 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100556 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100557 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100559 psa_algorithm_t algorithm,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if (PSA_ALG_IS_HMAC(algorithm)) {
563 if (key_type == PSA_KEY_TYPE_HMAC) {
564 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100565 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100568 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
569 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
570 * key. */
571 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
572 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
573 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
574 * the block length (larger than 1) for block ciphers. */
575 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
576 return PSA_SUCCESS;
577 }
578 }
579 }
580
581 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100582}
583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
585 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200586{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100587 if (slot->key.data != NULL) {
588 return PSA_ERROR_ALREADY_EXISTS;
589 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100591 slot->key.data = mbedtls_calloc(1, buffer_length);
592 if (slot->key.data == NULL) {
593 return PSA_ERROR_INSUFFICIENT_MEMORY;
594 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200595
Ronald Cronea0f8a62020-11-25 17:52:23 +0100596 slot->key.bytes = buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200598}
599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
601 const uint8_t *data,
602 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200603{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 psa_status_t status = psa_allocate_buffer_to_slot(slot,
605 data_length);
606 if (status != PSA_SUCCESS) {
607 return status;
608 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 memcpy(slot->key.data, data, data_length);
611 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200612}
613
Ronald Crona0fe59f2020-11-29 11:14:53 +0100614psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100615 const psa_key_attributes_t *attributes,
616 const uint8_t *data, size_t data_length,
617 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
621 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200623 /* zero-length keys are never supported. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (data_length == 0) {
625 return PSA_ERROR_NOT_SUPPORTED;
626 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 if (key_type_is_raw_bytes(type)) {
629 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200630
Steven Cooreman75b74362020-07-28 14:30:13 +0200631 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632 if (data_length > SIZE_MAX / 8) {
633 return PSA_ERROR_NOT_SUPPORTED;
634 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200635
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200636 /* Enforce a size limit, and in particular ensure that the bit
637 * size fits in its representation type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100638 if ((*bits) > PSA_MAX_KEY_BITS) {
639 return PSA_ERROR_NOT_SUPPORTED;
640 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642 status = validate_unstructured_key_bit_size(type, *bits);
643 if (status != PSA_SUCCESS) {
644 return status;
645 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200646
Ronald Crondd04d422020-11-28 15:14:42 +0100647 /* Copy the key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100649 *key_buffer_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100650 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 return PSA_SUCCESS;
653 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
John Durkop9814fa22020-11-04 12:28:15 -0800654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
656 if (PSA_KEY_TYPE_IS_ECC(type)) {
657 return mbedtls_psa_ecp_import_key(attributes,
658 data, data_length,
659 key_buffer, key_buffer_size,
660 key_buffer_length,
661 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100662 }
John Durkop9814fa22020-11-04 12:28:15 -0800663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700665#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100666 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
667 if (PSA_KEY_TYPE_IS_RSA(type)) {
668 return mbedtls_psa_rsa_import_key(attributes,
669 data, data_length,
670 key_buffer, key_buffer_size,
671 key_buffer_length,
672 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200673 }
John Durkop9814fa22020-11-04 12:28:15 -0800674#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
675 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100676 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100679}
680
Gilles Peskinef603c712019-01-19 13:40:11 +0100681/** Calculate the intersection of two algorithm usage policies.
682 *
683 * Return 0 (which allows no operation) on incompatibility.
684 */
685static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100686 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100687 psa_algorithm_t alg1,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100689{
Gilles Peskine549ea862019-05-22 11:45:59 +0200690 /* Common case: both sides actually specify the same policy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 if (alg1 == alg2) {
692 return alg1;
693 }
Steven Cooreman03488022021-02-18 12:07:20 +0100694 /* If the policies are from the same hash-and-sign family, check
695 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
697 PSA_ALG_IS_SIGN_HASH(alg2) &&
698 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
699 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
700 return alg2;
701 }
702 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
703 return alg1;
704 }
Steven Cooreman03488022021-02-18 12:07:20 +0100705 }
706 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100707 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100708 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
710 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
711 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
712 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
713 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100714 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100715
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100716 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
718 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
719 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
720 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100721 }
722 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
724 (alg1_len <= alg2_len)) {
725 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100726 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100727 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
728 (alg2_len <= alg1_len)) {
729 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100730 }
731 }
732 /* If the policies are from the same MAC family, check whether one
733 * of them is a minimum-MAC-length policy. Calculate the most
734 * restrictive tag length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
736 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
737 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100738 /* Validate the combination of key type and algorithm. Since the base
739 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100740 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
741 return 0;
742 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100743
Steven Cooreman31a876d2021-03-03 20:47:40 +0100744 /* Get the (exact or at-least) output lengths for both sides of the
745 * requested intersection. None of the currently supported algorithms
746 * have an output length dependent on the actual key size, so setting it
747 * to a bogus value of 0 is currently OK.
748 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100749 * Note that for at-least-this-length wildcard algorithms, the output
750 * length is set to the shortest allowed length, which allows us to
751 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100752 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
753 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100754 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100755
Steven Cooremana1d83222021-02-25 10:20:29 +0100756 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100757 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
758 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
759 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100760 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761
762 /* If only one is an at-least-this-length policy, the intersection would
763 * be the other (fixed-length) policy as long as said fixed length is
764 * equal to or larger than the shortest allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
766 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100767 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100768 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
769 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100771
Steven Cooremancd640932021-03-08 12:00:27 +0100772 /* If none of them are wildcards, check whether they define the same tag
773 * length. This is still possible here when one is default-length and
774 * the other specific-length. Ensure to always return the
775 * specific-length version for the intersection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100776 if (alg1_len == alg2_len) {
777 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
778 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100779 }
780 /* If the policies are incompatible, allow nothing. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100781 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100782}
783
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784static int psa_key_algorithm_permits(psa_key_type_t key_type,
785 psa_algorithm_t policy_alg,
786 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200787{
Gilles Peskine549ea862019-05-22 11:45:59 +0200788 /* Common case: the policy only allows requested_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100789 if (requested_alg == policy_alg) {
790 return 1;
791 }
Steven Cooreman03488022021-02-18 12:07:20 +0100792 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
793 * and requested_alg is the same hash-and-sign family with any hash,
794 * then requested_alg is compliant with policy_alg. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
796 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
797 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
798 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100799 }
800 /* If policy_alg is a wildcard AEAD algorithm of the same base as
801 * the requested algorithm, check the requested tag length to be
802 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 if (PSA_ALG_IS_AEAD(policy_alg) &&
804 PSA_ALG_IS_AEAD(requested_alg) &&
805 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
806 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
807 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
808 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
809 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100810 }
Steven Cooremancd640932021-03-08 12:00:27 +0100811 /* If policy_alg is a MAC algorithm of the same base as the requested
812 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 if (PSA_ALG_IS_MAC(policy_alg) &&
814 PSA_ALG_IS_MAC(requested_alg) &&
815 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
816 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100817 /* Validate the combination of key type and algorithm. Since the policy
818 * and requested algorithms are the same, we only need this once. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
820 return 0;
821 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100822
Steven Cooreman31a876d2021-03-03 20:47:40 +0100823 /* Get both the requested output length for the algorithm which is to be
824 * verified, and the default output length for the base algorithm.
825 * Note that none of the currently supported algorithms have an output
826 * length dependent on actual key size, so setting it to a bogus value
827 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100828 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100830 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 key_type, 0,
832 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 /* If the policy is default-length, only allow an algorithm with
835 * a declared exact-length matching the default. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
837 return requested_output_length == default_output_length;
838 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100839
840 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100841 * length exactly matches the default length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
843 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
844 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100845 }
846
Steven Cooremancd640932021-03-08 12:00:27 +0100847 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
848 * check for the requested MAC length to be equal to or longer than the
849 * minimum allowed length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
851 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
852 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100853 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200854 }
Steven Cooremance48e852020-10-05 16:02:45 +0200855 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200856 * a key derivation with that key agreement should also be allowed. This
857 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
859 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
860 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
861 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200862 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100863 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100864 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200865}
866
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100867/** Test whether a policy permits an algorithm.
868 *
869 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100870 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100871 * \note This function requires providing the key type for which the policy is
872 * being validated, since some algorithm policy definitions (e.g. MAC)
873 * have different properties depending on what kind of cipher it is
874 * combined with.
875 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100876 * \retval PSA_SUCCESS When \p alg is a specific algorithm
877 * allowed by the \p policy.
878 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
879 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
880 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100881 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100882static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
883 psa_key_type_t key_type,
884 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100885{
Steven Cooremand788fab2021-02-25 11:29:17 +0100886 /* '0' is not a valid algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100887 if (alg == 0) {
888 return PSA_ERROR_INVALID_ARGUMENT;
889 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100890
Steven Cooreman7e39f052021-02-23 14:18:32 +0100891 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100892 if (PSA_ALG_IS_WILDCARD(alg)) {
893 return PSA_ERROR_INVALID_ARGUMENT;
894 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
897 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
898 return PSA_SUCCESS;
899 } else {
900 return PSA_ERROR_NOT_PERMITTED;
901 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100902}
903
Gilles Peskinef603c712019-01-19 13:40:11 +0100904/** Restrict a key policy based on a constraint.
905 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100906 * \note This function requires providing the key type for which the policy is
907 * being restricted, since some algorithm policy definitions (e.g. MAC)
908 * have different properties depending on what kind of cipher it is
909 * combined with.
910 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100911 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100912 * \param[in,out] policy The policy to restrict.
913 * \param[in] constraint The policy constraint to apply.
914 *
915 * \retval #PSA_SUCCESS
916 * \c *policy contains the intersection of the original value of
917 * \c *policy and \c *constraint.
918 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100919 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100920 * \c *policy is unchanged.
921 */
922static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100923 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100924 psa_key_policy_t *policy,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100925 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100926{
927 psa_algorithm_t intersection_alg =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 psa_key_policy_algorithm_intersection(key_type, policy->alg,
929 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200930 psa_algorithm_t intersection_alg2 =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100931 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
932 constraint->alg2);
933 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
934 return PSA_ERROR_INVALID_ARGUMENT;
935 }
936 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
937 return PSA_ERROR_INVALID_ARGUMENT;
938 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100939 policy->usage &= constraint->usage;
940 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200941 policy->alg2 = intersection_alg2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100943}
944
Ronald Cron5c522922020-11-14 16:35:34 +0100945/** Get the description of a key given its identifier and policy constraints
946 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200947 *
Ronald Cron5c522922020-11-14 16:35:34 +0100948 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100949 * nonzero, the key must allow operations with this algorithm. If \p alg is
950 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100951 *
Ronald Cron5c522922020-11-14 16:35:34 +0100952 * In case of a persistent key, the function loads the description of the key
953 * into a key slot if not already done.
954 *
955 * On success, the returned key slot is locked. It is the responsibility of
956 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200957 */
Ronald Cron5c522922020-11-14 16:35:34 +0100958static psa_status_t psa_get_and_lock_key_slot_with_policy(
959 mbedtls_svc_key_id_t key,
960 psa_key_slot_t **p_slot,
961 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +0100963{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
965 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 status = psa_get_and_lock_key_slot(key, p_slot);
968 if (status != PSA_SUCCESS) {
969 return status;
970 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100972
973 /* Enforce that usage policy for the key slot contains all the flags
974 * required by the usage parameter. There is one exception: public
975 * keys can always be exported, so we treat public key objects as
976 * if they had the export flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +0100978 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +0100982 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100984 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985
Shaun Case0e7791f2021-12-20 21:14:10 -0800986 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 if (alg != 0) {
988 status = psa_key_policy_permits(&slot->attr.policy,
989 slot->attr.type,
990 alg);
991 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +0100992 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100996 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997
998error:
999 *p_slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001000 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001003}
Darryl Green940d72c2018-07-13 13:18:51 +01001004
Ronald Cron5c522922020-11-14 16:35:34 +01001005/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006 *
1007 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001008 * available, as opposed to a key in a secure element and/or to be used
1009 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001011 * This is a temporary function that may be used instead of
1012 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1013 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 *
Ronald Cron5c522922020-11-14 16:35:34 +01001015 * On success, the returned key slot is locked. It is the responsibility of the
1016 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 */
Ronald Cron5c522922020-11-14 16:35:34 +01001018static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1019 mbedtls_svc_key_id_t key,
1020 psa_key_slot_t **p_slot,
1021 psa_key_usage_t usage,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001023{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001024 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1025 usage, alg);
1026 if (status != PSA_SUCCESS) {
1027 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1031 psa_unlock_key_slot(*p_slot);
1032 *p_slot = NULL;
1033 return PSA_ERROR_NOT_SUPPORTED;
1034 }
1035
1036 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001037}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001040{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001041 /* Data pointer will always be either a valid pointer or NULL in an
1042 * initialized slot, so we can just free it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001043 if (slot->key.data != NULL) {
1044 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1045 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001046
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001047 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001048 slot->key.data = NULL;
1049 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001052}
1053
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001054/** Completely wipe a slot in memory, including its policy.
1055 * Persistent storage is not affected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001056psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001059
1060 /*
1061 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001062 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001063 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1064 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001065 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 if (slot->lock_count != 1) {
Ronald Cronddd3d052020-10-30 14:07:07 +01001068#ifdef MBEDTLS_CHECK_PARAMS
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 MBEDTLS_PARAM_FAILED(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001070#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 status = PSA_ERROR_CORRUPTION_DETECTED;
1072 }
1073
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001074 /* Multipart operations may still be using the key. This is safe
1075 * because all multipart operation objects are independent from
1076 * the key slot: if they need to access the key after the setup
1077 * phase, they have a copy of the key. Note that this means that
1078 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001079 /* At this point, key material and other type-specific content has
1080 * been wiped. Clear remaining metadata. We can call memset and not
1081 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 memset(slot, 0, sizeof(*slot));
1083 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001084}
1085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001088 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001089 psa_status_t status; /* status of the last operation */
1090 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1092 psa_se_drv_table_entry_t *driver;
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 if (mbedtls_svc_key_id_is_null(key)) {
1096 return PSA_SUCCESS;
1097 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001098
Ronald Cronf2911112020-10-29 17:51:10 +01001099 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001100 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001101 * key, this will load the key description from persistent memory if not
1102 * done yet. We cannot avoid this loading as without it we don't know if
1103 * the key is operated by an SE or not and this information is needed by
1104 * the current implementation.
1105 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001106 status = psa_get_and_lock_key_slot(key, &slot);
1107 if (status != PSA_SUCCESS) {
1108 return status;
1109 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001110
Ronald Cronf2911112020-10-29 17:51:10 +01001111 /*
1112 * If the key slot containing the key description is under access by the
1113 * library (apart from the present access), the key cannot be destroyed
1114 * yet. For the time being, just return in error. Eventually (to be
1115 * implemented), the key should be destroyed when all accesses have
1116 * stopped.
1117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001118 if (slot->lock_count > 1) {
1119 psa_unlock_key_slot(slot);
1120 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001121 }
1122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine251c7742021-04-21 22:32:05 +02001124 /* Refuse the destruction of a read-only key (which may or may not work
1125 * if we attempt it, depending on whether the key is merely read-only
1126 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001127 * Just do the best we can, which is to wipe the copy in memory
1128 * (done in this function's cleanup code). */
1129 overall_status = PSA_ERROR_NOT_PERMITTED;
1130 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001131 }
1132
Gilles Peskine354f7672019-07-12 23:46:38 +02001133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001134 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1135 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001136 /* For a key in a secure element, we need to do three things:
1137 * remove the key file in internal storage, destroy the
1138 * key inside the secure element, and update the driver's
1139 * persistent data. Start a transaction that will encompass these
1140 * three actions. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001141 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001142 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001144 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 status = psa_crypto_save_transaction();
1146 if (status != PSA_SUCCESS) {
1147 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001148 /* We should still try to destroy the key in the secure
1149 * element and the key metadata in storage. This is especially
1150 * important if the error is that the storage is full.
1151 * But how to do it exactly without risking an inconsistent
1152 * state after a reset?
1153 * https://github.com/ARMmbed/mbed-crypto/issues/215
1154 */
1155 overall_status = status;
1156 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001157 }
1158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 status = psa_destroy_se_key(driver,
1160 psa_key_slot_get_slot_number(slot));
1161 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001163 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001164 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001165#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1166
Darryl Greend49a4992018-06-18 17:27:26 +01001167#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1169 status = psa_destroy_persistent_key(slot->attr.id);
1170 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001171 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001172 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001173
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001174 /* TODO: other slots may have a copy of the same key. We should
1175 * invalidate them.
1176 * https://github.com/ARMmbed/mbed-crypto/issues/214
1177 */
Darryl Greend49a4992018-06-18 17:27:26 +01001178 }
1179#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001180
Gilles Peskinefc762652019-07-22 19:30:34 +02001181#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182 if (driver != NULL) {
1183 status = psa_save_se_persistent_data(driver);
1184 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 }
1187 status = psa_crypto_stop_transaction();
1188 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001189 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001190 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001191 }
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1193
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001194exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001195 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001196 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 overall_status = status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 }
1200 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201}
1202
John Durkop07cc04a2020-11-16 22:08:34 -08001203#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001204 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001205static psa_status_t psa_get_rsa_public_exponent(
1206 const mbedtls_rsa_context *rsa,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001208{
1209 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001211 uint8_t *buffer = NULL;
1212 size_t buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1216 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001218 }
1219 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001220 /* It's the default value, which is reported as an empty string,
1221 * so there's nothing to do. */
1222 goto exit;
1223 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 buflen = mbedtls_mpi_size(&mpi);
1226 buffer = mbedtls_calloc(1, buflen);
1227 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001228 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1229 goto exit;
1230 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1232 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 attributes->domain_parameters = buffer;
1236 attributes->domain_parameters_size = buflen;
1237
1238exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001239 mbedtls_mpi_free(&mpi);
1240 if (ret != 0) {
1241 mbedtls_free(buffer);
1242 }
1243 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001244}
John Durkop07cc04a2020-11-16 22:08:34 -08001245#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001246 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001247
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001248/** Retrieve all the publicly-accessible attributes of a key.
1249 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001250psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1251 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001254 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001255 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1260 if (status != PSA_SUCCESS) {
1261 return status;
1262 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001263
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001264 attributes->core = slot->attr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1266 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001267
1268#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1270 psa_set_key_slot_number(attributes,
1271 psa_key_slot_get_slot_number(slot));
1272 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001275 switch (slot->attr.type) {
John Durkop07cc04a2020-11-16 22:08:34 -08001276#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001277 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001278 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001280 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001281 * is not yet implemented.
1282 * https://github.com/ARMmbed/mbed-crypto/issues/216
1283 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001285 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001286
Ronald Cron90857082020-11-25 14:58:33 +01001287 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 slot->attr.type,
1289 slot->key.data,
1290 slot->key.bytes,
1291 &rsa);
1292 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001293 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 status = psa_get_rsa_public_exponent(rsa,
1297 attributes);
1298 mbedtls_rsa_free(rsa);
1299 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001300 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001301 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001302#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001303 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001304 default:
1305 /* Nothing else to do. */
1306 break;
1307 }
1308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 if (status != PSA_SUCCESS) {
1310 psa_reset_key_attributes(attributes);
1311 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316}
1317
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319psa_status_t psa_get_key_slot_number(
1320 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324 *slot_number = attributes->slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001325 return PSA_SUCCESS;
1326 } else {
1327 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001328 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001329}
1330#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1333 size_t key_buffer_size,
1334 uint8_t *data,
1335 size_t data_size,
1336 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001337{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 if (key_buffer_size > data_size) {
1339 return PSA_ERROR_BUFFER_TOO_SMALL;
1340 }
1341 memcpy(data, key_buffer, key_buffer_size);
1342 memset(data + key_buffer_size, 0,
1343 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 *data_length = key_buffer_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001346}
1347
Ronald Cron7285cda2020-11-26 14:46:37 +01001348psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 const psa_key_attributes_t *attributes,
1350 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 psa_key_type_t type = attributes->core.type;
1354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001355 if (key_type_is_raw_bytes(type) ||
1356 PSA_KEY_TYPE_IS_RSA(type) ||
1357 PSA_KEY_TYPE_IS_ECC(type)) {
1358 return psa_export_key_buffer_internal(
1359 key_buffer, key_buffer_size,
1360 data, data_size, data_length);
1361 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362 /* This shouldn't happen in the reference implementation, but
1363 it is valid for a special-purpose implementation to omit
1364 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366 }
1367}
1368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1370 uint8_t *data,
1371 size_t data_size,
1372 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001373{
1374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1375 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1376 psa_key_slot_t *slot;
1377
Ronald Crone907e552021-01-18 13:22:38 +01001378 /* Reject a zero-length output buffer now, since this can never be a
1379 * valid key representation. This way we know that data must be a valid
1380 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 if (data_size == 0) {
1382 return PSA_ERROR_BUFFER_TOO_SMALL;
1383 }
Ronald Crone907e552021-01-18 13:22:38 +01001384
Ronald Cron9486f9d2020-11-26 13:36:23 +01001385 /* Set the key to empty now, so that even when there are errors, we always
1386 * set data_length to a value between 0 and data_size. On error, setting
1387 * the key to empty is a good choice because an empty key representation is
1388 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001389 *data_length = 0;
1390
Ronald Cron9486f9d2020-11-26 13:36:23 +01001391 /* Export requires the EXPORT flag. There is an exception for public keys,
1392 * which don't require any flag, but
1393 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1394 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1396 PSA_KEY_USAGE_EXPORT, 0);
1397 if (status != PSA_SUCCESS) {
1398 return status;
1399 }
mohammad160306e79202018-03-28 13:17:44 +03001400
Ronald Crond18b5f82020-11-25 16:46:05 +01001401 psa_key_attributes_t attributes = {
1402 .core = slot->attr
1403 };
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 status = psa_driver_wrapper_export_key(&attributes,
1405 slot->key.data, slot->key.bytes,
1406 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001407
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001411}
1412
Ronald Cron7285cda2020-11-26 14:46:37 +01001413psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001414 const psa_key_attributes_t *attributes,
1415 const uint8_t *key_buffer,
1416 size_t key_buffer_size,
1417 uint8_t *data,
1418 size_t data_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420{
Ronald Crond18b5f82020-11-25 16:46:05 +01001421 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type)) {
1424 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001425 /* Exporting public -> public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426 return psa_export_key_buffer_internal(
1427 key_buffer, key_buffer_size,
1428 data, data_size, data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 if (PSA_KEY_TYPE_IS_RSA(type)) {
John Durkop0e005192020-10-31 22:06:54 -07001432#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1434 return mbedtls_psa_rsa_export_public_key(attributes,
1435 key_buffer,
1436 key_buffer_size,
1437 data,
1438 data_size,
1439 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001440#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001441 /* We don't know how to convert a private RSA key to public. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001442 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001443#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1444 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 } else {
John Durkop6ba40d12020-11-10 08:50:04 -08001446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001447 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1448 return mbedtls_psa_ecp_export_public_key(attributes,
1449 key_buffer,
1450 key_buffer_size,
1451 data,
1452 data_size,
1453 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001454#else
1455 /* We don't know how to convert a private ECC key to public */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 return PSA_ERROR_NOT_SUPPORTED;
John Durkop9814fa22020-11-04 12:28:15 -08001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001459 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001460 } else {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001461 /* This shouldn't happen in the reference implementation, but
1462 it is valid for a special-purpose implementation to omit
1463 support for exporting certain key types. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001465 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466}
Ronald Crond18b5f82020-11-25 16:46:05 +01001467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1469 uint8_t *data,
1470 size_t data_size,
1471 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001472{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001475 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001476
Ronald Crone907e552021-01-18 13:22:38 +01001477 /* Reject a zero-length output buffer now, since this can never be a
1478 * valid key representation. This way we know that data must be a valid
1479 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 if (data_size == 0) {
1481 return PSA_ERROR_BUFFER_TOO_SMALL;
1482 }
Ronald Crone907e552021-01-18 13:22:38 +01001483
Darryl Greendd8fb772018-11-07 16:00:44 +00001484 /* Set the key to empty now, so that even when there are errors, we always
1485 * set data_length to a value between 0 and data_size. On error, setting
1486 * the key to empty is a good choice because an empty key representation is
1487 * unlikely to be accepted anywhere. */
1488 *data_length = 0;
1489
1490 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1492 if (status != PSA_SUCCESS) {
1493 return status;
1494 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1497 status = PSA_ERROR_INVALID_ARGUMENT;
1498 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001499 }
1500
Ronald Crond18b5f82020-11-25 16:46:05 +01001501 psa_key_attributes_t attributes = {
1502 .core = slot->attr
1503 };
Ronald Cron67227982020-11-26 15:16:05 +01001504 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001505 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001507
1508exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001511 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001512}
1513
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001514MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1515 "One or more key attribute flag is listed as both external-only and dual-use")
1516MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1517 "One or more key attribute flag is listed as both internal-only and dual-use")
1518MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1519 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001520
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001521/** Validate that a key policy is internally well-formed.
1522 *
1523 * This function only rejects invalid policies. It does not validate the
1524 * consistency of the policy with respect to other attributes of the key
1525 * such as the key type.
1526 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001527static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001528{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1530 PSA_KEY_USAGE_COPY |
1531 PSA_KEY_USAGE_ENCRYPT |
1532 PSA_KEY_USAGE_DECRYPT |
1533 PSA_KEY_USAGE_SIGN_MESSAGE |
1534 PSA_KEY_USAGE_VERIFY_MESSAGE |
1535 PSA_KEY_USAGE_SIGN_HASH |
1536 PSA_KEY_USAGE_VERIFY_HASH |
1537 PSA_KEY_USAGE_DERIVE)) != 0) {
1538 return PSA_ERROR_INVALID_ARGUMENT;
1539 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001542}
1543
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544/** Validate the internal consistency of key attributes.
1545 *
1546 * This function only rejects invalid attribute values. If does not
1547 * validate the consistency of the attributes with any key data that may
1548 * be involved in the creation of the key.
1549 *
1550 * Call this function early in the key creation process.
1551 *
1552 * \param[in] attributes Key attributes for the new key.
1553 * \param[out] p_drv On any return, the driver for the key, if any.
1554 * NULL for a transparent key.
1555 *
1556 */
1557static psa_status_t psa_validate_key_attributes(
1558 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001560{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001561 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1563 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 status = psa_validate_key_location(lifetime, p_drv);
1566 if (status != PSA_SUCCESS) {
1567 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001568 }
1569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001570 status = psa_validate_key_persistence(lifetime);
1571 if (status != PSA_SUCCESS) {
1572 return status;
1573 }
1574
1575 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1576 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1577 return PSA_ERROR_INVALID_ARGUMENT;
1578 }
1579 } else {
1580 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1581 return PSA_ERROR_INVALID_ARGUMENT;
1582 }
1583 }
1584
1585 status = psa_validate_key_policy(&attributes->core.policy);
1586 if (status != PSA_SUCCESS) {
1587 return status;
1588 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001589
1590 /* Refuse to create overly large keys.
1591 * Note that this doesn't trigger on import if the attributes don't
1592 * explicitly specify a size (so psa_get_key_bits returns 0), so
1593 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001594 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1595 return PSA_ERROR_NOT_SUPPORTED;
1596 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001597
Gilles Peskine91e8c332019-08-02 19:19:39 +02001598 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1600 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1601 return PSA_ERROR_INVALID_ARGUMENT;
1602 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001605}
1606
Gilles Peskine4747d192019-04-17 15:05:45 +02001607/** Prepare a key slot to receive key material.
1608 *
1609 * This function allocates a key slot and sets its metadata.
1610 *
1611 * If this function fails, call psa_fail_key_creation().
1612 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001613 * This function is intended to be used as follows:
1614 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001615 * it with the specified attributes, and in case of a volatile key assign it
1616 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 * -# Populate the slot with the key material.
1618 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1619 * In case of failure at any step, stop the sequence and call
1620 * psa_fail_key_creation().
1621 *
Ronald Cron5c522922020-11-14 16:35:34 +01001622 * On success, the key slot is locked. It is the responsibility of the caller
1623 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001624 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001625 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001626 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 * \param[out] p_slot On success, a pointer to the prepared slot.
1628 * \param[out] p_drv On any return, the driver for the key, if any.
1629 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 *
1631 * \retval #PSA_SUCCESS
1632 * The key slot is ready to receive key material.
1633 * \return If this function fails, the key slot is an invalid state.
1634 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 */
1636static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001637 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001639 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001641{
1642 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001643 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001644 psa_key_slot_t *slot;
1645
Gilles Peskinedf179142019-07-15 22:02:14 +02001646 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 *p_drv = NULL;
1648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 status = psa_validate_key_attributes(attributes, p_drv);
1650 if (status != PSA_SUCCESS) {
1651 return status;
1652 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1655 if (status != PSA_SUCCESS) {
1656 return status;
1657 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 slot = *p_slot;
1659
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001660 /* We're storing the declared bit-size of the key. It's up to each
1661 * creation mechanism to verify that this information is correct.
1662 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001663 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001664 * is optional (import, copy). In case of a volatile key, assign it the
1665 * volatile key identifier associated to the slot returned to contain its
1666 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001667
1668 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001670#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1671 slot->attr.id = volatile_key_id;
1672#else
1673 slot->attr.id.key_id = volatile_key_id;
1674#endif
1675 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001676
Gilles Peskine91e8c332019-08-02 19:19:39 +02001677 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001678 * external-only flags, query `attributes`. Thanks to the check
1679 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001680 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001681 * may have set. */
1682 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001683
Gilles Peskinecbaff462019-07-12 23:46:04 +02001684#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001685 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001686 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001687 * create the key file in internal storage, create the
1688 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689 * persistent data. This is done by starting a transaction that will
1690 * encompass these three actions.
1691 * For registering a volatile key, we just need to find an appropriate
1692 * slot number inside the SE. Since the key is designated volatile, creating
1693 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001694 /* The first thing to do is to find a slot number for the new key.
1695 * We save the slot number in persistent storage as part of the
1696 * transaction data. It will be needed to recover if the power
1697 * fails during the key creation process, to clean up on the secure
1698 * element side after restarting. Obtaining a slot number from the
1699 * secure element driver updates its persistent state, but we do not yet
1700 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001701 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001702 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001703 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1705 &slot_number);
1706 if (status != PSA_SUCCESS) {
1707 return status;
1708 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1711 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001713 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001714 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 status = psa_crypto_save_transaction();
1716 if (status != PSA_SUCCESS) {
1717 (void) psa_crypto_stop_transaction();
1718 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001719 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001720 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001721
1722 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001724 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001727 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001729 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001730#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001732 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001733}
Gilles Peskine4747d192019-04-17 15:05:45 +02001734
1735/** Finalize the creation of a key once its key material has been set.
1736 *
1737 * This entails writing the key to persistent storage.
1738 *
1739 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 * See the documentation of psa_start_key_creation() for the intended use
1741 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001742 *
Ronald Cron5c522922020-11-14 16:35:34 +01001743 * If the finalization succeeds, the function unlocks the key slot (it was
1744 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1745 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001746 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 * \param[in,out] slot Pointer to the slot with key material.
1748 * \param[in] driver The secure element driver for the key,
1749 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001750 * \param[out] key On success, identifier of the key. Note that the
1751 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 *
1753 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001754 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001755 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1756 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1757 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1758 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1759 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1760 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001761 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001762 * \return If this function fails, the key slot is an invalid state.
1763 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001765static psa_status_t psa_finish_key_creation(
1766 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001767 psa_se_drv_table_entry_t *driver,
1768 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001769{
1770 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001771 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001772 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001773
1774#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001776#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001777 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001778 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001779 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001781
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001782 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1783 sizeof(data.slot_number),
1784 "Slot number size does not match psa_se_key_data_storage_t");
1785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1787 status = psa_save_persistent_key(&slot->attr,
1788 (uint8_t *) &data,
1789 sizeof(data));
1790 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001791#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1792 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001793 /* Key material is saved in export representation in the slot, so
1794 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 status = psa_save_persistent_key(&slot->attr,
1796 slot->key.data,
1797 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001798 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001800#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1801
Gilles Peskinecbaff462019-07-12 23:46:04 +02001802#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001803 /* Finish the transaction for a key creation. This does not
1804 * happen when registering an existing key. Detect this case
1805 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001806 * creation of a persistent key in a secure element requires a transaction,
1807 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001808 if (driver != NULL &&
1809 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1810 status = psa_save_se_persistent_data(driver);
1811 if (status != PSA_SUCCESS) {
1812 psa_destroy_persistent_key(slot->attr.id);
1813 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001814 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001816 }
1817#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001820 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001821 status = psa_unlock_key_slot(slot);
1822 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001823 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001825 }
Ronald Cron50972942020-11-14 11:28:25 +01001826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001828}
1829
1830/** Abort the creation of a key.
1831 *
1832 * You may call this function after calling psa_start_key_creation(),
1833 * or after psa_finish_key_creation() fails. In other circumstances, this
1834 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001835 * See the documentation of psa_start_key_creation() for the intended use
1836 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001837 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001838 * \param[in,out] slot Pointer to the slot with key material.
1839 * \param[in] driver The secure element driver for the key,
1840 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001841 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842static void psa_fail_key_creation(psa_key_slot_t *slot,
1843 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001844{
Gilles Peskine011e4282019-06-26 18:34:38 +02001845 (void) driver;
1846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001848 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001850
1851#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001852 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001853 * element, and the failure happened later (when saving metadata
1854 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001855 * element.
1856 * https://github.com/ARMmbed/mbed-crypto/issues/217
1857 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001858
Gilles Peskined7729582019-08-05 15:55:54 +02001859 /* Abort the ongoing transaction if any (there may not be one if
1860 * the creation process failed before starting one, or if the
1861 * key creation is a registration of a key in a secure element).
1862 * Earlier functions must already have done what it takes to undo any
1863 * partial creation. All that's left is to update the transaction data
1864 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001865 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001866#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001869}
1870
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001871/** Validate optional attributes during key creation.
1872 *
1873 * Some key attributes are optional during key creation. If they are
1874 * specified in the attributes structure, check that they are consistent
1875 * with the data in the slot.
1876 *
1877 * This function should be called near the end of key creation, after
1878 * the slot in memory is fully populated but before saving persistent data.
1879 */
1880static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001882 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 if (attributes->core.type != 0) {
1885 if (attributes->core.type != slot->attr.type) {
1886 return PSA_ERROR_INVALID_ARGUMENT;
1887 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888 }
1889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001890 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001891#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1893 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001894 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001895 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001897
Ronald Cron90857082020-11-25 14:58:33 +01001898 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 slot->attr.type,
1900 slot->key.data,
1901 slot->key.bytes,
1902 &rsa);
1903 if (status != PSA_SUCCESS) {
1904 return status;
1905 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 mbedtls_mpi_init(&actual);
1908 mbedtls_mpi_init(&required);
1909 ret = mbedtls_rsa_export(rsa,
1910 NULL, NULL, NULL, NULL, &actual);
1911 mbedtls_rsa_free(rsa);
1912 mbedtls_free(rsa);
1913 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001914 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001915 }
1916 ret = mbedtls_mpi_read_binary(&required,
1917 attributes->domain_parameters,
1918 attributes->domain_parameters_size);
1919 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 }
1922 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001924 }
1925rsa_exit:
1926 mbedtls_mpi_free(&actual);
1927 mbedtls_mpi_free(&required);
1928 if (ret != 0) {
1929 return mbedtls_to_psa_error(ret);
1930 }
1931 } else
John Durkop9814fa22020-11-04 12:28:15 -08001932#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1933 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001935 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 }
1937 }
1938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 if (attributes->core.bits != 0) {
1940 if (attributes->core.bits != slot->attr.bits) {
1941 return PSA_ERROR_INVALID_ARGUMENT;
1942 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 }
1944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001945 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001946}
1947
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001948psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1949 const uint8_t *data,
1950 size_t data_length,
1951 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001952{
1953 psa_status_t status;
1954 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001955 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001956 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957
Ronald Cron81709fc2020-11-14 12:10:32 +01001958 *key = MBEDTLS_SVC_KEY_ID_INIT;
1959
Gilles Peskine0f84d622019-09-12 19:03:13 +02001960 /* Reject zero-length symmetric keys (including raw data key objects).
1961 * This also rejects any key which might be encoded as an empty string,
1962 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 if (data_length == 0) {
1964 return PSA_ERROR_INVALID_ARGUMENT;
1965 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1968 &slot, &driver);
1969 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973 /* In the case of a transparent key or an opaque key stored in local
1974 * storage (thus not in the case of generating a key in a secure element
1975 * or cryptoprocessor with storage), we have to allocate a buffer to
1976 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001977 if (slot->key.data == NULL) {
1978 status = psa_allocate_buffer_to_slot(slot, data_length);
1979 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001980 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001982 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983
1984 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001985 status = psa_driver_wrapper_import_key(attributes,
1986 data, data_length,
1987 slot->key.data,
1988 slot->key.bytes,
1989 &slot->key.bytes, &bits);
1990 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001991 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001992 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001995 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001996 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001997 status = PSA_ERROR_INVALID_ARGUMENT;
1998 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001999 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002001 status = psa_validate_optional_attributes(slot, attributes);
2002 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002003 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002006 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 if (status != PSA_SUCCESS) {
2009 psa_fail_key_creation(slot, driver);
2010 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002013}
2014
Gilles Peskined7729582019-08-05 15:55:54 +02002015#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2016psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002017 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002018{
2019 psa_status_t status;
2020 psa_key_slot_t *slot = NULL;
2021 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002022 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002023
2024 /* Leaving attributes unspecified is not currently supported.
2025 * It could make sense to query the key type and size from the
2026 * secure element, but not all secure elements support this
2027 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2029 return PSA_ERROR_NOT_SUPPORTED;
2030 }
2031 if (psa_get_key_bits(attributes) == 0) {
2032 return PSA_ERROR_NOT_SUPPORTED;
2033 }
Gilles Peskined7729582019-08-05 15:55:54 +02002034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2036 &slot, &driver);
2037 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002038 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039 }
Gilles Peskined7729582019-08-05 15:55:54 +02002040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002042
2043exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 if (status != PSA_SUCCESS) {
2045 psa_fail_key_creation(slot, driver);
2046 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002047
Gilles Peskined7729582019-08-05 15:55:54 +02002048 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 psa_close_key(key);
2050 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002051}
2052#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002054static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2055 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002056{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 psa_status_t status = psa_copy_key_material_into_slot(target,
2058 source->key.data,
2059 source->key.bytes);
2060 if (status != PSA_SUCCESS) {
2061 return status;
2062 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Steven Cooreman398aee52020-10-13 14:35:45 +02002064 target->attr.type = source->attr.type;
2065 target->attr.bits = source->attr.bits;
2066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002068}
2069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002070psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2071 const psa_key_attributes_t *specified_attributes,
2072 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076 psa_key_slot_t *source_slot = NULL;
2077 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2082
Ronald Cron5c522922020-11-14 16:35:34 +01002083 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2085 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 status = psa_validate_optional_attributes(source_slot,
2090 specified_attributes);
2091 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002092 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 status = psa_restrict_key_policy(source_slot->attr.type,
2096 &actual_attributes.core.policy,
2097 &source_slot->attr.policy);
2098 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002100 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2103 &target_slot, &driver);
2104 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002105 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002110 /* Copying to a secure element is not implemented yet. */
2111 status = PSA_ERROR_NOT_SUPPORTED;
2112 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002113 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002114#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002117 /*
2118 * Copying through an opaque driver is not implemented yet, consider
2119 * a lifetime with an external location as an invalid parameter for
2120 * now.
2121 */
2122 status = PSA_ERROR_INVALID_ARGUMENT;
2123 goto exit;
2124 }
2125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 status = psa_copy_key_material(source_slot, target_slot);
2127 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 if (status != PSA_SUCCESS) {
2134 psa_fail_key_creation(target_slot, driver);
2135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140}
2141
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002142
2143
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002144/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002145/* Message digests */
2146/****************************************************************/
2147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002148psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002149{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002150 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002151 if (operation->id == 0) {
2152 return PSA_SUCCESS;
2153 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002155 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 operation->id = 0;
2157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002159}
2160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002161psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2162 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002165
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002168 status = PSA_ERROR_BAD_STATE;
2169 goto exit;
2170 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002173 status = PSA_ERROR_INVALID_ARGUMENT;
2174 goto exit;
2175 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002176
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002177 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2178 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002181 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002182
2183exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 if (status != PSA_SUCCESS) {
2185 psa_hash_abort(operation);
2186 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002187
2188 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189}
2190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2192 const uint8_t *input,
2193 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002197 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002198 status = PSA_ERROR_BAD_STATE;
2199 goto exit;
2200 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002201
Steven Cooremanc8288352021-03-04 14:02:19 +01002202 /* Don't require hash implementations to behave correctly on a
2203 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 if (input_length == 0) {
2205 return PSA_SUCCESS;
2206 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002209
2210exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 if (status != PSA_SUCCESS) {
2212 psa_hash_abort(operation);
2213 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216}
2217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2219 uint8_t *hash,
2220 size_t hash_size,
2221 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002223 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 if (operation->id == 0) {
2225 return PSA_ERROR_BAD_STATE;
2226 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227
Steven Cooreman1e582352021-02-18 17:24:37 +01002228 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 operation, hash, hash_size, hash_length);
2230 psa_hash_abort(operation);
2231 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232}
2233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2235 const uint8_t *hash,
2236 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002238 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002240 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 operation,
2242 actual_hash, sizeof(actual_hash),
2243 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002246 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002247 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002250 status = PSA_ERROR_INVALID_SIGNATURE;
2251 goto exit;
2252 }
2253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002255 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002257
2258exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2260 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002261 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002264 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265}
2266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002267psa_status_t psa_hash_compute(psa_algorithm_t alg,
2268 const uint8_t *input, size_t input_length,
2269 uint8_t *hash, size_t hash_size,
2270 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002271{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002272 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002273 if (!PSA_ALG_IS_HASH(alg)) {
2274 return PSA_ERROR_INVALID_ARGUMENT;
2275 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2278 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002279}
2280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281psa_status_t psa_hash_compare(psa_algorithm_t alg,
2282 const uint8_t *input, size_t input_length,
2283 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002284{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002285 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002286 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 if (!PSA_ALG_IS_HASH(alg)) {
2289 return PSA_ERROR_INVALID_ARGUMENT;
2290 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002291
Steven Cooreman1e582352021-02-18 17:24:37 +01002292 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293 alg, input, input_length,
2294 actual_hash, sizeof(actual_hash),
2295 &actual_hash_length);
2296 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002297 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298 }
2299 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002300 status = PSA_ERROR_INVALID_SIGNATURE;
2301 goto exit;
2302 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002303 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002304 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002306
2307exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2309 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002310}
2311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2313 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002314{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (source_operation->id == 0 ||
2316 target_operation->id != 0) {
2317 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002318 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2321 target_operation);
2322 if (status != PSA_SUCCESS) {
2323 psa_hash_abort(target_operation);
2324 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002327}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328
2329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331/* MAC */
2332/****************************************************************/
2333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002334psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335{
Steven Cooreman07897832021-03-19 18:28:56 +01002336 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 if (operation->id == 0) {
2338 return PSA_SUCCESS;
2339 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002342 operation->mac_size = 0;
2343 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002344 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348
Ronald Cron1c650a12021-06-17 16:33:22 +02002349static psa_status_t psa_mac_finalize_alg_and_key_validation(
2350 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002351 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 psa_key_type_t key_type = psa_get_key_type(attributes);
2356 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002358 if (!PSA_ALG_IS_MAC(alg)) {
2359 return PSA_ERROR_INVALID_ARGUMENT;
2360 }
Steven Cooreman07897832021-03-19 18:28:56 +01002361
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002362 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002363 status = psa_mac_key_can_do(alg, key_type);
2364 if (status != PSA_SUCCESS) {
2365 return status;
2366 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002367
Steven Cooremana6474de2021-05-10 11:13:41 +02002368 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002372 /* A very short MAC is too short for security since it can be
2373 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2374 * so we make this our minimum, even though 32 bits is still
2375 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002377 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2380 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002381 /* It's impossible to "truncate" to a larger length than the full length
2382 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002384 }
2385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002387 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2388 * that is disabled in the compile-time configuration. The result can
2389 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2390 * configuration into account. In this case, force a return of
2391 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2392 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2393 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2394 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2395 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002397 }
2398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002400}
2401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002402static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2403 mbedtls_svc_key_id_t key,
2404 psa_algorithm_t alg,
2405 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002406{
2407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002409 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002410
2411 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002413 status = PSA_ERROR_BAD_STATE;
2414 goto exit;
2415 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002416
2417 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 key,
2419 &slot,
2420 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2421 alg);
2422 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002423 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002425
2426 psa_key_attributes_t attributes = {
2427 .core = slot->attr
2428 };
2429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002430 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2431 &operation->mac_size);
2432 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002433 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002435
2436 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002437 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002438 if (is_sign) {
2439 status = psa_driver_wrapper_mac_sign_setup(operation,
2440 &attributes,
2441 slot->key.data,
2442 slot->key.bytes,
2443 alg);
2444 } else {
2445 status = psa_driver_wrapper_mac_verify_setup(operation,
2446 &attributes,
2447 slot->key.data,
2448 slot->key.bytes,
2449 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002450 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002451
Gilles Peskinefbfac682018-07-08 20:51:54 +02002452exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 if (status != PSA_SUCCESS) {
2454 psa_mac_abort(operation);
2455 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002457 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002460}
2461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2463 mbedtls_svc_key_id_t key,
2464 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002465{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002467}
2468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002469psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2470 mbedtls_svc_key_id_t key,
2471 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002473 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002474}
2475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002476psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2477 const uint8_t *input,
2478 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 if (operation->id == 0) {
2481 return PSA_ERROR_BAD_STATE;
2482 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483
Steven Cooreman11743f92021-03-19 18:38:46 +01002484 /* Don't require hash implementations to behave correctly on a
2485 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 if (input_length == 0) {
2487 return PSA_SUCCESS;
2488 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002490 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2491 input, input_length);
2492 if (status != PSA_SUCCESS) {
2493 psa_mac_abort(operation);
2494 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497}
2498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002499psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2500 uint8_t *mac,
2501 size_t mac_size,
2502 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002503{
Steven Cooreman87885df2021-03-19 19:04:39 +01002504 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2505 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002508 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002509 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002510 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002512 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002513 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002514 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002515 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002516
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002517 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2518 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002520 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002521 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002522 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002525 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002526 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002527 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002528
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002529 status = psa_driver_wrapper_mac_sign_finish(operation,
2530 mac, operation->mac_size,
2531 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002532
Dave Rodgman478ab542021-06-25 09:09:02 +01002533exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002534 /* In case of success, set the potential excess room in the output buffer
2535 * to an invalid value, to avoid potentially leaking a longer MAC.
2536 * In case of error, set the output length and content to a safe default,
2537 * such that in case the caller misses an error check, the output would be
2538 * an unachievable MAC.
2539 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002540 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002541 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002542 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002543 }
mohammad16036df908f2018-04-02 08:34:15 -07002544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002545 if (mac_size > operation->mac_size) {
2546 memset(&mac[operation->mac_size], '!',
2547 mac_size - operation->mac_size);
2548 }
Ronald Cron882eb782021-06-18 13:05:48 +02002549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002550 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002553}
2554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002555psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2556 const uint8_t *mac,
2557 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558{
Steven Cooreman87885df2021-03-19 19:04:39 +01002559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2560 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002562 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002563 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002564 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002565 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002568 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002569 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002570 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002572 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002573 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002574 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002575 }
2576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 status = psa_driver_wrapper_mac_verify_finish(operation,
2578 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002579
Dave Rodgman478ab542021-06-25 09:09:02 +01002580exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584}
2585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2587 psa_algorithm_t alg,
2588 const uint8_t *input,
2589 size_t input_length,
2590 uint8_t *mac,
2591 size_t mac_size,
2592 size_t *mac_length,
2593 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002594{
2595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002596 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2597 psa_key_slot_t *slot;
2598 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599
Ronald Crondbb86462021-06-17 17:17:20 +02002600 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 key,
2602 &slot,
2603 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2604 alg);
2605 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002606 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002608
Ronald Crondbb86462021-06-17 17:17:20 +02002609 psa_key_attributes_t attributes = {
2610 .core = slot->attr
2611 };
2612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2614 &operation_mac_size);
2615 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002616 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002620 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002621 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002622 }
2623
2624 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002625 &attributes,
2626 slot->key.data, slot->key.bytes,
2627 alg,
2628 input, input_length,
2629 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002630
2631exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002632 /* In case of success, set the potential excess room in the output buffer
2633 * to an invalid value, to avoid potentially leaking a longer MAC.
2634 * In case of error, set the output length and content to a safe default,
2635 * such that in case the caller misses an error check, the output would be
2636 * an unachievable MAC.
2637 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002638 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002639 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002640 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002641 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 if (mac_size > operation_mac_size) {
2643 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2644 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002646 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002649}
2650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002652 psa_algorithm_t alg,
2653 const uint8_t *input,
2654 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002655 uint8_t *mac,
2656 size_t mac_size,
2657 size_t *mac_length)
2658{
2659 return psa_mac_compute_internal(key, alg,
2660 input, input_length,
2661 mac, mac_size, mac_length, 1);
2662}
2663
2664psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2665 psa_algorithm_t alg,
2666 const uint8_t *input,
2667 size_t input_length,
2668 const uint8_t *mac,
2669 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002670{
2671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002672 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2673 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 status = psa_mac_compute_internal(key, alg,
2676 input, input_length,
2677 actual_mac, sizeof(actual_mac),
2678 &actual_mac_length, 0);
2679 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002680 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002683 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002684 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002685 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002686 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
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 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002691
2692exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002696}
Gilles Peskine20035e32018-02-03 22:44:14 +01002697
Gilles Peskine20035e32018-02-03 22:44:14 +01002698/****************************************************************/
2699/* Asymmetric cryptography */
2700/****************************************************************/
2701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2703 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002704{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705 if (input_is_message) {
2706 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2707 return PSA_ERROR_INVALID_ARGUMENT;
2708 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002710 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2711 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2712 return PSA_ERROR_INVALID_ARGUMENT;
2713 }
2714 }
2715 } else {
2716 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2717 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002718 }
2719 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002722}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2725 int input_is_message,
2726 psa_algorithm_t alg,
2727 const uint8_t *input,
2728 size_t input_length,
2729 uint8_t *signature,
2730 size_t signature_size,
2731 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002732{
2733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2734 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2735 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002736
2737 *signature_length = 0;
2738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002739 status = psa_sign_verify_check_alg(input_is_message, alg);
2740 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002741 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002743
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002744 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002745 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002746 * buffer can in principle be empty since it doesn't actually have
2747 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 if (signature_size == 0) {
2749 return PSA_ERROR_BUFFER_TOO_SMALL;
2750 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002751
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002752 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753 key, &slot,
2754 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2755 PSA_KEY_USAGE_SIGN_HASH,
2756 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002759 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002762 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002763 status = PSA_ERROR_INVALID_ARGUMENT;
2764 goto exit;
2765 }
2766
2767 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002768 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002769 };
2770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002772 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002773 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002774 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775 signature, signature_size, signature_length);
2776 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002777
2778 status = psa_driver_wrapper_sign_hash(
2779 &attributes, slot->key.data, slot->key.bytes,
2780 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002782 }
2783
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002784
2785exit:
2786 /* Fill the unused part of the output buffer (the whole buffer on error,
2787 * the trailing part on success) with something that isn't a valid signature
2788 * (barring an attack on the signature and deliberately-crafted input),
2789 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002790 if (status == PSA_SUCCESS) {
2791 memset(signature + *signature_length, '!',
2792 signature_size - *signature_length);
2793 } else {
2794 memset(signature, '!', signature_size);
2795 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002796 /* If signature_size is 0 then we have nothing to do. We must not call
2797 * memset because signature may be NULL in this case. */
2798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002802}
2803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2805 int input_is_message,
2806 psa_algorithm_t alg,
2807 const uint8_t *input,
2808 size_t input_length,
2809 const uint8_t *signature,
2810 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002811{
2812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2813 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2814 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 status = psa_sign_verify_check_alg(input_is_message, alg);
2817 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002818 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002819 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002820
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002821 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002822 key, &slot,
2823 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2824 PSA_KEY_USAGE_VERIFY_HASH,
2825 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002827 if (status != PSA_SUCCESS) {
2828 return status;
2829 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002830
2831 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002832 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002833 };
2834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002836 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002837 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002838 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 signature, signature_length);
2840 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841 status = psa_driver_wrapper_verify_hash(
2842 &attributes, slot->key.data, slot->key.bytes,
2843 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002844 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002845 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002848
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002849 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002850
2851}
2852
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002853psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002854 const psa_key_attributes_t *attributes,
2855 const uint8_t *key_buffer,
2856 size_t key_buffer_size,
2857 psa_algorithm_t alg,
2858 const uint8_t *input,
2859 size_t input_length,
2860 uint8_t *signature,
2861 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002863{
2864 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002867 size_t hash_length;
2868 uint8_t hash[PSA_HASH_MAX_SIZE];
2869
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002870 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002871 PSA_ALG_SIGN_GET_HASH(alg),
2872 input, input_length,
2873 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002876 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002877 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002878
2879 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 attributes, key_buffer, key_buffer_size,
2881 alg, hash, hash_length,
2882 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002883 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002885 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002886}
2887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002888psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2889 psa_algorithm_t alg,
2890 const uint8_t *input,
2891 size_t input_length,
2892 uint8_t *signature,
2893 size_t signature_size,
2894 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002895{
2896 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002897 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002899}
2900
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002901psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002902 const psa_key_attributes_t *attributes,
2903 const uint8_t *key_buffer,
2904 size_t key_buffer_size,
2905 psa_algorithm_t alg,
2906 const uint8_t *input,
2907 size_t input_length,
2908 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002910{
2911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002914 size_t hash_length;
2915 uint8_t hash[PSA_HASH_MAX_SIZE];
2916
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002917 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 PSA_ALG_SIGN_GET_HASH(alg),
2919 input, input_length,
2920 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002923 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002924 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002925
2926 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 attributes, key_buffer, key_buffer_size,
2928 alg, hash, hash_length,
2929 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002930 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002933}
2934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2936 psa_algorithm_t alg,
2937 const uint8_t *input,
2938 size_t input_length,
2939 const uint8_t *signature,
2940 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002941{
2942 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002943 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002945}
2946
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002947psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002948 const psa_key_attributes_t *attributes,
2949 const uint8_t *key_buffer, size_t key_buffer_size,
2950 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002952{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002953 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2954 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2955 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002956#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2958 return mbedtls_psa_rsa_sign_hash(
2959 attributes,
2960 key_buffer, key_buffer_size,
2961 alg, hash, hash_length,
2962 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002963#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2964 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002965 } else {
2966 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002967 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002968 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2969 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002970#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2972 return mbedtls_psa_ecdsa_sign_hash(
2973 attributes,
2974 key_buffer, key_buffer_size,
2975 alg, hash, hash_length,
2976 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002977#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2978 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002979 } else {
2980 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002981 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002982 }
Ronald Cron4501c982021-03-19 20:09:32 +01002983
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002984 (void) key_buffer;
2985 (void) key_buffer_size;
2986 (void) hash;
2987 (void) hash_length;
2988 (void) signature;
2989 (void) signature_size;
2990 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002992 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002993}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
2996 psa_algorithm_t alg,
2997 const uint8_t *hash,
2998 size_t hash_length,
2999 uint8_t *signature,
3000 size_t signature_size,
3001 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003002{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003003 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003004 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003005 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003006}
3007
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003008psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003009 const psa_key_attributes_t *attributes,
3010 const uint8_t *key_buffer, size_t key_buffer_size,
3011 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003012 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003013{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3015 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3016 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003017#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3019 return mbedtls_psa_rsa_verify_hash(
3020 attributes,
3021 key_buffer, key_buffer_size,
3022 alg, hash, hash_length,
3023 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003024#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3025 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 } else {
3027 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003028 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003029 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3030 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003031#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003032 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3033 return mbedtls_psa_ecdsa_verify_hash(
3034 attributes,
3035 key_buffer, key_buffer_size,
3036 alg, hash, hash_length,
3037 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003038#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3039 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040 } else {
3041 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003042 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003043 }
Ronald Cron4501c982021-03-19 20:09:32 +01003044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003045 (void) key_buffer;
3046 (void) key_buffer_size;
3047 (void) hash;
3048 (void) hash_length;
3049 (void) signature;
3050 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003053}
3054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3056 psa_algorithm_t alg,
3057 const uint8_t *hash,
3058 size_t hash_length,
3059 const uint8_t *signature,
3060 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003061{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003062 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003063 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003065}
3066
John Durkop0e005192020-10-31 22:06:54 -07003067#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3069 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003070{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003071 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3072 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3073 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3074 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003075}
John Durkop0e005192020-10-31 22:06:54 -07003076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003078psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3079 psa_algorithm_t alg,
3080 const uint8_t *input,
3081 size_t input_length,
3082 const uint8_t *salt,
3083 size_t salt_length,
3084 uint8_t *output,
3085 size_t output_size,
3086 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003087{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003090 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003091
Darryl Green5cc689a2018-07-24 15:34:10 +01003092 (void) input;
3093 (void) input_length;
3094 (void) salt;
3095 (void) output;
3096 (void) output_size;
3097
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003098 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3101 return PSA_ERROR_INVALID_ARGUMENT;
3102 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003103
Ronald Cron5c522922020-11-14 16:35:34 +01003104 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3106 if (status != PSA_SUCCESS) {
3107 return status;
3108 }
3109 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3110 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003111 status = PSA_ERROR_INVALID_ARGUMENT;
3112 goto exit;
3113 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003118 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3120 slot->key.data,
3121 slot->key.bytes,
3122 &rsa);
3123 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003124 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003125 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128 status = PSA_ERROR_BUFFER_TOO_SMALL;
3129 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003130 }
Ronald Cron7207d572021-09-08 14:28:35 +02003131#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3132 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003133 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003134#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003135 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003136 mbedtls_rsa_pkcs1_encrypt(rsa,
3137 mbedtls_psa_get_random,
3138 MBEDTLS_PSA_RANDOM_STATE,
3139 MBEDTLS_RSA_PUBLIC,
3140 input_length,
3141 input,
3142 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003143#else
3144 status = PSA_ERROR_NOT_SUPPORTED;
3145#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003146 } else
3147 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003148#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003150 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003151 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3152 mbedtls_psa_get_random,
3153 MBEDTLS_PSA_RANDOM_STATE,
3154 MBEDTLS_RSA_PUBLIC,
3155 salt, salt_length,
3156 input_length,
3157 input,
3158 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003159#else
3160 status = PSA_ERROR_NOT_SUPPORTED;
3161#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003163 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164 }
Ronald Cron7207d572021-09-08 14:28:35 +02003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168 if (status == PSA_SUCCESS) {
3169 *output_length = mbedtls_rsa_get_len(rsa);
3170 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 mbedtls_rsa_free(rsa);
3173 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003174#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003175 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003177 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003179
3180exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003181 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003184}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003186psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3187 psa_algorithm_t alg,
3188 const uint8_t *input,
3189 size_t input_length,
3190 const uint8_t *salt,
3191 size_t salt_length,
3192 uint8_t *output,
3193 size_t output_size,
3194 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003196 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003197 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003198 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003199
Darryl Green5cc689a2018-07-24 15:34:10 +01003200 (void) input;
3201 (void) input_length;
3202 (void) salt;
3203 (void) output;
3204 (void) output_size;
3205
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003206 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3209 return PSA_ERROR_INVALID_ARGUMENT;
3210 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003211
Ronald Cron5c522922020-11-14 16:35:34 +01003212 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3214 if (status != PSA_SUCCESS) {
3215 return status;
3216 }
3217 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003218 status = PSA_ERROR_INVALID_ARGUMENT;
3219 goto exit;
3220 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003222 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003223#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003224 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003225 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3227 slot->key.data,
3228 slot->key.bytes,
3229 &rsa);
3230 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003231 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003232 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003234 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003235 status = PSA_ERROR_INVALID_ARGUMENT;
3236 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003237 }
Ronald Cron7207d572021-09-08 14:28:35 +02003238#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3239 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003241 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003242#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003243 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003244 mbedtls_rsa_pkcs1_decrypt(rsa,
3245 mbedtls_psa_get_random,
3246 MBEDTLS_PSA_RANDOM_STATE,
3247 MBEDTLS_RSA_PRIVATE,
3248 output_length,
3249 input,
3250 output,
3251 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003252#else
3253 status = PSA_ERROR_NOT_SUPPORTED;
3254#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003255 } else
3256 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003257#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003259 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003260 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3261 mbedtls_psa_get_random,
3262 MBEDTLS_PSA_RANDOM_STATE,
3263 MBEDTLS_RSA_PRIVATE,
3264 salt, salt_length,
3265 output_length,
3266 input,
3267 output,
3268 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003269#else
3270 status = PSA_ERROR_NOT_SUPPORTED;
3271#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003273 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003275
Ronald Cron7207d572021-09-08 14:28:35 +02003276#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003278rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003279 mbedtls_rsa_free(rsa);
3280 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003281#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3282 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003284 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003286
3287exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003291}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003292
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003293
3294
mohammad1603503973b2018-03-12 15:59:30 +02003295/****************************************************************/
3296/* Symmetric cryptography */
3297/****************************************************************/
3298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003299static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3300 mbedtls_svc_key_id_t key,
3301 psa_algorithm_t alg,
3302 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003303{
3304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3305 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003306 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3308 PSA_KEY_USAGE_ENCRYPT :
3309 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003310
3311 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003313 status = PSA_ERROR_BAD_STATE;
3314 goto exit;
3315 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003318 status = PSA_ERROR_INVALID_ARGUMENT;
3319 goto exit;
3320 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3323 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003324 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003326
Ronald Cron49fafa92021-03-10 08:34:23 +01003327 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003328 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003329 * so we only set it (in the driver wrapper) after resources have been
3330 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003331 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003333 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003335 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003337 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 }
3339 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003340
Ronald Crona4af55f2020-12-14 14:36:06 +01003341 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003343 };
3344
Ronald Cronab99ac22020-10-01 16:38:28 +02003345 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346 if (cipher_operation == MBEDTLS_ENCRYPT) {
3347 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3348 &attributes,
3349 slot->key.data,
3350 slot->key.bytes,
3351 alg);
3352 } else {
3353 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3354 &attributes,
3355 slot->key.data,
3356 slot->key.bytes,
3357 alg);
3358 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003359
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003360exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 if (status != PSA_SUCCESS) {
3362 psa_cipher_abort(operation);
3363 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003367 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003368}
3369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3371 mbedtls_svc_key_id_t key,
3372 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003373{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003374 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003375}
3376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3378 mbedtls_svc_key_id_t key,
3379 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003380{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003381 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003382}
3383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3385 uint8_t *iv,
3386 size_t iv_size,
3387 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003388{
Ronald Cron6d051732020-10-01 14:10:20 +02003389 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003390 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3391 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003393 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003394 status = PSA_ERROR_BAD_STATE;
3395 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003396 }
3397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003399 status = PSA_ERROR_BAD_STATE;
3400 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003401 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003402
Ronald Cronc423acb2021-07-05 12:31:44 +02003403 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003404 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003405 status = PSA_ERROR_BUFFER_TOO_SMALL;
3406 goto exit;
3407 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003410 status = PSA_ERROR_GENERIC_ERROR;
3411 goto exit;
3412 }
3413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 status = psa_generate_random(local_iv, default_iv_length);
3415 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003416 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 }
Ronald Cron5618a392021-03-26 09:52:26 +01003418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 status = psa_driver_wrapper_cipher_set_iv(operation,
3420 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003421
3422exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003423 if (status == PSA_SUCCESS) {
3424 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003425 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003426 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003428 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003429 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003430 }
Ronald Cron6d051732020-10-01 14:10:20 +02003431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003433}
3434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003435psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3436 const uint8_t *iv,
3437 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003438{
Ronald Cron6d051732020-10-01 14:10:20 +02003439 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003442 status = PSA_ERROR_BAD_STATE;
3443 goto exit;
3444 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003447 status = PSA_ERROR_BAD_STATE;
3448 goto exit;
3449 }
Ronald Crona0d68172021-03-26 10:15:08 +01003450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003451 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003452 status = PSA_ERROR_INVALID_ARGUMENT;
3453 goto exit;
3454 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 status = psa_driver_wrapper_cipher_set_iv(operation,
3457 iv,
3458 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003459
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003460exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003462 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003463 } else {
3464 psa_cipher_abort(operation);
3465 }
3466 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003467}
3468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003469psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3470 const uint8_t *input,
3471 size_t input_length,
3472 uint8_t *output,
3473 size_t output_size,
3474 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003475{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003476 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003478 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003479 status = PSA_ERROR_BAD_STATE;
3480 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003481 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003484 status = PSA_ERROR_BAD_STATE;
3485 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003486 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 status = psa_driver_wrapper_cipher_update(operation,
3489 input,
3490 input_length,
3491 output,
3492 output_size,
3493 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003494
3495exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003496 if (status != PSA_SUCCESS) {
3497 psa_cipher_abort(operation);
3498 }
Ronald Cron6d051732020-10-01 14:10:20 +02003499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003501}
3502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003503psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3504 uint8_t *output,
3505 size_t output_size,
3506 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003507{
David Saadab4ecc272019-02-14 13:48:10 +02003508 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003510 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003511 status = PSA_ERROR_BAD_STATE;
3512 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003513 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003516 status = PSA_ERROR_BAD_STATE;
3517 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003518 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 status = psa_driver_wrapper_cipher_finish(operation,
3521 output,
3522 output_size,
3523 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003524
3525exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 if (status == PSA_SUCCESS) {
3527 return psa_cipher_abort(operation);
3528 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003529 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003530 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003532 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003533 }
mohammad1603503973b2018-03-12 15:59:30 +02003534}
3535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003537{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003539 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003540 * in use. It's ok to call abort on such an object, and there's
3541 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003542 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003543 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003546
Ronald Cron49fafa92021-03-10 08:34:23 +01003547 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003548 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003549 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003551 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003552}
3553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003554psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3555 psa_algorithm_t alg,
3556 const uint8_t *input,
3557 size_t input_length,
3558 uint8_t *output,
3559 size_t output_size,
3560 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003561{
3562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003564 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003565 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3566 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003569 status = PSA_ERROR_INVALID_ARGUMENT;
3570 goto exit;
3571 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003573 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3574 PSA_KEY_USAGE_ENCRYPT,
3575 alg);
3576 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003577 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003579
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003580 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003581 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003582 };
3583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003584 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3585 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003586 status = PSA_ERROR_GENERIC_ERROR;
3587 goto exit;
3588 }
3589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 if (default_iv_length > 0) {
3591 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003592 status = PSA_ERROR_BUFFER_TOO_SMALL;
3593 goto exit;
3594 }
3595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 status = psa_generate_random(local_iv, default_iv_length);
3597 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003598 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003599 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003600 }
3601
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003602 status = psa_driver_wrapper_cipher_encrypt(
3603 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003604 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 mbedtls_buffer_offset(output, default_iv_length),
3606 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003607
3608exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003609 unlock_status = psa_unlock_key_slot(slot);
3610 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003611 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003612 }
Ronald Cron16377072021-07-08 19:00:07 +02003613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 if (status == PSA_SUCCESS) {
3615 if (default_iv_length > 0) {
3616 memcpy(output, local_iv, default_iv_length);
3617 }
3618 *output_length += default_iv_length;
3619 } else {
3620 *output_length = 0;
3621 }
3622
3623 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003624}
3625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3627 psa_algorithm_t alg,
3628 const uint8_t *input,
3629 size_t input_length,
3630 uint8_t *output,
3631 size_t output_size,
3632 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003633{
3634 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003635 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003636 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003639 status = PSA_ERROR_INVALID_ARGUMENT;
3640 goto exit;
3641 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003643 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3644 PSA_KEY_USAGE_DECRYPT,
3645 alg);
3646 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003647 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003648 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003649
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003650 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003651 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003652 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003654 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003655 status = PSA_ERROR_INVALID_ARGUMENT;
3656 goto exit;
3657 }
3658
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003659 status = psa_driver_wrapper_cipher_decrypt(
3660 &attributes, slot->key.data, slot->key.bytes,
3661 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003662 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003663
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003664exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003665 unlock_status = psa_unlock_key_slot(slot);
3666 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003667 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003671 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 }
Ronald Cron16377072021-07-08 19:00:07 +02003673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003675}
3676
3677
mohammad16035955c982018-04-26 00:53:03 +03003678/****************************************************************/
3679/* AEAD */
3680/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3683 psa_algorithm_t alg,
3684 const uint8_t *nonce,
3685 size_t nonce_length,
3686 const uint8_t *additional_data,
3687 size_t additional_data_length,
3688 const uint8_t *plaintext,
3689 size_t plaintext_length,
3690 uint8_t *ciphertext,
3691 size_t ciphertext_size,
3692 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003693{
Ronald Cron215633c2021-03-16 17:15:37 +01003694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3695 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003696
mohammad1603f08a5502018-06-03 15:05:47 +03003697 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3700 return PSA_ERROR_NOT_SUPPORTED;
3701 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003702
Ronald Cron9a986162021-03-26 12:40:07 +01003703 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3705 if (status != PSA_SUCCESS) {
3706 return status;
3707 }
mohammad16035955c982018-04-26 00:53:03 +03003708
Ronald Cron215633c2021-03-16 17:15:37 +01003709 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003711 };
mohammad16035955c982018-04-26 00:53:03 +03003712
Ronald Cronde822812021-03-17 16:08:20 +01003713 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003714 &attributes, slot->key.data, slot->key.bytes,
3715 alg,
3716 nonce, nonce_length,
3717 additional_data, additional_data_length,
3718 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003719 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3722 memset(ciphertext, 0, ciphertext_size);
3723 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003725 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003728}
3729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3731 psa_algorithm_t alg,
3732 const uint8_t *nonce,
3733 size_t nonce_length,
3734 const uint8_t *additional_data,
3735 size_t additional_data_length,
3736 const uint8_t *ciphertext,
3737 size_t ciphertext_length,
3738 uint8_t *plaintext,
3739 size_t plaintext_size,
3740 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003741{
Ronald Cron215633c2021-03-16 17:15:37 +01003742 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3743 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003744
Gilles Peskineee652a32018-06-01 19:23:52 +02003745 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3748 return PSA_ERROR_NOT_SUPPORTED;
3749 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003750
Ronald Cron9a986162021-03-26 12:40:07 +01003751 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003752 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3753 if (status != PSA_SUCCESS) {
3754 return status;
3755 }
mohammad16035955c982018-04-26 00:53:03 +03003756
Ronald Cron215633c2021-03-16 17:15:37 +01003757 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003759 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003760
Ronald Cronde822812021-03-17 16:08:20 +01003761 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003762 &attributes, slot->key.data, slot->key.bytes,
3763 alg,
3764 nonce, nonce_length,
3765 additional_data, additional_data_length,
3766 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003769 if (status != PSA_SUCCESS && plaintext_size != 0) {
3770 memset(plaintext, 0, plaintext_size);
3771 }
mohammad160360a64d02018-06-03 17:20:42 +03003772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003773 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003775 return status;
mohammad16035955c982018-04-26 00:53:03 +03003776}
3777
Gilles Peskinee59236f2018-01-27 23:32:46 +01003778/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003779/* Generators */
3780/****************************************************************/
3781
John Durkop07cc04a2020-11-16 22:08:34 -08003782#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3783 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3784 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3785#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003786#endif /* At least one builtin KDF */
3787
3788#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3789 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3790 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3791static psa_status_t psa_key_derivation_start_hmac(
3792 psa_mac_operation_t *operation,
3793 psa_algorithm_t hash_alg,
3794 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003795 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003796{
3797 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003799 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3800 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003802
Steven Cooreman15f0d922021-05-07 14:14:37 +02003803 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003804 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003806 status = psa_driver_wrapper_mac_sign_setup(operation,
3807 &attributes,
3808 hmac_key, hmac_key_length,
3809 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003811 psa_reset_key_attributes(&attributes);
3812 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003813}
3814#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003815
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003816#define HKDF_STATE_INIT 0 /* no input yet */
3817#define HKDF_STATE_STARTED 1 /* got salt */
3818#define HKDF_STATE_KEYED 2 /* got key */
3819#define HKDF_STATE_OUTPUT 3 /* output started */
3820
Gilles Peskinecbe66502019-05-16 16:59:18 +02003821static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003823{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003824 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3825 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3826 } else {
3827 return operation->alg;
3828 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003829}
3830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003832{
3833 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3835 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003836 /* The object has (apparently) been initialized but it is not
3837 * in use. It's ok to call abort on such an object, and there's
3838 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003839 } else
John Durkopd0321952020-10-29 21:37:36 -07003840#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3842 mbedtls_free(operation->ctx.hkdf.info);
3843 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3844 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003845#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3846#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3847 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003848 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3849 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3850 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3851 if (operation->ctx.tls12_prf.secret != NULL) {
3852 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3853 operation->ctx.tls12_prf.secret_length);
3854 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003855 }
3856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 if (operation->ctx.tls12_prf.seed != NULL) {
3858 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3859 operation->ctx.tls12_prf.seed_length);
3860 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003861 }
3862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003863 if (operation->ctx.tls12_prf.label != NULL) {
3864 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3865 operation->ctx.tls12_prf.label_length);
3866 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003867 }
3868
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003869 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003870
3871 /* We leave the fields Ai and output_block to be erased safely by the
3872 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003874#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3875 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003876 {
3877 status = PSA_ERROR_BAD_STATE;
3878 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 mbedtls_platform_zeroize(operation, sizeof(*operation));
3880 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003881}
3882
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003883psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003885{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003887 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003889 }
3890
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003891 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003893}
3894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3896 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003897{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 if (operation->alg == 0) {
3899 return PSA_ERROR_BAD_STATE;
3900 }
3901 if (capacity > operation->capacity) {
3902 return PSA_ERROR_INVALID_ARGUMENT;
3903 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003904 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003906}
3907
John Durkopd0321952020-10-29 21:37:36 -07003908#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003909/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003910 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003911static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3912 psa_algorithm_t hash_alg,
3913 uint8_t *output,
3914 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003916 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003917 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003918 psa_status_t status;
3919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003920 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3921 return PSA_ERROR_BAD_STATE;
3922 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003923 hkdf->state = HKDF_STATE_OUTPUT;
3924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003925 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003926 /* Copy what remains of the current block */
3927 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003929 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 }
3931 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003932 output += n;
3933 output_length -= n;
3934 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003938 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003939 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003940 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003941 * object was corrupted or if this function is called directly
3942 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 if (hkdf->block_number == 0xff) {
3944 return PSA_ERROR_BAD_STATE;
3945 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003946
3947 /* We need a new block */
3948 ++hkdf->block_number;
3949 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003951 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3952 hash_alg,
3953 hkdf->prk,
3954 hash_length);
3955 if (status != PSA_SUCCESS) {
3956 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003957 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003958
3959 if (hkdf->block_number != 1) {
3960 status = psa_mac_update(&hkdf->hmac,
3961 hkdf->output_block,
3962 hash_length);
3963 if (status != PSA_SUCCESS) {
3964 return status;
3965 }
3966 }
3967 status = psa_mac_update(&hkdf->hmac,
3968 hkdf->info,
3969 hkdf->info_length);
3970 if (status != PSA_SUCCESS) {
3971 return status;
3972 }
3973 status = psa_mac_update(&hkdf->hmac,
3974 &hkdf->block_number, 1);
3975 if (status != PSA_SUCCESS) {
3976 return status;
3977 }
3978 status = psa_mac_sign_finish(&hkdf->hmac,
3979 hkdf->output_block,
3980 sizeof(hkdf->output_block),
3981 &hmac_output_length);
3982 if (status != PSA_SUCCESS) {
3983 return status;
3984 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003985 }
3986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003988}
John Durkop07cc04a2020-11-16 22:08:34 -08003989#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003990
John Durkop07cc04a2020-11-16 22:08:34 -08003991#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3992 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003993static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3994 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01003996{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
3998 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003999 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4000 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004001 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004002
Janos Follath7742fee2019-06-17 12:58:10 +01004003 /* We can't be wanting more output after block 0xff, otherwise
4004 * the capacity check in psa_key_derivation_output_bytes() would have
4005 * prevented this call. It could happen only if the operation
4006 * object was corrupted or if this function is called directly
4007 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004008 if (tls12_prf->block_number == 0xff) {
4009 return PSA_ERROR_CORRUPTION_DETECTED;
4010 }
Janos Follath7742fee2019-06-17 12:58:10 +01004011
4012 /* We need a new block */
4013 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004014 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004015
4016 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4017 *
4018 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4019 *
4020 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4021 * HMAC_hash(secret, A(2) + seed) +
4022 * HMAC_hash(secret, A(3) + seed) + ...
4023 *
4024 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004025 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004026 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004027 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004028 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004029 * is currently extracted as `output_block` and where i is
4030 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004031 */
4032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004033 status = psa_key_derivation_start_hmac(&hmac,
4034 hash_alg,
4035 tls12_prf->secret,
4036 tls12_prf->secret_length);
4037 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004038 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004039 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004040
4041 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004043 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4044 * the variable seed and in this instance means it in the context of the
4045 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 status = psa_mac_update(&hmac,
4047 tls12_prf->label,
4048 tls12_prf->label_length);
4049 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004050 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051 }
4052 status = psa_mac_update(&hmac,
4053 tls12_prf->seed,
4054 tls12_prf->seed_length);
4055 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004056 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057 }
4058 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004059 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4061 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004062 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004063 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004064 }
4065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 status = psa_mac_sign_finish(&hmac,
4067 tls12_prf->Ai, hash_length,
4068 &hmac_output_length);
4069 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004070 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004071 }
4072 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004073 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004074 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004075
4076 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 status = psa_key_derivation_start_hmac(&hmac,
4078 hash_alg,
4079 tls12_prf->secret,
4080 tls12_prf->secret_length);
4081 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004082 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 }
4084 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_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->label, tls12_prf->label_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->seed, tls12_prf->seed_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_sign_finish(&hmac,
4097 tls12_prf->output_block, hash_length,
4098 &hmac_output_length);
4099 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004100 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004101 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004102
Janos Follath7742fee2019-06-17 12:58:10 +01004103
4104cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004105 cleanup_status = psa_mac_abort(&hmac);
4106 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004107 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004110 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004111}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004112
Janos Follath844eb0e2019-06-19 12:10:49 +01004113static psa_status_t psa_key_derivation_tls12_prf_read(
4114 psa_tls12_prf_key_derivation_t *tls12_prf,
4115 psa_algorithm_t alg,
4116 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004118{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004119 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4120 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004121 psa_status_t status;
4122 uint8_t offset, length;
4123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004124 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004125 case PSA_TLS12_PRF_STATE_LABEL_SET:
4126 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4127 break;
4128 case PSA_TLS12_PRF_STATE_OUTPUT:
4129 break;
4130 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004132 }
4133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004134 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004135 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004136 if (tls12_prf->left_in_block == 0) {
4137 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4138 alg);
4139 if (status != PSA_SUCCESS) {
4140 return status;
4141 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004142
4143 continue;
4144 }
4145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004147 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004148 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004149 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004150 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004151
4152 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004153 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004154 output += length;
4155 output_length -= length;
4156 tls12_prf->left_in_block -= length;
4157 }
4158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004159 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004160}
John Durkop07cc04a2020-11-16 22:08:34 -08004161#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4162 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004163
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004164psa_status_t psa_key_derivation_output_bytes(
4165 psa_key_derivation_operation_t *operation,
4166 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004168{
4169 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004173 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004175 }
4176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004179 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004181 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004182 goto exit;
4183 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004186 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004187 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4188 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004190 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004191 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004192 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194
John Durkopd0321952020-10-29 21:37:36 -07004195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004196 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4197 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4198 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4199 output, output_length);
4200 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004201#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4202#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4203 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4205 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4206 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4207 kdf_alg, output,
4208 output_length);
4209 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004210#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4211 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004212 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004213 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004214 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004215 }
4216
4217exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004218 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004219 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004220 * This allows us to differentiate between exhausted operations and
4221 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4222 * operations. */
4223 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004224 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004225 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004226 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004227 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004228 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004229}
4230
David Brown7807bf72021-02-09 16:28:23 -07004231#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004232static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004233{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 if (data_size >= 8) {
4235 mbedtls_des_key_set_parity(data);
4236 }
4237 if (data_size >= 16) {
4238 mbedtls_des_key_set_parity(data + 8);
4239 }
4240 if (data_size >= 24) {
4241 mbedtls_des_key_set_parity(data + 16);
4242 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004243}
David Brown7807bf72021-02-09 16:28:23 -07004244#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004245
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004246static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004247 psa_key_slot_t *slot,
4248 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004249 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004250{
4251 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004253 psa_status_t status;
4254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004255 if (!key_type_is_raw_bytes(slot->attr.type)) {
4256 return PSA_ERROR_INVALID_ARGUMENT;
4257 }
4258 if (bits % 8 != 0) {
4259 return PSA_ERROR_INVALID_ARGUMENT;
4260 }
4261 data = mbedtls_calloc(1, bytes);
4262 if (data == NULL) {
4263 return PSA_ERROR_INSUFFICIENT_MEMORY;
4264 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 status = psa_key_derivation_output_bytes(operation, data, bytes);
4267 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004268 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 }
David Brown12ca5032021-02-11 11:02:00 -07004270#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004271 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4272 psa_des_set_key_parity(data, bytes);
4273 }
David Brown8107e312021-02-12 08:08:46 -07004274#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004276 status = psa_allocate_buffer_to_slot(slot, bytes);
4277 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004278 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 }
Ronald Crondd04d422020-11-28 15:14:42 +01004280
Ronald Cronbf33c932020-11-28 18:06:53 +01004281 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004282 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004284 };
4285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 status = psa_driver_wrapper_import_key(&attributes,
4287 data, bytes,
4288 slot->key.data,
4289 slot->key.bytes,
4290 &slot->key.bytes, &bits);
4291 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004292 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004294
4295exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 mbedtls_free(data);
4297 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004298}
4299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004300psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4301 psa_key_derivation_operation_t *operation,
4302 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004303{
4304 psa_status_t status;
4305 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004306 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004307
Ronald Cron81709fc2020-11-14 12:10:32 +01004308 *key = MBEDTLS_SVC_KEY_ID_INIT;
4309
Gilles Peskine0f84d622019-09-12 19:03:13 +02004310 /* Reject any attempt to create a zero-length key so that we don't
4311 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 if (psa_get_key_bits(attributes) == 0) {
4313 return PSA_ERROR_INVALID_ARGUMENT;
4314 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004315
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 if (operation->alg == PSA_ALG_NONE) {
4317 return PSA_ERROR_BAD_STATE;
4318 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 if (!operation->can_output_key) {
4321 return PSA_ERROR_NOT_PERMITTED;
4322 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4325 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004327 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004328 /* Deriving a key in a secure element is not implemented yet. */
4329 status = PSA_ERROR_NOT_SUPPORTED;
4330 }
4331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 if (status == PSA_SUCCESS) {
4333 status = psa_generate_derived_key_internal(slot,
4334 attributes->core.bits,
4335 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004336 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004337 if (status == PSA_SUCCESS) {
4338 status = psa_finish_key_creation(slot, driver, key);
4339 }
4340 if (status != PSA_SUCCESS) {
4341 psa_fail_key_creation(slot, driver);
4342 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004344 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004345}
4346
Gilles Peskineeab56e42018-07-12 17:12:33 +02004347
4348
4349/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004350/* Key derivation */
4351/****************************************************************/
4352
Steven Cooreman932ffb72021-02-15 12:14:32 +01004353#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004355{
4356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4358 return 1;
4359 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004360#endif
4361#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4363 return 1;
4364 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004365#endif
4366#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4368 return 1;
4369 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004370#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004372}
4373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004375{
4376 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004377 psa_status_t status = psa_hash_setup(&operation, alg);
4378 psa_hash_abort(&operation);
4379 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004380}
4381
Gilles Peskine969c5d62019-01-16 15:53:06 +01004382static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004385{
Janos Follath5fe19732019-06-20 15:09:30 +01004386 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4387 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004388 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004389
Gilles Peskine969c5d62019-01-16 15:53:06 +01004390 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 if (!is_kdf_alg_supported(kdf_alg)) {
4392 return PSA_ERROR_NOT_SUPPORTED;
4393 }
John Durkop07cc04a2020-11-16 22:08:34 -08004394
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004395 /* All currently supported key derivation algorithms are based on a
4396 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004397 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4398 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4399 if (hash_size == 0) {
4400 return PSA_ERROR_NOT_SUPPORTED;
4401 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004402
4403 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4404 * we might fail later, which is somewhat unfriendly and potentially
4405 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004406 psa_status_t status = psa_hash_try_support(hash_alg);
4407 if (status != PSA_SUCCESS) {
4408 return status;
4409 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004411 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4412 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4413 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4414 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004416
Gilles Peskinecdacf042021-04-29 21:10:00 +02004417 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004418 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004419}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004421static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004422{
4423#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 if (alg == PSA_ALG_ECDH) {
4425 return PSA_SUCCESS;
4426 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004427#endif
4428 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004430}
John Durkop07cc04a2020-11-16 22:08:34 -08004431#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4434 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004435{
4436 psa_status_t status;
4437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 if (operation->alg != 0) {
4439 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004442 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4443 return PSA_ERROR_INVALID_ARGUMENT;
4444 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4445#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4446 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4447 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4448 status = psa_key_agreement_try_support(ka_alg);
4449 if (status != PSA_SUCCESS) {
4450 return status;
4451 }
4452 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4453#else
4454 return PSA_ERROR_NOT_SUPPORTED;
4455#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4456 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4457#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4458 status = psa_key_derivation_setup_kdf(operation, alg);
4459#else
4460 return PSA_ERROR_NOT_SUPPORTED;
4461#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4462 } else {
4463 return PSA_ERROR_INVALID_ARGUMENT;
4464 }
4465
4466 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004468 }
4469 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004470}
4471
John Durkopd0321952020-10-29 21:37:36 -07004472#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004473static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4474 psa_algorithm_t hash_alg,
4475 psa_key_derivation_step_t step,
4476 const uint8_t *data,
4477 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478{
4479 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004481 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 if (hkdf->state != HKDF_STATE_INIT) {
4483 return PSA_ERROR_BAD_STATE;
4484 } else {
4485 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4486 hash_alg,
4487 data, data_length);
4488 if (status != PSA_SUCCESS) {
4489 return status;
4490 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004491 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004492 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004493 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004494 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 if (hkdf->state == HKDF_STATE_INIT) {
4497 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4498 hash_alg,
4499 NULL, 0);
4500 if (status != PSA_SUCCESS) {
4501 return status;
4502 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004503 hkdf->state = HKDF_STATE_STARTED;
4504 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 if (hkdf->state != HKDF_STATE_STARTED) {
4506 return PSA_ERROR_BAD_STATE;
4507 }
4508 status = psa_mac_update(&hkdf->hmac,
4509 data, data_length);
4510 if (status != PSA_SUCCESS) {
4511 return status;
4512 }
4513 status = psa_mac_sign_finish(&hkdf->hmac,
4514 hkdf->prk,
4515 sizeof(hkdf->prk),
4516 &data_length);
4517 if (status != PSA_SUCCESS) {
4518 return status;
4519 }
4520 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004521 hkdf->block_number = 0;
4522 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004524 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 if (hkdf->state == HKDF_STATE_OUTPUT) {
4526 return PSA_ERROR_BAD_STATE;
4527 }
4528 if (hkdf->info_set) {
4529 return PSA_ERROR_BAD_STATE;
4530 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004531 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 if (data_length != 0) {
4533 hkdf->info = mbedtls_calloc(1, data_length);
4534 if (hkdf->info == NULL) {
4535 return PSA_ERROR_INSUFFICIENT_MEMORY;
4536 }
4537 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004538 }
4539 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004541 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004542 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004543 }
4544}
John Durkop07cc04a2020-11-16 22:08:34 -08004545#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004546
John Durkop07cc04a2020-11-16 22:08:34 -08004547#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4548 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4550 const uint8_t *data,
4551 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004552{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4554 return PSA_ERROR_BAD_STATE;
4555 }
Janos Follathf08e2652019-06-13 09:05:41 +01004556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557 if (data_length != 0) {
4558 prf->seed = mbedtls_calloc(1, data_length);
4559 if (prf->seed == NULL) {
4560 return PSA_ERROR_INSUFFICIENT_MEMORY;
4561 }
Janos Follathf08e2652019-06-13 09:05:41 +01004562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004564 prf->seed_length = data_length;
4565 }
Janos Follathf08e2652019-06-13 09:05:41 +01004566
Gilles Peskine43f958b2020-12-13 14:55:14 +01004567 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004569 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004570}
4571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4573 const uint8_t *data,
4574 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004575{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004576 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4577 return PSA_ERROR_BAD_STATE;
4578 }
Janos Follath81550542019-06-13 14:26:34 +01004579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 if (data_length != 0) {
4581 prf->secret = mbedtls_calloc(1, data_length);
4582 if (prf->secret == NULL) {
4583 return PSA_ERROR_INSUFFICIENT_MEMORY;
4584 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004586 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004587 prf->secret_length = data_length;
4588 }
Janos Follath81550542019-06-13 14:26:34 +01004589
Gilles Peskine43f958b2020-12-13 14:55:14 +01004590 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004592 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004593}
4594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4596 const uint8_t *data,
4597 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004598{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004599 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4600 return PSA_ERROR_BAD_STATE;
4601 }
Janos Follath63028dd2019-06-13 09:15:47 +01004602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603 if (data_length != 0) {
4604 prf->label = mbedtls_calloc(1, data_length);
4605 if (prf->label == NULL) {
4606 return PSA_ERROR_INSUFFICIENT_MEMORY;
4607 }
Janos Follath63028dd2019-06-13 09:15:47 +01004608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004610 prf->label_length = data_length;
4611 }
Janos Follath63028dd2019-06-13 09:15:47 +01004612
Gilles Peskine43f958b2020-12-13 14:55:14 +01004613 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004616}
4617
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004618static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4619 psa_key_derivation_step_t step,
4620 const uint8_t *data,
4621 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004622{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004624 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004626 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004628 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004629 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004630 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004632 }
4633}
John Durkop07cc04a2020-11-16 22:08:34 -08004634#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4635 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4636
4637#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4638static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4639 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004640 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004641 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004642{
4643 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004644 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004645 uint8_t *cur = pms;
4646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4648 return PSA_ERROR_INVALID_ARGUMENT;
4649 }
John Durkop07cc04a2020-11-16 22:08:34 -08004650
4651 /* Quoting RFC 4279, Section 2:
4652 *
4653 * The premaster secret is formed as follows: if the PSK is N octets
4654 * long, concatenate a uint16 with the value N, N zero octets, a second
4655 * uint16 with the value N, and the PSK itself.
4656 */
4657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658 *cur++ = MBEDTLS_BYTE_1(data_length);
4659 *cur++ = MBEDTLS_BYTE_0(data_length);
4660 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004661 cur += data_length;
4662 *cur++ = pms[0];
4663 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004664 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004665 cur += data_length;
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004669 mbedtls_platform_zeroize(pms, sizeof(pms));
4670 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004671}
Janos Follath6660f0e2019-06-17 08:44:03 +01004672
4673static psa_status_t psa_tls12_prf_psk_to_ms_input(
4674 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004675 psa_key_derivation_step_t step,
4676 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004678{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004679 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4680 return psa_tls12_prf_psk_to_ms_set_key(prf,
4681 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004682 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004685}
John Durkop07cc04a2020-11-16 22:08:34 -08004686#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004687
Gilles Peskineb8965192019-09-24 16:21:10 +02004688/** Check whether the given key type is acceptable for the given
4689 * input step of a key derivation.
4690 *
4691 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4692 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4693 * Both secret and non-secret inputs can alternatively have the type
4694 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4695 * that the input was passed as a buffer rather than via a key object.
4696 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004697static int psa_key_derivation_check_input_type(
4698 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004700{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004702 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 if (key_type == PSA_KEY_TYPE_DERIVE) {
4704 return PSA_SUCCESS;
4705 }
4706 if (key_type == PSA_KEY_TYPE_NONE) {
4707 return PSA_SUCCESS;
4708 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004709 break;
4710 case PSA_KEY_DERIVATION_INPUT_LABEL:
4711 case PSA_KEY_DERIVATION_INPUT_SALT:
4712 case PSA_KEY_DERIVATION_INPUT_INFO:
4713 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4715 return PSA_SUCCESS;
4716 }
4717 if (key_type == PSA_KEY_TYPE_NONE) {
4718 return PSA_SUCCESS;
4719 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004720 break;
4721 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004722 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004723}
4724
Janos Follathb80a94e2019-06-12 15:54:46 +01004725static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004727 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004728 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004729 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004731{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004732 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004733 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004735 status = psa_key_derivation_check_input_type(step, key_type);
4736 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004737 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004739
John Durkopd0321952020-10-29 21:37:36 -07004740#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4742 status = psa_hkdf_input(&operation->ctx.hkdf,
4743 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4744 step, data, data_length);
4745 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004746#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4747#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004748 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4749 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4750 step, data, data_length);
4751 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004752#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4753#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4755 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4756 step, data, data_length);
4757 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004758#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004759 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004761 (void) data;
4762 (void) data_length;
4763 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004765 }
4766
Gilles Peskine224b0d62019-09-23 18:13:17 +02004767exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004768 if (status != PSA_SUCCESS) {
4769 psa_key_derivation_abort(operation);
4770 }
4771 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004772}
4773
Janos Follath51f4a0f2019-06-14 11:35:55 +01004774psa_status_t psa_key_derivation_input_bytes(
4775 psa_key_derivation_operation_t *operation,
4776 psa_key_derivation_step_t step,
4777 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004779{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004780 return psa_key_derivation_input_internal(operation, step,
4781 PSA_KEY_TYPE_NONE,
4782 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004783}
4784
Janos Follath51f4a0f2019-06-14 11:35:55 +01004785psa_status_t psa_key_derivation_input_key(
4786 psa_key_derivation_operation_t *operation,
4787 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004789{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004790 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004791 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004793
Ronald Cron5c522922020-11-14 16:35:34 +01004794 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004795 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4796 if (status != PSA_SUCCESS) {
4797 psa_key_derivation_abort(operation);
4798 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004799 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004800
4801 /* Passing a key object as a SECRET input unlocks the permission
4802 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004804 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 status = psa_key_derivation_input_internal(operation,
4808 step, slot->attr.type,
4809 slot->key.data,
4810 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004812 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004815}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004816
4817
4818
4819/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004820/* Key agreement */
4821/****************************************************************/
4822
John Durkop6ba40d12020-11-10 08:50:04 -08004823#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4825 size_t peer_key_length,
4826 const mbedtls_ecp_keypair *our_key,
4827 uint8_t *shared_secret,
4828 size_t shared_secret_size,
4829 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004830{
Steven Cooremand4867872020-08-05 16:31:39 +02004831 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004832 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004833 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004834 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4836 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004837
Ronald Cron90857082020-11-25 14:58:33 +01004838 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004839 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4840 bits,
4841 peer_key,
4842 peer_key_length,
4843 &their_key);
4844 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004845 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004846 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004847
Jaeden Amero97271b32019-01-10 19:38:51 +00004848 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4850 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004851 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004852 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004853 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4855 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004856 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004858
Jaeden Amero97271b32019-01-10 19:38:51 +00004859 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 mbedtls_ecdh_calc_secret(&ecdh,
4861 shared_secret_length,
4862 shared_secret, shared_secret_size,
4863 mbedtls_psa_get_random,
4864 MBEDTLS_PSA_RANDOM_STATE));
4865 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004866 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 }
4868 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004869 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004871
4872exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 if (status != PSA_SUCCESS) {
4874 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4875 }
4876 mbedtls_ecdh_free(&ecdh);
4877 mbedtls_ecp_keypair_free(their_key);
4878 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004881}
John Durkop6ba40d12020-11-10 08:50:04 -08004882#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004883
Gilles Peskine01d718c2018-09-18 12:01:02 +02004884#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4887 psa_key_slot_t *private_key,
4888 const uint8_t *peer_key,
4889 size_t peer_key_length,
4890 uint8_t *shared_secret,
4891 size_t shared_secret_size,
4892 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004893{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004895#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004896 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4898 return PSA_ERROR_INVALID_ARGUMENT;
4899 }
Steven Cooremana2371e52020-07-28 14:30:39 +02004900 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004901 psa_status_t status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004902 private_key->attr.type,
4903 private_key->attr.bits,
4904 private_key->key.data,
4905 private_key->key.bytes,
4906 &ecp);
4907 if (status != PSA_SUCCESS) {
4908 return status;
4909 }
4910 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4911 ecp,
4912 shared_secret, shared_secret_size,
4913 shared_secret_length);
4914 mbedtls_ecp_keypair_free(ecp);
4915 mbedtls_free(ecp);
4916 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004917#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004918 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004919 (void) private_key;
4920 (void) peer_key;
4921 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004922 (void) shared_secret;
4923 (void) shared_secret_size;
4924 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004925 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004926 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004927}
4928
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004929/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004930 * to potentially free embedded data structures and wipe confidential data.
4931 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004932static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4933 psa_key_derivation_step_t step,
4934 psa_key_slot_t *private_key,
4935 const uint8_t *peer_key,
4936 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004937{
4938 psa_status_t status;
4939 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4940 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004941 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004942
4943 /* Step 1: run the secret agreement algorithm to generate the shared
4944 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004945 status = psa_key_agreement_raw_internal(ka_alg,
4946 private_key,
4947 peer_key, peer_key_length,
4948 shared_secret,
4949 sizeof(shared_secret),
4950 &shared_secret_length);
4951 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02004952 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004953 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02004954
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004955 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004956 * the shared secret. A shared secret is permitted wherever a key
4957 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004958 status = psa_key_derivation_input_internal(operation, step,
4959 PSA_KEY_TYPE_DERIVE,
4960 shared_secret,
4961 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02004962exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
4964 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004965}
4966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
4968 psa_key_derivation_step_t step,
4969 mbedtls_svc_key_id_t private_key,
4970 const uint8_t *peer_key,
4971 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02004972{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004974 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004975 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004976
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4978 return PSA_ERROR_INVALID_ARGUMENT;
4979 }
Ronald Cron5c522922020-11-14 16:35:34 +01004980 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4982 if (status != PSA_SUCCESS) {
4983 return status;
4984 }
4985 status = psa_key_agreement_internal(operation, step,
4986 slot,
4987 peer_key, peer_key_length);
4988 if (status != PSA_SUCCESS) {
4989 psa_key_derivation_abort(operation);
4990 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004991 /* If a private key has been added as SECRET, we allow the derived
4992 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004993 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004994 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004995 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004996 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004998 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005001}
5002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5004 mbedtls_svc_key_id_t private_key,
5005 const uint8_t *peer_key,
5006 size_t peer_key_length,
5007 uint8_t *output,
5008 size_t output_size,
5009 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005010{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005012 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005013 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005016 status = PSA_ERROR_INVALID_ARGUMENT;
5017 goto exit;
5018 }
Ronald Cron5c522922020-11-14 16:35:34 +01005019 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5021 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005022 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005024
Gilles Peskine42313fb2022-04-14 00:17:15 +02005025 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5026 * for the output size. The PSA specification only guarantees that this
5027 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5028 * but it might be nice to allow smaller buffers if the output fits.
5029 * At the time of writing this comment, with only ECDH implemented,
5030 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5031 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5032 * be exact for it as well. */
5033 size_t expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5035 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005036 status = PSA_ERROR_BUFFER_TOO_SMALL;
5037 goto exit;
5038 }
5039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040 status = psa_key_agreement_raw_internal(alg, slot,
5041 peer_key, peer_key_length,
5042 output, output_size,
5043 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005044
5045exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005046 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005047 /* If an error happens and is not handled properly, the output
5048 * may be used as a key to protect sensitive data. Arrange for such
5049 * a key to be random, which is likely to result in decryption or
5050 * verification errors. This is better than filling the buffer with
5051 * some constant data such as zeros, which would result in the data
5052 * being protected with a reproducible, easily knowable key.
5053 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005054 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005055 *output_length = output_size;
5056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005058 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005061}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005062
5063
Gilles Peskine30524eb2020-11-13 17:02:26 +01005064
Gilles Peskine86a440b2018-11-12 18:39:40 +01005065/****************************************************************/
5066/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005067/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005068
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005069#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5070#include "mbedtls/entropy_poll.h"
5071#endif
5072
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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005207psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5208 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005209{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210 if (global_data.initialized) {
5211 return PSA_ERROR_NOT_PERMITTED;
5212 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005214 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5215 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5216 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5217 return PSA_ERROR_INVALID_ARGUMENT;
5218 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005221}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005222#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005223
Ronald Cron3772afe2021-02-08 16:10:05 +01005224/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005225 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005226 * \param type The key type
5227 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005228 *
5229 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005230 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005231 * \retval #PSA_ERROR_INVALID_ARGUMENT
5232 * The size in bits of the key is not valid.
5233 * \retval #PSA_ERROR_NOT_SUPPORTED
5234 * The type and/or the size in bits of the key or the combination of
5235 * the two is not supported.
5236 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005237static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005238 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005239{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 if (key_type_is_raw_bytes(type)) {
5243 status = validate_unstructured_key_bit_size(type, bits);
5244 if (status != PSA_SUCCESS) {
5245 return status;
5246 }
5247 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005248#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005249 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5250 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5251 return PSA_ERROR_NOT_SUPPORTED;
5252 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005253
Ronald Cron01b2aba2020-10-05 09:42:02 +02005254 /* Accept only byte-aligned keys, for the same reasons as
5255 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 if (bits % 8 != 0) {
5257 return PSA_ERROR_NOT_SUPPORTED;
5258 }
5259 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005260#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005261
Ronald Cron5c4d3862020-12-07 11:07:24 +01005262#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005263 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005264 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265 return PSA_SUCCESS;
5266 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005267#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005268 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005269 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005270 }
5271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005273}
5274
Ronald Cron55ed0592020-10-05 10:30:40 +02005275psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005276 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005278{
5279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005280 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 if ((attributes->domain_parameters == NULL) &&
5283 (attributes->domain_parameters_size != 0)) {
5284 return PSA_ERROR_INVALID_ARGUMENT;
5285 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if (key_type_is_raw_bytes(type)) {
5288 status = psa_generate_random(key_buffer, key_buffer_size);
5289 if (status != PSA_SUCCESS) {
5290 return status;
5291 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005292
David Brown12ca5032021-02-11 11:02:00 -07005293#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005294 if (type == PSA_KEY_TYPE_DES) {
5295 psa_des_set_key_parity(key_buffer, key_buffer_size);
5296 }
David Brown8107e312021-02-12 08:08:46 -07005297#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005298 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005299
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005300#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5301 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5303 return mbedtls_psa_rsa_generate_key(attributes,
5304 key_buffer,
5305 key_buffer_size,
5306 key_buffer_length);
5307 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005308#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5309 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005310
John Durkop9814fa22020-11-04 12:28:15 -08005311#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5313 return mbedtls_psa_ecp_generate_key(attributes,
5314 key_buffer,
5315 key_buffer_size,
5316 key_buffer_length);
5317 } else
John Durkop9814fa22020-11-04 12:28:15 -08005318#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005319 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005320 (void) key_buffer_length;
5321 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005322 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005325}
Darryl Green0c6575a2018-11-07 16:05:30 +00005326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005327psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5328 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005329{
5330 psa_status_t status;
5331 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005332 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005333 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005334
Ronald Cron81709fc2020-11-14 12:10:32 +01005335 *key = MBEDTLS_SVC_KEY_ID_INIT;
5336
Gilles Peskine0f84d622019-09-12 19:03:13 +02005337 /* Reject any attempt to create a zero-length key so that we don't
5338 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005339 if (psa_get_key_bits(attributes) == 0) {
5340 return PSA_ERROR_INVALID_ARGUMENT;
5341 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005342
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005343 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5345 return PSA_ERROR_INVALID_ARGUMENT;
5346 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005348 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5349 &slot, &driver);
5350 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005351 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 }
Gilles Peskine11792082019-08-06 18:36:36 +02005353
Ronald Cron977c2472020-10-13 08:32:21 +02005354 /* In the case of a transparent key or an opaque key stored in local
5355 * storage (thus not in the case of generating a key in a secure element
5356 * or cryptoprocessor with storage), we have to allocate a buffer to
5357 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005358 if (slot->key.data == NULL) {
5359 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5360 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005361 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005362 attributes->core.type, attributes->core.bits);
5363 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005364 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005366
5367 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005368 attributes->core.type,
5369 attributes->core.bits);
5370 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005371 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005372 attributes, &key_buffer_size);
5373 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005374 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 }
Ronald Cron977c2472020-10-13 08:32:21 +02005376 }
Ronald Cron977c2472020-10-13 08:32:21 +02005377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5379 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005380 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 }
Ronald Cron977c2472020-10-13 08:32:21 +02005382 }
5383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005384 status = psa_driver_wrapper_generate_key(attributes,
5385 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005387 if (status != PSA_SUCCESS) {
5388 psa_remove_key_data_from_memory(slot);
5389 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005390
Gilles Peskine11792082019-08-06 18:36:36 +02005391exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005392 if (status == PSA_SUCCESS) {
5393 status = psa_finish_key_creation(slot, driver, key);
5394 }
5395 if (status != PSA_SUCCESS) {
5396 psa_fail_key_creation(slot, driver);
5397 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005400}
5401
Gilles Peskinee59236f2018-01-27 23:32:46 +01005402/****************************************************************/
5403/* Module setup */
5404/****************************************************************/
5405
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005406#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005407psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005408 void (* entropy_init)(mbedtls_entropy_context *ctx),
5409 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005410{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005411 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5412 return PSA_ERROR_BAD_STATE;
5413 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005414 global_data.rng.entropy_init = entropy_init;
5415 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005417}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005418#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005420void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005421{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005422 psa_wipe_all_key_slots();
5423 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5424 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005425 }
5426 /* Wipe all remaining data, including configuration.
5427 * In particular, this sets all state indicator to the value
5428 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005430
5431 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005432 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005433}
5434
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005435#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5436/** Recover a transaction that was interrupted by a power failure.
5437 *
5438 * This function is called during initialization, before psa_crypto_init()
5439 * returns. If this function returns a failure status, the initialization
5440 * fails.
5441 */
5442static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005443 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005444{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005446 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5447 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005448 /* TODO - fall through to the failure case until this
5449 * is implemented.
5450 * https://github.com/ARMmbed/mbed-crypto/issues/218
5451 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005452 default:
5453 /* We found an unsupported transaction in the storage.
5454 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005455 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005456 }
5457}
5458#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005460psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005461{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005462 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005463
Gilles Peskinec6b69072018-11-20 21:42:52 +01005464 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005465 if (global_data.initialized != 0) {
5466 return PSA_SUCCESS;
5467 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005468
Gilles Peskine30524eb2020-11-13 17:02:26 +01005469 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005471 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005472 status = mbedtls_psa_random_seed(&global_data.rng);
5473 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005474 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005475 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005476 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 status = psa_initialize_key_slots();
5479 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005480 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005481 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005482
Ronald Cron088d5d02021-04-10 16:57:30 +02005483 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005484 status = psa_driver_wrapper_init();
5485 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005486 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005487 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005488
Gilles Peskine4b734222019-07-24 15:56:31 +02005489#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490 status = psa_crypto_load_transaction();
5491 if (status == PSA_SUCCESS) {
5492 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5493 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005494 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 }
5496 status = psa_crypto_stop_transaction();
5497 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005498 /* There's no transaction to complete. It's all good. */
5499 status = PSA_SUCCESS;
5500 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005501#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005502
Gilles Peskinec6b69072018-11-20 21:42:52 +01005503 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005504 global_data.initialized = 1;
5505
5506exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 if (status != PSA_SUCCESS) {
5508 mbedtls_psa_crypto_free();
5509 }
5510 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511}
5512
5513#endif /* MBEDTLS_PSA_CRYPTO_C */