blob: d48ad91a3befd1f0d9784a2ceed1389a227bfade [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;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001475 psa_key_attributes_t attributes;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001476 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001477
Ronald Crone907e552021-01-18 13:22:38 +01001478 /* Reject a zero-length output buffer now, since this can never be a
1479 * valid key representation. This way we know that data must be a valid
1480 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 if (data_size == 0) {
1482 return PSA_ERROR_BUFFER_TOO_SMALL;
1483 }
Ronald Crone907e552021-01-18 13:22:38 +01001484
Darryl Greendd8fb772018-11-07 16:00:44 +00001485 /* Set the key to empty now, so that even when there are errors, we always
1486 * set data_length to a value between 0 and data_size. On error, setting
1487 * the key to empty is a good choice because an empty key representation is
1488 * unlikely to be accepted anywhere. */
1489 *data_length = 0;
1490
1491 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1493 if (status != PSA_SUCCESS) {
1494 return status;
1495 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1498 status = PSA_ERROR_INVALID_ARGUMENT;
1499 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001500 }
1501
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01001502 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001503 .core = slot->attr
1504 };
Ronald Cron67227982020-11-26 15:16:05 +01001505 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001506 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
1509exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001510 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001513}
1514
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001515MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1516 "One or more key attribute flag is listed as both external-only and dual-use")
1517MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1518 "One or more key attribute flag is listed as both internal-only and dual-use")
1519MBEDTLS_STATIC_ASSERT((PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1520 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001522/** Validate that a key policy is internally well-formed.
1523 *
1524 * This function only rejects invalid policies. It does not validate the
1525 * consistency of the policy with respect to other attributes of the key
1526 * such as the key type.
1527 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001529{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1531 PSA_KEY_USAGE_COPY |
1532 PSA_KEY_USAGE_ENCRYPT |
1533 PSA_KEY_USAGE_DECRYPT |
1534 PSA_KEY_USAGE_SIGN_MESSAGE |
1535 PSA_KEY_USAGE_VERIFY_MESSAGE |
1536 PSA_KEY_USAGE_SIGN_HASH |
1537 PSA_KEY_USAGE_VERIFY_HASH |
1538 PSA_KEY_USAGE_DERIVE)) != 0) {
1539 return PSA_ERROR_INVALID_ARGUMENT;
1540 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001543}
1544
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001545/** Validate the internal consistency of key attributes.
1546 *
1547 * This function only rejects invalid attribute values. If does not
1548 * validate the consistency of the attributes with any key data that may
1549 * be involved in the creation of the key.
1550 *
1551 * Call this function early in the key creation process.
1552 *
1553 * \param[in] attributes Key attributes for the new key.
1554 * \param[out] p_drv On any return, the driver for the key, if any.
1555 * NULL for a transparent key.
1556 *
1557 */
1558static psa_status_t psa_validate_key_attributes(
1559 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001561{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001562 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1564 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 status = psa_validate_key_location(lifetime, p_drv);
1567 if (status != PSA_SUCCESS) {
1568 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001569 }
1570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001571 status = psa_validate_key_persistence(lifetime);
1572 if (status != PSA_SUCCESS) {
1573 return status;
1574 }
1575
1576 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1577 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1578 return PSA_ERROR_INVALID_ARGUMENT;
1579 }
1580 } else {
1581 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1582 return PSA_ERROR_INVALID_ARGUMENT;
1583 }
1584 }
1585
1586 status = psa_validate_key_policy(&attributes->core.policy);
1587 if (status != PSA_SUCCESS) {
1588 return status;
1589 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
1591 /* Refuse to create overly large keys.
1592 * Note that this doesn't trigger on import if the attributes don't
1593 * explicitly specify a size (so psa_get_key_bits returns 0), so
1594 * psa_import_key() needs its own checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1596 return PSA_ERROR_NOT_SUPPORTED;
1597 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598
Gilles Peskine91e8c332019-08-02 19:19:39 +02001599 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001600 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1601 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1602 return PSA_ERROR_INVALID_ARGUMENT;
1603 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001606}
1607
Gilles Peskine4747d192019-04-17 15:05:45 +02001608/** Prepare a key slot to receive key material.
1609 *
1610 * This function allocates a key slot and sets its metadata.
1611 *
1612 * If this function fails, call psa_fail_key_creation().
1613 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001614 * This function is intended to be used as follows:
1615 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001616 * it with the specified attributes, and in case of a volatile key assign it
1617 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001618 * -# Populate the slot with the key material.
1619 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1620 * In case of failure at any step, stop the sequence and call
1621 * psa_fail_key_creation().
1622 *
Ronald Cron5c522922020-11-14 16:35:34 +01001623 * On success, the key slot is locked. It is the responsibility of the caller
1624 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001625 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001626 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001627 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 * \param[out] p_slot On success, a pointer to the prepared slot.
1629 * \param[out] p_drv On any return, the driver for the key, if any.
1630 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 *
1632 * \retval #PSA_SUCCESS
1633 * The key slot is ready to receive key material.
1634 * \return If this function fails, the key slot is an invalid state.
1635 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 */
1637static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001638 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 psa_key_slot_t **p_slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001642{
1643 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001644 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 psa_key_slot_t *slot;
1646
Gilles Peskinedf179142019-07-15 22:02:14 +02001647 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001648 *p_drv = NULL;
1649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001650 status = psa_validate_key_attributes(attributes, p_drv);
1651 if (status != PSA_SUCCESS) {
1652 return status;
1653 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1656 if (status != PSA_SUCCESS) {
1657 return status;
1658 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001659 slot = *p_slot;
1660
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001661 /* We're storing the declared bit-size of the key. It's up to each
1662 * creation mechanism to verify that this information is correct.
1663 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001664 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001665 * is optional (import, copy). In case of a volatile key, assign it the
1666 * volatile key identifier associated to the slot returned to contain its
1667 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668
1669 slot->attr = attributes->core;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001671#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1672 slot->attr.id = volatile_key_id;
1673#else
1674 slot->attr.id.key_id = volatile_key_id;
1675#endif
1676 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001677
Gilles Peskine91e8c332019-08-02 19:19:39 +02001678 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001679 * external-only flags, query `attributes`. Thanks to the check
1680 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001681 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001682 * may have set. */
1683 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001684
Gilles Peskinecbaff462019-07-12 23:46:04 +02001685#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001686 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001687 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001688 * create the key file in internal storage, create the
1689 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001690 * persistent data. This is done by starting a transaction that will
1691 * encompass these three actions.
1692 * For registering a volatile key, we just need to find an appropriate
1693 * slot number inside the SE. Since the key is designated volatile, creating
1694 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001695 /* The first thing to do is to find a slot number for the new key.
1696 * We save the slot number in persistent storage as part of the
1697 * transaction data. It will be needed to recover if the power
1698 * fails during the key creation process, to clean up on the secure
1699 * element side after restarting. Obtaining a slot number from the
1700 * secure element driver updates its persistent state, but we do not yet
1701 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001702 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001704 psa_key_slot_number_t slot_number;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001705 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1706 &slot_number);
1707 if (status != PSA_SUCCESS) {
1708 return status;
1709 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1712 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001713 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001714 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001715 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001716 status = psa_crypto_save_transaction();
1717 if (status != PSA_SUCCESS) {
1718 (void) psa_crypto_stop_transaction();
1719 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001721 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001722
1723 status = psa_copy_key_material_into_slot(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001725 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001728 /* Key registration only makes sense with a secure element. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001730 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001731#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1732
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001733 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001734}
Gilles Peskine4747d192019-04-17 15:05:45 +02001735
1736/** Finalize the creation of a key once its key material has been set.
1737 *
1738 * This entails writing the key to persistent storage.
1739 *
1740 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * See the documentation of psa_start_key_creation() for the intended use
1742 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 *
Ronald Cron5c522922020-11-14 16:35:34 +01001744 * If the finalization succeeds, the function unlocks the key slot (it was
1745 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1746 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001747 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 * \param[in,out] slot Pointer to the slot with key material.
1749 * \param[in] driver The secure element driver for the key,
1750 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001751 * \param[out] key On success, identifier of the key. Note that the
1752 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001753 *
1754 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001755 * The key was successfully created.
Gilles Peskineec1eff32023-02-14 19:21:09 +01001756 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1757 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1758 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1759 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1760 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1761 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001762 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001763 * \return If this function fails, the key slot is an invalid state.
1764 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001766static psa_status_t psa_finish_key_creation(
1767 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001768 psa_se_drv_table_entry_t *driver,
1769 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001770{
1771 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001772 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001774
1775#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001779 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001780 psa_key_slot_number_t slot_number =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001782
Tom Cosgrovebdd01a72023-03-08 14:19:51 +00001783 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1784 sizeof(data.slot_number),
1785 "Slot number size does not match psa_se_key_data_storage_t");
1786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1788 status = psa_save_persistent_key(&slot->attr,
1789 (uint8_t *) &data,
1790 sizeof(data));
1791 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001792#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1793 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001794 /* Key material is saved in export representation in the slot, so
1795 * just pass the slot buffer for storage. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001796 status = psa_save_persistent_key(&slot->attr,
1797 slot->key.data,
1798 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001799 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001801#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1802
Gilles Peskinecbaff462019-07-12 23:46:04 +02001803#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001804 /* Finish the transaction for a key creation. This does not
1805 * happen when registering an existing key. Detect this case
1806 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001807 * creation of a persistent key in a secure element requires a transaction,
1808 * but registration or volatile key creation doesn't use one). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 if (driver != NULL &&
1810 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1811 status = psa_save_se_persistent_data(driver);
1812 if (status != PSA_SUCCESS) {
1813 psa_destroy_persistent_key(slot->attr.id);
1814 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001817 }
1818#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001821 *key = slot->attr.id;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 status = psa_unlock_key_slot(slot);
1823 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001824 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001826 }
Ronald Cron50972942020-11-14 11:28:25 +01001827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001829}
1830
1831/** Abort the creation of a key.
1832 *
1833 * You may call this function after calling psa_start_key_creation(),
1834 * or after psa_finish_key_creation() fails. In other circumstances, this
1835 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001836 * See the documentation of psa_start_key_creation() for the intended use
1837 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001839 * \param[in,out] slot Pointer to the slot with key material.
1840 * \param[in] driver The secure element driver for the key,
1841 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001843static void psa_fail_key_creation(psa_key_slot_t *slot,
1844 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001845{
Gilles Peskine011e4282019-06-26 18:34:38 +02001846 (void) driver;
1847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001848 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001851
1852#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001853 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 * element, and the failure happened later (when saving metadata
1855 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001856 * element.
1857 * https://github.com/ARMmbed/mbed-crypto/issues/217
1858 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001859
Gilles Peskined7729582019-08-05 15:55:54 +02001860 /* Abort the ongoing transaction if any (there may not be one if
1861 * the creation process failed before starting one, or if the
1862 * key creation is a registration of a key in a secure element).
1863 * Earlier functions must already have done what it takes to undo any
1864 * partial creation. All that's left is to update the transaction data
1865 * itself. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001866 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001870}
1871
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001872/** Validate optional attributes during key creation.
1873 *
1874 * Some key attributes are optional during key creation. If they are
1875 * specified in the attributes structure, check that they are consistent
1876 * with the data in the slot.
1877 *
1878 * This function should be called near the end of key creation, after
1879 * the slot in memory is fully populated but before saving persistent data.
1880 */
1881static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001882 const psa_key_slot_t *slot,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001884{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001885 if (attributes->core.type != 0) {
1886 if (attributes->core.type != slot->attr.type) {
1887 return PSA_ERROR_INVALID_ARGUMENT;
1888 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001889 }
1890
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001891 if (attributes->domain_parameters_size != 0) {
John Durkop0e005192020-10-31 22:06:54 -07001892#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001893 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1894 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001895 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001896 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001898
Ronald Cron90857082020-11-25 14:58:33 +01001899 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001900 slot->attr.type,
1901 slot->key.data,
1902 slot->key.bytes,
1903 &rsa);
1904 if (status != PSA_SUCCESS) {
1905 return status;
1906 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 mbedtls_mpi_init(&actual);
1909 mbedtls_mpi_init(&required);
1910 ret = mbedtls_rsa_export(rsa,
1911 NULL, NULL, NULL, NULL, &actual);
1912 mbedtls_rsa_free(rsa);
1913 mbedtls_free(rsa);
1914 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 }
1917 ret = mbedtls_mpi_read_binary(&required,
1918 attributes->domain_parameters,
1919 attributes->domain_parameters_size);
1920 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 }
1923 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 }
1926rsa_exit:
1927 mbedtls_mpi_free(&actual);
1928 mbedtls_mpi_free(&required);
1929 if (ret != 0) {
1930 return mbedtls_to_psa_error(ret);
1931 }
1932 } else
John Durkop9814fa22020-11-04 12:28:15 -08001933#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1934 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001936 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001937 }
1938 }
1939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 if (attributes->core.bits != 0) {
1941 if (attributes->core.bits != slot->attr.bits) {
1942 return PSA_ERROR_INVALID_ARGUMENT;
1943 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 }
1945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947}
1948
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001949psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
1950 const uint8_t *data,
1951 size_t data_length,
1952 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001953{
1954 psa_status_t status;
1955 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001956 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001957 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958
Ronald Cron81709fc2020-11-14 12:10:32 +01001959 *key = MBEDTLS_SVC_KEY_ID_INIT;
1960
Gilles Peskine0f84d622019-09-12 19:03:13 +02001961 /* Reject zero-length symmetric keys (including raw data key objects).
1962 * This also rejects any key which might be encoded as an empty string,
1963 * which is never valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001964 if (data_length == 0) {
1965 return PSA_ERROR_INVALID_ARGUMENT;
1966 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02001967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001968 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
1969 &slot, &driver);
1970 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001974 /* In the case of a transparent key or an opaque key stored in local
1975 * storage (thus not in the case of generating a key in a secure element
1976 * or cryptoprocessor with storage), we have to allocate a buffer to
1977 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 if (slot->key.data == NULL) {
1979 status = psa_allocate_buffer_to_slot(slot, data_length);
1980 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001981 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982 }
Gilles Peskine5d309672019-07-12 23:47:28 +02001983 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001984
1985 bits = slot->attr.bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001986 status = psa_driver_wrapper_import_key(attributes,
1987 data, data_length,
1988 slot->key.data,
1989 slot->key.bytes,
1990 &slot->key.bytes, &bits);
1991 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001992 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001993 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001997 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001998 status = PSA_ERROR_INVALID_ARGUMENT;
1999 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002000 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 status = psa_validate_optional_attributes(slot, attributes);
2003 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002004 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002009 if (status != PSA_SUCCESS) {
2010 psa_fail_key_creation(slot, driver);
2011 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002013 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002014}
2015
Gilles Peskined7729582019-08-05 15:55:54 +02002016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2017psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002018 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002019{
2020 psa_status_t status;
2021 psa_key_slot_t *slot = NULL;
2022 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002024
2025 /* Leaving attributes unspecified is not currently supported.
2026 * It could make sense to query the key type and size from the
2027 * secure element, but not all secure elements support this
2028 * and the driver HAL doesn't currently support it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002029 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2030 return PSA_ERROR_NOT_SUPPORTED;
2031 }
2032 if (psa_get_key_bits(attributes) == 0) {
2033 return PSA_ERROR_NOT_SUPPORTED;
2034 }
Gilles Peskined7729582019-08-05 15:55:54 +02002035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002036 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2037 &slot, &driver);
2038 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002039 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 }
Gilles Peskined7729582019-08-05 15:55:54 +02002041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002042 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002043
2044exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 if (status != PSA_SUCCESS) {
2046 psa_fail_key_creation(slot, driver);
2047 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002048
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* Registration doesn't keep the key in RAM. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 psa_close_key(key);
2051 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002052}
2053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055static psa_status_t psa_copy_key_material(const psa_key_slot_t *source,
2056 psa_key_slot_t *target)
Gilles Peskinef603c712019-01-19 13:40:11 +01002057{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002058 psa_status_t status = psa_copy_key_material_into_slot(target,
2059 source->key.data,
2060 source->key.bytes);
2061 if (status != PSA_SUCCESS) {
2062 return status;
2063 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002064
Steven Cooreman398aee52020-10-13 14:35:45 +02002065 target->attr.type = source->attr.type;
2066 target->attr.bits = source->attr.bits;
2067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01002069}
2070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2072 const psa_key_attributes_t *specified_attributes,
2073 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002074{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002076 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002077 psa_key_slot_t *source_slot = NULL;
2078 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002080 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002081
Ronald Cron81709fc2020-11-14 12:10:32 +01002082 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2083
Ronald Cron5c522922020-11-14 16:35:34 +01002084 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2086 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002087 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002088 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002090 status = psa_validate_optional_attributes(source_slot,
2091 specified_attributes);
2092 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002093 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 status = psa_restrict_key_policy(source_slot->attr.type,
2097 &actual_attributes.core.policy,
2098 &source_slot->attr.policy);
2099 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002100 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2104 &target_slot, &driver);
2105 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002108
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002110 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002111 /* Copying to a secure element is not implemented yet. */
2112 status = PSA_ERROR_NOT_SUPPORTED;
2113 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002115#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002117 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
Ronald Cron6cc66312021-04-02 12:27:47 +02002118 /*
2119 * Copying through an opaque driver is not implemented yet, consider
2120 * a lifetime with an external location as an invalid parameter for
2121 * now.
2122 */
2123 status = PSA_ERROR_INVALID_ARGUMENT;
2124 goto exit;
2125 }
2126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 status = psa_copy_key_material(source_slot, target_slot);
2128 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002129 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002130 }
Gilles Peskinef603c712019-01-19 13:40:11 +01002131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002133exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 if (status != PSA_SUCCESS) {
2135 psa_fail_key_creation(target_slot, driver);
2136 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002138 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002141}
2142
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002143
2144
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002145/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002146/* Message digests */
2147/****************************************************************/
2148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002149psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002151 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (operation->id == 0) {
2153 return PSA_SUCCESS;
2154 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002157 operation->id = 0;
2158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002159 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002160}
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2163 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 if (operation->id != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002169 status = PSA_ERROR_BAD_STATE;
2170 goto exit;
2171 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002174 status = PSA_ERROR_INVALID_ARGUMENT;
2175 goto exit;
2176 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002177
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002178 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2179 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002183
2184exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 if (status != PSA_SUCCESS) {
2186 psa_hash_abort(operation);
2187 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002188
2189 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190}
2191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2193 const uint8_t *input,
2194 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002196 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198 if (operation->id == 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002199 status = PSA_ERROR_BAD_STATE;
2200 goto exit;
2201 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002202
Steven Cooremanc8288352021-03-04 14:02:19 +01002203 /* Don't require hash implementations to behave correctly on a
2204 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002205 if (input_length == 0) {
2206 return PSA_SUCCESS;
2207 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002210
2211exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002212 if (status != PSA_SUCCESS) {
2213 psa_hash_abort(operation);
2214 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217}
2218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2220 uint8_t *hash,
2221 size_t hash_size,
2222 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002224 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002225 if (operation->id == 0) {
2226 return PSA_ERROR_BAD_STATE;
2227 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228
Steven Cooreman1e582352021-02-18 17:24:37 +01002229 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 operation, hash, hash_size, hash_length);
2231 psa_hash_abort(operation);
2232 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233}
2234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2236 const uint8_t *hash,
2237 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002239 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002241 psa_status_t status = psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 operation,
2243 actual_hash, sizeof(actual_hash),
2244 &actual_hash_length);
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002247 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 if (actual_hash_length != hash_length) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002251 status = PSA_ERROR_INVALID_SIGNATURE;
2252 goto exit;
2253 }
2254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002256 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002258
2259exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2261 if (status != PSA_SUCCESS) {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002262 psa_hash_abort(operation);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002263 }
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266}
2267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002268psa_status_t psa_hash_compute(psa_algorithm_t alg,
2269 const uint8_t *input, size_t input_length,
2270 uint8_t *hash, size_t hash_size,
2271 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002272{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002273 *hash_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 if (!PSA_ALG_IS_HASH(alg)) {
2275 return PSA_ERROR_INVALID_ARGUMENT;
2276 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002278 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2279 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002280}
2281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002282psa_status_t psa_hash_compare(psa_algorithm_t alg,
2283 const uint8_t *input, size_t input_length,
2284 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002285{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002286 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002287 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 if (!PSA_ALG_IS_HASH(alg)) {
2290 return PSA_ERROR_INVALID_ARGUMENT;
2291 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002292
Steven Cooreman1e582352021-02-18 17:24:37 +01002293 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002294 alg, input, input_length,
2295 actual_hash, sizeof(actual_hash),
2296 &actual_hash_length);
2297 if (status != PSA_SUCCESS) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002298 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 }
2300 if (actual_hash_length != hash_length) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002301 status = PSA_ERROR_INVALID_SIGNATURE;
2302 goto exit;
2303 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002305 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 }
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002307
2308exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002309 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2310 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002311}
2312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002313psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2314 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002315{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 if (source_operation->id == 0 ||
2317 target_operation->id != 0) {
2318 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002319 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2322 target_operation);
2323 if (status != PSA_SUCCESS) {
2324 psa_hash_abort(target_operation);
2325 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002328}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329
2330
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002332/* MAC */
2333/****************************************************************/
2334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002335psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336{
Steven Cooreman07897832021-03-19 18:28:56 +01002337 /* Aborting a non-active operation is allowed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 if (operation->id == 0) {
2339 return PSA_SUCCESS;
2340 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002343 operation->mac_size = 0;
2344 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002345 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002347 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348}
2349
Ronald Cron1c650a12021-06-17 16:33:22 +02002350static psa_status_t psa_mac_finalize_alg_and_key_validation(
2351 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002352 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002354{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002356 psa_key_type_t key_type = psa_get_key_type(attributes);
2357 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 if (!PSA_ALG_IS_MAC(alg)) {
2360 return PSA_ERROR_INVALID_ARGUMENT;
2361 }
Steven Cooreman07897832021-03-19 18:28:56 +01002362
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002363 /* Validate the combination of key type and algorithm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002364 status = psa_mac_key_can_do(alg, key_type);
2365 if (status != PSA_SUCCESS) {
2366 return status;
2367 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002368
Steven Cooremana6474de2021-05-10 11:13:41 +02002369 /* Get the output length for the algorithm and key combination */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002370 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002372 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002373 /* A very short MAC is too short for security since it can be
2374 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2375 * so we make this our minimum, even though 32 bits is still
2376 * too small for security. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002378 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002380 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2381 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002382 /* It's impossible to "truncate" to a larger length than the full length
2383 * of the algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002384 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002385 }
2386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinebc795822022-03-16 13:54:49 +01002388 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2389 * that is disabled in the compile-time configuration. The result can
2390 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2391 * configuration into account. In this case, force a return of
2392 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2393 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2394 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2395 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2396 * systematically generated tests. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinebc795822022-03-16 13:54:49 +01002398 }
2399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 return PSA_SUCCESS;
Ronald Cron1c650a12021-06-17 16:33:22 +02002401}
2402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2404 mbedtls_svc_key_id_t key,
2405 psa_algorithm_t alg,
2406 int is_sign)
Ronald Cron1c650a12021-06-17 16:33:22 +02002407{
2408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2409 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002410 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002411 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002412
2413 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002415 status = PSA_ERROR_BAD_STATE;
2416 goto exit;
2417 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002418
2419 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002420 key,
2421 &slot,
2422 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2423 alg);
2424 if (status != PSA_SUCCESS) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002425 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002427
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002428 attributes = (psa_key_attributes_t) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002429 .core = slot->attr
2430 };
2431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2433 &operation->mac_size);
2434 if (status != PSA_SUCCESS) {
Ronald Cron1c650a12021-06-17 16:33:22 +02002435 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002436 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002437
2438 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002439 /* Dispatch the MAC setup call with validated input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 if (is_sign) {
2441 status = psa_driver_wrapper_mac_sign_setup(operation,
2442 &attributes,
2443 slot->key.data,
2444 slot->key.bytes,
2445 alg);
2446 } else {
2447 status = psa_driver_wrapper_mac_verify_setup(operation,
2448 &attributes,
2449 slot->key.data,
2450 slot->key.bytes,
2451 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002452 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453
Gilles Peskinefbfac682018-07-08 20:51:54 +02002454exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 if (status != PSA_SUCCESS) {
2456 psa_mac_abort(operation);
2457 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002461 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462}
2463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2465 mbedtls_svc_key_id_t key,
2466 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002467{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002468 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002469}
2470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2472 mbedtls_svc_key_id_t key,
2473 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002474{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002476}
2477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002478psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2479 const uint8_t *input,
2480 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 if (operation->id == 0) {
2483 return PSA_ERROR_BAD_STATE;
2484 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485
Steven Cooreman11743f92021-03-19 18:38:46 +01002486 /* Don't require hash implementations to behave correctly on a
2487 * zero-length input, which may have an invalid pointer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002488 if (input_length == 0) {
2489 return PSA_SUCCESS;
2490 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2493 input, input_length);
2494 if (status != PSA_SUCCESS) {
2495 psa_mac_abort(operation);
2496 }
Steven Cooreman11743f92021-03-19 18:38:46 +01002497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002498 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499}
2500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002501psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2502 uint8_t *mac,
2503 size_t mac_size,
2504 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002505{
Steven Cooreman87885df2021-03-19 19:04:39 +01002506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2507 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002509 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002510 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002511 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002512 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 if (!operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002515 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002516 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002517 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002518
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002519 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2520 * once all the error checks are done. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (operation->mac_size == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002522 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002523 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002524 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002526 if (mac_size < operation->mac_size) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002527 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002528 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002529 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 status = psa_driver_wrapper_mac_sign_finish(operation,
2532 mac, operation->mac_size,
2533 mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002534
Dave Rodgman478ab542021-06-25 09:09:02 +01002535exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002536 /* In case of success, set the potential excess room in the output buffer
2537 * to an invalid value, to avoid potentially leaking a longer MAC.
2538 * In case of error, set the output length and content to a safe default,
2539 * such that in case the caller misses an error check, the output would be
2540 * an unachievable MAC.
2541 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 if (status != PSA_SUCCESS) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002543 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002544 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002545 }
mohammad16036df908f2018-04-02 08:34:15 -07002546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 if (mac_size > operation->mac_size) {
2548 memset(&mac[operation->mac_size], '!',
2549 mac_size - operation->mac_size);
2550 }
Ronald Cron882eb782021-06-18 13:05:48 +02002551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002554 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002555}
2556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2558 const uint8_t *mac,
2559 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002560{
Steven Cooreman87885df2021-03-19 19:04:39 +01002561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2562 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002563
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002564 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002565 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002566 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002567 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 if (operation->is_sign) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002570 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002571 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002572 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 if (operation->mac_size != mac_length) {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002575 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002576 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002577 }
2578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002579 status = psa_driver_wrapper_mac_verify_finish(operation,
2580 mac, mac_length);
Steven Cooreman15f0d922021-05-07 14:14:37 +02002581
Dave Rodgman478ab542021-06-25 09:09:02 +01002582exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002585 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586}
2587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002588static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2589 psa_algorithm_t alg,
2590 const uint8_t *input,
2591 size_t input_length,
2592 uint8_t *mac,
2593 size_t mac_size,
2594 size_t *mac_length,
2595 int is_sign)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002596{
2597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002598 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002599 psa_key_attributes_t attributes;
Ronald Crondbb86462021-06-17 17:17:20 +02002600 psa_key_slot_t *slot;
2601 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002602
Ronald Crondbb86462021-06-17 17:17:20 +02002603 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002604 key,
2605 &slot,
2606 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2607 alg);
2608 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002609 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002610 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002611
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002612 attributes = (psa_key_attributes_t) {
Ronald Crondbb86462021-06-17 17:17:20 +02002613 .core = slot->attr
2614 };
2615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2617 &operation_mac_size);
2618 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002619 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002620 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 if (mac_size < operation_mac_size) {
Ronald Crondbb86462021-06-17 17:17:20 +02002623 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002624 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002625 }
2626
2627 status = psa_driver_wrapper_mac_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002628 &attributes,
2629 slot->key.data, slot->key.bytes,
2630 alg,
2631 input, input_length,
2632 mac, operation_mac_size, mac_length);
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002633
2634exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002635 /* In case of success, set the potential excess room in the output buffer
2636 * to an invalid value, to avoid potentially leaking a longer MAC.
2637 * In case of error, set the output length and content to a safe default,
2638 * such that in case the caller misses an error check, the output would be
2639 * an unachievable MAC.
2640 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 if (status != PSA_SUCCESS) {
Ronald Crondbb86462021-06-17 17:17:20 +02002642 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002643 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002644 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 if (mac_size > operation_mac_size) {
2646 memset(&mac[operation_mac_size], '!', mac_size - operation_mac_size);
2647 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 unlock_status = psa_unlock_key_slot(slot);
Ronald Crondbb86462021-06-17 17:17:20 +02002650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002652}
2653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002655 psa_algorithm_t alg,
2656 const uint8_t *input,
2657 size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002658 uint8_t *mac,
2659 size_t mac_size,
2660 size_t *mac_length)
2661{
2662 return psa_mac_compute_internal(key, alg,
2663 input, input_length,
2664 mac, mac_size, mac_length, 1);
2665}
2666
2667psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2668 psa_algorithm_t alg,
2669 const uint8_t *input,
2670 size_t input_length,
2671 const uint8_t *mac,
2672 size_t mac_length)
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002673{
2674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002675 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2676 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 status = psa_mac_compute_internal(key, alg,
2679 input, input_length,
2680 actual_mac, sizeof(actual_mac),
2681 &actual_mac_length, 0);
2682 if (status != PSA_SUCCESS) {
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002683 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002685
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 if (mac_length != actual_mac_length) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002687 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002688 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002689 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Cron4ed83682021-06-18 14:51:29 +02002691 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002692 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002693 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002694
2695exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 return status;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002699}
Gilles Peskine20035e32018-02-03 22:44:14 +01002700
Gilles Peskine20035e32018-02-03 22:44:14 +01002701/****************************************************************/
2702/* Asymmetric cryptography */
2703/****************************************************************/
2704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002705static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2706 psa_algorithm_t alg)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002707{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 if (input_is_message) {
2709 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2710 return PSA_ERROR_INVALID_ARGUMENT;
2711 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2714 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2715 return PSA_ERROR_INVALID_ARGUMENT;
2716 }
2717 }
2718 } else {
2719 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2720 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002721 }
2722 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 return PSA_SUCCESS;
gabor-mezei-arm56980482021-05-05 13:56:27 +02002725}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2728 int input_is_message,
2729 psa_algorithm_t alg,
2730 const uint8_t *input,
2731 size_t input_length,
2732 uint8_t *signature,
2733 size_t signature_size,
2734 size_t *signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002735{
2736 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2737 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002738 psa_key_attributes_t attributes;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002739 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002740
2741 *signature_length = 0;
2742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 status = psa_sign_verify_check_alg(input_is_message, alg);
2744 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002745 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002746 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002747
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002748 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002749 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002750 * buffer can in principle be empty since it doesn't actually have
2751 * to be a hash.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 if (signature_size == 0) {
2753 return PSA_ERROR_BUFFER_TOO_SMALL;
2754 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002755
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002756 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 key, &slot,
2758 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2759 PSA_KEY_USAGE_SIGN_HASH,
2760 alg);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002762 if (status != PSA_SUCCESS) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002763 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002767 status = PSA_ERROR_INVALID_ARGUMENT;
2768 goto exit;
2769 }
2770
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01002771 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002773 };
2774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002776 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002777 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002778 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 signature, signature_size, signature_length);
2780 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002781
2782 status = psa_driver_wrapper_sign_hash(
2783 &attributes, slot->key.data, slot->key.bytes,
2784 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 signature, signature_size, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002786 }
2787
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002788
2789exit:
2790 /* Fill the unused part of the output buffer (the whole buffer on error,
2791 * the trailing part on success) with something that isn't a valid signature
2792 * (barring an attack on the signature and deliberately-crafted input),
2793 * in case the caller doesn't check the return status properly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 if (status == PSA_SUCCESS) {
2795 memset(signature + *signature_length, '!',
2796 signature_size - *signature_length);
2797 } else {
2798 memset(signature, '!', signature_size);
2799 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002800 /* If signature_size is 0 then we have nothing to do. We must not call
2801 * memset because signature may be NULL in this case. */
2802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806}
2807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2809 int input_is_message,
2810 psa_algorithm_t alg,
2811 const uint8_t *input,
2812 size_t input_length,
2813 const uint8_t *signature,
2814 size_t signature_length)
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002815{
2816 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2817 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2818 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 status = psa_sign_verify_check_alg(input_is_message, alg);
2821 if (status != PSA_SUCCESS) {
gabor-mezei-arm56980482021-05-05 13:56:27 +02002822 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 }
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002824
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002825 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826 key, &slot,
2827 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2828 PSA_KEY_USAGE_VERIFY_HASH,
2829 alg);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002831 if (status != PSA_SUCCESS) {
2832 return status;
2833 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002834
2835 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 .core = slot->attr
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002837 };
2838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002839 if (input_is_message) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002840 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002841 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002842 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002843 signature, signature_length);
2844 } else {
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002845 status = psa_driver_wrapper_verify_hash(
2846 &attributes, slot->key.data, slot->key.bytes,
2847 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848 signature, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002849 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002854
2855}
2856
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002857psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002858 const psa_key_attributes_t *attributes,
2859 const uint8_t *key_buffer,
2860 size_t key_buffer_size,
2861 psa_algorithm_t alg,
2862 const uint8_t *input,
2863 size_t input_length,
2864 uint8_t *signature,
2865 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002867{
2868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002871 size_t hash_length;
2872 uint8_t hash[PSA_HASH_MAX_SIZE];
2873
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002874 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 PSA_ALG_SIGN_GET_HASH(alg),
2876 input, input_length,
2877 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002880 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002882
2883 return psa_driver_wrapper_sign_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 attributes, key_buffer, key_buffer_size,
2885 alg, hash, hash_length,
2886 signature, signature_size, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002887 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002890}
2891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002892psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2893 psa_algorithm_t alg,
2894 const uint8_t *input,
2895 size_t input_length,
2896 uint8_t *signature,
2897 size_t signature_size,
2898 size_t *signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002899{
2900 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002901 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902 signature, signature_size, signature_length);
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002903}
2904
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002905psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002906 const psa_key_attributes_t *attributes,
2907 const uint8_t *key_buffer,
2908 size_t key_buffer_size,
2909 psa_algorithm_t alg,
2910 const uint8_t *input,
2911 size_t input_length,
2912 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002914{
2915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002917 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002918 size_t hash_length;
2919 uint8_t hash[PSA_HASH_MAX_SIZE];
2920
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002921 status = psa_driver_wrapper_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 PSA_ALG_SIGN_GET_HASH(alg),
2923 input, input_length,
2924 hash, sizeof(hash), &hash_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 if (status != PSA_SUCCESS) {
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002927 return status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 }
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002929
2930 return psa_driver_wrapper_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002931 attributes, key_buffer, key_buffer_size,
2932 alg, hash, hash_length,
2933 signature, signature_length);
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002934 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002937}
2938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
2940 psa_algorithm_t alg,
2941 const uint8_t *input,
2942 size_t input_length,
2943 const uint8_t *signature,
2944 size_t signature_length)
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002945{
2946 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002947 key, 1, alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 signature, signature_length);
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002949}
2950
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002951psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002952 const psa_key_attributes_t *attributes,
2953 const uint8_t *key_buffer, size_t key_buffer_size,
2954 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskine20035e32018-02-03 22:44:14 +01002956{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
2958 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
2959 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002960#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002961 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2962 return mbedtls_psa_rsa_sign_hash(
2963 attributes,
2964 key_buffer, key_buffer_size,
2965 alg, hash, hash_length,
2966 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01002967#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2968 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 } else {
2970 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02002971 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
2973 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01002974#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2976 return mbedtls_psa_ecdsa_sign_hash(
2977 attributes,
2978 key_buffer, key_buffer_size,
2979 alg, hash, hash_length,
2980 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01002981#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2982 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 } else {
2984 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01002985 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002986 }
Ronald Cron4501c982021-03-19 20:09:32 +01002987
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002988 (void) key_buffer;
2989 (void) key_buffer_size;
2990 (void) hash;
2991 (void) hash_length;
2992 (void) signature;
2993 (void) signature_size;
2994 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01002995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01002997}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002999psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3000 psa_algorithm_t alg,
3001 const uint8_t *hash,
3002 size_t hash_length,
3003 uint8_t *signature,
3004 size_t signature_size,
3005 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003006{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003007 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003008 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003010}
3011
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003012psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003013 const psa_key_attributes_t *attributes,
3014 const uint8_t *key_buffer, size_t key_buffer_size,
3015 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003016 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003017{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3019 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3020 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003021#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003022 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3023 return mbedtls_psa_rsa_verify_hash(
3024 attributes,
3025 key_buffer, key_buffer_size,
3026 alg, hash, hash_length,
3027 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003028#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3029 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 } else {
3031 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003032 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003033 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3034 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003035#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003036 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3037 return mbedtls_psa_ecdsa_verify_hash(
3038 attributes,
3039 key_buffer, key_buffer_size,
3040 alg, hash, hash_length,
3041 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003042#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3043 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 } else {
3045 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003046 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003047 }
Ronald Cron4501c982021-03-19 20:09:32 +01003048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 (void) key_buffer;
3050 (void) key_buffer_size;
3051 (void) hash;
3052 (void) hash_length;
3053 (void) signature;
3054 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003056 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003057}
3058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3060 psa_algorithm_t alg,
3061 const uint8_t *hash,
3062 size_t hash_length,
3063 const uint8_t *signature,
3064 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003065{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003066 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003067 key, 0, alg, hash, hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003069}
3070
John Durkop0e005192020-10-31 22:06:54 -07003071#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003072static void psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
3073 mbedtls_rsa_context *rsa)
Gilles Peskine072ac562018-06-30 00:21:29 +02003074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
3076 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
3077 mbedtls_md_type_t md_alg = mbedtls_md_get_type(md_info);
3078 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Gilles Peskine072ac562018-06-30 00:21:29 +02003079}
John Durkop0e005192020-10-31 22:06:54 -07003080#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3083 psa_algorithm_t alg,
3084 const uint8_t *input,
3085 size_t input_length,
3086 const uint8_t *salt,
3087 size_t salt_length,
3088 uint8_t *output,
3089 size_t output_size,
3090 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003094 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003095
Darryl Green5cc689a2018-07-24 15:34:10 +01003096 (void) input;
3097 (void) input_length;
3098 (void) salt;
3099 (void) output;
3100 (void) output_size;
3101
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003102 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3105 return PSA_ERROR_INVALID_ARGUMENT;
3106 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003107
Ronald Cron5c522922020-11-14 16:35:34 +01003108 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3110 if (status != PSA_SUCCESS) {
3111 return status;
3112 }
3113 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3114 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003115 status = PSA_ERROR_INVALID_ARGUMENT;
3116 goto exit;
3117 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003120#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003121 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003122 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3124 slot->key.data,
3125 slot->key.bytes,
3126 &rsa);
3127 if (status != PSA_SUCCESS) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128 goto rsa_exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 }
Steven Cooremana2371e52020-07-28 14:30:39 +02003130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003131 if (output_size < mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003132 status = PSA_ERROR_BUFFER_TOO_SMALL;
3133 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003134 }
Ronald Cron7207d572021-09-08 14:28:35 +02003135#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3136 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003138#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003139 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 mbedtls_rsa_pkcs1_encrypt(rsa,
3141 mbedtls_psa_get_random,
3142 MBEDTLS_PSA_RANDOM_STATE,
3143 MBEDTLS_RSA_PUBLIC,
3144 input_length,
3145 input,
3146 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003147#else
3148 status = PSA_ERROR_NOT_SUPPORTED;
3149#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 } else
3151 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003152#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003153 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003154 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
3156 mbedtls_psa_get_random,
3157 MBEDTLS_PSA_RANDOM_STATE,
3158 MBEDTLS_RSA_PUBLIC,
3159 salt, salt_length,
3160 input_length,
3161 input,
3162 output));
Ronald Cron7207d572021-09-08 14:28:35 +02003163#else
3164 status = PSA_ERROR_NOT_SUPPORTED;
3165#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168 }
Ronald Cron7207d572021-09-08 14:28:35 +02003169#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003171rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 if (status == PSA_SUCCESS) {
3173 *output_length = mbedtls_rsa_get_len(rsa);
3174 }
Steven Cooremana01795d2020-07-24 22:48:15 +02003175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003176 mbedtls_rsa_free(rsa);
3177 mbedtls_free(rsa);
John Durkop9814fa22020-11-04 12:28:15 -08003178#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003179 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003180 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003181 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003183
3184exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003185 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003187 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003188}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3191 psa_algorithm_t alg,
3192 const uint8_t *input,
3193 size_t input_length,
3194 const uint8_t *salt,
3195 size_t salt_length,
3196 uint8_t *output,
3197 size_t output_size,
3198 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003201 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003202 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003203
Darryl Green5cc689a2018-07-24 15:34:10 +01003204 (void) input;
3205 (void) input_length;
3206 (void) salt;
3207 (void) output;
3208 (void) output_size;
3209
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003210 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3213 return PSA_ERROR_INVALID_ARGUMENT;
3214 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003215
Ronald Cron5c522922020-11-14 16:35:34 +01003216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003217 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3218 if (status != PSA_SUCCESS) {
3219 return status;
3220 }
3221 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003222 status = PSA_ERROR_INVALID_ARGUMENT;
3223 goto exit;
3224 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 if (slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Ronald Cron7207d572021-09-08 14:28:35 +02003227#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003229 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 status = mbedtls_psa_rsa_load_representation(slot->attr.type,
3231 slot->key.data,
3232 slot->key.bytes,
3233 &rsa);
3234 if (status != PSA_SUCCESS) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003236 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003238 if (input_length != mbedtls_rsa_get_len(rsa)) {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003239 status = PSA_ERROR_INVALID_ARGUMENT;
3240 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003241 }
Ronald Cron7207d572021-09-08 14:28:35 +02003242#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3243 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003245 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Ronald Cron7207d572021-09-08 14:28:35 +02003246#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003247 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248 mbedtls_rsa_pkcs1_decrypt(rsa,
3249 mbedtls_psa_get_random,
3250 MBEDTLS_PSA_RANDOM_STATE,
3251 MBEDTLS_RSA_PRIVATE,
3252 output_length,
3253 input,
3254 output,
3255 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003256#else
3257 status = PSA_ERROR_NOT_SUPPORTED;
3258#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 } else
3260 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Ronald Cron7207d572021-09-08 14:28:35 +02003261#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 psa_rsa_oaep_set_padding_mode(alg, rsa);
Steven Cooreman4fed4552020-08-03 14:46:03 +02003263 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
3265 mbedtls_psa_get_random,
3266 MBEDTLS_PSA_RANDOM_STATE,
3267 MBEDTLS_RSA_PRIVATE,
3268 salt, salt_length,
3269 output_length,
3270 input,
3271 output,
3272 output_size));
Ronald Cron7207d572021-09-08 14:28:35 +02003273#else
3274 status = PSA_ERROR_NOT_SUPPORTED;
3275#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 } else {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003277 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003279
Ronald Cron7207d572021-09-08 14:28:35 +02003280#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003281 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003282rsa_exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003283 mbedtls_rsa_free(rsa);
3284 mbedtls_free(rsa);
John Durkop6ba40d12020-11-10 08:50:04 -08003285#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3286 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 } else {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003288 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003290
3291exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003294 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003295}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003296
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003297
3298
mohammad1603503973b2018-03-12 15:59:30 +02003299/****************************************************************/
3300/* Symmetric cryptography */
3301/****************************************************************/
3302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
3304 mbedtls_svc_key_id_t key,
3305 psa_algorithm_t alg,
3306 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02003307{
3308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3309 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003310 psa_key_attributes_t attributes;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003311 psa_key_slot_t *slot = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
3313 PSA_KEY_USAGE_ENCRYPT :
3314 PSA_KEY_USAGE_DECRYPT);
Ronald Cronab99ac22020-10-01 16:38:28 +02003315
3316 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 if (operation->id != 0) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003318 status = PSA_ERROR_BAD_STATE;
3319 goto exit;
3320 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003323 status = PSA_ERROR_INVALID_ARGUMENT;
3324 goto exit;
3325 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
3328 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003329 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003331
Ronald Cron49fafa92021-03-10 08:34:23 +01003332 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003333 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003334 * so we only set it (in the driver wrapper) after resources have been
3335 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003336 operation->iv_set = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02003338 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 } else if (slot->attr.type == PSA_KEY_TYPE_ARC4) {
Gilles Peskine4a83c102022-04-20 17:00:22 +02003340 operation->iv_required = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003341 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02003342 operation->iv_required = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003343 }
3344 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02003345
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003346 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01003348 };
3349
Ronald Cronab99ac22020-10-01 16:38:28 +02003350 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 if (cipher_operation == MBEDTLS_ENCRYPT) {
3352 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
3353 &attributes,
3354 slot->key.data,
3355 slot->key.bytes,
3356 alg);
3357 } else {
3358 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
3359 &attributes,
3360 slot->key.data,
3361 slot->key.bytes,
3362 alg);
3363 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003364
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003365exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 if (status != PSA_SUCCESS) {
3367 psa_cipher_abort(operation);
3368 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003370 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003372 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02003373}
3374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
3376 mbedtls_svc_key_id_t key,
3377 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003378{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003380}
3381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003382psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
3383 mbedtls_svc_key_id_t key,
3384 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02003385{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003386 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02003387}
3388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
3390 uint8_t *iv,
3391 size_t iv_size,
3392 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003393{
Ronald Cron6d051732020-10-01 14:10:20 +02003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003395 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergey04eb7c02023-03-06 15:37:23 -07003396 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01003397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003399 status = PSA_ERROR_BAD_STATE;
3400 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003401 }
3402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003404 status = PSA_ERROR_BAD_STATE;
3405 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003406 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003407
Ronald Cronc423acb2021-07-05 12:31:44 +02003408 default_iv_length = operation->default_iv_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01003410 status = PSA_ERROR_BUFFER_TOO_SMALL;
3411 goto exit;
3412 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003413
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003414 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc423acb2021-07-05 12:31:44 +02003415 status = PSA_ERROR_GENERIC_ERROR;
3416 goto exit;
3417 }
3418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 status = psa_generate_random(local_iv, default_iv_length);
3420 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01003421 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 }
Ronald Cron5618a392021-03-26 09:52:26 +01003423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 status = psa_driver_wrapper_cipher_set_iv(operation,
3425 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01003426
3427exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 if (status == PSA_SUCCESS) {
3429 memcpy(iv, local_iv, default_iv_length);
Ronald Cronc423acb2021-07-05 12:31:44 +02003430 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003431 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003432 } else {
Ronald Cronc423acb2021-07-05 12:31:44 +02003433 *iv_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003434 psa_cipher_abort(operation);
Ronald Cronc423acb2021-07-05 12:31:44 +02003435 }
Ronald Cron6d051732020-10-01 14:10:20 +02003436
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003438}
3439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
3441 const uint8_t *iv,
3442 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02003443{
Ronald Cron6d051732020-10-01 14:10:20 +02003444 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003447 status = PSA_ERROR_BAD_STATE;
3448 goto exit;
3449 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003451 if (operation->iv_set || !operation->iv_required) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003452 status = PSA_ERROR_BAD_STATE;
3453 goto exit;
3454 }
Ronald Crona0d68172021-03-26 10:15:08 +01003455
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003456 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003457 status = PSA_ERROR_INVALID_ARGUMENT;
3458 goto exit;
3459 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003461 status = psa_driver_wrapper_cipher_set_iv(operation,
3462 iv,
3463 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02003464
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003465exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003466 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03003467 operation->iv_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 } else {
3469 psa_cipher_abort(operation);
3470 }
3471 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003472}
3473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003474psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
3475 const uint8_t *input,
3476 size_t input_length,
3477 uint8_t *output,
3478 size_t output_size,
3479 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003480{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003484 status = PSA_ERROR_BAD_STATE;
3485 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003486 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003488 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003489 status = PSA_ERROR_BAD_STATE;
3490 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003491 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 status = psa_driver_wrapper_cipher_update(operation,
3494 input,
3495 input_length,
3496 output,
3497 output_size,
3498 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003499
3500exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003501 if (status != PSA_SUCCESS) {
3502 psa_cipher_abort(operation);
3503 }
Ronald Cron6d051732020-10-01 14:10:20 +02003504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 return status;
mohammad1603503973b2018-03-12 15:59:30 +02003506}
3507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003508psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
3509 uint8_t *output,
3510 size_t output_size,
3511 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02003512{
David Saadab4ecc272019-02-14 13:48:10 +02003513 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 if (operation->id == 0) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003516 status = PSA_ERROR_BAD_STATE;
3517 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003518 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 if (operation->iv_required && !operation->iv_set) {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003521 status = PSA_ERROR_BAD_STATE;
3522 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003523 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003525 status = psa_driver_wrapper_cipher_finish(operation,
3526 output,
3527 output_size,
3528 output_length);
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003529
3530exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 if (status == PSA_SUCCESS) {
3532 return psa_cipher_abort(operation);
3533 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003534 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003535 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01003536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003538 }
mohammad1603503973b2018-03-12 15:59:30 +02003539}
3540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003541psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02003542{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02003544 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003545 * in use. It's ok to call abort on such an object, and there's
3546 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003547 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02003548 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02003551
Ronald Cron49fafa92021-03-10 08:34:23 +01003552 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003553 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003554 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003556 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02003557}
3558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003559psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
3560 psa_algorithm_t alg,
3561 const uint8_t *input,
3562 size_t input_length,
3563 uint8_t *output,
3564 size_t output_size,
3565 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003566{
3567 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003568 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003569 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003570 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003571 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3572 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003575 status = PSA_ERROR_INVALID_ARGUMENT;
3576 goto exit;
3577 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3580 PSA_KEY_USAGE_ENCRYPT,
3581 alg);
3582 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003583 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003584 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003585
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003586 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003588 };
3589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003590 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
3591 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003592 status = PSA_ERROR_GENERIC_ERROR;
3593 goto exit;
3594 }
3595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 if (default_iv_length > 0) {
3597 if (output_size < default_iv_length) {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003598 status = PSA_ERROR_BUFFER_TOO_SMALL;
3599 goto exit;
3600 }
3601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 status = psa_generate_random(local_iv, default_iv_length);
3603 if (status != PSA_SUCCESS) {
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003604 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003606 }
3607
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003608 status = psa_driver_wrapper_cipher_encrypt(
3609 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003610 alg, local_iv, default_iv_length, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 mbedtls_buffer_offset(output, default_iv_length),
3612 output_size - default_iv_length, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003613
3614exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003615 unlock_status = psa_unlock_key_slot(slot);
3616 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003617 status = unlock_status;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003618 }
Ronald Cron16377072021-07-08 19:00:07 +02003619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003620 if (status == PSA_SUCCESS) {
3621 if (default_iv_length > 0) {
3622 memcpy(output, local_iv, default_iv_length);
3623 }
3624 *output_length += default_iv_length;
3625 } else {
3626 *output_length = 0;
3627 }
3628
3629 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003630}
3631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
3633 psa_algorithm_t alg,
3634 const uint8_t *input,
3635 size_t input_length,
3636 uint8_t *output,
3637 size_t output_size,
3638 size_t *output_length)
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003639{
3640 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003641 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003642 psa_key_attributes_t attributes;
Ronald Cron16377072021-07-08 19:00:07 +02003643 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron16377072021-07-08 19:00:07 +02003646 status = PSA_ERROR_INVALID_ARGUMENT;
3647 goto exit;
3648 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3651 PSA_KEY_USAGE_DECRYPT,
3652 alg);
3653 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003654 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003656
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01003657 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 .core = slot->attr
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003659 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003662 status = PSA_ERROR_INVALID_ARGUMENT;
3663 goto exit;
3664 }
3665
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003666 status = psa_driver_wrapper_cipher_decrypt(
3667 &attributes, slot->key.data, slot->key.bytes,
3668 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003669 output, output_size, output_length);
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003670
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003671exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 unlock_status = psa_unlock_key_slot(slot);
3673 if (status == PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003674 status = unlock_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003675 }
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003677 if (status != PSA_SUCCESS) {
Ronald Cron16377072021-07-08 19:00:07 +02003678 *output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 }
Ronald Cron16377072021-07-08 19:00:07 +02003680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003681 return status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003682}
3683
3684
mohammad16035955c982018-04-26 00:53:03 +03003685/****************************************************************/
3686/* AEAD */
3687/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
3690 psa_algorithm_t alg,
3691 const uint8_t *nonce,
3692 size_t nonce_length,
3693 const uint8_t *additional_data,
3694 size_t additional_data_length,
3695 const uint8_t *plaintext,
3696 size_t plaintext_length,
3697 uint8_t *ciphertext,
3698 size_t ciphertext_size,
3699 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03003700{
Ronald Cron215633c2021-03-16 17:15:37 +01003701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3702 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003703
mohammad1603f08a5502018-06-03 15:05:47 +03003704 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3707 return PSA_ERROR_NOT_SUPPORTED;
3708 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003709
Ronald Cron9a986162021-03-26 12:40:07 +01003710 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003711 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3712 if (status != PSA_SUCCESS) {
3713 return status;
3714 }
mohammad16035955c982018-04-26 00:53:03 +03003715
Ronald Cron215633c2021-03-16 17:15:37 +01003716 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003717 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003718 };
mohammad16035955c982018-04-26 00:53:03 +03003719
Ronald Cronde822812021-03-17 16:08:20 +01003720 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003721 &attributes, slot->key.data, slot->key.bytes,
3722 alg,
3723 nonce, nonce_length,
3724 additional_data, additional_data_length,
3725 plaintext, plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02003727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728 if (status != PSA_SUCCESS && ciphertext_size != 0) {
3729 memset(ciphertext, 0, ciphertext_size);
3730 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03003733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02003735}
3736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
3738 psa_algorithm_t alg,
3739 const uint8_t *nonce,
3740 size_t nonce_length,
3741 const uint8_t *additional_data,
3742 size_t additional_data_length,
3743 const uint8_t *ciphertext,
3744 size_t ciphertext_length,
3745 uint8_t *plaintext,
3746 size_t plaintext_size,
3747 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03003748{
Ronald Cron215633c2021-03-16 17:15:37 +01003749 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3750 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003751
Gilles Peskineee652a32018-06-01 19:23:52 +02003752 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
3755 return PSA_ERROR_NOT_SUPPORTED;
3756 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01003757
Ronald Cron9a986162021-03-26 12:40:07 +01003758 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003759 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3760 if (status != PSA_SUCCESS) {
3761 return status;
3762 }
mohammad16035955c982018-04-26 00:53:03 +03003763
Ronald Cron215633c2021-03-16 17:15:37 +01003764 psa_key_attributes_t attributes = {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01003766 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003767
Ronald Cronde822812021-03-17 16:08:20 +01003768 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003769 &attributes, slot->key.data, slot->key.bytes,
3770 alg,
3771 nonce, nonce_length,
3772 additional_data, additional_data_length,
3773 ciphertext, ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02003775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003776 if (status != PSA_SUCCESS && plaintext_size != 0) {
3777 memset(plaintext, 0, plaintext_size);
3778 }
mohammad160360a64d02018-06-03 17:20:42 +03003779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01003781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 return status;
mohammad16035955c982018-04-26 00:53:03 +03003783}
3784
Gilles Peskinee59236f2018-01-27 23:32:46 +01003785/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003786/* Generators */
3787/****************************************************************/
3788
John Durkop07cc04a2020-11-16 22:08:34 -08003789#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3790 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3791 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3792#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003793#endif /* At least one builtin KDF */
3794
3795#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3796 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3797 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3798static psa_status_t psa_key_derivation_start_hmac(
3799 psa_mac_operation_t *operation,
3800 psa_algorithm_t hash_alg,
3801 const uint8_t *hmac_key,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 size_t hmac_key_length)
Steven Cooreman9878a162021-05-06 17:58:36 +02003803{
3804 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003806 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
3807 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
3808 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman9878a162021-05-06 17:58:36 +02003809
Steven Cooreman15f0d922021-05-07 14:14:37 +02003810 operation->is_sign = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003811 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman15f0d922021-05-07 14:14:37 +02003812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 status = psa_driver_wrapper_mac_sign_setup(operation,
3814 &attributes,
3815 hmac_key, hmac_key_length,
3816 PSA_ALG_HMAC(hash_alg));
Steven Cooreman9878a162021-05-06 17:58:36 +02003817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003818 psa_reset_key_attributes(&attributes);
3819 return status;
Steven Cooreman9878a162021-05-06 17:58:36 +02003820}
3821#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003822
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003823#define HKDF_STATE_INIT 0 /* no input yet */
3824#define HKDF_STATE_STARTED 1 /* got salt */
3825#define HKDF_STATE_KEYED 2 /* got key */
3826#define HKDF_STATE_OUTPUT 3 /* output started */
3827
Gilles Peskinecbe66502019-05-16 16:59:18 +02003828static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003830{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
3832 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
3833 } else {
3834 return operation->alg;
3835 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003836}
3837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003838psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003839{
3840 psa_status_t status = PSA_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003841 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
3842 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02003843 /* The object has (apparently) been initialized but it is not
3844 * in use. It's ok to call abort on such an object, and there's
3845 * nothing to do. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 } else
John Durkopd0321952020-10-29 21:37:36 -07003847#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003848 if (PSA_ALG_IS_HKDF(kdf_alg)) {
3849 mbedtls_free(operation->ctx.hkdf.info);
3850 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
3851 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003852#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3853#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3854 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
3856 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
3857 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
3858 if (operation->ctx.tls12_prf.secret != NULL) {
3859 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
3860 operation->ctx.tls12_prf.secret_length);
3861 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003862 }
3863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003864 if (operation->ctx.tls12_prf.seed != NULL) {
3865 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
3866 operation->ctx.tls12_prf.seed_length);
3867 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01003868 }
3869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003870 if (operation->ctx.tls12_prf.label != NULL) {
3871 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
3872 operation->ctx.tls12_prf.label_length);
3873 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01003874 }
3875
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003876 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003877
3878 /* We leave the fields Ai and output_block to be erased safely by the
3879 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 } else
John Durkop07cc04a2020-11-16 22:08:34 -08003881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3882 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003883 {
3884 status = PSA_ERROR_BAD_STATE;
3885 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 mbedtls_platform_zeroize(operation, sizeof(*operation));
3887 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003888}
3889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003890psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02003892{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003894 /* This is a blank key derivation operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003895 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003896 }
3897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003898 *capacity = operation->capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900}
3901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003902psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
3903 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003904{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003905 if (operation->alg == 0) {
3906 return PSA_ERROR_BAD_STATE;
3907 }
3908 if (capacity > operation->capacity) {
3909 return PSA_ERROR_INVALID_ARGUMENT;
3910 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003911 operation->capacity = capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003913}
3914
John Durkopd0321952020-10-29 21:37:36 -07003915#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003916/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917 * of the expand phase of the HKDF algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
3919 psa_algorithm_t hash_alg,
3920 uint8_t *output,
3921 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003922{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003923 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremanb27e3502021-04-29 19:11:25 +02003924 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003925 psa_status_t status;
3926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 if (hkdf->state < HKDF_STATE_KEYED || !hkdf->info_set) {
3928 return PSA_ERROR_BAD_STATE;
3929 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003930 hkdf->state = HKDF_STATE_OUTPUT;
3931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003933 /* Copy what remains of the current block */
3934 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003935 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003936 n = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 }
3938 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02003939 output += n;
3940 output_length -= n;
3941 hkdf->offset_in_block += n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02003943 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003944 }
Gilles Peskined54931c2018-07-17 21:06:59 +02003945 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003946 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003947 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003948 * object was corrupted or if this function is called directly
3949 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 if (hkdf->block_number == 0xff) {
3951 return PSA_ERROR_BAD_STATE;
3952 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003953
3954 /* We need a new block */
3955 ++hkdf->block_number;
3956 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003957
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003958 status = psa_key_derivation_start_hmac(&hkdf->hmac,
3959 hash_alg,
3960 hkdf->prk,
3961 hash_length);
3962 if (status != PSA_SUCCESS) {
3963 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003964 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965
3966 if (hkdf->block_number != 1) {
3967 status = psa_mac_update(&hkdf->hmac,
3968 hkdf->output_block,
3969 hash_length);
3970 if (status != PSA_SUCCESS) {
3971 return status;
3972 }
3973 }
3974 status = psa_mac_update(&hkdf->hmac,
3975 hkdf->info,
3976 hkdf->info_length);
3977 if (status != PSA_SUCCESS) {
3978 return status;
3979 }
3980 status = psa_mac_update(&hkdf->hmac,
3981 &hkdf->block_number, 1);
3982 if (status != PSA_SUCCESS) {
3983 return status;
3984 }
3985 status = psa_mac_sign_finish(&hkdf->hmac,
3986 hkdf->output_block,
3987 sizeof(hkdf->output_block),
3988 &hmac_output_length);
3989 if (status != PSA_SUCCESS) {
3990 return status;
3991 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003992 }
3993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003994 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003995}
John Durkop07cc04a2020-11-16 22:08:34 -08003996#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003997
John Durkop07cc04a2020-11-16 22:08:34 -08003998#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3999 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004000static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4001 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004002 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01004003{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
4005 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004006 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4007 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004008 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004009
Janos Follath7742fee2019-06-17 12:58:10 +01004010 /* We can't be wanting more output after block 0xff, otherwise
4011 * the capacity check in psa_key_derivation_output_bytes() would have
4012 * prevented this call. It could happen only if the operation
4013 * object was corrupted or if this function is called directly
4014 * inside the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004015 if (tls12_prf->block_number == 0xff) {
4016 return PSA_ERROR_CORRUPTION_DETECTED;
4017 }
Janos Follath7742fee2019-06-17 12:58:10 +01004018
4019 /* We need a new block */
4020 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004021 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004022
4023 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4024 *
4025 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4026 *
4027 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4028 * HMAC_hash(secret, A(2) + seed) +
4029 * HMAC_hash(secret, A(3) + seed) + ...
4030 *
4031 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004032 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004033 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004034 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004035 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004036 * is currently extracted as `output_block` and where i is
4037 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004038 */
4039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004040 status = psa_key_derivation_start_hmac(&hmac,
4041 hash_alg,
4042 tls12_prf->secret,
4043 tls12_prf->secret_length);
4044 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004045 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004046 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004047
4048 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004049 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004050 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4051 * the variable seed and in this instance means it in the context of the
4052 * P_hash function, where seed = label + seed.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004053 status = psa_mac_update(&hmac,
4054 tls12_prf->label,
4055 tls12_prf->label_length);
4056 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004057 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 }
4059 status = psa_mac_update(&hmac,
4060 tls12_prf->seed,
4061 tls12_prf->seed_length);
4062 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004063 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 }
4065 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01004066 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004067 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4068 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004069 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004071 }
4072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004073 status = psa_mac_sign_finish(&hmac,
4074 tls12_prf->Ai, hash_length,
4075 &hmac_output_length);
4076 if (hmac_output_length != hash_length) {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004077 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004078 }
4079 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004080 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004081 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004082
4083 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004084 status = psa_key_derivation_start_hmac(&hmac,
4085 hash_alg,
4086 tls12_prf->secret,
4087 tls12_prf->secret_length);
4088 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004089 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004090 }
4091 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
4092 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004093 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004094 }
4095 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
4096 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004097 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098 }
4099 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
4100 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004101 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 }
4103 status = psa_mac_sign_finish(&hmac,
4104 tls12_prf->output_block, hash_length,
4105 &hmac_output_length);
4106 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004107 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004108 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004109
Janos Follath7742fee2019-06-17 12:58:10 +01004110
4111cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004112 cleanup_status = psa_mac_abort(&hmac);
4113 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01004114 status = cleanup_status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004115 }
Janos Follathea29bfb2019-06-19 12:21:20 +01004116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01004118}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004119
Janos Follath844eb0e2019-06-19 12:10:49 +01004120static psa_status_t psa_key_derivation_tls12_prf_read(
4121 psa_tls12_prf_key_derivation_t *tls12_prf,
4122 psa_algorithm_t alg,
4123 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004124 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01004125{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004126 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
4127 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01004128 psa_status_t status;
4129 uint8_t offset, length;
4130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 switch (tls12_prf->state) {
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004132 case PSA_TLS12_PRF_STATE_LABEL_SET:
4133 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4134 break;
4135 case PSA_TLS12_PRF_STATE_OUTPUT:
4136 break;
4137 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 return PSA_ERROR_BAD_STATE;
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004139 }
4140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004141 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004142 /* Check if we have fully processed the current block. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004143 if (tls12_prf->left_in_block == 0) {
4144 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
4145 alg);
4146 if (status != PSA_SUCCESS) {
4147 return status;
4148 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004149
4150 continue;
4151 }
4152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004153 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01004154 length = (uint8_t) output_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01004156 length = tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004157 }
Janos Follath844eb0e2019-06-19 12:10:49 +01004158
4159 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004160 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01004161 output += length;
4162 output_length -= length;
4163 tls12_prf->left_in_block -= length;
4164 }
4165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004166 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01004167}
John Durkop07cc04a2020-11-16 22:08:34 -08004168#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4169 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004170
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004171psa_status_t psa_key_derivation_output_bytes(
4172 psa_key_derivation_operation_t *operation,
4173 uint8_t *output,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175{
4176 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004177 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004179 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004180 /* This is a blank operation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004182 }
4183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004188 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004189 goto exit;
4190 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004191 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004193 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4195 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004197 * output_length > 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004198 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004199 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004200 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004201
John Durkopd0321952020-10-29 21:37:36 -07004202#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4204 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4205 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, hash_alg,
4206 output, output_length);
4207 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004208#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4209#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4210 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004211 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4212 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4213 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
4214 kdf_alg, output,
4215 output_length);
4216 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004217#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4218 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004220 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004222 }
4223
4224exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004225 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004226 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004227 * This allows us to differentiate between exhausted operations and
4228 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4229 * operations. */
4230 psa_algorithm_t alg = operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004231 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004233 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004234 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004236}
4237
David Brown7807bf72021-02-09 16:28:23 -07004238#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02004240{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004241 if (data_size >= 8) {
4242 mbedtls_des_key_set_parity(data);
4243 }
4244 if (data_size >= 16) {
4245 mbedtls_des_key_set_parity(data + 8);
4246 }
4247 if (data_size >= 24) {
4248 mbedtls_des_key_set_parity(data + 16);
4249 }
Gilles Peskine08542d82018-07-19 17:05:42 +02004250}
David Brown7807bf72021-02-09 16:28:23 -07004251#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004252
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004253static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004254 psa_key_slot_t *slot,
4255 size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004256 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02004257{
4258 uint8_t *data = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004259 size_t bytes = PSA_BITS_TO_BYTES(bits);
Gilles Peskineeab56e42018-07-12 17:12:33 +02004260 psa_status_t status;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004261 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004263 if (!key_type_is_raw_bytes(slot->attr.type)) {
4264 return PSA_ERROR_INVALID_ARGUMENT;
4265 }
4266 if (bits % 8 != 0) {
4267 return PSA_ERROR_INVALID_ARGUMENT;
4268 }
4269 data = mbedtls_calloc(1, bytes);
4270 if (data == NULL) {
4271 return PSA_ERROR_INSUFFICIENT_MEMORY;
4272 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004274 status = psa_key_derivation_output_bytes(operation, data, bytes);
4275 if (status != PSA_SUCCESS) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02004276 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004277 }
David Brown12ca5032021-02-11 11:02:00 -07004278#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 if (slot->attr.type == PSA_KEY_TYPE_DES) {
4280 psa_des_set_key_parity(data, bytes);
4281 }
David Brown8107e312021-02-12 08:08:46 -07004282#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284 status = psa_allocate_buffer_to_slot(slot, bytes);
4285 if (status != PSA_SUCCESS) {
Paul Elliottda3e7db2021-02-09 18:58:20 +00004286 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 }
Ronald Crondd04d422020-11-28 15:14:42 +01004288
Ronald Cronbf33c932020-11-28 18:06:53 +01004289 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004290 attributes = (psa_key_attributes_t) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004292 };
4293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 status = psa_driver_wrapper_import_key(&attributes,
4295 data, bytes,
4296 slot->key.data,
4297 slot->key.bytes,
4298 &slot->key.bytes, &bits);
4299 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01004300 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02004302
4303exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 mbedtls_free(data);
4305 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004306}
4307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
4309 psa_key_derivation_operation_t *operation,
4310 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004311{
4312 psa_status_t status;
4313 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004314 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004315
Ronald Cron81709fc2020-11-14 12:10:32 +01004316 *key = MBEDTLS_SVC_KEY_ID_INIT;
4317
Gilles Peskine0f84d622019-09-12 19:03:13 +02004318 /* Reject any attempt to create a zero-length key so that we don't
4319 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 if (psa_get_key_bits(attributes) == 0) {
4321 return PSA_ERROR_INVALID_ARGUMENT;
4322 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02004323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 if (operation->alg == PSA_ALG_NONE) {
4325 return PSA_ERROR_BAD_STATE;
4326 }
Dave Rodgman021e7242021-11-16 10:32:48 +00004327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 if (!operation->can_output_key) {
4329 return PSA_ERROR_NOT_PERMITTED;
4330 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
4333 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004334#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004335 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004336 /* Deriving a key in a secure element is not implemented yet. */
4337 status = PSA_ERROR_NOT_SUPPORTED;
4338 }
4339#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004340 if (status == PSA_SUCCESS) {
4341 status = psa_generate_derived_key_internal(slot,
4342 attributes->core.bits,
4343 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004344 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 if (status == PSA_SUCCESS) {
4346 status = psa_finish_key_creation(slot, driver, key);
4347 }
4348 if (status != PSA_SUCCESS) {
4349 psa_fail_key_creation(slot, driver);
4350 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004353}
4354
Gilles Peskineeab56e42018-07-12 17:12:33 +02004355
4356
4357/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004358/* Key derivation */
4359/****************************************************************/
4360
Steven Cooreman932ffb72021-02-15 12:14:32 +01004361#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004363{
4364#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4366 return 1;
4367 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004368#endif
4369#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4371 return 1;
4372 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004373#endif
4374#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004375 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4376 return 1;
4377 }
Gilles Peskinecdacf042021-04-29 21:10:00 +02004378#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379 return 0;
Gilles Peskinecdacf042021-04-29 21:10:00 +02004380}
4381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004383{
4384 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 psa_status_t status = psa_hash_setup(&operation, alg);
4386 psa_hash_abort(&operation);
4387 return status;
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004388}
4389
Gilles Peskine969c5d62019-01-16 15:53:06 +01004390static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004391 psa_key_derivation_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004392 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004393{
Janos Follath5fe19732019-06-20 15:09:30 +01004394 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4395 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01004397
Gilles Peskine969c5d62019-01-16 15:53:06 +01004398 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004399 if (!is_kdf_alg_supported(kdf_alg)) {
4400 return PSA_ERROR_NOT_SUPPORTED;
4401 }
John Durkop07cc04a2020-11-16 22:08:34 -08004402
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004403 /* All currently supported key derivation algorithms are based on a
4404 * hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004405 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
4406 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
4407 if (hash_size == 0) {
4408 return PSA_ERROR_NOT_SUPPORTED;
4409 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004410
4411 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4412 * we might fail later, which is somewhat unfriendly and potentially
4413 * risk-prone. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004414 psa_status_t status = psa_hash_try_support(hash_alg);
4415 if (status != PSA_SUCCESS) {
4416 return status;
4417 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004419 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
4420 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
4421 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
4422 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004423 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004424
Gilles Peskinecdacf042021-04-29 21:10:00 +02004425 operation->capacity = 255 * hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427}
Gilles Peskine739c98c2021-04-29 21:34:33 +02004428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine739c98c2021-04-29 21:34:33 +02004430{
4431#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 if (alg == PSA_ALG_ECDH) {
4433 return PSA_SUCCESS;
4434 }
Gilles Peskine739c98c2021-04-29 21:34:33 +02004435#endif
4436 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine739c98c2021-04-29 21:34:33 +02004438}
John Durkop07cc04a2020-11-16 22:08:34 -08004439#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
4442 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004443{
4444 psa_status_t status;
4445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004446 if (operation->alg != 0) {
4447 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004450 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
4451 return PSA_ERROR_INVALID_ARGUMENT;
4452 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
4453#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4454 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
4455 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
4456 status = psa_key_agreement_try_support(ka_alg);
4457 if (status != PSA_SUCCESS) {
4458 return status;
4459 }
4460 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
4461#else
4462 return PSA_ERROR_NOT_SUPPORTED;
4463#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4464 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
4465#if defined(AT_LEAST_ONE_BUILTIN_KDF)
4466 status = psa_key_derivation_setup_kdf(operation, alg);
4467#else
4468 return PSA_ERROR_NOT_SUPPORTED;
4469#endif /* AT_LEAST_ONE_BUILTIN_KDF */
4470 } else {
4471 return PSA_ERROR_INVALID_ARGUMENT;
4472 }
4473
4474 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004476 }
4477 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478}
4479
John Durkopd0321952020-10-29 21:37:36 -07004480#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004481static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
4482 psa_algorithm_t hash_alg,
4483 psa_key_derivation_step_t step,
4484 const uint8_t *data,
4485 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004486{
4487 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004488 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02004489 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004490 if (hkdf->state != HKDF_STATE_INIT) {
4491 return PSA_ERROR_BAD_STATE;
4492 } else {
4493 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4494 hash_alg,
4495 data, data_length);
4496 if (status != PSA_SUCCESS) {
4497 return status;
4498 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02004499 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004500 return PSA_SUCCESS;
Steven Cooremanb27e3502021-04-29 19:11:25 +02004501 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004502 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004503 /* If no salt was provided, use an empty salt. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 if (hkdf->state == HKDF_STATE_INIT) {
4505 status = psa_key_derivation_start_hmac(&hkdf->hmac,
4506 hash_alg,
4507 NULL, 0);
4508 if (status != PSA_SUCCESS) {
4509 return status;
4510 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004511 hkdf->state = HKDF_STATE_STARTED;
4512 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004513 if (hkdf->state != HKDF_STATE_STARTED) {
4514 return PSA_ERROR_BAD_STATE;
4515 }
4516 status = psa_mac_update(&hkdf->hmac,
4517 data, data_length);
4518 if (status != PSA_SUCCESS) {
4519 return status;
4520 }
4521 status = psa_mac_sign_finish(&hkdf->hmac,
4522 hkdf->prk,
4523 sizeof(hkdf->prk),
4524 &data_length);
4525 if (status != PSA_SUCCESS) {
4526 return status;
4527 }
4528 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Gilles Peskine2b522db2019-04-12 15:11:49 +02004529 hkdf->block_number = 0;
4530 hkdf->state = HKDF_STATE_KEYED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02004532 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004533 if (hkdf->state == HKDF_STATE_OUTPUT) {
4534 return PSA_ERROR_BAD_STATE;
4535 }
4536 if (hkdf->info_set) {
4537 return PSA_ERROR_BAD_STATE;
4538 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004539 hkdf->info_length = data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 if (data_length != 0) {
4541 hkdf->info = mbedtls_calloc(1, data_length);
4542 if (hkdf->info == NULL) {
4543 return PSA_ERROR_INSUFFICIENT_MEMORY;
4544 }
4545 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004546 }
4547 hkdf->info_set = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004548 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004549 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004551 }
4552}
John Durkop07cc04a2020-11-16 22:08:34 -08004553#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004554
John Durkop07cc04a2020-11-16 22:08:34 -08004555#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4556 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004557static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
4558 const uint8_t *data,
4559 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01004560{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
4562 return PSA_ERROR_BAD_STATE;
4563 }
Janos Follathf08e2652019-06-13 09:05:41 +01004564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004565 if (data_length != 0) {
4566 prf->seed = mbedtls_calloc(1, data_length);
4567 if (prf->seed == NULL) {
4568 return PSA_ERROR_INSUFFICIENT_MEMORY;
4569 }
Janos Follathf08e2652019-06-13 09:05:41 +01004570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004572 prf->seed_length = data_length;
4573 }
Janos Follathf08e2652019-06-13 09:05:41 +01004574
Gilles Peskine43f958b2020-12-13 14:55:14 +01004575 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01004578}
4579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
4581 const uint8_t *data,
4582 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01004583{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
4585 return PSA_ERROR_BAD_STATE;
4586 }
Janos Follath81550542019-06-13 14:26:34 +01004587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588 if (data_length != 0) {
4589 prf->secret = mbedtls_calloc(1, data_length);
4590 if (prf->secret == NULL) {
4591 return PSA_ERROR_INSUFFICIENT_MEMORY;
4592 }
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 memcpy(prf->secret, data, data_length);
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004595 prf->secret_length = data_length;
4596 }
Janos Follath81550542019-06-13 14:26:34 +01004597
Gilles Peskine43f958b2020-12-13 14:55:14 +01004598 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01004601}
4602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004603static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
4604 const uint8_t *data,
4605 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01004606{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
4608 return PSA_ERROR_BAD_STATE;
4609 }
Janos Follath63028dd2019-06-13 09:15:47 +01004610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 if (data_length != 0) {
4612 prf->label = mbedtls_calloc(1, data_length);
4613 if (prf->label == NULL) {
4614 return PSA_ERROR_INSUFFICIENT_MEMORY;
4615 }
Janos Follath63028dd2019-06-13 09:15:47 +01004616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01004618 prf->label_length = data_length;
4619 }
Janos Follath63028dd2019-06-13 09:15:47 +01004620
Gilles Peskine43f958b2020-12-13 14:55:14 +01004621 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004623 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01004624}
4625
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004626static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
4627 psa_key_derivation_step_t step,
4628 const uint8_t *data,
4629 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01004630{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004631 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01004632 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004633 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01004634 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01004636 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01004638 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01004640 }
4641}
John Durkop07cc04a2020-11-16 22:08:34 -08004642#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4643 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4644
4645#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4646static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4647 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004648 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004649 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08004650{
4651 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004652 uint8_t pms[4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE];
John Durkop07cc04a2020-11-16 22:08:34 -08004653 uint8_t *cur = pms;
4654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004655 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
4656 return PSA_ERROR_INVALID_ARGUMENT;
4657 }
John Durkop07cc04a2020-11-16 22:08:34 -08004658
4659 /* Quoting RFC 4279, Section 2:
4660 *
4661 * The premaster secret is formed as follows: if the PSK is N octets
4662 * long, concatenate a uint16 with the value N, N zero octets, a second
4663 * uint16 with the value N, and the PSK itself.
4664 */
4665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 *cur++ = MBEDTLS_BYTE_1(data_length);
4667 *cur++ = MBEDTLS_BYTE_0(data_length);
4668 memset(cur, 0, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004669 cur += data_length;
4670 *cur++ = pms[0];
4671 *cur++ = pms[1];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004672 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08004673 cur += data_length;
4674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08004676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004677 mbedtls_platform_zeroize(pms, sizeof(pms));
4678 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08004679}
Janos Follath6660f0e2019-06-17 08:44:03 +01004680
4681static psa_status_t psa_tls12_prf_psk_to_ms_input(
4682 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004683 psa_key_derivation_step_t step,
4684 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004685 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01004686{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
4688 return psa_tls12_prf_psk_to_ms_set_key(prf,
4689 data, data_length);
Janos Follath0c1ed842019-06-28 13:35:36 +01004690 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 return psa_tls12_prf_input(prf, step, data, data_length);
Janos Follath6660f0e2019-06-17 08:44:03 +01004693}
John Durkop07cc04a2020-11-16 22:08:34 -08004694#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004695
Gilles Peskineb8965192019-09-24 16:21:10 +02004696/** Check whether the given key type is acceptable for the given
4697 * input step of a key derivation.
4698 *
4699 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4700 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4701 * Both secret and non-secret inputs can alternatively have the type
4702 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4703 * that the input was passed as a buffer rather than via a key object.
4704 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004705static int psa_key_derivation_check_input_type(
4706 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004707 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02004708{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004710 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 if (key_type == PSA_KEY_TYPE_DERIVE) {
4712 return PSA_SUCCESS;
4713 }
4714 if (key_type == PSA_KEY_TYPE_NONE) {
4715 return PSA_SUCCESS;
4716 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004717 break;
4718 case PSA_KEY_DERIVATION_INPUT_LABEL:
4719 case PSA_KEY_DERIVATION_INPUT_SALT:
4720 case PSA_KEY_DERIVATION_INPUT_INFO:
4721 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004722 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
4723 return PSA_SUCCESS;
4724 }
4725 if (key_type == PSA_KEY_TYPE_NONE) {
4726 return PSA_SUCCESS;
4727 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004728 break;
4729 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004730 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02004731}
4732
Janos Follathb80a94e2019-06-12 15:54:46 +01004733static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004735 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004736 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004737 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004738 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004739{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004740 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004742
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 status = psa_key_derivation_check_input_type(step, key_type);
4744 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02004745 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02004747
John Durkopd0321952020-10-29 21:37:36 -07004748#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 if (PSA_ALG_IS_HKDF(kdf_alg)) {
4750 status = psa_hkdf_input(&operation->ctx.hkdf,
4751 PSA_ALG_HKDF_GET_HASH(kdf_alg),
4752 step, data, data_length);
4753 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004754#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4755#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004756 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
4757 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
4758 step, data, data_length);
4759 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004760#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4761#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
4763 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
4764 step, data, data_length);
4765 } else
John Durkop07cc04a2020-11-16 22:08:34 -08004766#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004767 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004769 (void) data;
4770 (void) data_length;
4771 (void) kdf_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 }
4774
Gilles Peskine224b0d62019-09-23 18:13:17 +02004775exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004776 if (status != PSA_SUCCESS) {
4777 psa_key_derivation_abort(operation);
4778 }
4779 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004780}
4781
Janos Follath51f4a0f2019-06-14 11:35:55 +01004782psa_status_t psa_key_derivation_input_bytes(
4783 psa_key_derivation_operation_t *operation,
4784 psa_key_derivation_step_t step,
4785 const uint8_t *data,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004787{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 return psa_key_derivation_input_internal(operation, step,
4789 PSA_KEY_TYPE_NONE,
4790 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004791}
4792
Janos Follath51f4a0f2019-06-14 11:35:55 +01004793psa_status_t psa_key_derivation_input_key(
4794 psa_key_derivation_operation_t *operation,
4795 psa_key_derivation_step_t step,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004796 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004797{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004798 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004799 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004800 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004801
Ronald Cron5c522922020-11-14 16:35:34 +01004802 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4804 if (status != PSA_SUCCESS) {
4805 psa_key_derivation_abort(operation);
4806 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02004807 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004808
4809 /* Passing a key object as a SECRET input unlocks the permission
4810 * to output to a key object. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004812 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004813 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 status = psa_key_derivation_input_internal(operation,
4816 step, slot->attr.type,
4817 slot->key.data,
4818 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004823}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004824
4825
4826
4827/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004828/* Key agreement */
4829/****************************************************************/
4830
John Durkop6ba40d12020-11-10 08:50:04 -08004831#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004832static psa_status_t psa_key_agreement_ecdh(const uint8_t *peer_key,
4833 size_t peer_key_length,
4834 const mbedtls_ecp_keypair *our_key,
4835 uint8_t *shared_secret,
4836 size_t shared_secret_size,
4837 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004838{
Steven Cooremand4867872020-08-05 16:31:39 +02004839 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004840 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004841 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004842 size_t bits = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004843 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(our_key->grp.id, &bits);
4844 mbedtls_ecdh_init(&ecdh);
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004845
Ronald Cron90857082020-11-25 14:58:33 +01004846 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
4848 bits,
4849 peer_key,
4850 peer_key_length,
4851 &their_key);
4852 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004853 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004855
Jaeden Amero97271b32019-01-10 19:38:51 +00004856 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857 mbedtls_ecdh_get_params(&ecdh, their_key, MBEDTLS_ECDH_THEIRS));
4858 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004859 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 }
Jaeden Amero97271b32019-01-10 19:38:51 +00004861 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004862 mbedtls_ecdh_get_params(&ecdh, our_key, MBEDTLS_ECDH_OURS));
4863 if (status != PSA_SUCCESS) {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004864 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004866
Jaeden Amero97271b32019-01-10 19:38:51 +00004867 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868 mbedtls_ecdh_calc_secret(&ecdh,
4869 shared_secret_length,
4870 shared_secret, shared_secret_size,
4871 mbedtls_psa_get_random,
4872 MBEDTLS_PSA_RANDOM_STATE));
4873 if (status != PSA_SUCCESS) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004874 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004875 }
4876 if (PSA_BITS_TO_BYTES(bits) != *shared_secret_length) {
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004877 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 }
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004879
4880exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004881 if (status != PSA_SUCCESS) {
4882 mbedtls_platform_zeroize(shared_secret, shared_secret_size);
4883 }
4884 mbedtls_ecdh_free(&ecdh);
4885 mbedtls_ecp_keypair_free(their_key);
4886 mbedtls_free(their_key);
Steven Cooremana2371e52020-07-28 14:30:39 +02004887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004889}
John Durkop6ba40d12020-11-10 08:50:04 -08004890#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004891
Gilles Peskine01d718c2018-09-18 12:01:02 +02004892#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004894static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
4895 psa_key_slot_t *private_key,
4896 const uint8_t *peer_key,
4897 size_t peer_key_length,
4898 uint8_t *shared_secret,
4899 size_t shared_secret_size,
4900 size_t *shared_secret_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004901{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004902 mbedtls_ecp_keypair *ecp = NULL;
4903 psa_status_t status;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +01004904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08004906#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004907 case PSA_ALG_ECDH:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004908 if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key->attr.type)) {
4909 return PSA_ERROR_INVALID_ARGUMENT;
4910 }
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004911 status = mbedtls_psa_ecp_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004912 private_key->attr.type,
4913 private_key->attr.bits,
4914 private_key->key.data,
4915 private_key->key.bytes,
4916 &ecp);
4917 if (status != PSA_SUCCESS) {
4918 return status;
4919 }
4920 status = psa_key_agreement_ecdh(peer_key, peer_key_length,
4921 ecp,
4922 shared_secret, shared_secret_size,
4923 shared_secret_length);
4924 mbedtls_ecp_keypair_free(ecp);
4925 mbedtls_free(ecp);
4926 return status;
John Durkop6ba40d12020-11-10 08:50:04 -08004927#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004928 default:
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01004929 (void) ecp;
4930 (void) status;
Gilles Peskine93f85002018-11-16 16:43:31 +01004931 (void) private_key;
4932 (void) peer_key;
4933 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004934 (void) shared_secret;
4935 (void) shared_secret_size;
4936 (void) shared_secret_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004938 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004939}
4940
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004941/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004942 * to potentially free embedded data structures and wipe confidential data.
4943 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004944static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
4945 psa_key_derivation_step_t step,
4946 psa_key_slot_t *private_key,
4947 const uint8_t *peer_key,
4948 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02004949{
4950 psa_status_t status;
4951 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4952 size_t shared_secret_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004953 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02004954
4955 /* Step 1: run the secret agreement algorithm to generate the shared
4956 * secret. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004957 status = psa_key_agreement_raw_internal(ka_alg,
4958 private_key,
4959 peer_key, peer_key_length,
4960 shared_secret,
4961 sizeof(shared_secret),
4962 &shared_secret_length);
4963 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02004964 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004965 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02004966
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004967 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004968 * the shared secret. A shared secret is permitted wherever a key
4969 * of type DERIVE is permitted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004970 status = psa_key_derivation_input_internal(operation, step,
4971 PSA_KEY_TYPE_DERIVE,
4972 shared_secret,
4973 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02004974exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
4976 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004977}
4978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004979psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
4980 psa_key_derivation_step_t step,
4981 mbedtls_svc_key_id_t private_key,
4982 const uint8_t *peer_key,
4983 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02004984{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004986 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004987 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004989 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
4990 return PSA_ERROR_INVALID_ARGUMENT;
4991 }
Ronald Cron5c522922020-11-14 16:35:34 +01004992 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004993 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
4994 if (status != PSA_SUCCESS) {
4995 return status;
4996 }
4997 status = psa_key_agreement_internal(operation, step,
4998 slot,
4999 peer_key, peer_key_length);
5000 if (status != PSA_SUCCESS) {
5001 psa_key_derivation_abort(operation);
5002 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005003 /* If a private key has been added as SECRET, we allow the derived
5004 * key material to be used as a key in PSA Crypto. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005005 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005006 operation->can_output_key = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005008 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005012 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005013}
5014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005015psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
5016 mbedtls_svc_key_id_t private_key,
5017 const uint8_t *peer_key,
5018 size_t peer_key_length,
5019 uint8_t *output,
5020 size_t output_size,
5021 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005022{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005024 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005025 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005026 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005027
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005028 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005029 status = PSA_ERROR_INVALID_ARGUMENT;
5030 goto exit;
5031 }
Ronald Cron5c522922020-11-14 16:35:34 +01005032 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005033 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
5034 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005035 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005037
Gilles Peskine42313fb2022-04-14 00:17:15 +02005038 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
5039 * for the output size. The PSA specification only guarantees that this
5040 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
5041 * but it might be nice to allow smaller buffers if the output fits.
5042 * At the time of writing this comment, with only ECDH implemented,
5043 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
5044 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
5045 * be exact for it as well. */
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +01005046 expected_length =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005047 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
5048 if (output_size < expected_length) {
Gilles Peskine42313fb2022-04-14 00:17:15 +02005049 status = PSA_ERROR_BUFFER_TOO_SMALL;
5050 goto exit;
5051 }
5052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 status = psa_key_agreement_raw_internal(alg, slot,
5054 peer_key, peer_key_length,
5055 output, output_size,
5056 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005057
5058exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02005060 /* If an error happens and is not handled properly, the output
5061 * may be used as a key to protect sensitive data. Arrange for such
5062 * a key to be random, which is likely to result in decryption or
5063 * verification errors. This is better than filling the buffer with
5064 * some constant data such as zeros, which would result in the data
5065 * being protected with a reproducible, easily knowable key.
5066 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005067 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02005068 *output_length = output_size;
5069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02005072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005074}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005075
5076
Gilles Peskine30524eb2020-11-13 17:02:26 +01005077
Gilles Peskine86a440b2018-11-12 18:39:40 +01005078/****************************************************************/
5079/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005080/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005081
Gilles Peskine9a9d5ee2023-04-28 21:00:28 +02005082#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5083#include "mbedtls/entropy_poll.h"
5084#endif
5085
Gilles Peskine30524eb2020-11-13 17:02:26 +01005086/** Initialize the PSA random generator.
5087 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005089{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005090#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005092#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5093
Gilles Peskine30524eb2020-11-13 17:02:26 +01005094 /* Set default configuration if
5095 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005096 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005097 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005098 }
5099 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005100 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005103 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005104#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5105 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5106 /* The PSA entropy injection feature depends on using NV seed as an entropy
5107 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005108 mbedtls_entropy_add_source(&rng->entropy,
5109 mbedtls_nv_seed_poll, NULL,
5110 MBEDTLS_ENTROPY_BLOCK_SIZE,
5111 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01005112#endif
5113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005114 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005115#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005116}
5117
5118/** Deinitialize the PSA random generator.
5119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005121{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005122#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005123 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005124#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
5126 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005127#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005128}
5129
5130/** Seed the PSA random generator.
5131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01005133{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005134#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5135 /* Do nothing: the external RNG seeds itself. */
5136 (void) rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005138#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005139 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005140 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
5141 drbg_seed, sizeof(drbg_seed) - 1);
5142 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005143#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005144}
5145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146psa_status_t psa_generate_random(uint8_t *output,
5147 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005148{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005149 GUARD_MODULE_INITIALIZED;
5150
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005151#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5152
5153 size_t output_length = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005154 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
5155 output, output_size,
5156 &output_length);
5157 if (status != PSA_SUCCESS) {
5158 return status;
5159 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005160 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00005161 * for the external RNG interface. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005162 if (output_length != output_size) {
5163 return PSA_ERROR_INSUFFICIENT_ENTROPY;
5164 }
5165 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005166
5167#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005170 size_t request_size =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005171 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5172 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5173 output_size);
5174 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
5175 output, request_size);
5176 if (ret != 0) {
5177 return mbedtls_to_psa_error(ret);
5178 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01005179 output_size -= request_size;
5180 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005181 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005183#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005184}
5185
Gilles Peskine8814fc42020-12-14 15:33:44 +01005186/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005187 *
5188 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5189 * `psa_generate_random(...)`. The state parameter is ignored since the
5190 * PSA API doesn't support passing an explicit state.
5191 *
5192 * In the non-external case, psa_generate_random() calls an
5193 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5194 * and semantics as mbedtls_psa_get_random(). As an optimization,
5195 * instead of doing this back-and-forth between the PSA API and the
5196 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5197 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005198 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005199#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5200int mbedtls_psa_get_random(void *p_rng,
5201 unsigned char *output,
5202 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01005203{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005204 /* This function takes a pointer to the RNG state because that's what
5205 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5206 * its own state internally and doesn't let the caller access that state.
5207 * So we just ignore the state parameter, and in practice we'll pass
5208 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005209 (void) p_rng;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005210 psa_status_t status = psa_generate_random(output, output_size);
5211 if (status == PSA_SUCCESS) {
5212 return 0;
5213 } else {
5214 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
5215 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01005216}
5217#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5218
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005219#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
5221 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005222{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005223 if (global_data.initialized) {
5224 return PSA_ERROR_NOT_PERMITTED;
5225 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005227 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
5228 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
5229 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
5230 return PSA_ERROR_INVALID_ARGUMENT;
5231 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005233 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005234}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005235#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005236
Ronald Cron3772afe2021-02-08 16:10:05 +01005237/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005238 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005239 * \param type The key type
5240 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005241 *
5242 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005243 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005244 * \retval #PSA_ERROR_INVALID_ARGUMENT
5245 * The size in bits of the key is not valid.
5246 * \retval #PSA_ERROR_NOT_SUPPORTED
5247 * The type and/or the size in bits of the key or the combination of
5248 * the two is not supported.
5249 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005250static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005252{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005253 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 if (key_type_is_raw_bytes(type)) {
5256 status = validate_unstructured_key_bit_size(type, bits);
5257 if (status != PSA_SUCCESS) {
5258 return status;
5259 }
5260 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005261#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005262 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5263 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
5264 return PSA_ERROR_NOT_SUPPORTED;
5265 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005266
Ronald Cron01b2aba2020-10-05 09:42:02 +02005267 /* Accept only byte-aligned keys, for the same reasons as
5268 * in psa_import_rsa_key(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005269 if (bits % 8 != 0) {
5270 return PSA_ERROR_NOT_SUPPORTED;
5271 }
5272 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005273#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005274
Ronald Cron5c4d3862020-12-07 11:07:24 +01005275#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005276 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01005277 /* To avoid empty block, return successfully here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005278 return PSA_SUCCESS;
5279 } else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005280#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005281 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005283 }
5284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005285 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005286}
5287
Ronald Cron55ed0592020-10-05 10:30:40 +02005288psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005289 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005290 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005291{
5292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005293 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005295 if ((attributes->domain_parameters == NULL) &&
5296 (attributes->domain_parameters_size != 0)) {
5297 return PSA_ERROR_INVALID_ARGUMENT;
5298 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02005299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005300 if (key_type_is_raw_bytes(type)) {
5301 status = psa_generate_random(key_buffer, key_buffer_size);
5302 if (status != PSA_SUCCESS) {
5303 return status;
5304 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005305
David Brown12ca5032021-02-11 11:02:00 -07005306#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005307 if (type == PSA_KEY_TYPE_DES) {
5308 psa_des_set_key_parity(key_buffer, key_buffer_size);
5309 }
David Brown8107e312021-02-12 08:08:46 -07005310#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005311 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01005312
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005313#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5314 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005315 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
5316 return mbedtls_psa_rsa_generate_key(attributes,
5317 key_buffer,
5318 key_buffer_size,
5319 key_buffer_length);
5320 } else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005321#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5322 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005323
John Durkop9814fa22020-11-04 12:28:15 -08005324#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
5326 return mbedtls_psa_ecp_generate_key(attributes,
5327 key_buffer,
5328 key_buffer_size,
5329 key_buffer_length);
5330 } else
John Durkop9814fa22020-11-04 12:28:15 -08005331#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005332 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005333 (void) key_buffer_length;
5334 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02005335 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005337 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005338}
Darryl Green0c6575a2018-11-07 16:05:30 +00005339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005340psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
5341 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005342{
5343 psa_status_t status;
5344 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005345 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005346 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005347
Ronald Cron81709fc2020-11-14 12:10:32 +01005348 *key = MBEDTLS_SVC_KEY_ID_INIT;
5349
Gilles Peskine0f84d622019-09-12 19:03:13 +02005350 /* Reject any attempt to create a zero-length key so that we don't
5351 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 if (psa_get_key_bits(attributes) == 0) {
5353 return PSA_ERROR_INVALID_ARGUMENT;
5354 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02005355
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005356 /* Reject any attempt to create a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005357 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
5358 return PSA_ERROR_INVALID_ARGUMENT;
5359 }
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005361 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
5362 &slot, &driver);
5363 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02005364 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005365 }
Gilles Peskine11792082019-08-06 18:36:36 +02005366
Ronald Cron977c2472020-10-13 08:32:21 +02005367 /* In the case of a transparent key or an opaque key stored in local
5368 * storage (thus not in the case of generating a key in a secure element
5369 * or cryptoprocessor with storage), we have to allocate a buffer to
5370 * hold the generated key material. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005371 if (slot->key.data == NULL) {
5372 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
5373 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005374 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005375 attributes->core.type, attributes->core.bits);
5376 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005377 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005378 }
Ronald Cron3772afe2021-02-08 16:10:05 +01005379
5380 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 attributes->core.type,
5382 attributes->core.bits);
5383 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02005384 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005385 attributes, &key_buffer_size);
5386 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01005387 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005388 }
Ronald Cron977c2472020-10-13 08:32:21 +02005389 }
Ronald Cron977c2472020-10-13 08:32:21 +02005390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005391 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
5392 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02005393 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005394 }
Ronald Cron977c2472020-10-13 08:32:21 +02005395 }
5396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005397 status = psa_driver_wrapper_generate_key(attributes,
5398 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02005399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005400 if (status != PSA_SUCCESS) {
5401 psa_remove_key_data_from_memory(slot);
5402 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02005403
Gilles Peskine11792082019-08-06 18:36:36 +02005404exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005405 if (status == PSA_SUCCESS) {
5406 status = psa_finish_key_creation(slot, driver, key);
5407 }
5408 if (status != PSA_SUCCESS) {
5409 psa_fail_key_creation(slot, driver);
5410 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005411
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005412 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005413}
5414
Gilles Peskinee59236f2018-01-27 23:32:46 +01005415/****************************************************************/
5416/* Module setup */
5417/****************************************************************/
5418
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005419#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005420psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005421 void (* entropy_init)(mbedtls_entropy_context *ctx),
5422 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01005423{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005424 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5425 return PSA_ERROR_BAD_STATE;
5426 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01005427 global_data.rng.entropy_init = entropy_init;
5428 global_data.rng.entropy_free = entropy_free;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01005430}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005431#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005432
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005433void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005434{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005435 psa_wipe_all_key_slots();
5436 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
5437 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005438 }
5439 /* Wipe all remaining data, including configuration.
5440 * In particular, this sets all state indicator to the value
5441 * indicating "uninitialized". */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005442 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron088d5d02021-04-10 16:57:30 +02005443
5444 /* Terminate drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01005446}
5447
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005448#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5449/** Recover a transaction that was interrupted by a power failure.
5450 *
5451 * This function is called during initialization, before psa_crypto_init()
5452 * returns. If this function returns a failure status, the initialization
5453 * fails.
5454 */
5455static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005457{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005458 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005459 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5460 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005461 /* TODO - fall through to the failure case until this
5462 * is implemented.
5463 * https://github.com/ARMmbed/mbed-crypto/issues/218
5464 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005465 default:
5466 /* We found an unsupported transaction in the storage.
5467 * We don't know what state the storage is in. Give up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005469 }
5470}
5471#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005473psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005474{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005475 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005476
Gilles Peskinec6b69072018-11-20 21:42:52 +01005477 /* Double initialization is explicitly allowed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 if (global_data.initialized != 0) {
5479 return PSA_SUCCESS;
5480 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005481
Gilles Peskine30524eb2020-11-13 17:02:26 +01005482 /* Initialize and seed the random generator. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005483 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01005484 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 status = mbedtls_psa_random_seed(&global_data.rng);
5486 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01005487 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005489 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 status = psa_initialize_key_slots();
5492 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01005493 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005494 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01005495
Ronald Cron088d5d02021-04-10 16:57:30 +02005496 /* Init drivers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005497 status = psa_driver_wrapper_init();
5498 if (status != PSA_SUCCESS) {
Gilles Peskined9348f22019-10-01 15:22:29 +02005499 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 }
Gilles Peskined9348f22019-10-01 15:22:29 +02005501
Gilles Peskine4b734222019-07-24 15:56:31 +02005502#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 status = psa_crypto_load_transaction();
5504 if (status == PSA_SUCCESS) {
5505 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
5506 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005507 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005508 }
5509 status = psa_crypto_stop_transaction();
5510 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02005511 /* There's no transaction to complete. It's all good. */
5512 status = PSA_SUCCESS;
5513 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005514#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005515
Gilles Peskinec6b69072018-11-20 21:42:52 +01005516 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517 global_data.initialized = 1;
5518
5519exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 if (status != PSA_SUCCESS) {
5521 mbedtls_psa_crypto_free();
5522 }
5523 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524}
5525
David Horstmann957f9802023-10-30 20:29:43 +00005526psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
5527 uint8_t *input_copy, size_t input_copy_len)
5528{
5529 if (input_len > input_copy_len) {
5530 return PSA_ERROR_BUFFER_TOO_SMALL;
5531 }
5532
5533 memcpy(input_copy, input, input_len);
5534
5535 return PSA_SUCCESS;
5536}
5537
David Horstmann2bd296e2023-10-30 20:37:12 +00005538psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
5539 uint8_t *output, size_t output_len)
5540{
5541 if (output_len < output_copy_len) {
5542 return PSA_ERROR_BUFFER_TOO_SMALL;
5543 }
5544 memcpy(output, output_copy, output_copy_len);
5545 return PSA_SUCCESS;
5546}
5547
Gilles Peskinee59236f2018-01-27 23:32:46 +01005548#endif /* MBEDTLS_PSA_CRYPTO_C */