blob: c3b8335ae5a18cd7f725f06c0d1bf8b8310417b9 [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"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020084#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020086#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
87 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020089#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020090#endif
91
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010092/****************************************************************/
93/* Global data, support functions and library management */
94/****************************************************************/
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099}
100
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100101/* Values for psa_global_data_t::rng_state */
102#define RNG_NOT_INITIALIZED 0
103#define RNG_INITIALIZED 1
104#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100107 uint8_t initialized;
108 uint8_t rng_state;
109 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100110 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100111} psa_global_data_t;
112
113static psa_global_data_t global_data;
114
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100115#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
116mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
117 &global_data.rng.drbg;
118#endif
119
itayzafrir0adf0fc2018-09-06 16:24:41 +0300120#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (global_data.initialized == 0) \
122 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100124int psa_can_do_hash(psa_algorithm_t hash_alg)
125{
126 (void) hash_alg;
127 return global_data.drivers_initialized;
128}
Valerio Settia55f0422023-07-10 15:34:41 +0200129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200131 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200132static int psa_is_dh_key_size_valid(size_t bits)
133{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200134 if (bits != 2048 && bits != 3072 && bits != 4096 &&
135 bits != 6144 && bits != 8192) {
136 return 0;
137 }
138
139 return 1;
140}
Valerio Settia55f0422023-07-10 15:34:41 +0200141#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200142 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200143 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100147 /* Mbed TLS error codes can combine a high-level error code and a
148 * low-level error code. The low-level error usually reflects the
149 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 int low_level_ret = -(-ret & 0x007f);
151 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100152 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100154
155 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
156 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200158 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
159 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
160 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
161 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
162 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200166 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100172 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100174
175 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100179
Gilles Peskine26869f22019-05-06 15:25:00 +0200180 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200182
183 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200185 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200187
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100192 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
204 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100205 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
206 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
210 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100215
216 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
Gilles Peskinee59236f2018-01-27 23:32:46 +0100219 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
220 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
221 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100223
224 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200226 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100228 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
Gilles Peskine82e57d12020-11-13 21:31:17 +0100231#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100233 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
234 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100235 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
238 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100240 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100242#endif
243
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200293 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100295
Gilles Peskineff2d2002019-05-06 15:26:23 +0200296 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200298 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200300
Gilles Peskinea5905292018-02-07 20:59:33 +0100301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100316 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200318
itayzafrir5c753392018-05-08 11:18:38 +0300319 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300320 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300322 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300324 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300326 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
327 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100331 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100333
Paul Elliott588f8ed2022-12-02 18:10:26 +0000334 case MBEDTLS_ERR_ECP_IN_PROGRESS:
335 return PSA_OPERATION_INCOMPLETE;
336
Janos Follatha13b9052019-11-22 12:48:59 +0000337 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300339
Gilles Peskinee59236f2018-01-27 23:32:46 +0100340 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100342 }
343}
344
Paul Elliottdc42ca82023-02-24 18:11:59 +0000345/**
346 * \brief For output buffers which contain "tags"
347 * (outputs that may be checked for validity like
348 * hashes, MACs and signatures), fill the unused
349 * part of the output buffer (the whole buffer on
350 * error, the trailing part on success) with
351 * something that isn't a valid tag (barring an
352 * attack on the tag and deliberately-crafted
353 * input), in case the caller doesn't check the
354 * return status properly.
355 *
356 * \param output_buffer Pointer to buffer to wipe. May not be NULL
357 * unless \p output_buffer_size is zero.
358 * \param status Status of function called to generate
359 * output_buffer originally
360 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
361 * could be NULL.
362 * \param output_buffer_length Length of data written to output_buffer, must be
363 * less than \p output_buffer_size
364 */
365static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000366 size_t output_buffer_size, size_t output_buffer_length)
367{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000368 size_t offset = 0;
369
370 if (output_buffer_size == 0) {
371 /* If output_buffer_size is 0 then we have nothing to do. We must not
372 call memset because output_buffer may be NULL in this case */
373 return;
374 }
375
376 if (status == PSA_SUCCESS) {
377 offset = output_buffer_length;
378 }
379
380 memset(output_buffer + offset, '!', output_buffer_size - offset);
381}
382
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200383
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200384
385
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386/****************************************************************/
387/* Key management */
388/****************************************************************/
389
Valerio Settia9aab1a2023-06-19 13:39:54 +0200390#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200391psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
392 size_t *bits)
393{
394 switch (grpid) {
Dave Rodgman6f682032023-08-16 18:44:32 +0100395#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200396 case MBEDTLS_ECP_DP_SECP192R1:
397 *bits = 192;
398 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100399#endif
400#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200401 case MBEDTLS_ECP_DP_SECP224R1:
402 *bits = 224;
403 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100404#endif
405#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200406 case MBEDTLS_ECP_DP_SECP256R1:
407 *bits = 256;
408 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100409#endif
410#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200411 case MBEDTLS_ECP_DP_SECP384R1:
412 *bits = 384;
413 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100414#endif
415#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200416 case MBEDTLS_ECP_DP_SECP521R1:
417 *bits = 521;
418 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100419#endif
420#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200421 case MBEDTLS_ECP_DP_BP256R1:
422 *bits = 256;
423 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100424#endif
425#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200426 case MBEDTLS_ECP_DP_BP384R1:
427 *bits = 384;
428 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100429#endif
430#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200431 case MBEDTLS_ECP_DP_BP512R1:
432 *bits = 512;
433 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100434#endif
435#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200436 case MBEDTLS_ECP_DP_CURVE25519:
437 *bits = 255;
438 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100439#endif
440#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200441 case MBEDTLS_ECP_DP_SECP192K1:
442 *bits = 192;
443 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100444#endif
445#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200446 case MBEDTLS_ECP_DP_SECP224K1:
447 *bits = 224;
448 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100449#endif
450#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200451 case MBEDTLS_ECP_DP_SECP256K1:
452 *bits = 256;
453 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100454#endif
455#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200456 case MBEDTLS_ECP_DP_CURVE448:
457 *bits = 448;
458 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100459#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200460 default:
461 *bits = 0;
462 return 0;
463 }
464}
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
467 size_t bits,
468 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469{
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100471 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100473#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100474 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100476#endif
477#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100478 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100480#endif
481#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100482 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100484#endif
485#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100486 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100488#endif
489#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100490 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100492 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (bits_is_sloppy) {
494 return MBEDTLS_ECP_DP_SECP521R1;
495 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100497#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100498 }
499 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100500
Paul Elliott8ff510a2020-06-02 17:19:28 +0100501 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100504 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100506#endif
507#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100508 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100510#endif
511#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100512 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100515 }
516 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100517
Paul Elliott8ff510a2020-06-02 17:19:28 +0100518 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (bits_is_sloppy) {
525 return MBEDTLS_ECP_DP_CURVE25519;
526 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100528#endif
529#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100530 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100532#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100533 }
534 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100535
Paul Elliott8ff510a2020-06-02 17:19:28 +0100536 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100538#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100539 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#endif
542#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100543 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100545#endif
546#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100547 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100549#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100550 }
551 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200552 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100553
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100554 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200556}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200557#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
560 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200561{
562 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200564 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200565 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200566 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200567 case PSA_KEY_TYPE_PASSWORD:
568 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200569 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100570#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (bits != 128 && bits != 192 && bits != 256) {
573 return PSA_ERROR_INVALID_ARGUMENT;
574 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200575 break;
576#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200577#if defined(PSA_WANT_KEY_TYPE_ARIA)
578 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (bits != 128 && bits != 192 && bits != 256) {
580 return PSA_ERROR_INVALID_ARGUMENT;
581 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200582 break;
583#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100584#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200585 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (bits != 128 && bits != 192 && bits != 256) {
587 return PSA_ERROR_INVALID_ARGUMENT;
588 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200589 break;
590#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100591#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200592 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (bits != 64 && bits != 128 && bits != 192) {
594 return PSA_ERROR_INVALID_ARGUMENT;
595 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200596 break;
597#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100598#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200599 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (bits != 256) {
601 return PSA_ERROR_INVALID_ARGUMENT;
602 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200603 break;
604#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200607 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (bits % 8 != 0) {
609 return PSA_ERROR_INVALID_ARGUMENT;
610 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613}
614
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100615/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100616 *
Steven Cooremancd640932021-03-08 12:00:27 +0100617 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
618 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100619 *
620 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100621 * \param[in] key_type The key type of the key to be used with the
622 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100623 *
624 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100625 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100626 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100627 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100629MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100630 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100632{
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (PSA_ALG_IS_HMAC(algorithm)) {
634 if (key_type == PSA_KEY_TYPE_HMAC) {
635 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100636 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100637 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
640 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
641 * key. */
642 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
643 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
644 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
645 * the block length (larger than 1) for block ciphers. */
646 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
647 return PSA_SUCCESS;
648 }
649 }
650 }
651
652 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100653}
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
656 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200657{
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (slot->key.data != NULL) {
659 return PSA_ERROR_ALREADY_EXISTS;
660 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 slot->key.data = mbedtls_calloc(1, buffer_length);
663 if (slot->key.data == NULL) {
664 return PSA_ERROR_INSUFFICIENT_MEMORY;
665 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200666
Ronald Cronea0f8a62020-11-25 17:52:23 +0100667 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200669}
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
672 const uint8_t *data,
673 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200674{
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 psa_status_t status = psa_allocate_buffer_to_slot(slot,
676 data_length);
677 if (status != PSA_SUCCESS) {
678 return status;
679 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 memcpy(slot->key.data, data, data_length);
682 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200683}
684
Ronald Crona0fe59f2020-11-29 11:14:53 +0100685psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100686 const psa_key_attributes_t *attributes,
687 const uint8_t *data, size_t data_length,
688 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100691 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
692 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200694 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (data_length == 0) {
696 return PSA_ERROR_NOT_SUPPORTED;
697 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (key_type_is_raw_bytes(type)) {
700 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
703 *bits);
704 if (status != PSA_SUCCESS) {
705 return status;
706 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200707
Ronald Crondd04d422020-11-28 15:14:42 +0100708 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100710 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return PSA_SUCCESS;
714 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200715#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100717 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200718 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100719 return PSA_ERROR_INVALID_ARGUMENT;
720 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200721 return mbedtls_psa_ffdh_import_key(attributes,
722 data, data_length,
723 key_buffer, key_buffer_size,
724 key_buffer_length,
725 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100726 }
Valerio Settia55f0422023-07-10 15:34:41 +0200727#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200728 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200729#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
731 if (PSA_KEY_TYPE_IS_ECC(type)) {
732 return mbedtls_psa_ecp_import_key(attributes,
733 data, data_length,
734 key_buffer, key_buffer_size,
735 key_buffer_length,
736 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100737 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200738#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800739 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200740#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
741 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
743 if (PSA_KEY_TYPE_IS_RSA(type)) {
744 return mbedtls_psa_rsa_import_key(attributes,
745 data, data_length,
746 key_buffer, key_buffer_size,
747 key_buffer_length,
748 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200749 }
Valerio Settife478902023-07-25 12:27:19 +0200750#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
751 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800752 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100753 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100756}
757
Gilles Peskinef603c712019-01-19 13:40:11 +0100758/** Calculate the intersection of two algorithm usage policies.
759 *
760 * Return 0 (which allows no operation) on incompatibility.
761 */
762static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100763 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100764 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100766{
Gilles Peskine549ea862019-05-22 11:45:59 +0200767 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (alg1 == alg2) {
769 return alg1;
770 }
Steven Cooreman03488022021-02-18 12:07:20 +0100771 /* If the policies are from the same hash-and-sign family, check
772 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
774 PSA_ALG_IS_SIGN_HASH(alg2) &&
775 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
776 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
777 return alg2;
778 }
779 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
780 return alg1;
781 }
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
783 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100784 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100785 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
787 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
788 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
789 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
790 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100791 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100792
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100793 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
795 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
796 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
797 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100798 }
799 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
801 (alg1_len <= alg2_len)) {
802 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100803 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
805 (alg2_len <= alg1_len)) {
806 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100807 }
808 }
809 /* If the policies are from the same MAC family, check whether one
810 * of them is a minimum-MAC-length policy. Calculate the most
811 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
813 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
814 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100815 /* Validate the combination of key type and algorithm. Since the base
816 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
818 return 0;
819 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100820
Steven Cooreman31a876d2021-03-03 20:47:40 +0100821 /* Get the (exact or at-least) output lengths for both sides of the
822 * requested intersection. None of the currently supported algorithms
823 * have an output length dependent on the actual key size, so setting it
824 * to a bogus value of 0 is currently OK.
825 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826 * Note that for at-least-this-length wildcard algorithms, the output
827 * length is set to the shortest allowed length, which allows us to
828 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
830 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100831 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832
Steven Cooremana1d83222021-02-25 10:20:29 +0100833 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
835 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
836 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100837 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100838
839 /* If only one is an at-least-this-length policy, the intersection would
840 * be the other (fixed-length) policy as long as said fixed length is
841 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
843 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100844 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
846 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100847 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100848
Steven Cooremancd640932021-03-08 12:00:27 +0100849 /* If none of them are wildcards, check whether they define the same tag
850 * length. This is still possible here when one is default-length and
851 * the other specific-length. Ensure to always return the
852 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (alg1_len == alg2_len) {
854 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
855 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 }
857 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100859}
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int psa_key_algorithm_permits(psa_key_type_t key_type,
862 psa_algorithm_t policy_alg,
863 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200864{
Gilles Peskine549ea862019-05-22 11:45:59 +0200865 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (requested_alg == policy_alg) {
867 return 1;
868 }
Steven Cooreman03488022021-02-18 12:07:20 +0100869 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
870 * and requested_alg is the same hash-and-sign family with any hash,
871 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
873 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
874 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
875 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100876 }
877 /* If policy_alg is a wildcard AEAD algorithm of the same base as
878 * the requested algorithm, check the requested tag length to be
879 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (PSA_ALG_IS_AEAD(policy_alg) &&
881 PSA_ALG_IS_AEAD(requested_alg) &&
882 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
883 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
884 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
885 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
886 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100887 }
Steven Cooremancd640932021-03-08 12:00:27 +0100888 /* If policy_alg is a MAC algorithm of the same base as the requested
889 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (PSA_ALG_IS_MAC(policy_alg) &&
891 PSA_ALG_IS_MAC(requested_alg) &&
892 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
893 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100894 /* Validate the combination of key type and algorithm. Since the policy
895 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
897 return 0;
898 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100899
Steven Cooreman31a876d2021-03-03 20:47:40 +0100900 /* Get both the requested output length for the algorithm which is to be
901 * verified, and the default output length for the base algorithm.
902 * Note that none of the currently supported algorithms have an output
903 * length dependent on actual key size, so setting it to a bogus value
904 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100905 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100907 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 key_type, 0,
909 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100910
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100911 /* If the policy is default-length, only allow an algorithm with
912 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
914 return requested_output_length == default_output_length;
915 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100916
917 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100918 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
920 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
921 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100922 }
923
Steven Cooremancd640932021-03-08 12:00:27 +0100924 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
925 * check for the requested MAC length to be equal to or longer than the
926 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
928 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
929 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100930 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200931 }
Steven Cooremance48e852020-10-05 16:02:45 +0200932 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200933 * a key derivation with that key agreement should also be allowed. This
934 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
936 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
937 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
938 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200939 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100940 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942}
943
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100944/** Test whether a policy permits an algorithm.
945 *
946 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100947 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100948 * \note This function requires providing the key type for which the policy is
949 * being validated, since some algorithm policy definitions (e.g. MAC)
950 * have different properties depending on what kind of cipher it is
951 * combined with.
952 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100953 * \retval PSA_SUCCESS When \p alg is a specific algorithm
954 * allowed by the \p policy.
955 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
956 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
957 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
960 psa_key_type_t key_type,
961 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100962{
Steven Cooremand788fab2021-02-25 11:29:17 +0100963 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (alg == 0) {
965 return PSA_ERROR_INVALID_ARGUMENT;
966 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100967
Steven Cooreman7e39f052021-02-23 14:18:32 +0100968 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (PSA_ALG_IS_WILDCARD(alg)) {
970 return PSA_ERROR_INVALID_ARGUMENT;
971 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
974 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
975 return PSA_SUCCESS;
976 } else {
977 return PSA_ERROR_NOT_PERMITTED;
978 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100979}
980
Gilles Peskinef603c712019-01-19 13:40:11 +0100981/** Restrict a key policy based on a constraint.
982 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100983 * \note This function requires providing the key type for which the policy is
984 * being restricted, since some algorithm policy definitions (e.g. MAC)
985 * have different properties depending on what kind of cipher it is
986 * combined with.
987 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100988 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100989 * \param[in,out] policy The policy to restrict.
990 * \param[in] constraint The policy constraint to apply.
991 *
992 * \retval #PSA_SUCCESS
993 * \c *policy contains the intersection of the original value of
994 * \c *policy and \c *constraint.
995 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100996 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100997 * \c *policy is unchanged.
998 */
999static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001000 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001001 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001003{
1004 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1006 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001007 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1009 constraint->alg2);
1010 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1011 return PSA_ERROR_INVALID_ARGUMENT;
1012 }
1013 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1014 return PSA_ERROR_INVALID_ARGUMENT;
1015 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001016 policy->usage &= constraint->usage;
1017 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001018 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001020}
1021
Przemek Stekiel061f6942022-11-30 10:51:35 +01001022/** Get the description of a key given its identifier and policy constraints
1023 * and lock it.
1024 *
1025 * The key must have allow all the usage flags set in \p usage. If \p alg is
1026 * nonzero, the key must allow operations with this algorithm. If \p alg is
1027 * zero, the algorithm is not checked.
1028 *
1029 * In case of a persistent key, the function loads the description of the key
1030 * into a key slot if not already done.
1031 *
1032 * On success, the returned key slot is locked. It is the responsibility of
1033 * the caller to unlock the key slot when it does not access it anymore.
1034 */
1035static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001036 mbedtls_svc_key_id_t key,
1037 psa_key_slot_t **p_slot,
1038 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001040{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001042 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 status = psa_get_and_lock_key_slot(key, p_slot);
1045 if (status != PSA_SUCCESS) {
1046 return status;
1047 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001048 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001049
1050 /* Enforce that usage policy for the key slot contains all the flags
1051 * required by the usage parameter. There is one exception: public
1052 * keys can always be exported, so we treat public key objects as
1053 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001055 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001059 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001060 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001061 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001062
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001063 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (alg != 0) {
1065 status = psa_key_policy_permits(&slot->attr.policy,
1066 slot->attr.type,
1067 alg);
1068 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001069 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001071 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001074
1075error:
1076 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001080}
Darryl Green940d72c2018-07-13 13:18:51 +01001081
Ronald Cron5c522922020-11-14 16:35:34 +01001082/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001083 *
1084 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001085 * available, as opposed to a key in a secure element and/or to be used
1086 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001087 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001088 * This is a temporary function that may be used instead of
1089 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1090 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001091 *
Ronald Cron5c522922020-11-14 16:35:34 +01001092 * On success, the returned key slot is locked. It is the responsibility of the
1093 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001094 */
Ronald Cron5c522922020-11-14 16:35:34 +01001095static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1096 mbedtls_svc_key_id_t key,
1097 psa_key_slot_t **p_slot,
1098 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001100{
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1102 usage, alg);
1103 if (status != PSA_SUCCESS) {
1104 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001105 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1108 psa_unlock_key_slot(*p_slot);
1109 *p_slot = NULL;
1110 return PSA_ERROR_NOT_SUPPORTED;
1111 }
1112
1113 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001114}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001117{
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001119 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001121
Ronald Cronea0f8a62020-11-25 17:52:23 +01001122 slot->key.data = NULL;
1123 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001126}
1127
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128/** Completely wipe a slot in memory, including its policy.
1129 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001131{
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 /*
1135 * As the return error code may not be handled in case of multiple errors,
1136 * do our best to report an unexpected lock counter. Assert with
1137 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1138 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1139 * function is called as part of the execution of a test suite, the
1140 * execution of the test suite is stopped in error if the assertion fails.
1141 */
1142 if (slot->lock_count != 1) {
1143 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001144 status = PSA_ERROR_CORRUPTION_DETECTED;
1145 }
1146
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001147 /* Multipart operations may still be using the key. This is safe
1148 * because all multipart operation objects are independent from
1149 * the key slot: if they need to access the key after the setup
1150 * phase, they have a copy of the key. Note that this means that
1151 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001152 /* At this point, key material and other type-specific content has
1153 * been wiped. Clear remaining metadata. We can call memset and not
1154 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 memset(slot, 0, sizeof(*slot));
1156 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001157}
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001161 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162 psa_status_t status; /* status of the last operation */
1163 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1165 psa_se_drv_table_entry_t *driver;
1166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_svc_key_id_is_null(key)) {
1169 return PSA_SUCCESS;
1170 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001171
Ronald Cronf2911112020-10-29 17:51:10 +01001172 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001173 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001174 * key, this will load the key description from persistent memory if not
1175 * done yet. We cannot avoid this loading as without it we don't know if
1176 * the key is operated by an SE or not and this information is needed by
1177 * the current implementation.
1178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_get_and_lock_key_slot(key, &slot);
1180 if (status != PSA_SUCCESS) {
1181 return status;
1182 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001183
Ronald Cronf2911112020-10-29 17:51:10 +01001184 /*
1185 * If the key slot containing the key description is under access by the
1186 * library (apart from the present access), the key cannot be destroyed
1187 * yet. For the time being, just return in error. Eventually (to be
1188 * implemented), the key should be destroyed when all accesses have
1189 * stopped.
1190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (slot->lock_count > 1) {
1192 psa_unlock_key_slot(slot);
1193 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001194 }
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001197 /* Refuse the destruction of a read-only key (which may or may not work
1198 * if we attempt it, depending on whether the key is merely read-only
1199 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001200 * Just do the best we can, which is to wipe the copy in memory
1201 * (done in this function's cleanup code). */
1202 overall_status = PSA_ERROR_NOT_PERMITTED;
1203 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001204 }
1205
Gilles Peskine354f7672019-07-12 23:46:38 +02001206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1208 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001209 /* For a key in a secure element, we need to do three things:
1210 * remove the key file in internal storage, destroy the
1211 * key inside the secure element, and update the driver's
1212 * persistent data. Start a transaction that will encompass these
1213 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001215 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001217 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 status = psa_crypto_save_transaction();
1219 if (status != PSA_SUCCESS) {
1220 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001221 /* We should still try to destroy the key in the secure
1222 * element and the key metadata in storage. This is especially
1223 * important if the error is that the storage is full.
1224 * But how to do it exactly without risking an inconsistent
1225 * state after a reset?
1226 * https://github.com/ARMmbed/mbed-crypto/issues/215
1227 */
1228 overall_status = status;
1229 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 status = psa_destroy_se_key(driver,
1233 psa_key_slot_get_slot_number(slot));
1234 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001235 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1239
Darryl Greend49a4992018-06-18 17:27:26 +01001240#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1242 status = psa_destroy_persistent_key(slot->attr.id);
1243 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001244 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001246
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001247 /* TODO: other slots may have a copy of the same key. We should
1248 * invalidate them.
1249 * https://github.com/ARMmbed/mbed-crypto/issues/214
1250 */
Darryl Greend49a4992018-06-18 17:27:26 +01001251 }
1252#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001253
Gilles Peskinefc762652019-07-22 19:30:34 +02001254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (driver != NULL) {
1256 status = psa_save_se_persistent_data(driver);
1257 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001258 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 }
1260 status = psa_crypto_stop_transaction();
1261 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001264 }
1265#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1266
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001269 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001271 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 }
1273 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274}
1275
Valerio Settib2bcedb2023-07-10 11:24:00 +02001276#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001277 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001278static psa_status_t psa_get_rsa_public_exponent(
1279 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001281{
1282 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 uint8_t *buffer = NULL;
1285 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1289 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 }
1292 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001293 /* It's the default value, which is reported as an empty string,
1294 * so there's nothing to do. */
1295 goto exit;
1296 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 buflen = mbedtls_mpi_size(&mpi);
1299 buffer = mbedtls_calloc(1, buflen);
1300 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001301 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1302 goto exit;
1303 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1305 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001306 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001308 attributes->domain_parameters = buffer;
1309 attributes->domain_parameters_size = buflen;
1310
1311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_mpi_free(&mpi);
1313 if (ret != 0) {
1314 mbedtls_free(buffer);
1315 }
1316 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001317}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001318#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001319 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001320
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001321/** Retrieve all the publicly-accessible attributes of a key.
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1324 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001325{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001327 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001328 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1333 if (status != PSA_SUCCESS) {
1334 return status;
1335 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001336
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001337 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1339 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001340
1341#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1343 psa_set_key_slot_number(attributes,
1344 psa_key_slot_get_slot_number(slot));
1345 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001346#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001349#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1350 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001351 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001352 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001353 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001354 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001355 * is not yet implemented.
1356 * https://github.com/ARMmbed/mbed-crypto/issues/216
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001359 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001360
Ronald Cron90857082020-11-25 14:58:33 +01001361 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 slot->attr.type,
1363 slot->key.data,
1364 slot->key.bytes,
1365 &rsa);
1366 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001367 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 status = psa_get_rsa_public_exponent(rsa,
1371 attributes);
1372 mbedtls_rsa_free(rsa);
1373 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001374 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001375 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001376#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1377 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001378 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001379 default:
1380 /* Nothing else to do. */
1381 break;
1382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (status != PSA_SUCCESS) {
1385 psa_reset_key_attributes(attributes);
1386 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391}
1392
Gilles Peskinec8000c02019-08-02 20:15:51 +02001393#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1394psa_status_t psa_get_key_slot_number(
1395 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001397{
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001399 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return PSA_SUCCESS;
1401 } else {
1402 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001403 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001404}
1405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1408 size_t key_buffer_size,
1409 uint8_t *data,
1410 size_t data_size,
1411 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001412{
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (key_buffer_size > data_size) {
1414 return PSA_ERROR_BUFFER_TOO_SMALL;
1415 }
1416 memcpy(data, key_buffer, key_buffer_size);
1417 memset(data + key_buffer_size, 0,
1418 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001419 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001421}
1422
Ronald Cron7285cda2020-11-26 14:46:37 +01001423psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 const psa_key_attributes_t *attributes,
1425 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427{
Ronald Crond18b5f82020-11-25 16:46:05 +01001428 psa_key_type_t type = attributes->core.type;
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (key_type_is_raw_bytes(type) ||
1431 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001432 PSA_KEY_TYPE_IS_ECC(type) ||
1433 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return psa_export_key_buffer_internal(
1435 key_buffer, key_buffer_size,
1436 data, data_size, data_length);
1437 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 /* This shouldn't happen in the reference implementation, but
1439 it is valid for a special-purpose implementation to omit
1440 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001442 }
1443}
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1446 uint8_t *data,
1447 size_t data_size,
1448 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001449{
1450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1451 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1452 psa_key_slot_t *slot;
1453
Ronald Crone907e552021-01-18 13:22:38 +01001454 /* Reject a zero-length output buffer now, since this can never be a
1455 * valid key representation. This way we know that data must be a valid
1456 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (data_size == 0) {
1458 return PSA_ERROR_BUFFER_TOO_SMALL;
1459 }
Ronald Crone907e552021-01-18 13:22:38 +01001460
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461 /* Set the key to empty now, so that even when there are errors, we always
1462 * set data_length to a value between 0 and data_size. On error, setting
1463 * the key to empty is a good choice because an empty key representation is
1464 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001465 *data_length = 0;
1466
Ronald Cron9486f9d2020-11-26 13:36:23 +01001467 /* Export requires the EXPORT flag. There is an exception for public keys,
1468 * which don't require any flag, but
1469 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1472 PSA_KEY_USAGE_EXPORT, 0);
1473 if (status != PSA_SUCCESS) {
1474 return status;
1475 }
mohammad160306e79202018-03-28 13:17:44 +03001476
Ronald Crond18b5f82020-11-25 16:46:05 +01001477 psa_key_attributes_t attributes = {
1478 .core = slot->attr
1479 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 status = psa_driver_wrapper_export_key(&attributes,
1481 slot->key.data, slot->key.bytes,
1482 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001487}
1488
Ronald Cron7285cda2020-11-26 14:46:37 +01001489psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001490 const psa_key_attributes_t *attributes,
1491 const uint8_t *key_buffer,
1492 size_t key_buffer_size,
1493 uint8_t *data,
1494 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001496{
Ronald Crond18b5f82020-11-25 16:46:05 +01001497 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001498
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001499 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001500 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1501 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001502 /* Exporting public -> public */
1503 return psa_export_key_buffer_internal(
1504 key_buffer, key_buffer_size,
1505 data, data_size, data_length);
1506 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001507#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001508 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1509 return mbedtls_psa_rsa_export_public_key(attributes,
1510 key_buffer,
1511 key_buffer_size,
1512 data,
1513 data_size,
1514 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001515#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001516 /* We don't know how to convert a private RSA key to public. */
1517 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001518#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001519 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001520 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001521#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001522 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1523 return mbedtls_psa_ecp_export_public_key(attributes,
1524 key_buffer,
1525 key_buffer_size,
1526 data,
1527 data_size,
1528 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001529#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001530 /* We don't know how to convert a private ECC key to public */
1531 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001532#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001533 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001534 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001535#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001536 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001537 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001538 key_buffer,
1539 key_buffer_size,
1540 data, data_size,
1541 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001542#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001543 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001544#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001545 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001547 (void) key_buffer;
1548 (void) key_buffer_size;
1549 (void) data;
1550 (void) data_size;
1551 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001553 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554}
Ronald Crond18b5f82020-11-25 16:46:05 +01001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1557 uint8_t *data,
1558 size_t data_size,
1559 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001560{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001562 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001563 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001564 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001565
Ronald Crone907e552021-01-18 13:22:38 +01001566 /* Reject a zero-length output buffer now, since this can never be a
1567 * valid key representation. This way we know that data must be a valid
1568 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (data_size == 0) {
1570 return PSA_ERROR_BUFFER_TOO_SMALL;
1571 }
Ronald Crone907e552021-01-18 13:22:38 +01001572
Darryl Greendd8fb772018-11-07 16:00:44 +00001573 /* Set the key to empty now, so that even when there are errors, we always
1574 * set data_length to a value between 0 and data_size. On error, setting
1575 * the key to empty is a good choice because an empty key representation is
1576 * unlikely to be accepted anywhere. */
1577 *data_length = 0;
1578
1579 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1581 if (status != PSA_SUCCESS) {
1582 return status;
1583 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1586 status = PSA_ERROR_INVALID_ARGUMENT;
1587 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001588 }
1589
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001590 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001591 .core = slot->attr
1592 };
Ronald Cron67227982020-11-26 15:16:05 +01001593 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001594 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001596
1597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001601}
1602
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001603MBEDTLS_STATIC_ASSERT(
1604 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1605 "One or more key attribute flag is listed as both external-only and dual-use")
1606MBEDTLS_STATIC_ASSERT(
1607 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1608 "One or more key attribute flag is listed as both internal-only and dual-use")
1609MBEDTLS_STATIC_ASSERT(
1610 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1611 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001612
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001613/** Validate that a key policy is internally well-formed.
1614 *
1615 * This function only rejects invalid policies. It does not validate the
1616 * consistency of the policy with respect to other attributes of the key
1617 * such as the key type.
1618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001620{
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1622 PSA_KEY_USAGE_COPY |
1623 PSA_KEY_USAGE_ENCRYPT |
1624 PSA_KEY_USAGE_DECRYPT |
1625 PSA_KEY_USAGE_SIGN_MESSAGE |
1626 PSA_KEY_USAGE_VERIFY_MESSAGE |
1627 PSA_KEY_USAGE_SIGN_HASH |
1628 PSA_KEY_USAGE_VERIFY_HASH |
1629 PSA_KEY_USAGE_VERIFY_DERIVATION |
1630 PSA_KEY_USAGE_DERIVE)) != 0) {
1631 return PSA_ERROR_INVALID_ARGUMENT;
1632 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001635}
1636
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001637/** Validate the internal consistency of key attributes.
1638 *
1639 * This function only rejects invalid attribute values. If does not
1640 * validate the consistency of the attributes with any key data that may
1641 * be involved in the creation of the key.
1642 *
1643 * Call this function early in the key creation process.
1644 *
1645 * \param[in] attributes Key attributes for the new key.
1646 * \param[out] p_drv On any return, the driver for the key, if any.
1647 * NULL for a transparent key.
1648 *
1649 */
1650static psa_status_t psa_validate_key_attributes(
1651 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001653{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001654 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1656 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 status = psa_validate_key_location(lifetime, p_drv);
1659 if (status != PSA_SUCCESS) {
1660 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001661 }
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 status = psa_validate_key_persistence(lifetime);
1664 if (status != PSA_SUCCESS) {
1665 return status;
1666 }
1667
1668 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1669 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1670 return PSA_ERROR_INVALID_ARGUMENT;
1671 }
1672 } else {
1673 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1674 return PSA_ERROR_INVALID_ARGUMENT;
1675 }
1676 }
1677
1678 status = psa_validate_key_policy(&attributes->core.policy);
1679 if (status != PSA_SUCCESS) {
1680 return status;
1681 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001682
1683 /* Refuse to create overly large keys.
1684 * Note that this doesn't trigger on import if the attributes don't
1685 * explicitly specify a size (so psa_get_key_bits returns 0), so
1686 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1688 return PSA_ERROR_NOT_SUPPORTED;
1689 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001690
Gilles Peskine91e8c332019-08-02 19:19:39 +02001691 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1693 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1694 return PSA_ERROR_INVALID_ARGUMENT;
1695 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698}
1699
Gilles Peskine4747d192019-04-17 15:05:45 +02001700/** Prepare a key slot to receive key material.
1701 *
1702 * This function allocates a key slot and sets its metadata.
1703 *
1704 * If this function fails, call psa_fail_key_creation().
1705 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001706 * This function is intended to be used as follows:
1707 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001708 * it with the specified attributes, and in case of a volatile key assign it
1709 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001710 * -# Populate the slot with the key material.
1711 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1712 * In case of failure at any step, stop the sequence and call
1713 * psa_fail_key_creation().
1714 *
Ronald Cron5c522922020-11-14 16:35:34 +01001715 * On success, the key slot is locked. It is the responsibility of the caller
1716 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001717 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001718 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001719 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001720 * \param[out] p_slot On success, a pointer to the prepared slot.
1721 * \param[out] p_drv On any return, the driver for the key, if any.
1722 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 *
1724 * \retval #PSA_SUCCESS
1725 * The key slot is ready to receive key material.
1726 * \return If this function fails, the key slot is an invalid state.
1727 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 */
1729static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001730 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001732 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001734{
1735 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001736 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 psa_key_slot_t *slot;
1738
Gilles Peskinedf179142019-07-15 22:02:14 +02001739 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001740 *p_drv = NULL;
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 status = psa_validate_key_attributes(attributes, p_drv);
1743 if (status != PSA_SUCCESS) {
1744 return status;
1745 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1748 if (status != PSA_SUCCESS) {
1749 return status;
1750 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 slot = *p_slot;
1752
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001753 /* We're storing the declared bit-size of the key. It's up to each
1754 * creation mechanism to verify that this information is correct.
1755 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001756 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001757 * is optional (import, copy). In case of a volatile key, assign it the
1758 * volatile key identifier associated to the slot returned to contain its
1759 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001760
1761 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001763#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1764 slot->attr.id = volatile_key_id;
1765#else
1766 slot->attr.id.key_id = volatile_key_id;
1767#endif
1768 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001769
Gilles Peskine91e8c332019-08-02 19:19:39 +02001770 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001771 * external-only flags, query `attributes`. Thanks to the check
1772 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001773 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001774 * may have set. */
1775 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001776
Gilles Peskinecbaff462019-07-12 23:46:04 +02001777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001778 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001779 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001780 * create the key file in internal storage, create the
1781 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 * persistent data. This is done by starting a transaction that will
1783 * encompass these three actions.
1784 * For registering a volatile key, we just need to find an appropriate
1785 * slot number inside the SE. Since the key is designated volatile, creating
1786 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001787 /* The first thing to do is to find a slot number for the new key.
1788 * We save the slot number in persistent storage as part of the
1789 * transaction data. It will be needed to recover if the power
1790 * fails during the key creation process, to clean up on the secure
1791 * element side after restarting. Obtaining a slot number from the
1792 * secure element driver updates its persistent state, but we do not yet
1793 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001794 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001796 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1798 &slot_number);
1799 if (status != PSA_SUCCESS) {
1800 return status;
1801 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1804 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001805 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001806 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001807 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 status = psa_crypto_save_transaction();
1809 if (status != PSA_SUCCESS) {
1810 (void) psa_crypto_stop_transaction();
1811 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001812 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001813 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001814
1815 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001817 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001820 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001822 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001823#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001826}
Gilles Peskine4747d192019-04-17 15:05:45 +02001827
1828/** Finalize the creation of a key once its key material has been set.
1829 *
1830 * This entails writing the key to persistent storage.
1831 *
1832 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001833 * See the documentation of psa_start_key_creation() for the intended use
1834 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 *
Ronald Cron5c522922020-11-14 16:35:34 +01001836 * If the finalization succeeds, the function unlocks the key slot (it was
1837 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1838 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001839 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001840 * \param[in,out] slot Pointer to the slot with key material.
1841 * \param[in] driver The secure element driver for the key,
1842 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001843 * \param[out] key On success, identifier of the key. Note that the
1844 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001845 *
1846 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001847 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1849 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1850 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1851 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1852 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1853 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001854 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001855 * \return If this function fails, the key slot is an invalid state.
1856 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001857 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001858static psa_status_t psa_finish_key_creation(
1859 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001860 psa_se_drv_table_entry_t *driver,
1861 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001862{
1863 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001864 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001865 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001866
1867#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001869#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001871 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001872 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001874
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001875 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1876 sizeof(data.slot_number),
1877 "Slot number size does not match psa_se_key_data_storage_t");
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1880 status = psa_save_persistent_key(&slot->attr,
1881 (uint8_t *) &data,
1882 sizeof(data));
1883 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001884#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1885 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001886 /* Key material is saved in export representation in the slot, so
1887 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 status = psa_save_persistent_key(&slot->attr,
1889 slot->key.data,
1890 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001891 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001893#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1894
Gilles Peskinecbaff462019-07-12 23:46:04 +02001895#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001896 /* Finish the transaction for a key creation. This does not
1897 * happen when registering an existing key. Detect this case
1898 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001899 * creation of a persistent key in a secure element requires a transaction,
1900 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (driver != NULL &&
1902 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1903 status = psa_save_se_persistent_data(driver);
1904 if (status != PSA_SUCCESS) {
1905 psa_destroy_persistent_key(slot->attr.id);
1906 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001907 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001909 }
1910#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001913 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 status = psa_unlock_key_slot(slot);
1915 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001916 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001918 }
Ronald Cron50972942020-11-14 11:28:25 +01001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001921}
1922
1923/** Abort the creation of a key.
1924 *
1925 * You may call this function after calling psa_start_key_creation(),
1926 * or after psa_finish_key_creation() fails. In other circumstances, this
1927 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001928 * See the documentation of psa_start_key_creation() for the intended use
1929 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001931 * \param[in,out] slot Pointer to the slot with key material.
1932 * \param[in] driver The secure element driver for the key,
1933 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001934 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935static void psa_fail_key_creation(psa_key_slot_t *slot,
1936 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001937{
Gilles Peskine011e4282019-06-26 18:34:38 +02001938 (void) driver;
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001943
1944#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001945 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001946 * element, and the failure happened later (when saving metadata
1947 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001948 * element.
1949 * https://github.com/ARMmbed/mbed-crypto/issues/217
1950 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001951
Gilles Peskined7729582019-08-05 15:55:54 +02001952 /* Abort the ongoing transaction if any (there may not be one if
1953 * the creation process failed before starting one, or if the
1954 * key creation is a registration of a key in a secure element).
1955 * Earlier functions must already have done what it takes to undo any
1956 * partial creation. All that's left is to update the transaction data
1957 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001962}
1963
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001964/** Validate optional attributes during key creation.
1965 *
1966 * Some key attributes are optional during key creation. If they are
1967 * specified in the attributes structure, check that they are consistent
1968 * with the data in the slot.
1969 *
1970 * This function should be called near the end of key creation, after
1971 * the slot in memory is fully populated but before saving persistent data.
1972 */
1973static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976{
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if (attributes->core.type != 0) {
1978 if (attributes->core.type != slot->attr.type) {
1979 return PSA_ERROR_INVALID_ARGUMENT;
1980 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 }
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001984#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1985 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1987 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001988 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001989 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001991
Ronald Cron90857082020-11-25 14:58:33 +01001992 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 slot->attr.type,
1994 slot->key.data,
1995 slot->key.bytes,
1996 &rsa);
1997 if (status != PSA_SUCCESS) {
1998 return status;
1999 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 mbedtls_mpi_init(&actual);
2002 mbedtls_mpi_init(&required);
2003 ret = mbedtls_rsa_export(rsa,
2004 NULL, NULL, NULL, NULL, &actual);
2005 mbedtls_rsa_free(rsa);
2006 mbedtls_free(rsa);
2007 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 }
2010 ret = mbedtls_mpi_read_binary(&required,
2011 attributes->domain_parameters,
2012 attributes->domain_parameters_size);
2013 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 }
2016 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 }
2019rsa_exit:
2020 mbedtls_mpi_free(&actual);
2021 mbedtls_mpi_free(&required);
2022 if (ret != 0) {
2023 return mbedtls_to_psa_error(ret);
2024 }
2025 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002026#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2027 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002028 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 }
2032 }
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (attributes->core.bits != 0) {
2035 if (attributes->core.bits != slot->attr.bits) {
2036 return PSA_ERROR_INVALID_ARGUMENT;
2037 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002038 }
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041}
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2044 const uint8_t *data,
2045 size_t data_length,
2046 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002047{
2048 psa_status_t status;
2049 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002050 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002051 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302052 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002053
Ronald Cron81709fc2020-11-14 12:10:32 +01002054 *key = MBEDTLS_SVC_KEY_ID_INIT;
2055
Gilles Peskine0f84d622019-09-12 19:03:13 +02002056 /* Reject zero-length symmetric keys (including raw data key objects).
2057 * This also rejects any key which might be encoded as an empty string,
2058 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (data_length == 0) {
2060 return PSA_ERROR_INVALID_ARGUMENT;
2061 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002062
Archana449608b2021-09-08 15:36:05 +05302063 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (data_length > SIZE_MAX / 8) {
2065 return PSA_ERROR_NOT_SUPPORTED;
2066 }
Archana6ed4bda2021-08-04 10:47:15 +05302067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2069 &slot, &driver);
2070 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002074 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302075 * storage ( thus not in the case of importing a key in a secure element
2076 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2077 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (slot->key.data == NULL) {
2079 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302080 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 attributes, data, data_length, &storage_size);
2082 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 }
Archanad8a83dc2021-06-14 10:04:16 +05302085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 status = psa_allocate_buffer_to_slot(slot, storage_size);
2087 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002090 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002091
2092 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 status = psa_driver_wrapper_import_key(attributes,
2094 data, data_length,
2095 slot->key.data,
2096 slot->key.bytes,
2097 &slot->key.bytes, &bits);
2098 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002103 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002105 status = PSA_ERROR_INVALID_ARGUMENT;
2106 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002107 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002108
Archana6ed4bda2021-08-04 10:47:15 +05302109 /* Enforce a size limit, and in particular ensure that the bit
2110 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302112 status = PSA_ERROR_NOT_SUPPORTED;
2113 goto exit;
2114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 status = psa_validate_optional_attributes(slot, attributes);
2116 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (status != PSA_SUCCESS) {
2123 psa_fail_key_creation(slot, driver);
2124 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002127}
2128
Gilles Peskined7729582019-08-05 15:55:54 +02002129#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2130psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002132{
2133 psa_status_t status;
2134 psa_key_slot_t *slot = NULL;
2135 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002137
2138 /* Leaving attributes unspecified is not currently supported.
2139 * It could make sense to query the key type and size from the
2140 * secure element, but not all secure elements support this
2141 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2143 return PSA_ERROR_NOT_SUPPORTED;
2144 }
2145 if (psa_get_key_bits(attributes) == 0) {
2146 return PSA_ERROR_NOT_SUPPORTED;
2147 }
Gilles Peskined7729582019-08-05 15:55:54 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2150 &slot, &driver);
2151 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 }
Gilles Peskined7729582019-08-05 15:55:54 +02002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002156
2157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (status != PSA_SUCCESS) {
2159 psa_fail_key_creation(slot, driver);
2160 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002161
Gilles Peskined7729582019-08-05 15:55:54 +02002162 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 psa_close_key(key);
2164 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002165}
2166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2169 const psa_key_attributes_t *specified_attributes,
2170 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002171{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002173 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002174 psa_key_slot_t *source_slot = NULL;
2175 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002177 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302178 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002179
Ronald Cron81709fc2020-11-14 12:10:32 +01002180 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2181
Archana8a180362021-07-05 02:18:48 +05302182 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2184 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_validate_optional_attributes(source_slot,
2189 specified_attributes);
2190 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002193
Archana9d17bf42021-09-10 06:22:44 +05302194 /* The target key type and number of bits have been validated by
2195 * psa_validate_optional_attributes() to be either equal to zero or
2196 * equal to the ones of the source key. So it is safe to inherit
2197 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302198 * */
Archana9d17bf42021-09-10 06:22:44 +05302199 actual_attributes.core.bits = source_slot->attr.bits;
2200 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302201
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 status = psa_restrict_key_policy(source_slot->attr.type,
2204 &actual_attributes.core.policy,
2205 &source_slot->attr.policy);
2206 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2211 &target_slot, &driver);
2212 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 }
2215 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2216 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302217 /*
Archana9d17bf42021-09-10 06:22:44 +05302218 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302219 * the source key would need to be exported as plaintext and re-imported
2220 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302221 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302222 * appropriate API invocations from the application, if needed.
2223 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002224 status = PSA_ERROR_NOT_SUPPORTED;
2225 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002226 }
Archana8a180362021-07-05 02:18:48 +05302227 /*
2228 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302229 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302230 * - For opaque keys this translates to an invocation of the drivers'
2231 * copy_key entry point through the dispatch layer.
2232 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2234 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2235 &storage_size);
2236 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 }
Archana374fe5b2021-09-08 15:50:28 +05302239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2241 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 }
Archana374fe5b2021-09-08 15:50:28 +05302244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 status = psa_driver_wrapper_copy_key(&actual_attributes,
2246 source_slot->key.data,
2247 source_slot->key.bytes,
2248 target_slot->key.data,
2249 target_slot->key.bytes,
2250 &target_slot->key.bytes);
2251 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302252 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 }
2254 } else {
2255 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302256 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 source_slot->key.bytes);
2258 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 }
Archana8a180362021-07-05 02:18:48 +05302261 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (status != PSA_SUCCESS) {
2265 psa_fail_key_creation(target_slot, driver);
2266 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002267
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002271}
2272
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002273
2274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002275/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002276/* Message digests */
2277/****************************************************************/
2278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002281 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (operation->id == 0) {
2283 return PSA_SUCCESS;
2284 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002287 operation->id = 0;
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290}
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2293 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002296
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002297 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002299 status = PSA_ERROR_BAD_STATE;
2300 goto exit;
2301 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 status = PSA_ERROR_INVALID_ARGUMENT;
2305 goto exit;
2306 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002307
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002308 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2309 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002313
2314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (status != PSA_SUCCESS) {
2316 psa_hash_abort(operation);
2317 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002318
2319 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320}
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2323 const uint8_t *input,
2324 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002329 status = PSA_ERROR_BAD_STATE;
2330 goto exit;
2331 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002332
Steven Cooremanc8288352021-03-04 14:02:19 +01002333 /* Don't require hash implementations to behave correctly on a
2334 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (input_length == 0) {
2336 return PSA_SUCCESS;
2337 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002340
2341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (status != PSA_SUCCESS) {
2343 psa_hash_abort(operation);
2344 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347}
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2350 uint8_t *hash,
2351 size_t hash_size,
2352 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002354 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (operation->id == 0) {
2356 return PSA_ERROR_BAD_STATE;
2357 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358
Steven Cooreman1e582352021-02-18 17:24:37 +01002359 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 operation, hash, hash_size, hash_length);
2361 psa_hash_abort(operation);
2362 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363}
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2366 const uint8_t *hash,
2367 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368{
Ronald Cron69a63422021-10-18 09:47:58 +02002369 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002371 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 operation,
2373 actual_hash, sizeof(actual_hash),
2374 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002377 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002381 status = PSA_ERROR_INVALID_SIGNATURE;
2382 goto exit;
2383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002386 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002388
2389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2391 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002392 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396}
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398psa_status_t psa_hash_compute(psa_algorithm_t alg,
2399 const uint8_t *input, size_t input_length,
2400 uint8_t *hash, size_t hash_size,
2401 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002402{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002403 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (!PSA_ALG_IS_HASH(alg)) {
2405 return PSA_ERROR_INVALID_ARGUMENT;
2406 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2409 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002410}
2411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412psa_status_t psa_hash_compare(psa_algorithm_t alg,
2413 const uint8_t *input, size_t input_length,
2414 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002415{
Ronald Cron69a63422021-10-18 09:47:58 +02002416 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002417 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (!PSA_ALG_IS_HASH(alg)) {
2420 return PSA_ERROR_INVALID_ARGUMENT;
2421 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002422
Steven Cooreman1e582352021-02-18 17:24:37 +01002423 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 alg, input, input_length,
2425 actual_hash, sizeof(actual_hash),
2426 &actual_hash_length);
2427 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002428 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 }
2430 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002431 status = PSA_ERROR_INVALID_SIGNATURE;
2432 goto exit;
2433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002435 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002437
2438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2440 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002441}
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2444 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002445{
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (source_operation->id == 0 ||
2447 target_operation->id != 0) {
2448 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002449 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2452 target_operation);
2453 if (status != PSA_SUCCESS) {
2454 psa_hash_abort(target_operation);
2455 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002458}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459
2460
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462/* MAC */
2463/****************************************************************/
2464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002466{
Steven Cooremane6804192021-03-19 18:28:56 +01002467 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (operation->id == 0) {
2469 return PSA_SUCCESS;
2470 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002473 operation->mac_size = 0;
2474 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002475 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002478}
2479
Ronald Cron2dff3b22021-06-17 16:33:22 +02002480static psa_status_t psa_mac_finalize_alg_and_key_validation(
2481 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002482 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002485 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 psa_key_type_t key_type = psa_get_key_type(attributes);
2487 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (!PSA_ALG_IS_MAC(alg)) {
2490 return PSA_ERROR_INVALID_ARGUMENT;
2491 }
Steven Cooremane6804192021-03-19 18:28:56 +01002492
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002493 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 status = psa_mac_key_can_do(alg, key_type);
2495 if (status != PSA_SUCCESS) {
2496 return status;
2497 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002498
Steven Cooremand1a68f12021-05-10 11:13:41 +02002499 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002503 /* A very short MAC is too short for security since it can be
2504 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2505 * so we make this our minimum, even though 32 bits is still
2506 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002508 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2511 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002512 /* It's impossible to "truncate" to a larger length than the full length
2513 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002515 }
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002518 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2519 * that is disabled in the compile-time configuration. The result can
2520 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2521 * configuration into account. In this case, force a return of
2522 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2523 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2524 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2525 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2526 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002528 }
2529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002531}
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2534 mbedtls_svc_key_id_t key,
2535 psa_algorithm_t alg,
2536 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002537{
2538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2539 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002540 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002541 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542
2543 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002545 status = PSA_ERROR_BAD_STATE;
2546 goto exit;
2547 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548
2549 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 key,
2551 &slot,
2552 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2553 alg);
2554 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002558 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 .core = slot->attr
2560 };
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2563 &operation->mac_size);
2564 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567
2568 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002569 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (is_sign) {
2571 status = psa_driver_wrapper_mac_sign_setup(operation,
2572 &attributes,
2573 slot->key.data,
2574 slot->key.bytes,
2575 alg);
2576 } else {
2577 status = psa_driver_wrapper_mac_verify_setup(operation,
2578 &attributes,
2579 slot->key.data,
2580 slot->key.bytes,
2581 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002583
Gilles Peskinefbfac682018-07-08 20:51:54 +02002584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (status != PSA_SUCCESS) {
2586 psa_mac_abort(operation);
2587 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592}
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2595 mbedtls_svc_key_id_t key,
2596 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002597{
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002599}
2600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2602 mbedtls_svc_key_id_t key,
2603 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002604{
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002606}
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2609 const uint8_t *input,
2610 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002611{
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (operation->id == 0) {
2613 return PSA_ERROR_BAD_STATE;
2614 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002616 /* Don't require hash implementations to behave correctly on a
2617 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (input_length == 0) {
2619 return PSA_SUCCESS;
2620 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2623 input, input_length);
2624 if (status != PSA_SUCCESS) {
2625 psa_mac_abort(operation);
2626 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629}
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2632 uint8_t *mac,
2633 size_t mac_size,
2634 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002635{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2637 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002640 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002641 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002642 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002645 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002646 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002647 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002648
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002649 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2650 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002652 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002653 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002654 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002657 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002658 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002659 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 status = psa_driver_wrapper_mac_sign_finish(operation,
2662 mac, operation->mac_size,
2663 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002664
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002665exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002666 /* In case of success, set the potential excess room in the output buffer
2667 * to an invalid value, to avoid potentially leaking a longer MAC.
2668 * In case of error, set the output length and content to a safe default,
2669 * such that in case the caller misses an error check, the output would be
2670 * an unachievable MAC.
2671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002673 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002674 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002675 }
mohammad16036df908f2018-04-02 08:34:15 -07002676
Paul Elliottdc42ca82023-02-24 18:11:59 +00002677 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002682}
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2685 const uint8_t *mac,
2686 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002688 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2689 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002692 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002693 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002694 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002697 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002698 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002699 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002703 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002704 }
2705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 status = psa_driver_wrapper_mac_verify_finish(operation,
2707 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002708
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002709exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713}
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2716 psa_algorithm_t alg,
2717 const uint8_t *input,
2718 size_t input_length,
2719 uint8_t *mac,
2720 size_t mac_size,
2721 size_t *mac_length,
2722 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723{
2724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002725 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2726 psa_key_slot_t *slot;
2727 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002728 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729
Ronald Cron51131b52021-06-17 17:17:20 +02002730 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 key,
2732 &slot,
2733 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2734 alg);
2735 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002739 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002740 .core = slot->attr
2741 };
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2744 &operation_mac_size);
2745 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002750 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002751 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002752 }
2753
2754 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 &attributes,
2756 slot->key.data, slot->key.bytes,
2757 alg,
2758 input, input_length,
2759 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002760
2761exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002762 /* In case of success, set the potential excess room in the output buffer
2763 * to an invalid value, to avoid potentially leaking a longer MAC.
2764 * In case of error, set the output length and content to a safe default,
2765 * such that in case the caller misses an error check, the output would be
2766 * an unachievable MAC.
2767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002769 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002770 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002771 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002772
2773 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778}
2779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002781 psa_algorithm_t alg,
2782 const uint8_t *input,
2783 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 uint8_t *mac,
2785 size_t mac_size,
2786 size_t *mac_length)
2787{
2788 return psa_mac_compute_internal(key, alg,
2789 input, input_length,
2790 mac, mac_size, mac_length, 1);
2791}
2792
2793psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2794 psa_algorithm_t alg,
2795 const uint8_t *input,
2796 size_t input_length,
2797 const uint8_t *mac,
2798 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002799{
2800 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002801 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2802 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 status = psa_mac_compute_internal(key, alg,
2805 input, input_length,
2806 actual_mac, sizeof(actual_mac),
2807 &actual_mac_length, 0);
2808 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002809 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002813 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002815 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002817 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002818 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002819 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002820
2821exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002825}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002826
Gilles Peskinee59236f2018-01-27 23:32:46 +01002827/****************************************************************/
2828/* Asymmetric cryptography */
2829/****************************************************************/
2830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2832 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002833{
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (input_is_message) {
2835 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2836 return PSA_ERROR_INVALID_ARGUMENT;
2837 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2840 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2841 return PSA_ERROR_INVALID_ARGUMENT;
2842 }
2843 }
2844 } else {
2845 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2846 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002847 }
2848 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002851}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2854 int input_is_message,
2855 psa_algorithm_t alg,
2856 const uint8_t *input,
2857 size_t input_length,
2858 uint8_t *signature,
2859 size_t signature_size,
2860 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002861{
2862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2863 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2864 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002865 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002866
2867 *signature_length = 0;
2868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 status = psa_sign_verify_check_alg(input_is_message, alg);
2870 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002871 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002874 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002875 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002876 * buffer can in principle be empty since it doesn't actually have
2877 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (signature_size == 0) {
2879 return PSA_ERROR_BUFFER_TOO_SMALL;
2880 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002881
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002882 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 key, &slot,
2884 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2885 PSA_KEY_USAGE_SIGN_HASH,
2886 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002889 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002893 status = PSA_ERROR_INVALID_ARGUMENT;
2894 goto exit;
2895 }
2896
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002897 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899 };
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002902 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002903 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002904 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 signature, signature_size, signature_length);
2906 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002907
2908 status = psa_driver_wrapper_sign_hash(
2909 &attributes, slot->key.data, slot->key.bytes,
2910 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 }
2913
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914
2915exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002916 psa_wipe_tag_output_buffer(signature, status, signature_size,
2917 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922}
2923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2925 int input_is_message,
2926 psa_algorithm_t alg,
2927 const uint8_t *input,
2928 size_t input_length,
2929 const uint8_t *signature,
2930 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931{
2932 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2933 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2934 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 status = psa_sign_verify_check_alg(input_is_message, alg);
2937 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002938 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002940
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002941 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 key, &slot,
2943 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2944 PSA_KEY_USAGE_VERIFY_HASH,
2945 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (status != PSA_SUCCESS) {
2948 return status;
2949 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950
2951 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953 };
2954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002956 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002957 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002958 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 signature, signature_length);
2960 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002961 status = psa_driver_wrapper_verify_hash(
2962 &attributes, slot->key.data, slot->key.bytes,
2963 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002965 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002970
2971}
2972
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002973psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002974 const psa_key_attributes_t *attributes,
2975 const uint8_t *key_buffer,
2976 size_t key_buffer_size,
2977 psa_algorithm_t alg,
2978 const uint8_t *input,
2979 size_t input_length,
2980 uint8_t *signature,
2981 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002983{
2984 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002987 size_t hash_length;
2988 uint8_t hash[PSA_HASH_MAX_SIZE];
2989
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002990 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 PSA_ALG_SIGN_GET_HASH(alg),
2992 input, input_length,
2993 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002996 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002998
2999 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 attributes, key_buffer, key_buffer_size,
3001 alg, hash, hash_length,
3002 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003003 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003006}
3007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3009 psa_algorithm_t alg,
3010 const uint8_t *input,
3011 size_t input_length,
3012 uint8_t *signature,
3013 size_t signature_size,
3014 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003015{
3016 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003017 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003019}
3020
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003021psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003022 const psa_key_attributes_t *attributes,
3023 const uint8_t *key_buffer,
3024 size_t key_buffer_size,
3025 psa_algorithm_t alg,
3026 const uint8_t *input,
3027 size_t input_length,
3028 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003030{
3031 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003034 size_t hash_length;
3035 uint8_t hash[PSA_HASH_MAX_SIZE];
3036
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003037 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ALG_SIGN_GET_HASH(alg),
3039 input, input_length,
3040 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003043 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003045
3046 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 attributes, key_buffer, key_buffer_size,
3048 alg, hash, hash_length,
3049 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003050 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003053}
3054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3056 psa_algorithm_t alg,
3057 const uint8_t *input,
3058 size_t input_length,
3059 const uint8_t *signature,
3060 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003061{
3062 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003063 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003065}
3066
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003067psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003068 const psa_key_attributes_t *attributes,
3069 const uint8_t *key_buffer, size_t key_buffer_size,
3070 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003072{
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3074 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3075 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003076#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3078 return mbedtls_psa_rsa_sign_hash(
3079 attributes,
3080 key_buffer, key_buffer_size,
3081 alg, hash, hash_length,
3082 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003083#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3084 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 } else {
3086 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3089 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003090#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3092 return mbedtls_psa_ecdsa_sign_hash(
3093 attributes,
3094 key_buffer, key_buffer_size,
3095 alg, hash, hash_length,
3096 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003097#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3098 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 } else {
3100 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003101 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003102 }
Ronald Cron4501c982021-03-19 20:09:32 +01003103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 (void) key_buffer;
3105 (void) key_buffer_size;
3106 (void) hash;
3107 (void) hash_length;
3108 (void) signature;
3109 (void) signature_size;
3110 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003113}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3116 psa_algorithm_t alg,
3117 const uint8_t *hash,
3118 size_t hash_length,
3119 uint8_t *signature,
3120 size_t signature_size,
3121 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003122{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003123 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003124 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003126}
3127
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003128psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003129 const psa_key_attributes_t *attributes,
3130 const uint8_t *key_buffer, size_t key_buffer_size,
3131 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003133{
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3135 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3136 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003137#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3139 return mbedtls_psa_rsa_verify_hash(
3140 attributes,
3141 key_buffer, key_buffer_size,
3142 alg, hash, hash_length,
3143 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003144#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3145 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 } else {
3147 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3150 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003151#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3153 return mbedtls_psa_ecdsa_verify_hash(
3154 attributes,
3155 key_buffer, key_buffer_size,
3156 alg, hash, hash_length,
3157 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003158#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3159 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 } else {
3161 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003162 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003163 }
Ronald Cron4501c982021-03-19 20:09:32 +01003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 (void) key_buffer;
3166 (void) key_buffer_size;
3167 (void) hash;
3168 (void) hash_length;
3169 (void) signature;
3170 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003173}
3174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3176 psa_algorithm_t alg,
3177 const uint8_t *hash,
3178 size_t hash_length,
3179 const uint8_t *signature,
3180 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003181{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003182 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003183 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003185}
3186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3188 psa_algorithm_t alg,
3189 const uint8_t *input,
3190 size_t input_length,
3191 const uint8_t *salt,
3192 size_t salt_length,
3193 uint8_t *output,
3194 size_t output_size,
3195 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003198 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003199 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003200 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003201
Darryl Green5cc689a2018-07-24 15:34:10 +01003202 (void) input;
3203 (void) input_length;
3204 (void) salt;
3205 (void) output;
3206 (void) output_size;
3207
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003208 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3211 return PSA_ERROR_INVALID_ARGUMENT;
3212 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003213
Ronald Cron5c522922020-11-14 16:35:34 +01003214 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3216 if (status != PSA_SUCCESS) {
3217 return status;
3218 }
3219 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3220 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003221 status = PSA_ERROR_INVALID_ARGUMENT;
3222 goto exit;
3223 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003225 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003227 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003228
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003229 status = psa_driver_wrapper_asymmetric_encrypt(
3230 &attributes, slot->key.data, slot->key.bytes,
3231 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003237}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3240 psa_algorithm_t alg,
3241 const uint8_t *input,
3242 size_t input_length,
3243 const uint8_t *salt,
3244 size_t salt_length,
3245 uint8_t *output,
3246 size_t output_size,
3247 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003249 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003250 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003251 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003252 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003253
Darryl Green5cc689a2018-07-24 15:34:10 +01003254 (void) input;
3255 (void) input_length;
3256 (void) salt;
3257 (void) output;
3258 (void) output_size;
3259
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003260 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3263 return PSA_ERROR_INVALID_ARGUMENT;
3264 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003265
Ronald Cron5c522922020-11-14 16:35:34 +01003266 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3268 if (status != PSA_SUCCESS) {
3269 return status;
3270 }
3271 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003272 status = PSA_ERROR_INVALID_ARGUMENT;
3273 goto exit;
3274 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003276 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003278 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003280 status = psa_driver_wrapper_asymmetric_decrypt(
3281 &attributes, slot->key.data, slot->key.bytes,
3282 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003284
3285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003287
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003289}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003290
Paul Elliott9fe12f62022-11-30 19:16:02 +00003291/****************************************************************/
3292/* Asymmetric interruptible cryptography */
3293/****************************************************************/
3294
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003295static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3296
Paul Elliott9fe12f62022-11-30 19:16:02 +00003297void psa_interruptible_set_max_ops(uint32_t max_ops)
3298{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003299 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003300}
3301
3302uint32_t psa_interruptible_get_max_ops(void)
3303{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003304 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003305}
3306
Paul Elliott9fe12f62022-11-30 19:16:02 +00003307uint32_t psa_sign_hash_get_num_ops(
3308 const psa_sign_hash_interruptible_operation_t *operation)
3309{
Paul Elliott296ede92022-12-15 17:00:30 +00003310 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003311}
3312
3313uint32_t psa_verify_hash_get_num_ops(
3314 const psa_verify_hash_interruptible_operation_t *operation)
3315{
Paul Elliott296ede92022-12-15 17:00:30 +00003316 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003317}
3318
Paul Elliott59ad9452022-12-18 15:09:02 +00003319static psa_status_t psa_sign_hash_abort_internal(
3320 psa_sign_hash_interruptible_operation_t *operation)
3321{
3322 if (operation->id == 0) {
3323 /* The object has (apparently) been initialized but it is not (yet)
3324 * in use. It's ok to call abort on such an object, and there's
3325 * nothing to do. */
3326 return PSA_SUCCESS;
3327 }
3328
3329 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3330
3331 status = psa_driver_wrapper_sign_hash_abort(operation);
3332
3333 operation->id = 0;
3334
Paul Elliotte6145dc2023-02-07 12:51:21 +00003335 /* Do not clear either the error_occurred or num_ops elements here as they
3336 * only want to be cleared by the application calling abort, not by abort
3337 * being called at completion of an operation. */
3338
Paul Elliott59ad9452022-12-18 15:09:02 +00003339 return status;
3340}
3341
Paul Elliott9fe12f62022-11-30 19:16:02 +00003342psa_status_t psa_sign_hash_start(
3343 psa_sign_hash_interruptible_operation_t *operation,
3344 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3345 const uint8_t *hash, size_t hash_length)
3346{
3347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3348 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3349 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003350 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351
Paul Elliottc9774412023-02-06 15:14:07 +00003352 /* Check that start has not been previously called, or operation has not
3353 * previously errored. */
3354 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355 return PSA_ERROR_BAD_STATE;
3356 }
3357
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358 status = psa_sign_verify_check_alg(0, alg);
3359 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003360 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361 return status;
3362 }
3363
3364 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3365 PSA_KEY_USAGE_SIGN_HASH,
3366 alg);
3367
3368 if (status != PSA_SUCCESS) {
3369 goto exit;
3370 }
3371
3372 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3373 status = PSA_ERROR_INVALID_ARGUMENT;
3374 goto exit;
3375 }
3376
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003377 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378 .core = slot->attr
3379 };
3380
Paul Elliott296ede92022-12-15 17:00:30 +00003381 /* Ensure ops count gets reset, in case of operation re-use. */
3382 operation->num_ops = 0;
3383
Paul Elliott9fe12f62022-11-30 19:16:02 +00003384 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3385 slot->key.data,
3386 slot->key.bytes, alg,
3387 hash, hash_length);
3388exit:
3389
3390 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003391 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003392 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393 }
3394
3395 unlock_status = psa_unlock_key_slot(slot);
3396
Paul Elliottc9774412023-02-06 15:14:07 +00003397 if (unlock_status != PSA_SUCCESS) {
3398 operation->error_occurred = 1;
3399 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400
Paul Elliottc9774412023-02-06 15:14:07 +00003401 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402}
3403
3404
3405psa_status_t psa_sign_hash_complete(
3406 psa_sign_hash_interruptible_operation_t *operation,
3407 uint8_t *signature, size_t signature_size,
3408 size_t *signature_length)
3409{
3410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3411
3412 *signature_length = 0;
3413
Paul Elliottc9774412023-02-06 15:14:07 +00003414 /* Check that start has been called first, and that operation has not
3415 * previously errored. */
3416 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417 status = PSA_ERROR_BAD_STATE;
3418 goto exit;
3419 }
3420
Paul Elliott096abc42023-02-03 18:33:23 +00003421 /* Immediately reject a zero-length signature buffer. This guarantees that
3422 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 if (signature_size == 0) {
3424 status = PSA_ERROR_BUFFER_TOO_SMALL;
3425 goto exit;
3426 }
3427
3428 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3429 signature_size,
3430 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431
Paul Elliott296ede92022-12-15 17:00:30 +00003432 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003433 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003434
Paul Elliottebe225c2023-02-10 14:32:53 +00003435exit:
3436
Paul Elliott59200a22023-02-21 15:07:40 +00003437 psa_wipe_tag_output_buffer(signature, status, signature_size,
3438 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003439
Paul Elliott53bb3122023-02-10 14:22:22 +00003440 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003441 if (status != PSA_SUCCESS) {
3442 operation->error_occurred = 1;
3443 }
3444
Paul Elliott59ad9452022-12-18 15:09:02 +00003445 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003446 }
3447
3448 return status;
3449}
3450
3451psa_status_t psa_sign_hash_abort(
3452 psa_sign_hash_interruptible_operation_t *operation)
3453{
Paul Elliott59ad9452022-12-18 15:09:02 +00003454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3455
3456 status = psa_sign_hash_abort_internal(operation);
3457
3458 /* We clear the number of ops done here, so that it is not cleared when
3459 * the operation fails or succeeds, only on manual abort. */
3460 operation->num_ops = 0;
3461
Paul Elliottc9774412023-02-06 15:14:07 +00003462 /* Likewise, failure state. */
3463 operation->error_occurred = 0;
3464
Paul Elliott59ad9452022-12-18 15:09:02 +00003465 return status;
3466}
3467
3468static psa_status_t psa_verify_hash_abort_internal(
3469 psa_verify_hash_interruptible_operation_t *operation)
3470{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471 if (operation->id == 0) {
3472 /* The object has (apparently) been initialized but it is not (yet)
3473 * in use. It's ok to call abort on such an object, and there's
3474 * nothing to do. */
3475 return PSA_SUCCESS;
3476 }
3477
Paul Elliott59ad9452022-12-18 15:09:02 +00003478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3479
3480 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481
3482 operation->id = 0;
3483
Paul Elliotte6145dc2023-02-07 12:51:21 +00003484 /* Do not clear either the error_occurred or num_ops elements here as they
3485 * only want to be cleared by the application calling abort, not by abort
3486 * being called at completion of an operation. */
3487
Paul Elliott59ad9452022-12-18 15:09:02 +00003488 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003489}
3490
3491psa_status_t psa_verify_hash_start(
3492 psa_verify_hash_interruptible_operation_t *operation,
3493 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3494 const uint8_t *hash, size_t hash_length,
3495 const uint8_t *signature, size_t signature_length)
3496{
3497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3498 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3499 psa_key_slot_t *slot;
3500
Paul Elliottc9774412023-02-06 15:14:07 +00003501 /* Check that start has not been previously called, or operation has not
3502 * previously errored. */
3503 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003504 return PSA_ERROR_BAD_STATE;
3505 }
3506
3507 status = psa_sign_verify_check_alg(0, alg);
3508 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003509 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003510 return status;
3511 }
3512
3513 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3514 PSA_KEY_USAGE_VERIFY_HASH,
3515 alg);
3516
3517 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003518 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519 return status;
3520 }
3521
3522 psa_key_attributes_t attributes = {
3523 .core = slot->attr
3524 };
3525
Paul Elliott296ede92022-12-15 17:00:30 +00003526 /* Ensure ops count gets reset, in case of operation re-use. */
3527 operation->num_ops = 0;
3528
Paul Elliott9fe12f62022-11-30 19:16:02 +00003529 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3530 slot->key.data,
3531 slot->key.bytes,
3532 alg, hash, hash_length,
3533 signature, signature_length);
3534
3535 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003536 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003537 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003538 }
3539
3540 unlock_status = psa_unlock_key_slot(slot);
3541
Paul Elliottc9774412023-02-06 15:14:07 +00003542 if (unlock_status != PSA_SUCCESS) {
3543 operation->error_occurred = 1;
3544 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003545
Paul Elliottc9774412023-02-06 15:14:07 +00003546 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003547}
3548
3549psa_status_t psa_verify_hash_complete(
3550 psa_verify_hash_interruptible_operation_t *operation)
3551{
3552 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3553
Paul Elliottc9774412023-02-06 15:14:07 +00003554 /* Check that start has been called first, and that operation has not
3555 * previously errored. */
3556 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557 status = PSA_ERROR_BAD_STATE;
3558 goto exit;
3559 }
3560
3561 status = psa_driver_wrapper_verify_hash_complete(operation);
3562
Paul Elliott296ede92022-12-15 17:00:30 +00003563 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003564 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003565 operation);
3566
Paul Elliottebe225c2023-02-10 14:32:53 +00003567exit:
3568
Paul Elliott9fe12f62022-11-30 19:16:02 +00003569 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003570 if (status != PSA_SUCCESS) {
3571 operation->error_occurred = 1;
3572 }
3573
Paul Elliott59ad9452022-12-18 15:09:02 +00003574 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003575 }
3576
3577 return status;
3578}
3579
3580psa_status_t psa_verify_hash_abort(
3581 psa_verify_hash_interruptible_operation_t *operation)
3582{
Paul Elliott59ad9452022-12-18 15:09:02 +00003583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003584
Paul Elliott59ad9452022-12-18 15:09:02 +00003585 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586
Paul Elliott59ad9452022-12-18 15:09:02 +00003587 /* We clear the number of ops done here, so that it is not cleared when
3588 * the operation fails or succeeds, only on manual abort. */
3589 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003590
Paul Elliottc9774412023-02-06 15:14:07 +00003591 /* Likewise, failure state. */
3592 operation->error_occurred = 0;
3593
Paul Elliott59ad9452022-12-18 15:09:02 +00003594 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003595}
3596
Paul Elliott588f8ed2022-12-02 18:10:26 +00003597/****************************************************************/
3598/* Asymmetric interruptible cryptography internal */
3599/* implementations */
3600/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003601
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3603{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003604
Paul Elliott588f8ed2022-12-02 18:10:26 +00003605#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3606 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3607 defined(MBEDTLS_ECP_RESTARTABLE)
3608
3609 /* Internal implementation uses zero to indicate infinite number max ops,
3610 * therefore avoid this value, and set to minimum possible. */
3611 if (max_ops == 0) {
3612 max_ops = 1;
3613 }
3614
Paul Elliott588f8ed2022-12-02 18:10:26 +00003615 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003616#else
3617 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3619 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3620 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3621}
3622
Paul Elliott588f8ed2022-12-02 18:10:26 +00003623uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003624 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625{
3626#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3627 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3628 defined(MBEDTLS_ECP_RESTARTABLE)
3629
Paul Elliott93d9ca82023-02-15 18:14:21 +00003630 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003631#else
3632 (void) operation;
3633 return 0;
3634#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3635 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3636 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3637}
3638
3639uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003640 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641{
3642 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3643 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3644 defined(MBEDTLS_ECP_RESTARTABLE)
3645
Paul Elliott93d9ca82023-02-15 18:14:21 +00003646 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003647#else
3648 (void) operation;
3649 return 0;
3650#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3651 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3652 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3653}
3654
3655psa_status_t mbedtls_psa_sign_hash_start(
3656 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3657 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3658 size_t key_buffer_size, psa_algorithm_t alg,
3659 const uint8_t *hash, size_t hash_length)
3660{
3661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003662 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663
Paul Elliott068fe072023-01-16 13:59:15 +00003664 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3665 return PSA_ERROR_NOT_SUPPORTED;
3666 }
3667
3668 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003669 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003670 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671
3672#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003673 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3674 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
Paul Elliotta1c94092023-02-15 16:38:04 +00003676 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3677
Paul Elliott93d9ca82023-02-15 18:14:21 +00003678 /* Ensure num_ops is zero'ed in case of context re-use. */
3679 operation->num_ops = 0;
3680
Paul Elliott068fe072023-01-16 13:59:15 +00003681 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3682 attributes->core.bits,
3683 key_buffer,
3684 key_buffer_size,
3685 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003686
Paul Elliott068fe072023-01-16 13:59:15 +00003687 if (status != PSA_SUCCESS) {
3688 return status;
3689 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690
Paul Elliott1bc59df2023-02-05 13:41:57 +00003691 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003692 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003693
Paul Elliott068fe072023-01-16 13:59:15 +00003694 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003695 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003696 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003697
Paul Elliott0290a762023-02-09 14:30:24 +00003698 /* We only need to store the same length of hash as the private key size
3699 * here, it would be truncated by the internal implementation anyway. */
3700 required_hash_length = (hash_length < operation->coordinate_bytes ?
3701 hash_length : operation->coordinate_bytes);
3702
Paul Elliottba70ad42023-02-15 18:23:53 +00003703 if (required_hash_length > sizeof(operation->hash)) {
3704 /* Shouldn't happen, but better safe than sorry. */
3705 return PSA_ERROR_CORRUPTION_DETECTED;
3706 }
3707
Paul Elliott0290a762023-02-09 14:30:24 +00003708 memcpy(operation->hash, hash, required_hash_length);
3709 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003710
3711 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713#else
Paul Elliott068fe072023-01-16 13:59:15 +00003714 (void) operation;
3715 (void) key_buffer;
3716 (void) key_buffer_size;
3717 (void) alg;
3718 (void) hash;
3719 (void) hash_length;
3720 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003721 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliott068fe072023-01-16 13:59:15 +00003723 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3725 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3726 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727}
3728
3729psa_status_t mbedtls_psa_sign_hash_complete(
3730 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3731 uint8_t *signature, size_t signature_size,
3732 size_t *signature_length)
3733{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3735 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3736 defined(MBEDTLS_ECP_RESTARTABLE)
3737
Paul Elliotta1c94092023-02-15 16:38:04 +00003738 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003739 mbedtls_mpi r;
3740 mbedtls_mpi s;
3741
Paul Elliott46845252023-02-03 14:59:11 +00003742 mbedtls_mpi_init(&r);
3743 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003745 /* Ensure max_ops is set to the current value (or default). */
3746 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3747
Paul Elliotta1c94092023-02-15 16:38:04 +00003748 if (signature_size < 2 * operation->coordinate_bytes) {
3749 status = PSA_ERROR_BUFFER_TOO_SMALL;
3750 goto exit;
3751 }
3752
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003754
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3756 status = mbedtls_to_psa_error(
3757 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003758 &r,
3759 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760 &operation->ctx->d,
3761 operation->hash,
3762 operation->hash_length,
3763 operation->md_alg,
3764 mbedtls_psa_get_random,
3765 MBEDTLS_PSA_RANDOM_STATE,
3766 &operation->restart_ctx));
3767#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003768 status = PSA_ERROR_NOT_SUPPORTED;
3769 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3771 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772 status = mbedtls_to_psa_error(
3773 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003774 &r,
3775 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776 &operation->ctx->d,
3777 operation->hash,
3778 operation->hash_length,
3779 mbedtls_psa_get_random,
3780 MBEDTLS_PSA_RANDOM_STATE,
3781 mbedtls_psa_get_random,
3782 MBEDTLS_PSA_RANDOM_STATE,
3783 &operation->restart_ctx));
3784 }
3785
Paul Elliottf8e5b562023-02-19 18:43:45 +00003786 /* Hide the fact that the restart context only holds a delta of number of
3787 * ops done during the last operation, not an absolute value. */
3788 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3789
Paul Elliott724bd252023-02-08 12:35:08 +00003790 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003792 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003794 operation->coordinate_bytes)
3795 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796
3797 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003798 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003799 }
3800
3801 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003802 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003804 operation->coordinate_bytes,
3805 operation->coordinate_bytes)
3806 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807
3808 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003809 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810 }
3811
Paul Elliott1bc59df2023-02-05 13:41:57 +00003812 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813
Paul Elliott724bd252023-02-08 12:35:08 +00003814 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815 }
Paul Elliott724bd252023-02-08 12:35:08 +00003816
3817exit:
3818
3819 mbedtls_mpi_free(&r);
3820 mbedtls_mpi_free(&s);
3821 return status;
3822
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823 #else
3824
3825 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 (void) signature;
3827 (void) signature_size;
3828 (void) signature_length;
3829
3830 return PSA_ERROR_NOT_SUPPORTED;
3831
3832#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3833 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3834 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3835}
3836
3837psa_status_t mbedtls_psa_sign_hash_abort(
3838 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3839{
3840
3841#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3842 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3843 defined(MBEDTLS_ECP_RESTARTABLE)
3844
3845 if (operation->ctx) {
3846 mbedtls_ecdsa_free(operation->ctx);
3847 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003848 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003849 }
3850
3851 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3852
Paul Elliott93d9ca82023-02-15 18:14:21 +00003853 operation->num_ops = 0;
3854
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855 return PSA_SUCCESS;
3856
3857#else
3858
3859 (void) operation;
3860
3861 return PSA_ERROR_NOT_SUPPORTED;
3862
3863#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3864 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3865 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3866}
3867
3868psa_status_t mbedtls_psa_verify_hash_start(
3869 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3870 const psa_key_attributes_t *attributes,
3871 const uint8_t *key_buffer, size_t key_buffer_size,
3872 psa_algorithm_t alg,
3873 const uint8_t *hash, size_t hash_length,
3874 const uint8_t *signature, size_t signature_length)
3875{
3876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003877 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003878 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003879
Paul Elliott068fe072023-01-16 13:59:15 +00003880 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3881 return PSA_ERROR_NOT_SUPPORTED;
3882 }
3883
3884 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003885 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003886 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887
3888#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003889 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3890 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891
Paul Elliotta1c94092023-02-15 16:38:04 +00003892 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3893 mbedtls_mpi_init(&operation->r);
3894 mbedtls_mpi_init(&operation->s);
3895
Paul Elliott93d9ca82023-02-15 18:14:21 +00003896 /* Ensure num_ops is zero'ed in case of context re-use. */
3897 operation->num_ops = 0;
3898
Paul Elliott068fe072023-01-16 13:59:15 +00003899 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3900 attributes->core.bits,
3901 key_buffer,
3902 key_buffer_size,
3903 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003904
Paul Elliott068fe072023-01-16 13:59:15 +00003905 if (status != PSA_SUCCESS) {
3906 return status;
3907 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003908
Paul Elliottc569fc22023-02-10 13:02:54 +00003909 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910
Paul Elliott1bc59df2023-02-05 13:41:57 +00003911 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003912 return PSA_ERROR_INVALID_SIGNATURE;
3913 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003914
Paul Elliott068fe072023-01-16 13:59:15 +00003915 status = mbedtls_to_psa_error(
3916 mbedtls_mpi_read_binary(&operation->r,
3917 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003918 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919
Paul Elliott068fe072023-01-16 13:59:15 +00003920 if (status != PSA_SUCCESS) {
3921 return status;
3922 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003923
Paul Elliott068fe072023-01-16 13:59:15 +00003924 status = mbedtls_to_psa_error(
3925 mbedtls_mpi_read_binary(&operation->s,
3926 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003927 coordinate_bytes,
3928 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003929
Paul Elliott068fe072023-01-16 13:59:15 +00003930 if (status != PSA_SUCCESS) {
3931 return status;
3932 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003933
Paul Elliott2c9843f2023-02-15 17:32:42 +00003934 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003935
Paul Elliott2c9843f2023-02-15 17:32:42 +00003936 if (status != PSA_SUCCESS) {
3937 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003938 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003939
Paul Elliott0290a762023-02-09 14:30:24 +00003940 /* We only need to store the same length of hash as the private key size
3941 * here, it would be truncated by the internal implementation anyway. */
3942 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3943 coordinate_bytes);
3944
Paul Elliottba70ad42023-02-15 18:23:53 +00003945 if (required_hash_length > sizeof(operation->hash)) {
3946 /* Shouldn't happen, but better safe than sorry. */
3947 return PSA_ERROR_CORRUPTION_DETECTED;
3948 }
3949
Paul Elliott0290a762023-02-09 14:30:24 +00003950 memcpy(operation->hash, hash, required_hash_length);
3951 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003952
3953 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954#else
Paul Elliott068fe072023-01-16 13:59:15 +00003955 (void) operation;
3956 (void) key_buffer;
3957 (void) key_buffer_size;
3958 (void) alg;
3959 (void) hash;
3960 (void) hash_length;
3961 (void) signature;
3962 (void) signature_length;
3963 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003964 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003965 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliott068fe072023-01-16 13:59:15 +00003967 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003968#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3969 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3970 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971}
3972
3973psa_status_t mbedtls_psa_verify_hash_complete(
3974 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3975{
3976
3977#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3978 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3979 defined(MBEDTLS_ECP_RESTARTABLE)
3980
Paul Elliottf8e5b562023-02-19 18:43:45 +00003981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3982
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003983 /* Ensure max_ops is set to the current value (or default). */
3984 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3985
Paul Elliottf8e5b562023-02-19 18:43:45 +00003986 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003987 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3988 operation->hash,
3989 operation->hash_length,
3990 &operation->ctx->Q,
3991 &operation->r,
3992 &operation->s,
3993 &operation->restart_ctx));
3994
Paul Elliottf8e5b562023-02-19 18:43:45 +00003995 /* Hide the fact that the restart context only holds a delta of number of
3996 * ops done during the last operation, not an absolute value. */
3997 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3998
3999 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004000#else
4001 (void) operation;
4002
4003 return PSA_ERROR_NOT_SUPPORTED;
4004
4005#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4006 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4007 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4008}
4009
4010psa_status_t mbedtls_psa_verify_hash_abort(
4011 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4012{
4013
4014#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4015 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4016 defined(MBEDTLS_ECP_RESTARTABLE)
4017
4018 if (operation->ctx) {
4019 mbedtls_ecdsa_free(operation->ctx);
4020 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004021 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004022 }
4023
4024 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4025
Paul Elliott93d9ca82023-02-15 18:14:21 +00004026 operation->num_ops = 0;
4027
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028 mbedtls_mpi_free(&operation->r);
4029 mbedtls_mpi_free(&operation->s);
4030
4031 return PSA_SUCCESS;
4032
4033#else
4034 (void) operation;
4035
4036 return PSA_ERROR_NOT_SUPPORTED;
4037
4038#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4039 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4040 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4041}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004042
mohammad1603503973b2018-03-12 15:59:30 +02004043/****************************************************************/
4044/* Symmetric cryptography */
4045/****************************************************************/
4046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4048 mbedtls_svc_key_id_t key,
4049 psa_algorithm_t alg,
4050 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004051{
4052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4053 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004054 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4056 PSA_KEY_USAGE_ENCRYPT :
4057 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004058 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004059
4060 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004062 status = PSA_ERROR_BAD_STATE;
4063 goto exit;
4064 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004067 status = PSA_ERROR_INVALID_ARGUMENT;
4068 goto exit;
4069 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4072 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004075
Ronald Cron49fafa92021-03-10 08:34:23 +01004076 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004077 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004078 * so we only set it (in the driver wrapper) after resources have been
4079 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004080 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004082 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004084 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 }
4086 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004087
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004088 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004090 };
4091
Ronald Cronab99ac22020-10-01 16:38:28 +02004092 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 if (cipher_operation == MBEDTLS_ENCRYPT) {
4094 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4095 &attributes,
4096 slot->key.data,
4097 slot->key.bytes,
4098 alg);
4099 } else {
4100 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4101 &attributes,
4102 slot->key.data,
4103 slot->key.bytes,
4104 alg);
4105 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004106
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (status != PSA_SUCCESS) {
4109 psa_cipher_abort(operation);
4110 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004115}
4116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4118 mbedtls_svc_key_id_t key,
4119 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004120{
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004122}
4123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4125 mbedtls_svc_key_id_t key,
4126 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004127{
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004129}
4130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4132 uint8_t *iv,
4133 size_t iv_size,
4134 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004135{
Ronald Cron6d051732020-10-01 14:10:20 +02004136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004137 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4138 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004141 status = PSA_ERROR_BAD_STATE;
4142 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004143 }
4144
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004146 status = PSA_ERROR_BAD_STATE;
4147 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004149
Ronald Cron2fb90522021-07-05 12:31:44 +02004150 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004152 status = PSA_ERROR_BUFFER_TOO_SMALL;
4153 goto exit;
4154 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004157 status = PSA_ERROR_GENERIC_ERROR;
4158 goto exit;
4159 }
4160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 status = psa_generate_random(local_iv, default_iv_length);
4162 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 }
Ronald Cron5618a392021-03-26 09:52:26 +01004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 status = psa_driver_wrapper_cipher_set_iv(operation,
4167 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004168
4169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (status == PSA_SUCCESS) {
4171 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004172 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004173 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004175 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004177 }
Ronald Cron6d051732020-10-01 14:10:20 +02004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004180}
4181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4183 const uint8_t *iv,
4184 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004185{
Ronald Cron6d051732020-10-01 14:10:20 +02004186 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004189 status = PSA_ERROR_BAD_STATE;
4190 goto exit;
4191 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004194 status = PSA_ERROR_BAD_STATE;
4195 goto exit;
4196 }
Ronald Crona0d68172021-03-26 10:15:08 +01004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004199 status = PSA_ERROR_INVALID_ARGUMENT;
4200 goto exit;
4201 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 status = psa_driver_wrapper_cipher_set_iv(operation,
4204 iv,
4205 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004206
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004209 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 } else {
4211 psa_cipher_abort(operation);
4212 }
4213 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004214}
4215
Gilles Peskine449bd832023-01-11 14:50:10 +01004216psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4217 const uint8_t *input,
4218 size_t input_length,
4219 uint8_t *output,
4220 size_t output_size,
4221 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004222{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004226 status = PSA_ERROR_BAD_STATE;
4227 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004228 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004231 status = PSA_ERROR_BAD_STATE;
4232 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004233 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 status = psa_driver_wrapper_cipher_update(operation,
4236 input,
4237 input_length,
4238 output,
4239 output_size,
4240 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004241
4242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 if (status != PSA_SUCCESS) {
4244 psa_cipher_abort(operation);
4245 }
Ronald Cron6d051732020-10-01 14:10:20 +02004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004248}
4249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4251 uint8_t *output,
4252 size_t output_size,
4253 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004254{
David Saadab4ecc272019-02-14 13:48:10 +02004255 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004258 status = PSA_ERROR_BAD_STATE;
4259 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004260 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004263 status = PSA_ERROR_BAD_STATE;
4264 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004265 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 status = psa_driver_wrapper_cipher_finish(operation,
4268 output,
4269 output_size,
4270 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004271
4272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (status == PSA_SUCCESS) {
4274 return psa_cipher_abort(operation);
4275 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004276 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004280 }
mohammad1603503973b2018-03-12 15:59:30 +02004281}
4282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004284{
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004286 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004287 * in use. It's ok to call abort on such an object, and there's
4288 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004290 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004293
Ronald Cron49fafa92021-03-10 08:34:23 +01004294 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004295 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004296 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004299}
4300
Gilles Peskine449bd832023-01-11 14:50:10 +01004301psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4302 psa_algorithm_t alg,
4303 const uint8_t *input,
4304 size_t input_length,
4305 uint8_t *output,
4306 size_t output_size,
4307 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004308{
4309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004310 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004311 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004312 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4313 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004314 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004317 status = PSA_ERROR_INVALID_ARGUMENT;
4318 goto exit;
4319 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4322 PSA_KEY_USAGE_ENCRYPT,
4323 alg);
4324 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004327
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004328 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004330 };
4331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4333 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004334 status = PSA_ERROR_GENERIC_ERROR;
4335 goto exit;
4336 }
4337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 if (default_iv_length > 0) {
4339 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004340 status = PSA_ERROR_BUFFER_TOO_SMALL;
4341 goto exit;
4342 }
4343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 status = psa_generate_random(local_iv, default_iv_length);
4345 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004348 }
4349
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004350 status = psa_driver_wrapper_cipher_encrypt(
4351 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004352 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004353 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004355
4356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 unlock_status = psa_unlock_key_slot(slot);
4358 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004359 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004360 }
Ronald Cron23919522021-07-08 19:00:07 +02004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 if (status == PSA_SUCCESS) {
4363 if (default_iv_length > 0) {
4364 memcpy(output, local_iv, default_iv_length);
4365 }
4366 *output_length += default_iv_length;
4367 } else {
4368 *output_length = 0;
4369 }
4370
4371 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004372}
4373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4375 psa_algorithm_t alg,
4376 const uint8_t *input,
4377 size_t input_length,
4378 uint8_t *output,
4379 size_t output_size,
4380 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004381{
4382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004383 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004384 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004385 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004388 status = PSA_ERROR_INVALID_ARGUMENT;
4389 goto exit;
4390 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4393 PSA_KEY_USAGE_DECRYPT,
4394 alg);
4395 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004396 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004398
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004399 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004401 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4404 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004405 status = PSA_ERROR_INVALID_ARGUMENT;
4406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004408 status = PSA_ERROR_INVALID_ARGUMENT;
4409 goto exit;
4410 }
4411
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004412 status = psa_driver_wrapper_cipher_decrypt(
4413 &attributes, slot->key.data, slot->key.bytes,
4414 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004416
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 unlock_status = psa_unlock_key_slot(slot);
4419 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004420 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004424 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 }
Ronald Cron23919522021-07-08 19:00:07 +02004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004428}
4429
4430
mohammad16035955c982018-04-26 00:53:03 +03004431/****************************************************************/
4432/* AEAD */
4433/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004434
Paul Elliottbaff51c2021-09-28 17:44:45 +01004435/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004436static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004437{
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004439}
4440
4441/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004442static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4443 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004444{
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004448#if defined(PSA_WANT_ALG_GCM)
4449 case PSA_ALG_GCM:
4450 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 * arbitrarily large nonces. Please note that we do not generally
4452 * recommend the usage of nonces of greater length than
4453 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4454 * size, which can then lead to collisions if you encrypt a very
4455 * large number of messages.*/
4456 if (nonce_length != 0) {
4457 return PSA_SUCCESS;
4458 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004459 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004460#endif /* PSA_WANT_ALG_GCM */
4461#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004462 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (nonce_length >= 7 && nonce_length <= 13) {
4464 return PSA_SUCCESS;
4465 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004466 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004467#endif /* PSA_WANT_ALG_CCM */
4468#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004469 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (nonce_length == 12) {
4471 return PSA_SUCCESS;
4472 } else if (nonce_length == 8) {
4473 return PSA_ERROR_NOT_SUPPORTED;
4474 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004475 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004476#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004477 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004478 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004480 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004483}
4484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004486{
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4488 return PSA_ERROR_INVALID_ARGUMENT;
4489 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004492}
4493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4495 psa_algorithm_t alg,
4496 const uint8_t *nonce,
4497 size_t nonce_length,
4498 const uint8_t *additional_data,
4499 size_t additional_data_length,
4500 const uint8_t *plaintext,
4501 size_t plaintext_length,
4502 uint8_t *ciphertext,
4503 size_t ciphertext_size,
4504 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004505{
Ronald Cron215633c2021-03-16 17:15:37 +01004506 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4507 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004508
mohammad1603f08a5502018-06-03 15:05:47 +03004509 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 status = psa_aead_check_algorithm(alg);
4512 if (status != PSA_SUCCESS) {
4513 return status;
4514 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004515
Ronald Cron9a986162021-03-26 12:40:07 +01004516 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4518 if (status != PSA_SUCCESS) {
4519 return status;
4520 }
mohammad16035955c982018-04-26 00:53:03 +03004521
Ronald Cron215633c2021-03-16 17:15:37 +01004522 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004524 };
mohammad16035955c982018-04-26 00:53:03 +03004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 status = psa_aead_check_nonce_length(alg, nonce_length);
4527 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004528 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004530
Ronald Cronde822812021-03-17 16:08:20 +01004531 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004532 &attributes, slot->key.data, slot->key.bytes,
4533 alg,
4534 nonce, nonce_length,
4535 additional_data, additional_data_length,
4536 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4540 memset(ciphertext, 0, ciphertext_size);
4541 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004542
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004547}
4548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4550 psa_algorithm_t alg,
4551 const uint8_t *nonce,
4552 size_t nonce_length,
4553 const uint8_t *additional_data,
4554 size_t additional_data_length,
4555 const uint8_t *ciphertext,
4556 size_t ciphertext_length,
4557 uint8_t *plaintext,
4558 size_t plaintext_size,
4559 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004560{
Ronald Cron215633c2021-03-16 17:15:37 +01004561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4562 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004563
Gilles Peskineee652a32018-06-01 19:23:52 +02004564 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 status = psa_aead_check_algorithm(alg);
4567 if (status != PSA_SUCCESS) {
4568 return status;
4569 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004570
Ronald Cron9a986162021-03-26 12:40:07 +01004571 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4573 if (status != PSA_SUCCESS) {
4574 return status;
4575 }
mohammad16035955c982018-04-26 00:53:03 +03004576
Ronald Cron215633c2021-03-16 17:15:37 +01004577 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004579 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 status = psa_aead_check_nonce_length(alg, nonce_length);
4582 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004583 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004585
Ronald Cronde822812021-03-17 16:08:20 +01004586 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004587 &attributes, slot->key.data, slot->key.bytes,
4588 alg,
4589 nonce, nonce_length,
4590 additional_data, additional_data_length,
4591 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if (status != PSA_SUCCESS && plaintext_size != 0) {
4595 memset(plaintext, 0, plaintext_size);
4596 }
mohammad160360a64d02018-06-03 17:20:42 +03004597
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004598exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 return status;
mohammad16035955c982018-04-26 00:53:03 +03004602}
4603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4605{
4606 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004609#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004611 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4613 return PSA_ERROR_INVALID_ARGUMENT;
4614 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004615 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004616#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004617
Przemek Stekiel86679c72022-10-06 17:06:56 +02004618#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004620 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4622 return PSA_ERROR_INVALID_ARGUMENT;
4623 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004624 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004625#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004626
Przemek Stekiel86679c72022-10-06 17:06:56 +02004627#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004629 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (tag_len != 16) {
4631 return PSA_ERROR_INVALID_ARGUMENT;
4632 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004633 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004634#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004635
4636 default:
4637 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004639 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004641}
4642
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004643/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004644static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4645 int is_encrypt,
4646 mbedtls_svc_key_id_t key,
4647 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004648{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004649 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4650 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4651 psa_key_slot_t *slot = NULL;
4652 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004653 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 status = psa_aead_check_algorithm(alg);
4656 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004657 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004661 status = PSA_ERROR_BAD_STATE;
4662 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004663 }
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if (operation->nonce_set || operation->lengths_set ||
4666 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004667 status = PSA_ERROR_BAD_STATE;
4668 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004669 }
4670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004672 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004674 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4678 alg);
4679 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004680 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004683 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684 .core = slot->attr
4685 };
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004688 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if (is_encrypt) {
4692 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4693 &attributes,
4694 slot->key.data,
4695 slot->key.bytes,
4696 alg);
4697 } else {
4698 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4699 &attributes,
4700 slot->key.data,
4701 slot->key.bytes,
4702 alg);
4703 }
4704 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004705 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004709
4710exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004716 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 } else {
4718 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004720
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004722}
4723
Paul Elliott302ff6b2021-04-20 18:10:30 +01004724/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004725psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4726 mbedtls_svc_key_id_t key,
4727 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004728{
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004730}
4731
4732/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4734 mbedtls_svc_key_id_t key,
4735 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004736{
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004738}
4739
4740/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004741psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4742 uint8_t *nonce,
4743 size_t nonce_size,
4744 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004745{
4746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004747 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004748 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004749
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004750 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004753 status = PSA_ERROR_BAD_STATE;
4754 goto exit;
4755 }
4756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004758 status = PSA_ERROR_BAD_STATE;
4759 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760 }
4761
Paul Elliotte193ea82021-10-01 13:00:16 +01004762 /* For CCM, this size may not be correct according to the PSA
4763 * specification. The PSA Crypto 1.0.1 specification states:
4764 *
4765 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4766 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4767 *
4768 * However this restriction that L has to be the smallest integer is not
4769 * applied in practice, and it is not implementable here since the
4770 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4772 operation->alg);
4773 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004774 status = PSA_ERROR_BUFFER_TOO_SMALL;
4775 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776 }
4777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 status = psa_generate_random(local_nonce, required_nonce_size);
4779 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004780 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004784
Paul Elliott39dc6b82021-05-11 19:16:09 +01004785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (status == PSA_SUCCESS) {
4787 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 } else {
4790 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004791 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004794}
4795
4796/* Set the nonce for a multipart authenticated encryption or decryption
4797 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004798psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4799 const uint8_t *nonce,
4800 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004801{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004805 status = PSA_ERROR_BAD_STATE;
4806 goto exit;
4807 }
4808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004810 status = PSA_ERROR_BAD_STATE;
4811 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004812 }
4813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4815 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004816 status = PSA_ERROR_INVALID_ARGUMENT;
4817 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004818 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4821 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004822
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004825 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 } else {
4827 psa_aead_abort(operation);
4828 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831}
4832
4833/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4835 size_t ad_length,
4836 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004837{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004841 status = PSA_ERROR_BAD_STATE;
4842 goto exit;
4843 }
4844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (operation->lengths_set || operation->ad_started ||
4846 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004847 status = PSA_ERROR_BAD_STATE;
4848 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004849 }
4850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004852#if defined(PSA_WANT_ALG_GCM)
4853 case PSA_ALG_GCM:
4854 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 * bits. Without the guard this code will generate warnings on 32bit
4856 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004857#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 if (((uint64_t) ad_length) >> 61 != 0 ||
4859 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004860 status = PSA_ERROR_INVALID_ARGUMENT;
4861 goto exit;
4862 }
Paul Elliott325d3742021-09-27 17:56:28 +01004863#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004864 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004865#endif /* PSA_WANT_ALG_GCM */
4866#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004867 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004869 status = PSA_ERROR_INVALID_ARGUMENT;
4870 goto exit;
4871 }
4872 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004873#endif /* PSA_WANT_ALG_CCM */
4874#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004875 case PSA_ALG_CHACHA20_POLY1305:
4876 /* No length restrictions for ChaChaPoly. */
4877 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004878#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004879 default:
4880 break;
4881 }
Paul Elliott325d3742021-09-27 17:56:28 +01004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4884 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004885
Paul Elliott39dc6b82021-05-11 19:16:09 +01004886exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004888 operation->ad_remaining = ad_length;
4889 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004890 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 } else {
4892 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004893 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004897
4898/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4900 const uint8_t *input,
4901 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004902{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4904
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004906 status = PSA_ERROR_BAD_STATE;
4907 goto exit;
4908 }
4909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911 status = PSA_ERROR_BAD_STATE;
4912 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004913 }
4914
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (operation->lengths_set) {
4916 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004917 status = PSA_ERROR_INVALID_ARGUMENT;
4918 goto exit;
4919 }
4920
4921 operation->ad_remaining -= input_length;
4922 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004923#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004925 status = PSA_ERROR_BAD_STATE;
4926 goto exit;
4927 }
4928#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 status = psa_driver_wrapper_aead_update_ad(operation, input,
4931 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004932
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004935 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 } else {
4937 psa_aead_abort(operation);
4938 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941}
4942
4943/* Encrypt or decrypt a message fragment in an active multipart AEAD
4944 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004945psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4946 const uint8_t *input,
4947 size_t input_length,
4948 uint8_t *output,
4949 size_t output_size,
4950 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004951{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004952 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004953
4954 *output_length = 0;
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004957 status = PSA_ERROR_BAD_STATE;
4958 goto exit;
4959 }
4960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004962 status = PSA_ERROR_BAD_STATE;
4963 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004964 }
4965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004967 /* Additional data length was supplied, but not all the additional
4968 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004970 status = PSA_ERROR_INVALID_ARGUMENT;
4971 goto exit;
4972 }
4973
4974 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004976 status = PSA_ERROR_INVALID_ARGUMENT;
4977 goto exit;
4978 }
4979
4980 operation->body_remaining -= input_length;
4981 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004982#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004984 status = PSA_ERROR_BAD_STATE;
4985 goto exit;
4986 }
4987#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004988
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4990 output, output_size,
4991 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004992
Paul Elliott39dc6b82021-05-11 19:16:09 +01004993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004995 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 } else {
4997 psa_aead_abort(operation);
4998 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005001}
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005004{
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 if (operation->id == 0 || !operation->nonce_set) {
5006 return PSA_ERROR_BAD_STATE;
5007 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5010 operation->body_remaining != 0)) {
5011 return PSA_ERROR_INVALID_ARGUMENT;
5012 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005015}
5016
Paul Elliott302ff6b2021-04-20 18:10:30 +01005017/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5019 uint8_t *ciphertext,
5020 size_t ciphertext_size,
5021 size_t *ciphertext_length,
5022 uint8_t *tag,
5023 size_t tag_size,
5024 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005025{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005026 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5027
Paul Elliott302ff6b2021-04-20 18:10:30 +01005028 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005029 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005030
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 status = psa_aead_final_checks(operation);
5032 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005033 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005037 status = PSA_ERROR_BAD_STATE;
5038 goto exit;
5039 }
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5042 ciphertext_size,
5043 ciphertext_length,
5044 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005045
Paul Elliott39dc6b82021-05-11 19:16:09 +01005046exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005047
5048
Paul Elliottf47b0952021-05-21 18:02:33 +01005049 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005050 * the zero tag size, make sure the tag is set to something implausible.
5051 * Even if the operation succeeds, make sure we clear the rest of the
5052 * buffer to prevent potential leakage of anything previously placed in
5053 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005054 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005059}
5060
5061/* Finish authenticating and decrypting a message in a multipart AEAD
5062 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005063psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5064 uint8_t *plaintext,
5065 size_t plaintext_size,
5066 size_t *plaintext_length,
5067 const uint8_t *tag,
5068 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5071
Paul Elliott302ff6b2021-04-20 18:10:30 +01005072 *plaintext_length = 0;
5073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_aead_final_checks(operation);
5075 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005080 status = PSA_ERROR_BAD_STATE;
5081 goto exit;
5082 }
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5085 plaintext_size,
5086 plaintext_length,
5087 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005088
5089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005091
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005093}
5094
5095/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005096psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005097{
5098 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005101 /* The object has (apparently) been initialized but it is not (yet)
5102 * in use. It's ok to call abort on such an object, and there's
5103 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005105 }
5106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005110
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005112}
5113
Gilles Peskinee59236f2018-01-27 23:32:46 +01005114/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005115/* Generators */
5116/****************************************************************/
5117
Przemek Stekiel69c46792022-06-10 12:59:51 +02005118#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005119 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005120 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305121 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5122 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005123#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005124#endif /* At least one builtin KDF */
5125
Przemek Stekiel69c46792022-06-10 12:59:51 +02005126#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005127 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5128 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5129static psa_status_t psa_key_derivation_start_hmac(
5130 psa_mac_operation_t *operation,
5131 psa_algorithm_t hash_alg,
5132 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005134{
5135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5138 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5139 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005140
Steven Cooreman72f736a2021-05-07 14:14:37 +02005141 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 status = psa_driver_wrapper_mac_sign_setup(operation,
5145 &attributes,
5146 hmac_key, hmac_key_length,
5147 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 psa_reset_key_attributes(&attributes);
5150 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005151}
5152#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005153
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154#define HKDF_STATE_INIT 0 /* no input yet */
5155#define HKDF_STATE_STARTED 1 /* got salt */
5156#define HKDF_STATE_KEYED 2 /* got key */
5157#define HKDF_STATE_OUTPUT 3 /* output started */
5158
Gilles Peskinecbe66502019-05-16 16:59:18 +02005159static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161{
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5163 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5164 } else {
5165 return operation->alg;
5166 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005167}
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005170{
5171 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5173 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005174 /* The object has (apparently) been initialized but it is not
5175 * in use. It's ok to call abort on such an object, and there's
5176 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005178#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5180 mbedtls_free(operation->ctx.hkdf.info);
5181 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5182 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005183#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005184#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5185 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5187 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5188 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5189 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005190 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005192 }
5193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005195 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005197 }
5198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005200 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005202 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005203#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005205 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005207 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005208#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005209 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005210
5211 /* We leave the fields Ai and output_block to be erased safely by the
5212 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005214#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5215 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005216#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5218 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5219 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5220 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005221#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305222#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5223 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305224 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005225 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305226 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305227 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305228
5229 status = PSA_SUCCESS;
5230 } else
5231#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 {
5233 status = PSA_ERROR_BAD_STATE;
5234 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 mbedtls_platform_zeroize(operation, sizeof(*operation));
5236 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005237}
5238
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005241{
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005245 }
5246
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005249}
5250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5252 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005253{
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (operation->alg == 0) {
5255 return PSA_ERROR_BAD_STATE;
5256 }
5257 if (capacity > operation->capacity) {
5258 return PSA_ERROR_INVALID_ARGUMENT;
5259 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262}
5263
Przemek Stekiel69c46792022-06-10 12:59:51 +02005264#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005265/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005266static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5267 psa_algorithm_t kdf_alg,
5268 uint8_t *output,
5269 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005270{
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5272 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005273 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005274 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005275#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005277#else
5278 const uint8_t last_block = 0xff;
5279#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (hkdf->state < HKDF_STATE_KEYED ||
5282 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005283#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005285#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 )) {
5287 return PSA_ERROR_BAD_STATE;
5288 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005289 hkdf->state = HKDF_STATE_OUTPUT;
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005292 /* Copy what remains of the current block */
5293 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 }
5297 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005298 output += n;
5299 output_length -= n;
5300 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005302 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005304 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005305 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005307 * object was corrupted or if this function is called directly
5308 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (hkdf->block_number == last_block) {
5310 return PSA_ERROR_BAD_STATE;
5311 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005312
5313 /* We need a new block */
5314 ++hkdf->block_number;
5315 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5318 hash_alg,
5319 hkdf->prk,
5320 hash_length);
5321 if (status != PSA_SUCCESS) {
5322 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005324
5325 if (hkdf->block_number != 1) {
5326 status = psa_mac_update(&hkdf->hmac,
5327 hkdf->output_block,
5328 hash_length);
5329 if (status != PSA_SUCCESS) {
5330 return status;
5331 }
5332 }
5333 status = psa_mac_update(&hkdf->hmac,
5334 hkdf->info,
5335 hkdf->info_length);
5336 if (status != PSA_SUCCESS) {
5337 return status;
5338 }
5339 status = psa_mac_update(&hkdf->hmac,
5340 &hkdf->block_number, 1);
5341 if (status != PSA_SUCCESS) {
5342 return status;
5343 }
5344 status = psa_mac_sign_finish(&hkdf->hmac,
5345 hkdf->output_block,
5346 sizeof(hkdf->output_block),
5347 &hmac_output_length);
5348 if (status != PSA_SUCCESS) {
5349 return status;
5350 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005351 }
5352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005354}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005355#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005356
John Durkop07cc04a2020-11-16 22:08:34 -08005357#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5358 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005359static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5360 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005362{
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5364 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005365 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5366 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005368
Janos Follath7742fee2019-06-17 12:58:10 +01005369 /* We can't be wanting more output after block 0xff, otherwise
5370 * the capacity check in psa_key_derivation_output_bytes() would have
5371 * prevented this call. It could happen only if the operation
5372 * object was corrupted or if this function is called directly
5373 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 if (tls12_prf->block_number == 0xff) {
5375 return PSA_ERROR_CORRUPTION_DETECTED;
5376 }
Janos Follath7742fee2019-06-17 12:58:10 +01005377
5378 /* We need a new block */
5379 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005380 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005381
5382 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5383 *
5384 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5385 *
5386 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5387 * HMAC_hash(secret, A(2) + seed) +
5388 * HMAC_hash(secret, A(3) + seed) + ...
5389 *
5390 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005391 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005392 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005393 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005394 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005395 * is currently extracted as `output_block` and where i is
5396 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005397 */
5398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 status = psa_key_derivation_start_hmac(&hmac,
5400 hash_alg,
5401 tls12_prf->secret,
5402 tls12_prf->secret_length);
5403 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005404 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005406
5407 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005409 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5410 * the variable seed and in this instance means it in the context of the
5411 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 status = psa_mac_update(&hmac,
5413 tls12_prf->label,
5414 tls12_prf->label_length);
5415 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005416 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 }
5418 status = psa_mac_update(&hmac,
5419 tls12_prf->seed,
5420 tls12_prf->seed_length);
5421 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005422 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
5424 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005425 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5427 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005428 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005430 }
5431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 status = psa_mac_sign_finish(&hmac,
5433 tls12_prf->Ai, hash_length,
5434 &hmac_output_length);
5435 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005436 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 }
5438 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005441
5442 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 status = psa_key_derivation_start_hmac(&hmac,
5444 hash_alg,
5445 tls12_prf->secret,
5446 tls12_prf->secret_length);
5447 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005448 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 }
5450 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5451 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 }
5454 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5455 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 }
5458 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5459 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 }
5462 status = psa_mac_sign_finish(&hmac,
5463 tls12_prf->output_block, hash_length,
5464 &hmac_output_length);
5465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005468
Janos Follath7742fee2019-06-17 12:58:10 +01005469
5470cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 cleanup_status = psa_mac_abort(&hmac);
5472 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005473 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005477}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005478
Janos Follath844eb0e2019-06-19 12:10:49 +01005479static psa_status_t psa_key_derivation_tls12_prf_read(
5480 psa_tls12_prf_key_derivation_t *tls12_prf,
5481 psa_algorithm_t alg,
5482 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005484{
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5486 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005487 psa_status_t status;
5488 uint8_t offset, length;
5489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005491 case PSA_TLS12_PRF_STATE_LABEL_SET:
5492 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5493 break;
5494 case PSA_TLS12_PRF_STATE_OUTPUT:
5495 break;
5496 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005498 }
5499
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005501 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if (tls12_prf->left_in_block == 0) {
5503 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5504 alg);
5505 if (status != PSA_SUCCESS) {
5506 return status;
5507 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005508
5509 continue;
5510 }
5511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005513 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005515 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005517
5518 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005520 output += length;
5521 output_length -= length;
5522 tls12_prf->left_in_block -= length;
5523 }
5524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005526}
John Durkop07cc04a2020-11-16 22:08:34 -08005527#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5528 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005529
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005530#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5531static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5532 psa_tls12_ecjpake_to_pms_t *ecjpake,
5533 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005535{
5536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005537 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (output_length != 32) {
5540 return PSA_ERROR_INVALID_ARGUMENT;
5541 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5544 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5545 &output_size);
5546 if (status != PSA_SUCCESS) {
5547 return status;
5548 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 if (output_size != output_length) {
5551 return PSA_ERROR_GENERIC_ERROR;
5552 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005555}
5556#endif
5557
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305558#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5559static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5560 psa_pbkdf2_key_derivation_t *pbkdf2,
5561 psa_algorithm_t prf_alg,
5562 uint8_t prf_output_length,
5563 psa_key_attributes_t *attributes)
5564{
5565 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305566 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305567 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305568 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305569 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305570 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305571 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305572
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305573 mac_operation.is_sign = 1;
5574 mac_operation.mac_size = prf_output_length;
5575 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305576
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305577 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5578 attributes,
5579 pbkdf2->password,
5580 pbkdf2->password_length,
5581 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305582 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305583 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305584 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305585 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5586 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305587 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305588 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305589 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305590 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305591 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305592 }
5593 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5594 &mac_output_length);
5595 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305596 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305597 }
5598
5599 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305600 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305601 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305602 }
5603
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305604 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605
5606 for (i = 1; i < pbkdf2->input_cost; i++) {
5607 status = psa_driver_wrapper_mac_compute(attributes,
5608 pbkdf2->password,
5609 pbkdf2->password_length,
5610 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305611 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305612 &mac_output_length);
5613 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305614 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305615 }
5616
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305617 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305618 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305619
5620cleanup:
5621 /* Zeroise buffers to clear sensitive data from memory. */
5622 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5623 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305624}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305625
5626static psa_status_t psa_key_derivation_pbkdf2_read(
5627 psa_pbkdf2_key_derivation_t *pbkdf2,
5628 psa_algorithm_t kdf_alg,
5629 uint8_t *output,
5630 size_t output_length)
5631{
5632 psa_status_t status;
5633 psa_algorithm_t prf_alg;
5634 uint8_t prf_output_length;
5635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5636 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5638
5639 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5640 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5641 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5642 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305643 } else {
5644 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305645 }
5646
5647 switch (pbkdf2->state) {
5648 case PSA_PBKDF2_STATE_PASSWORD_SET:
5649 /* Initially we need a new block so bytes_used is equal to block size*/
5650 pbkdf2->bytes_used = prf_output_length;
5651 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5652 break;
5653 case PSA_PBKDF2_STATE_OUTPUT:
5654 break;
5655 default:
5656 return PSA_ERROR_BAD_STATE;
5657 }
5658
5659 while (output_length != 0) {
5660 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5661 if (n > output_length) {
5662 n = (uint8_t) output_length;
5663 }
5664 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5665 output += n;
5666 output_length -= n;
5667 pbkdf2->bytes_used += n;
5668
5669 if (output_length == 0) {
5670 break;
5671 }
5672
5673 /* We need a new block */
5674 pbkdf2->bytes_used = 0;
5675 pbkdf2->block_number++;
5676
5677 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5678 prf_output_length,
5679 &attributes);
5680 if (status != PSA_SUCCESS) {
5681 return status;
5682 }
5683 }
5684
5685 return PSA_SUCCESS;
5686}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305687#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5688
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005689psa_status_t psa_key_derivation_output_bytes(
5690 psa_key_derivation_operation_t *operation,
5691 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005693{
5694 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005700 }
5701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005703 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005704 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005706 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005707 goto exit;
5708 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005710 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005711 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005712 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5713 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005715 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005717 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005718 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005719
Przemek Stekiel69c46792022-06-10 12:59:51 +02005720#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5722 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5723 output, output_length);
5724 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005725#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005726#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5727 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5729 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5730 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5731 kdf_alg, output,
5732 output_length);
5733 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005734#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5735 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005736#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005738 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5740 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005741#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305742#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5743 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305744 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5745 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305746 } else
5747#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005748
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005750 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005752 }
5753
5754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005756 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 * This allows us to differentiate between exhausted operations and
5758 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5759 * operations. */
5760 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766}
5767
David Brown7807bf72021-02-09 16:28:23 -07005768#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005770{
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (data_size >= 8) {
5772 mbedtls_des_key_set_parity(data);
5773 }
5774 if (data_size >= 16) {
5775 mbedtls_des_key_set_parity(data + 8);
5776 }
5777 if (data_size >= 24) {
5778 mbedtls_des_key_set_parity(data + 16);
5779 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005780}
David Brown7807bf72021-02-09 16:28:23 -07005781#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005782
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005783/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 * ECC keys on a Weierstrass elliptic curve require the generation
5785 * of a private key which is an integer
5786 * in the range [1, N - 1], where N is the boundary of the private key domain:
5787 * N is the prime p for Diffie-Hellman, or the order of the
5788 * curve’s base point for ECC.
5789 *
5790 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5791 * This function generates the private key using the following process:
5792 *
5793 * 1. Draw a byte string of length ceiling(m/8) bytes.
5794 * 2. If m is not a multiple of 8, set the most significant
5795 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5796 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5797 * 4. If k > N - 2, discard the result and return to step 1.
5798 * 5. Output k + 1 as the private key.
5799 *
5800 * This method allows compliance to NIST standards, specifically the methods titled
5801 * Key-Pair Generation by Testing Candidates in the following publications:
5802 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5803 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5804 * Diffie-Hellman keys.
5805 *
5806 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5807 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5808 *
5809 * Note: Function allocates memory for *data buffer, so given *data should be
5810 * always NULL.
5811 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005812#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5813#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005814static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 psa_key_slot_t *slot,
5816 size_t bits,
5817 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005818 uint8_t **data
5819 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005820{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005821 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005822 mbedtls_mpi k;
5823 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005826 size_t m;
5827 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 mbedtls_mpi_init(&k);
5830 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005831
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005832 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005834 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005838 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005839 goto cleanup;
5840 }
5841
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005842 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005846
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005847 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005848 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005849 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005850
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005851 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005852
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005853 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005855
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005856 /* Note: This function is always called with *data == NULL and it
5857 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 *data = mbedtls_calloc(1, m_bytes);
5859 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005860 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005861 goto cleanup;
5862 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005865 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005867 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005869
5870 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005872 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005873 * (8 * ceiling(m/8) - m) bits of the first byte in
5874 * the string to zero.
5875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005877 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005878 }
5879
5880 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 * big-endian byte string.
5882 */
5883 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005884
5885 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 * Result of comparison is returned. When it indicates error
5887 * then this function is called again.
5888 */
5889 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890 }
5891
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5894 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005895cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 if (ret != 0) {
5897 status = mbedtls_to_psa_error(ret);
5898 }
5899 if (status != PSA_SUCCESS) {
5900 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901 *data = NULL;
5902 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 mbedtls_mpi_free(&k);
5904 mbedtls_mpi_free(&diff_N_2);
5905 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005906}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005907
5908/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5909 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5910 *
5911 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5912 * draw a 32-byte string and process it as specified in
5913 * Elliptic Curves for Security [RFC7748] §5.
5914 *
5915 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5916 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5917 *
5918 * Note: Function allocates memory for *data buffer, so given *data should be
5919 * always NULL.
5920 */
5921
5922static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5923 size_t bits,
5924 psa_key_derivation_operation_t *operation,
5925 uint8_t **data
5926 )
5927{
5928 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005932 case 255:
5933 output_length = 32;
5934 break;
5935 case 448:
5936 output_length = 56;
5937 break;
5938 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005940 break;
5941 }
5942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (*data == NULL) {
5946 return PSA_ERROR_INSUFFICIENT_MEMORY;
5947 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005952 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005956 case 255:
5957 (*data)[0] &= 248;
5958 (*data)[31] &= 127;
5959 (*data)[31] |= 64;
5960 break;
5961 case 448:
5962 (*data)[0] &= 252;
5963 (*data)[55] |= 128;
5964 break;
5965 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005967 break;
5968 }
5969
5970 return status;
5971}
Valerio Setti86587ab2023-06-27 13:09:30 +02005972#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5973static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5974 psa_key_slot_t *slot, size_t bits,
5975 psa_key_derivation_operation_t *operation, uint8_t **data)
5976{
5977 (void) slot;
5978 (void) bits;
5979 (void) operation;
5980 (void) data;
5981 return PSA_ERROR_NOT_SUPPORTED;
5982}
5983
5984static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5985 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5986{
5987 (void) bits;
5988 (void) operation;
5989 (void) data;
5990 return PSA_ERROR_NOT_SUPPORTED;
5991}
5992#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5993#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005994
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005995static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005996 psa_key_slot_t *slot,
5997 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005999{
6000 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306002 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006004 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6007 return PSA_ERROR_INVALID_ARGUMENT;
6008 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006009
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006010#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006011 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6013 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6014 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006015 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6017 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006018 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 }
6020 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006021 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6023 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006026 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006027 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006028#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006029 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 if (key_type_is_raw_bytes(slot->attr.type)) {
6031 if (bits % 8 != 0) {
6032 return PSA_ERROR_INVALID_ARGUMENT;
6033 }
6034 data = mbedtls_calloc(1, bytes);
6035 if (data == NULL) {
6036 return PSA_ERROR_INSUFFICIENT_MEMORY;
6037 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 status = psa_key_derivation_output_bytes(operation, data, bytes);
6040 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006041 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 }
David Brown12ca5032021-02-11 11:02:00 -07006043#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6045 psa_des_set_key_parity(data, bytes);
6046 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006047#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 } else {
6049 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006050 }
Ronald Crondd04d422020-11-28 15:14:42 +01006051
Ronald Cronbf33c932020-11-28 18:06:53 +01006052 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006053 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006055 };
6056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6058 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6059 &storage_size);
6060 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 }
Archanad8a83dc2021-06-14 10:04:16 +05306063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 status = psa_allocate_buffer_to_slot(slot, storage_size);
6065 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 }
Archanad8a83dc2021-06-14 10:04:16 +05306068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 status = psa_driver_wrapper_import_key(&attributes,
6070 data, bytes,
6071 slot->key.data,
6072 slot->key.bytes,
6073 &slot->key.bytes, &bits);
6074 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006075 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006077
6078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 mbedtls_free(data);
6080 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006081}
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6084 psa_key_derivation_operation_t *operation,
6085 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006086{
6087 psa_status_t status;
6088 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006089 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006090
Ronald Cron81709fc2020-11-14 12:10:32 +01006091 *key = MBEDTLS_SVC_KEY_ID_INIT;
6092
Gilles Peskine0f84d622019-09-12 19:03:13 +02006093 /* Reject any attempt to create a zero-length key so that we don't
6094 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (psa_get_key_bits(attributes) == 0) {
6096 return PSA_ERROR_INVALID_ARGUMENT;
6097 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (operation->alg == PSA_ALG_NONE) {
6100 return PSA_ERROR_BAD_STATE;
6101 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (!operation->can_output_key) {
6104 return PSA_ERROR_NOT_PERMITTED;
6105 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6108 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006111 /* Deriving a key in a secure element is not implemented yet. */
6112 status = PSA_ERROR_NOT_SUPPORTED;
6113 }
6114#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (status == PSA_SUCCESS) {
6116 status = psa_generate_derived_key_internal(slot,
6117 attributes->core.bits,
6118 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006119 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (status == PSA_SUCCESS) {
6121 status = psa_finish_key_creation(slot, driver, key);
6122 }
6123 if (status != PSA_SUCCESS) {
6124 psa_fail_key_creation(slot, driver);
6125 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006128}
6129
Gilles Peskineeab56e42018-07-12 17:12:33 +02006130
6131
6132/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006133/* Key derivation */
6134/****************************************************************/
6135
Steven Cooreman932ffb72021-02-15 12:14:32 +01006136#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006137static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006138{
6139#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6141 return 1;
6142 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006143#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006144#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6146 return 1;
6147 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006148#endif
6149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6151 return 1;
6152 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006153#endif
6154#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6156 return 1;
6157 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006158#endif
6159#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6161 return 1;
6162 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006163#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006164#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6166 return 1;
6167 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006168#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306169#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6170 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6171 return 1;
6172 }
6173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006175}
6176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006178{
6179 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 psa_status_t status = psa_hash_setup(&operation, alg);
6181 psa_hash_abort(&operation);
6182 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006183}
6184
Gilles Peskine969c5d62019-01-16 15:53:06 +01006185static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006186 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006188{
Janos Follath5fe19732019-06-20 15:09:30 +01006189 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6190 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006192
Gilles Peskine969c5d62019-01-16 15:53:06 +01006193 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (!is_kdf_alg_supported(kdf_alg)) {
6195 return PSA_ERROR_NOT_SUPPORTED;
6196 }
John Durkop07cc04a2020-11-16 22:08:34 -08006197
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006198 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006199 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6201 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6202 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6203 if (hash_size == 0) {
6204 return PSA_ERROR_NOT_SUPPORTED;
6205 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006206
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006207 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6208 * we might fail later, which is somewhat unfriendly and potentially
6209 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 psa_status_t status = psa_hash_try_support(hash_alg);
6211 if (status != PSA_SUCCESS) {
6212 return status;
6213 }
6214 } else {
6215 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006216 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6219 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6220 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6221 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006222 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006223#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006224 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6226 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006227 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006229#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6230 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 operation->capacity = 255 * hash_size;
6232 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006233}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006236{
6237#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (alg == PSA_ALG_ECDH) {
6239 return PSA_SUCCESS;
6240 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006241#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006242#if defined(PSA_WANT_ALG_FFDH)
6243 if (alg == PSA_ALG_FFDH) {
6244 return PSA_SUCCESS;
6245 }
6246#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006247 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006249}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006250
6251static int psa_key_derivation_allows_free_form_secret_input(
6252 psa_algorithm_t kdf_alg)
6253{
6254#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6255 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6256 return 0;
6257 }
6258#endif
6259 (void) kdf_alg;
6260 return 1;
6261}
John Durkop07cc04a2020-11-16 22:08:34 -08006262#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6265 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006266{
6267 psa_status_t status;
6268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (operation->alg != 0) {
6270 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006271 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6274 return PSA_ERROR_INVALID_ARGUMENT;
6275 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6276#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6277 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6278 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6279 status = psa_key_agreement_try_support(ka_alg);
6280 if (status != PSA_SUCCESS) {
6281 return status;
6282 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006283 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6284 return PSA_ERROR_INVALID_ARGUMENT;
6285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6287#else
6288 return PSA_ERROR_NOT_SUPPORTED;
6289#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6290 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6291#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6292 status = psa_key_derivation_setup_kdf(operation, alg);
6293#else
6294 return PSA_ERROR_NOT_SUPPORTED;
6295#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6296 } else {
6297 return PSA_ERROR_INVALID_ARGUMENT;
6298 }
6299
6300 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006301 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 }
6303 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006304}
6305
Przemek Stekiel69c46792022-06-10 12:59:51 +02006306#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006307static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6308 psa_algorithm_t kdf_alg,
6309 psa_key_derivation_step_t step,
6310 const uint8_t *data,
6311 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006312{
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006314 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006316 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006317#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6319 return PSA_ERROR_INVALID_ARGUMENT;
6320 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006321#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 if (hkdf->state != HKDF_STATE_INIT) {
6323 return PSA_ERROR_BAD_STATE;
6324 } else {
6325 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6326 hash_alg,
6327 data, data_length);
6328 if (status != PSA_SUCCESS) {
6329 return status;
6330 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006331 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006333 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006334 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006337 /* We shouldn't be in different state as HKDF_EXPAND only allows
6338 * two inputs: SECRET (this case) and INFO which does not modify
6339 * the state. It could happen only if the hkdf
6340 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (hkdf->state != HKDF_STATE_INIT) {
6342 return PSA_ERROR_BAD_STATE;
6343 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006344
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006345 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6347 return PSA_ERROR_INVALID_ARGUMENT;
6348 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 memcpy(hkdf->prk, data, data_length);
6351 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006352#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006353 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006354 /* HKDF: If no salt was provided, use an empty salt.
6355 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006357#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6359 return PSA_ERROR_BAD_STATE;
6360 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006361#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6363 hash_alg,
6364 NULL, 0);
6365 if (status != PSA_SUCCESS) {
6366 return status;
6367 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006368 hkdf->state = HKDF_STATE_STARTED;
6369 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 if (hkdf->state != HKDF_STATE_STARTED) {
6371 return PSA_ERROR_BAD_STATE;
6372 }
6373 status = psa_mac_update(&hkdf->hmac,
6374 data, data_length);
6375 if (status != PSA_SUCCESS) {
6376 return status;
6377 }
6378 status = psa_mac_sign_finish(&hkdf->hmac,
6379 hkdf->prk,
6380 sizeof(hkdf->prk),
6381 &data_length);
6382 if (status != PSA_SUCCESS) {
6383 return status;
6384 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006385 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006386
Gilles Peskine2b522db2019-04-12 15:11:49 +02006387 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006388 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006389#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006391 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006393 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006395#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006396 {
6397 /* Block 0 is empty, and the next block will be
6398 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006400 }
6401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006403 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006404#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6406 return PSA_ERROR_INVALID_ARGUMENT;
6407 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006408#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006409#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6411 hkdf->state == HKDF_STATE_INIT) {
6412 return PSA_ERROR_BAD_STATE;
6413 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006414#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (hkdf->state == HKDF_STATE_OUTPUT) {
6416 return PSA_ERROR_BAD_STATE;
6417 }
6418 if (hkdf->info_set) {
6419 return PSA_ERROR_BAD_STATE;
6420 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006421 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (data_length != 0) {
6423 hkdf->info = mbedtls_calloc(1, data_length);
6424 if (hkdf->info == NULL) {
6425 return PSA_ERROR_INSUFFICIENT_MEMORY;
6426 }
6427 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006428 }
6429 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006431 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006433 }
6434}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006435#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006436
John Durkop07cc04a2020-11-16 22:08:34 -08006437#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6438 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006439static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6440 const uint8_t *data,
6441 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006442{
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6444 return PSA_ERROR_BAD_STATE;
6445 }
Janos Follathf08e2652019-06-13 09:05:41 +01006446
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 if (data_length != 0) {
6448 prf->seed = mbedtls_calloc(1, data_length);
6449 if (prf->seed == NULL) {
6450 return PSA_ERROR_INSUFFICIENT_MEMORY;
6451 }
Janos Follathf08e2652019-06-13 09:05:41 +01006452
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006454 prf->seed_length = data_length;
6455 }
Janos Follathf08e2652019-06-13 09:05:41 +01006456
Gilles Peskine43f958b2020-12-13 14:55:14 +01006457 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006460}
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6463 const uint8_t *data,
6464 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006465{
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6467 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6468 return PSA_ERROR_BAD_STATE;
6469 }
Janos Follath81550542019-06-13 14:26:34 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (data_length != 0) {
6472 prf->secret = mbedtls_calloc(1, data_length);
6473 if (prf->secret == NULL) {
6474 return PSA_ERROR_INSUFFICIENT_MEMORY;
6475 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006478 prf->secret_length = data_length;
6479 }
Janos Follath81550542019-06-13 14:26:34 +01006480
Gilles Peskine43f958b2020-12-13 14:55:14 +01006481 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006484}
6485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6487 const uint8_t *data,
6488 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006489{
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6491 return PSA_ERROR_BAD_STATE;
6492 }
Janos Follath63028dd2019-06-13 09:15:47 +01006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (data_length != 0) {
6495 prf->label = mbedtls_calloc(1, data_length);
6496 if (prf->label == NULL) {
6497 return PSA_ERROR_INSUFFICIENT_MEMORY;
6498 }
Janos Follath63028dd2019-06-13 09:15:47 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006501 prf->label_length = data_length;
6502 }
Janos Follath63028dd2019-06-13 09:15:47 +01006503
Gilles Peskine43f958b2020-12-13 14:55:14 +01006504 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006507}
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6510 psa_key_derivation_step_t step,
6511 const uint8_t *data,
6512 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006513{
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006515 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006517 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006519 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006521 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006523 }
6524}
John Durkop07cc04a2020-11-16 22:08:34 -08006525#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6526 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6527
6528#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6529static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6530 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006531 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006533{
6534 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6536 4 + data_length + prf->other_secret_length :
6537 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6540 return PSA_ERROR_INVALID_ARGUMENT;
6541 }
John Durkop07cc04a2020-11-16 22:08:34 -08006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 uint8_t *pms = mbedtls_calloc(1, pms_len);
6544 if (pms == NULL) {
6545 return PSA_ERROR_INSUFFICIENT_MEMORY;
6546 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006547 uint8_t *cur = pms;
6548
6549 /* pure-PSK:
6550 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006551 *
6552 * The premaster secret is formed as follows: if the PSK is N octets
6553 * long, concatenate a uint16 with the value N, N zero octets, a second
6554 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006555 *
6556 * mixed-PSK:
6557 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6558 * follows: concatenate a uint16 with the length of the other secret,
6559 * the other secret itself, uint16 with the length of PSK, and the
6560 * PSK itself.
6561 * For details please check:
6562 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6563 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6564 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006565 */
6566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6568 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6569 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6570 if (prf->other_secret_length != 0) {
6571 memcpy(cur, prf->other_secret, prf->other_secret_length);
6572 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006573 cur += prf->other_secret_length;
6574 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 } else {
6576 *cur++ = MBEDTLS_BYTE_1(data_length);
6577 *cur++ = MBEDTLS_BYTE_0(data_length);
6578 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006579 cur += data_length;
6580 }
6581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 *cur++ = MBEDTLS_BYTE_1(data_length);
6583 *cur++ = MBEDTLS_BYTE_0(data_length);
6584 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006585 cur += data_length;
6586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006588
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006589 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006591}
Janos Follath6660f0e2019-06-17 08:44:03 +01006592
Przemek Stekiele47201b2022-04-19 13:53:28 +02006593static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6594 psa_tls12_prf_key_derivation_t *prf,
6595 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006597{
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6599 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006600 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006601
6602 if (data_length != 0) {
6603 prf->other_secret = mbedtls_calloc(1, data_length);
6604 if (prf->other_secret == NULL) {
6605 return PSA_ERROR_INSUFFICIENT_MEMORY;
6606 }
6607
6608 memcpy(prf->other_secret, data, data_length);
6609 prf->other_secret_length = data_length;
6610 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006611 prf->other_secret_length = 0;
6612 }
6613
6614 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006617}
6618
Janos Follath6660f0e2019-06-17 08:44:03 +01006619static psa_status_t psa_tls12_prf_psk_to_ms_input(
6620 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006621 psa_key_derivation_step_t step,
6622 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006624{
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006626 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 return psa_tls12_prf_psk_to_ms_set_key(prf,
6628 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006629 break;
6630 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6632 data,
6633 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006634 break;
6635 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006637 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006638
Przemek Stekiele47201b2022-04-19 13:53:28 +02006639 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006640}
John Durkop07cc04a2020-11-16 22:08:34 -08006641#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006642
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006643#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6644static psa_status_t psa_tls12_ecjpake_to_pms_input(
6645 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006646 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006647 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006649{
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6651 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6652 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006653 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006654
6655 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 if (data[0] != 0x04) {
6657 return PSA_ERROR_INVALID_ARGUMENT;
6658 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006659
6660 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006664}
6665#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306666
6667#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6668static psa_status_t psa_pbkdf2_set_input_cost(
6669 psa_pbkdf2_key_derivation_t *pbkdf2,
6670 psa_key_derivation_step_t step,
6671 uint64_t data)
6672{
6673 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6674 return PSA_ERROR_INVALID_ARGUMENT;
6675 }
6676
6677 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6678 return PSA_ERROR_BAD_STATE;
6679 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306680
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306681 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306682 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306683 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306684
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306685 if (data == 0) {
6686 return PSA_ERROR_INVALID_ARGUMENT;
6687 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306688
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306689 pbkdf2->input_cost = data;
6690 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6691
6692 return PSA_SUCCESS;
6693}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306694
6695static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6696 const uint8_t *data,
6697 size_t data_length)
6698{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306699 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6700 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6701 return PSA_ERROR_BAD_STATE;
6702 }
6703
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306704 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6705 pbkdf2->salt = mbedtls_calloc(1, data_length);
6706 if (pbkdf2->salt == NULL) {
6707 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306708 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306709
6710 memcpy(pbkdf2->salt, data, data_length);
6711 pbkdf2->salt_length = data_length;
6712 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306713 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306714
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306715 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6716 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306717 return PSA_ERROR_INSUFFICIENT_MEMORY;
6718 }
6719
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306720 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6721 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306722 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306723 mbedtls_free(pbkdf2->salt);
6724 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306725 }
6726
6727 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6728
6729 return PSA_SUCCESS;
6730}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306731
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306732static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306733 const uint8_t *input,
6734 size_t input_len,
6735 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306736 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306737{
6738 psa_status_t status = PSA_SUCCESS;
6739 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6740 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306741 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306742 } else {
6743 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306744 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306745 }
6746 return status;
6747}
6748
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306749static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306750 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306751 const uint8_t *data,
6752 size_t data_length)
6753{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306754 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306755 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6756 return PSA_ERROR_BAD_STATE;
6757 }
6758
6759 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306760 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6761 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6762 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6763 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306764 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306765 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306766 }
6767
6768 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6769
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306770 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306771}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306772
6773static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306774 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306775 psa_key_derivation_step_t step,
6776 const uint8_t *data,
6777 size_t data_length)
6778{
6779 switch (step) {
6780 case PSA_KEY_DERIVATION_INPUT_SALT:
6781 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6782 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306784 default:
6785 return PSA_ERROR_INVALID_ARGUMENT;
6786 }
6787}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306788#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6789
Gilles Peskineb8965192019-09-24 16:21:10 +02006790/** Check whether the given key type is acceptable for the given
6791 * input step of a key derivation.
6792 *
6793 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6794 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6795 * Both secret and non-secret inputs can alternatively have the type
6796 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6797 * that the input was passed as a buffer rather than via a key object.
6798 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006799static int psa_key_derivation_check_input_type(
6800 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006802{
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006804 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 if (key_type == PSA_KEY_TYPE_DERIVE) {
6806 return PSA_SUCCESS;
6807 }
6808 if (key_type == PSA_KEY_TYPE_NONE) {
6809 return PSA_SUCCESS;
6810 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006811 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006812 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 if (key_type == PSA_KEY_TYPE_DERIVE) {
6814 return PSA_SUCCESS;
6815 }
6816 if (key_type == PSA_KEY_TYPE_NONE) {
6817 return PSA_SUCCESS;
6818 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006819 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006820 case PSA_KEY_DERIVATION_INPUT_LABEL:
6821 case PSA_KEY_DERIVATION_INPUT_SALT:
6822 case PSA_KEY_DERIVATION_INPUT_INFO:
6823 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6825 return PSA_SUCCESS;
6826 }
6827 if (key_type == PSA_KEY_TYPE_NONE) {
6828 return PSA_SUCCESS;
6829 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006830 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306831 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6832 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6833 return PSA_SUCCESS;
6834 }
6835 if (key_type == PSA_KEY_TYPE_DERIVE) {
6836 return PSA_SUCCESS;
6837 }
6838 if (key_type == PSA_KEY_TYPE_NONE) {
6839 return PSA_SUCCESS;
6840 }
6841 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006842 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006844}
6845
Janos Follathb80a94e2019-06-12 15:54:46 +01006846static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006847 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006848 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006849 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006850 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006852{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006853 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006855
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 status = psa_key_derivation_check_input_type(step, key_type);
6857 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006858 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006860
Przemek Stekiel69c46792022-06-10 12:59:51 +02006861#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6863 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6864 step, data, data_length);
6865 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006866#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006867#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6869 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6870 step, data, data_length);
6871 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006872#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6873#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6875 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6876 step, data, data_length);
6877 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006878#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006879#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006881 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6883 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006884#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306885#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6886 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306887 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6888 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306889 } else
6890#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006891 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006892 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006893 (void) data;
6894 (void) data_length;
6895 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006897 }
6898
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (status != PSA_SUCCESS) {
6901 psa_key_derivation_abort(operation);
6902 }
6903 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006904}
6905
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306906static psa_status_t psa_key_derivation_input_integer_internal(
6907 psa_key_derivation_operation_t *operation,
6908 psa_key_derivation_step_t step,
6909 uint64_t value)
6910{
6911 psa_status_t status;
6912 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6913
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306914#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6915 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6916 status = psa_pbkdf2_set_input_cost(
6917 &operation->ctx.pbkdf2, step, value);
6918 } else
6919#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306920 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306921 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306922 (void) value;
6923 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306924 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306925 }
6926
6927 if (status != PSA_SUCCESS) {
6928 psa_key_derivation_abort(operation);
6929 }
6930 return status;
6931}
6932
Janos Follath51f4a0f2019-06-14 11:35:55 +01006933psa_status_t psa_key_derivation_input_bytes(
6934 psa_key_derivation_operation_t *operation,
6935 psa_key_derivation_step_t step,
6936 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006938{
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 return psa_key_derivation_input_internal(operation, step,
6940 PSA_KEY_TYPE_NONE,
6941 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006942}
6943
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306944psa_status_t psa_key_derivation_input_integer(
6945 psa_key_derivation_operation_t *operation,
6946 psa_key_derivation_step_t step,
6947 uint64_t value)
6948{
6949 return psa_key_derivation_input_integer_internal(operation, step, value);
6950}
6951
Janos Follath51f4a0f2019-06-14 11:35:55 +01006952psa_status_t psa_key_derivation_input_key(
6953 psa_key_derivation_operation_t *operation,
6954 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006956{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006959 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006960
Ronald Cron5c522922020-11-14 16:35:34 +01006961 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6963 if (status != PSA_SUCCESS) {
6964 psa_key_derivation_abort(operation);
6965 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006966 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006967
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306968 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6969 * permission to output to a key object. */
6970 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6971 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006972 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 status = psa_key_derivation_input_internal(operation,
6976 step, slot->attr.type,
6977 slot->key.data,
6978 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006979
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006983}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006984
6985
6986
6987/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006988/* Key agreement */
6989/****************************************************************/
6990
Gilles Peskine449bd832023-01-11 14:50:10 +01006991psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6992 const uint8_t *key_buffer,
6993 size_t key_buffer_size,
6994 psa_algorithm_t alg,
6995 const uint8_t *peer_key,
6996 size_t peer_key_length,
6997 uint8_t *shared_secret,
6998 size_t shared_secret_size,
6999 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007000{
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007002#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007003 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7005 key_buffer_size, alg,
7006 peer_key, peer_key_length,
7007 shared_secret,
7008 shared_secret_size,
7009 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007010#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007011
7012#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7013 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007014 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007015 peer_key,
7016 peer_key_length,
7017 key_buffer,
7018 key_buffer_size,
7019 shared_secret,
7020 shared_secret_size,
7021 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007022#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7023
Gilles Peskine01d718c2018-09-18 12:01:02 +02007024 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007025 (void) attributes;
7026 (void) key_buffer;
7027 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007028 (void) peer_key;
7029 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007030 (void) shared_secret;
7031 (void) shared_secret_size;
7032 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007034 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007035}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007036
Aditya Deshpande17845b82022-10-13 17:21:01 +01007037/** Internal function for raw key agreement
7038 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007039 * to the driver's implementation if a driver is present.
7040 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007041 * (psa_key_agreement_raw_builtin).
7042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007043static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7044 psa_key_slot_t *private_key,
7045 const uint8_t *peer_key,
7046 size_t peer_key_length,
7047 uint8_t *shared_secret,
7048 size_t shared_secret_size,
7049 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007050{
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7052 return PSA_ERROR_NOT_SUPPORTED;
7053 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007054
7055 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007057 };
7058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 return psa_driver_wrapper_key_agreement(&attributes,
7060 private_key->key.data,
7061 private_key->key.bytes, alg,
7062 peer_key, peer_key_length,
7063 shared_secret,
7064 shared_secret_size,
7065 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007066}
7067
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007068/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007069 * to potentially free embedded data structures and wipe confidential data.
7070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007071static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7072 psa_key_derivation_step_t step,
7073 psa_key_slot_t *private_key,
7074 const uint8_t *peer_key,
7075 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007076{
7077 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007078 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007079 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007081
7082 /* Step 1: run the secret agreement algorithm to generate the shared
7083 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 status = psa_key_agreement_raw_internal(ka_alg,
7085 private_key,
7086 peer_key, peer_key_length,
7087 shared_secret,
7088 sizeof(shared_secret),
7089 &shared_secret_length);
7090 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007093
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007094 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007095 * the shared secret. A shared secret is permitted wherever a key
7096 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 status = psa_key_derivation_input_internal(operation, step,
7098 PSA_KEY_TYPE_DERIVE,
7099 shared_secret,
7100 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7103 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007104}
7105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7107 psa_key_derivation_step_t step,
7108 mbedtls_svc_key_id_t private_key,
7109 const uint8_t *peer_key,
7110 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007114 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007115
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7117 return PSA_ERROR_INVALID_ARGUMENT;
7118 }
Ronald Cron5c522922020-11-14 16:35:34 +01007119 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7121 if (status != PSA_SUCCESS) {
7122 return status;
7123 }
7124 status = psa_key_agreement_internal(operation, step,
7125 slot,
7126 peer_key, peer_key_length);
7127 if (status != PSA_SUCCESS) {
7128 psa_key_derivation_abort(operation);
7129 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007130 /* If a private key has been added as SECRET, we allow the derived
7131 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007133 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007135 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007140}
7141
Gilles Peskine449bd832023-01-11 14:50:10 +01007142psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7143 mbedtls_svc_key_id_t private_key,
7144 const uint8_t *peer_key,
7145 size_t peer_key_length,
7146 uint8_t *output,
7147 size_t output_size,
7148 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007149{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007151 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007152 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007153 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007154
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007156 status = PSA_ERROR_INVALID_ARGUMENT;
7157 goto exit;
7158 }
Ronald Cron5c522922020-11-14 16:35:34 +01007159 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7161 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007164
Gilles Peskine3e561302022-04-14 00:17:15 +02007165 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7166 * for the output size. The PSA specification only guarantees that this
7167 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7168 * but it might be nice to allow smaller buffers if the output fits.
7169 * At the time of writing this comment, with only ECDH implemented,
7170 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7171 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7172 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007173 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7175 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007176 status = PSA_ERROR_BUFFER_TOO_SMALL;
7177 goto exit;
7178 }
7179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 status = psa_key_agreement_raw_internal(alg, slot,
7181 peer_key, peer_key_length,
7182 output, output_size,
7183 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007184
7185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007187 /* If an error happens and is not handled properly, the output
7188 * may be used as a key to protect sensitive data. Arrange for such
7189 * a key to be random, which is likely to result in decryption or
7190 * verification errors. This is better than filling the buffer with
7191 * some constant data such as zeros, which would result in the data
7192 * being protected with a reproducible, easily knowable key.
7193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007195 *output_length = output_size;
7196 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007197
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007201}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007202
7203
Gilles Peskine30524eb2020-11-13 17:02:26 +01007204
Gilles Peskine86a440b2018-11-12 18:39:40 +01007205/****************************************************************/
7206/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007207/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007208
Gilles Peskine672a7712023-04-28 21:00:28 +02007209#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7210#include "entropy_poll.h"
7211#endif
7212
Gilles Peskine30524eb2020-11-13 17:02:26 +01007213/** Initialize the PSA random generator.
7214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007216{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007217#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007219#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7220
Gilles Peskine30524eb2020-11-13 17:02:26 +01007221 /* Set default configuration if
7222 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007224 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 }
7226 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007231#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7232 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7233 /* The PSA entropy injection feature depends on using NV seed as an entropy
7234 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 mbedtls_entropy_add_source(&rng->entropy,
7236 mbedtls_nv_seed_poll, NULL,
7237 MBEDTLS_ENTROPY_BLOCK_SIZE,
7238 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007239#endif
7240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007242#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007243}
7244
7245/** Deinitialize the PSA random generator.
7246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007247static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007248{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007249#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007251#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7253 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007254#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007255}
7256
7257/** Seed the PSA random generator.
7258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007259static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007260{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007261#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7262 /* Do nothing: the external RNG seeds itself. */
7263 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007265#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007266 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7268 drbg_seed, sizeof(drbg_seed) - 1);
7269 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007270#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007271}
7272
Gilles Peskine449bd832023-01-11 14:50:10 +01007273psa_status_t psa_generate_random(uint8_t *output,
7274 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007275{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007276 GUARD_MODULE_INITIALIZED;
7277
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007278#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7279
7280 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7282 output, output_size,
7283 &output_length);
7284 if (status != PSA_SUCCESS) {
7285 return status;
7286 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007287 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007288 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 if (output_length != output_size) {
7290 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7291 }
7292 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007293
7294#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007297 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7299 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7300 output_size);
7301 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7302 output, request_size);
7303 if (ret != 0) {
7304 return mbedtls_to_psa_error(ret);
7305 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007306 output_size -= request_size;
7307 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007308 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007310#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007311}
7312
Gilles Peskine8814fc42020-12-14 15:33:44 +01007313/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007314 *
7315 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7316 * `psa_generate_random(...)`. The state parameter is ignored since the
7317 * PSA API doesn't support passing an explicit state.
7318 *
7319 * In the non-external case, psa_generate_random() calls an
7320 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7321 * and semantics as mbedtls_psa_get_random(). As an optimization,
7322 * instead of doing this back-and-forth between the PSA API and the
7323 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7324 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007326#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7327int mbedtls_psa_get_random(void *p_rng,
7328 unsigned char *output,
7329 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007330{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007331 /* This function takes a pointer to the RNG state because that's what
7332 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7333 * its own state internally and doesn't let the caller access that state.
7334 * So we just ignore the state parameter, and in practice we'll pass
7335 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007336 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 psa_status_t status = psa_generate_random(output, output_size);
7338 if (status == PSA_SUCCESS) {
7339 return 0;
7340 } else {
7341 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7342 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007343}
7344#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7345
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007346#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007347psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7348 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007349{
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 if (global_data.initialized) {
7351 return PSA_ERROR_NOT_PERMITTED;
7352 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007353
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7355 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7356 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7357 return PSA_ERROR_INVALID_ARGUMENT;
7358 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007361}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007362#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007363
Ronald Cron3772afe2021-02-08 16:10:05 +01007364/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007365 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007366 * \param type The key type
7367 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007368 *
7369 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007370 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007371 * \retval #PSA_ERROR_INVALID_ARGUMENT
7372 * The size in bits of the key is not valid.
7373 * \retval #PSA_ERROR_NOT_SUPPORTED
7374 * The type and/or the size in bits of the key or the combination of
7375 * the two is not supported.
7376 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007377static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007379{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007381
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (key_type_is_raw_bytes(type)) {
7383 status = psa_validate_unstructured_key_bit_size(type, bits);
7384 if (status != PSA_SUCCESS) {
7385 return status;
7386 }
7387 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007388#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7390 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7391 return PSA_ERROR_NOT_SUPPORTED;
7392 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007393 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007394 return PSA_ERROR_NOT_SUPPORTED;
7395 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007396
Ronald Cron01b2aba2020-10-05 09:42:02 +02007397 /* Accept only byte-aligned keys, for the same reasons as
7398 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if (bits % 8 != 0) {
7400 return PSA_ERROR_NOT_SUPPORTED;
7401 }
7402 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007403#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007404
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007405#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007407 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 return PSA_SUCCESS;
7409 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007410#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007411
Valerio Settia55f0422023-07-10 15:34:41 +02007412#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007413 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007414 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007415 return PSA_ERROR_NOT_SUPPORTED;
7416 }
7417 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007418#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007419 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007421 }
7422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007424}
7425
Ronald Cron55ed0592020-10-05 10:30:40 +02007426psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007427 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007429{
7430 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007431 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if ((attributes->domain_parameters == NULL) &&
7434 (attributes->domain_parameters_size != 0)) {
7435 return PSA_ERROR_INVALID_ARGUMENT;
7436 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (key_type_is_raw_bytes(type)) {
7439 status = psa_generate_random(key_buffer, key_buffer_size);
7440 if (status != PSA_SUCCESS) {
7441 return status;
7442 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007443
David Brown12ca5032021-02-11 11:02:00 -07007444#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (type == PSA_KEY_TYPE_DES) {
7446 psa_des_set_key_parity(key_buffer, key_buffer_size);
7447 }
David Brown8107e312021-02-12 08:08:46 -07007448#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007450
Valerio Setti76df8c12023-07-11 14:11:28 +02007451#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7453 return mbedtls_psa_rsa_generate_key(attributes,
7454 key_buffer,
7455 key_buffer_size,
7456 key_buffer_length);
7457 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007458#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007459
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007460#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7462 return mbedtls_psa_ecp_generate_key(attributes,
7463 key_buffer,
7464 key_buffer_size,
7465 key_buffer_length);
7466 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007467#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007468
Valerio Settia55f0422023-07-10 15:34:41 +02007469#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007470 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7471 return mbedtls_psa_ffdh_generate_key(attributes,
7472 key_buffer,
7473 key_buffer_size,
7474 key_buffer_length);
7475 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007476#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007477 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 (void) key_buffer_length;
7479 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007480 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007483}
Darryl Green0c6575a2018-11-07 16:05:30 +00007484
Gilles Peskine449bd832023-01-11 14:50:10 +01007485psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7486 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007487{
7488 psa_status_t status;
7489 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007490 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007491 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007492
Ronald Cron81709fc2020-11-14 12:10:32 +01007493 *key = MBEDTLS_SVC_KEY_ID_INIT;
7494
Gilles Peskine0f84d622019-09-12 19:03:13 +02007495 /* Reject any attempt to create a zero-length key so that we don't
7496 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 if (psa_get_key_bits(attributes) == 0) {
7498 return PSA_ERROR_INVALID_ARGUMENT;
7499 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007500
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007501 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7503 return PSA_ERROR_INVALID_ARGUMENT;
7504 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7507 &slot, &driver);
7508 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 }
Gilles Peskine11792082019-08-06 18:36:36 +02007511
Ronald Cron977c2472020-10-13 08:32:21 +02007512 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307513 * storage ( thus not in the case of generating a key in a secure element
7514 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7515 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 if (slot->key.data == NULL) {
7517 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7518 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007519 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 attributes->core.type, attributes->core.bits);
7521 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007524
7525 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 attributes->core.type,
7527 attributes->core.bits);
7528 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007529 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 attributes, &key_buffer_size);
7531 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 }
Ronald Cron977c2472020-10-13 08:32:21 +02007534 }
Ronald Cron977c2472020-10-13 08:32:21 +02007535
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7537 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007538 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 }
Ronald Cron977c2472020-10-13 08:32:21 +02007540 }
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 status = psa_driver_wrapper_generate_key(attributes,
7543 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007544
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 if (status != PSA_SUCCESS) {
7546 psa_remove_key_data_from_memory(slot);
7547 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007548
Gilles Peskine11792082019-08-06 18:36:36 +02007549exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 if (status == PSA_SUCCESS) {
7551 status = psa_finish_key_creation(slot, driver, key);
7552 }
7553 if (status != PSA_SUCCESS) {
7554 psa_fail_key_creation(slot, driver);
7555 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007556
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007558}
7559
Gilles Peskinee59236f2018-01-27 23:32:46 +01007560/****************************************************************/
7561/* Module setup */
7562/****************************************************************/
7563
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007564#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007565psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 void (* entropy_init)(mbedtls_entropy_context *ctx),
7567 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007568{
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7570 return PSA_ERROR_BAD_STATE;
7571 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007572 global_data.rng.entropy_init = entropy_init;
7573 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007575}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007576#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007579{
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 psa_wipe_all_key_slots();
7581 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7582 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007583 }
7584 /* Wipe all remaining data, including configuration.
7585 * In particular, this sets all state indicator to the value
7586 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007588
7589 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007591}
7592
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007593#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7594/** Recover a transaction that was interrupted by a power failure.
7595 *
7596 * This function is called during initialization, before psa_crypto_init()
7597 * returns. If this function returns a failure status, the initialization
7598 * fails.
7599 */
7600static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007602{
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007604 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7605 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 /* TODO - fall through to the failure case until this
7607 * is implemented.
7608 * https://github.com/ARMmbed/mbed-crypto/issues/218
7609 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007610 default:
7611 /* We found an unsupported transaction in the storage.
7612 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007614 }
7615}
7616#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007619{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007620 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007621
Gilles Peskinec6b69072018-11-20 21:42:52 +01007622 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 if (global_data.initialized != 0) {
7624 return PSA_SUCCESS;
7625 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007626
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007627 /* Init drivers */
7628 status = psa_driver_wrapper_init();
7629 if (status != PSA_SUCCESS) {
7630 goto exit;
7631 }
7632 global_data.drivers_initialized = 1;
7633
Gilles Peskine30524eb2020-11-13 17:02:26 +01007634 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007636 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 status = mbedtls_psa_random_seed(&global_data.rng);
7638 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007641 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 status = psa_initialize_key_slots();
7644 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007647
Gilles Peskine4b734222019-07-24 15:56:31 +02007648#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 status = psa_crypto_load_transaction();
7650 if (status == PSA_SUCCESS) {
7651 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7652 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007653 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 }
7655 status = psa_crypto_stop_transaction();
7656 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007657 /* There's no transaction to complete. It's all good. */
7658 status = PSA_SUCCESS;
7659 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007660#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007661
Gilles Peskinec6b69072018-11-20 21:42:52 +01007662 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007663 global_data.initialized = 1;
7664
7665exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 if (status != PSA_SUCCESS) {
7667 mbedtls_psa_crypto_free();
7668 }
7669 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007670}
7671
Yanray Wangffc3c482023-07-11 12:01:04 +08007672#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007673psa_status_t psa_crypto_driver_pake_get_password_len(
7674 const psa_crypto_driver_pake_inputs_t *inputs,
7675 size_t *password_len)
7676{
7677 if (inputs->password_len == 0) {
7678 return PSA_ERROR_BAD_STATE;
7679 }
7680
7681 *password_len = inputs->password_len;
7682
7683 return PSA_SUCCESS;
7684}
7685
7686psa_status_t psa_crypto_driver_pake_get_password(
7687 const psa_crypto_driver_pake_inputs_t *inputs,
7688 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7689{
7690 if (inputs->password_len == 0) {
7691 return PSA_ERROR_BAD_STATE;
7692 }
7693
7694 if (buffer_size < inputs->password_len) {
7695 return PSA_ERROR_BUFFER_TOO_SMALL;
7696 }
7697
7698 memcpy(buffer, inputs->password, inputs->password_len);
7699 *buffer_length = inputs->password_len;
7700
7701 return PSA_SUCCESS;
7702}
7703
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007704psa_status_t psa_crypto_driver_pake_get_user_len(
7705 const psa_crypto_driver_pake_inputs_t *inputs,
7706 size_t *user_len)
7707{
7708 if (inputs->user_len == 0) {
7709 return PSA_ERROR_BAD_STATE;
7710 }
7711
7712 *user_len = inputs->user_len;
7713
7714 return PSA_SUCCESS;
7715}
7716
7717psa_status_t psa_crypto_driver_pake_get_user(
7718 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007719 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007720{
7721 if (inputs->user_len == 0) {
7722 return PSA_ERROR_BAD_STATE;
7723 }
7724
Przemek Stekielc0e62502023-03-14 11:49:36 +01007725 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007726 return PSA_ERROR_BUFFER_TOO_SMALL;
7727 }
7728
Przemek Stekielc0e62502023-03-14 11:49:36 +01007729 memcpy(user_id, inputs->user, inputs->user_len);
7730 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007731
7732 return PSA_SUCCESS;
7733}
7734
7735psa_status_t psa_crypto_driver_pake_get_peer_len(
7736 const psa_crypto_driver_pake_inputs_t *inputs,
7737 size_t *peer_len)
7738{
7739 if (inputs->peer_len == 0) {
7740 return PSA_ERROR_BAD_STATE;
7741 }
7742
7743 *peer_len = inputs->peer_len;
7744
7745 return PSA_SUCCESS;
7746}
7747
7748psa_status_t psa_crypto_driver_pake_get_peer(
7749 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007750 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007751{
7752 if (inputs->peer_len == 0) {
7753 return PSA_ERROR_BAD_STATE;
7754 }
7755
Przemek Stekielc0e62502023-03-14 11:49:36 +01007756 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007757 return PSA_ERROR_BUFFER_TOO_SMALL;
7758 }
7759
Przemek Stekielc0e62502023-03-14 11:49:36 +01007760 memcpy(peer_id, inputs->peer, inputs->peer_len);
7761 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007762
7763 return PSA_SUCCESS;
7764}
7765
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007766psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7767 const psa_crypto_driver_pake_inputs_t *inputs,
7768 psa_pake_cipher_suite_t *cipher_suite)
7769{
7770 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7771 return PSA_ERROR_BAD_STATE;
7772 }
7773
7774 *cipher_suite = inputs->cipher_suite;
7775
7776 return PSA_SUCCESS;
7777}
7778
Neil Armstronga7d08c32022-06-01 18:21:20 +02007779psa_status_t psa_pake_setup(
7780 psa_pake_operation_t *operation,
7781 const psa_pake_cipher_suite_t *cipher_suite)
7782{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007784
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007785 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007786 status = PSA_ERROR_BAD_STATE;
7787 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007788 }
7789
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007790 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007791 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007792 status = PSA_ERROR_INVALID_ARGUMENT;
7793 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007794 }
7795
Przemek Stekiel51eac532022-12-07 11:04:51 +01007796 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7797
Przemek Stekiele12ed362022-12-21 12:54:46 +01007798 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007799 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7800 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007801 operation->data.inputs.cipher_suite = *cipher_suite;
7802
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007803#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007804 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007805 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007806 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007807
David Horstmann16f01512023-06-14 17:21:07 +01007808 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007809 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007810 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007811#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007812 {
7813 status = PSA_ERROR_NOT_SUPPORTED;
7814 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007815 }
7816
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007817 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7818
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007820exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007821 psa_pake_abort(operation);
7822 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007823}
7824
7825psa_status_t psa_pake_set_password_key(
7826 psa_pake_operation_t *operation,
7827 mbedtls_svc_key_id_t password)
7828{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007830 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7831 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007832 psa_key_attributes_t attributes;
7833 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007834
Przemek Stekiel51eac532022-12-07 11:04:51 +01007835 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007836 status = PSA_ERROR_BAD_STATE;
7837 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007838 }
7839
Przemek Stekiel6c764412022-11-22 14:05:12 +01007840 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7841 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007842 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007843 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007844 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007845 }
7846
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007847 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007848 .core = slot->attr
7849 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007850
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007851 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007852
Przemek Stekiel51eac532022-12-07 11:04:51 +01007853 if (type != PSA_KEY_TYPE_PASSWORD &&
7854 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7855 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007857 }
7858
Przemek Stekiel51eac532022-12-07 11:04:51 +01007859 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7860 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007861 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007862 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007863 }
7864
7865 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7866 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007867 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007869 if (status != PSA_SUCCESS) {
7870 psa_pake_abort(operation);
7871 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007872 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007873 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007874}
7875
7876psa_status_t psa_pake_set_user(
7877 psa_pake_operation_t *operation,
7878 const uint8_t *user_id,
7879 size_t user_id_len)
7880{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007882
7883 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884 status = PSA_ERROR_BAD_STATE;
7885 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007886 }
7887
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007889 status = PSA_ERROR_INVALID_ARGUMENT;
7890 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007891 }
7892
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007893 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007894 status = PSA_ERROR_BAD_STATE;
7895 goto exit;
7896 }
7897
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007898 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7899 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007900 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7901 goto exit;
7902 }
7903
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007904 memcpy(operation->data.inputs.user, user_id, user_id_len);
7905 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007906
7907 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007908exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007909 psa_pake_abort(operation);
7910 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007911}
7912
7913psa_status_t psa_pake_set_peer(
7914 psa_pake_operation_t *operation,
7915 const uint8_t *peer_id,
7916 size_t peer_id_len)
7917{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007918 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007919
7920 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007921 status = PSA_ERROR_BAD_STATE;
7922 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007923 }
7924
Przemek Stekiel51eac532022-12-07 11:04:51 +01007925 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007926 status = PSA_ERROR_INVALID_ARGUMENT;
7927 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007928 }
7929
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007930 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007931 status = PSA_ERROR_BAD_STATE;
7932 goto exit;
7933 }
7934
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007935 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7936 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007937 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7938 goto exit;
7939 }
7940
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007941 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7942 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007943
7944 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007946 psa_pake_abort(operation);
7947 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007948}
7949
7950psa_status_t psa_pake_set_role(
7951 psa_pake_operation_t *operation,
7952 psa_pake_role_t role)
7953{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007957 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007958 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007959 }
7960
Przemek Stekiel09104b82023-03-06 14:11:51 +01007961 switch (operation->alg) {
7962#if defined(PSA_WANT_ALG_JPAKE)
7963 case PSA_ALG_JPAKE:
7964 if (role == PSA_PAKE_ROLE_NONE) {
7965 return PSA_SUCCESS;
7966 }
7967 status = PSA_ERROR_INVALID_ARGUMENT;
7968 break;
7969#endif
7970 default:
7971 (void) role;
7972 status = PSA_ERROR_NOT_SUPPORTED;
7973 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007974 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007976 psa_pake_abort(operation);
7977 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007978}
7979
David Horstmanne7f21e62023-05-12 18:17:21 +01007980/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007981#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007982static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007983 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007984{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007985 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007986 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007987 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007988
David Horstmann024e5c52023-06-14 15:48:21 +01007989 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007990 is_x1 = (stage->outputs < 1);
7991 } else {
7992 is_x1 = (stage->inputs < 1);
7993 }
7994
David Horstmann74a3d8c2023-06-14 18:28:19 +01007995 key_share_step = is_x1 ?
7996 PSA_JPAKE_X1_STEP_KEY_SHARE :
7997 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007998 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007999 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8000 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8001 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8002 } else {
8003 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008004 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008005 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008006}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008007#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008008
Przemek Stekiel2797d372022-12-22 11:19:22 +01008009static psa_status_t psa_pake_complete_inputs(
8010 psa_pake_operation_t *operation)
8011{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008013 /* Create copy of the inputs on stack as inputs share memory
8014 with the driver context which will be setup by the driver. */
8015 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008016
Przemek Stekielfde11282023-03-13 16:06:09 +01008017 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008018 return PSA_ERROR_BAD_STATE;
8019 }
8020
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008021 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008022 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8023 return PSA_ERROR_BAD_STATE;
8024 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008025 }
8026
Przemek Stekiel18620a32023-01-17 16:34:52 +01008027 /* Clear driver context */
8028 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8029
8030 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008031
8032 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008033 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008034
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008035 /* User and peer are translated to role. */
8036 mbedtls_free(inputs.user);
8037 mbedtls_free(inputs.peer);
8038
Przemek Stekiel2797d372022-12-22 11:19:22 +01008039 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008040#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008041 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008042 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008043 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008044#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008045 {
8046 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008047 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008048 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008049 return status;
8050}
8051
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008052#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008053static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008054 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008055 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008056 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008057{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008058 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8059 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8060 step != PSA_PAKE_STEP_ZK_PROOF) {
8061 return PSA_ERROR_INVALID_ARGUMENT;
8062 }
8063
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008064 psa_jpake_computation_stage_t *computation_stage =
8065 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008066
David Horstmann5da95602023-06-08 15:37:12 +01008067 if (computation_stage->round != PSA_JPAKE_FIRST &&
8068 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008069 return PSA_ERROR_BAD_STATE;
8070 }
8071
David Horstmanne7f21e62023-05-12 18:17:21 +01008072 /* Check that the step we are given is the one we were expecting */
8073 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008074 return PSA_ERROR_BAD_STATE;
8075 }
8076
David Horstmanne7f21e62023-05-12 18:17:21 +01008077 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8078 computation_stage->inputs == 0 &&
8079 computation_stage->outputs == 0) {
8080 /* Start of the round, so function decides whether we are inputting
8081 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008082 computation_stage->io_mode = io_mode;
8083 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 /* Middle of the round so the mode we are in must match the function
8085 * called by the user */
8086 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008087 }
8088
8089 return PSA_SUCCESS;
8090}
8091
David Horstmanne7f21e62023-05-12 18:17:21 +01008092static psa_status_t psa_jpake_epilogue(
8093 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008094 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095{
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008097 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098
David Horstmanne7f21e62023-05-12 18:17:21 +01008099 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8100 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008101 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008102 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008103 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008104 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008105 }
8106 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008107 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008108 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008109 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008110 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008111 }
8112 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008113 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8114 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008115 /* End of a round, move to the next round */
8116 stage->inputs = 0;
8117 stage->outputs = 0;
8118 stage->round++;
8119 }
8120 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008121 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008122 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008123 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008124 return PSA_SUCCESS;
8125}
David Horstmanne7f21e62023-05-12 18:17:21 +01008126
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008127#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128
Neil Armstronga7d08c32022-06-01 18:21:20 +02008129psa_status_t psa_pake_output(
8130 psa_pake_operation_t *operation,
8131 psa_pake_step_t step,
8132 uint8_t *output,
8133 size_t output_size,
8134 size_t *output_length)
8135{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008137 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008138 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008139
8140 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 status = psa_pake_complete_inputs(operation);
8142 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008144 }
8145 }
8146
8147 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008148 status = PSA_ERROR_BAD_STATE;
8149 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008150 }
8151
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008152 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008153 status = PSA_ERROR_INVALID_ARGUMENT;
8154 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008155 }
8156
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008158#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008160 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008162 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008164 driver_step = convert_jpake_computation_stage_to_driver_step(
8165 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008167#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008169 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008170 status = PSA_ERROR_NOT_SUPPORTED;
8171 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 }
8173
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008174 status = psa_driver_wrapper_pake_output(operation, driver_step,
8175 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008176
8177 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008178 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008179 }
8180
8181 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008182#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008183 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008184 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008185 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008186 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008187 }
8188 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008189#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 status = PSA_ERROR_NOT_SUPPORTED;
8192 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193 }
8194
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008195 return PSA_SUCCESS;
8196exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008197 psa_pake_abort(operation);
8198 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008199}
8200
8201psa_status_t psa_pake_input(
8202 psa_pake_operation_t *operation,
8203 psa_pake_step_t step,
8204 const uint8_t *input,
8205 size_t input_length)
8206{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008208 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008209 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8210 operation->primitive,
8211 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008212
8213 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008214 status = psa_pake_complete_inputs(operation);
8215 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008216 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008217 }
8218 }
8219
8220 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008221 status = PSA_ERROR_BAD_STATE;
8222 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008223 }
8224
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008225 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008226 status = PSA_ERROR_INVALID_ARGUMENT;
8227 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008228 }
8229
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008231#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008232 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008233 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008234 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008235 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008236 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008237 driver_step = convert_jpake_computation_stage_to_driver_step(
8238 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008240#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008242 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008243 status = PSA_ERROR_NOT_SUPPORTED;
8244 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 }
8246
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008247 status = psa_driver_wrapper_pake_input(operation, driver_step,
8248 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008249
8250 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008251 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 }
8253
8254 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008255#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008257 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
8261 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008262#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 status = PSA_ERROR_NOT_SUPPORTED;
8265 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 }
8267
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008268 return PSA_SUCCESS;
8269exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008270 psa_pake_abort(operation);
8271 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008272}
8273
8274psa_status_t psa_pake_get_implicit_key(
8275 psa_pake_operation_t *operation,
8276 psa_key_derivation_operation_t *output)
8277{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008280 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008281 size_t shared_key_len = 0;
8282
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008283 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008284 status = PSA_ERROR_BAD_STATE;
8285 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008286 }
8287
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008288#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008289 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008290 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008291 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008292 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008293 status = PSA_ERROR_BAD_STATE;
8294 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008295 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008296 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008297#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008298 {
8299 status = PSA_ERROR_NOT_SUPPORTED;
8300 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008301 }
8302
Przemek Stekiel0c781802022-11-29 14:53:13 +01008303 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8304 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008305 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008306 &shared_key_len);
8307
8308 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008310 }
8311
8312 status = psa_key_derivation_input_bytes(output,
8313 PSA_KEY_DERIVATION_INPUT_SECRET,
8314 shared_key,
8315 shared_key_len);
8316
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008317 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008318exit:
8319 abort_status = psa_pake_abort(operation);
8320 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008321}
8322
8323psa_status_t psa_pake_abort(
8324 psa_pake_operation_t *operation)
8325{
Przemek Stekield69dca92023-01-31 19:59:20 +01008326 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327
Przemek Stekield69dca92023-01-31 19:59:20 +01008328 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008329 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008330 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008331
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008332 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8333 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008334 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008335 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008336 }
8337 if (operation->data.inputs.user != NULL) {
8338 mbedtls_free(operation->data.inputs.user);
8339 }
8340 if (operation->data.inputs.peer != NULL) {
8341 mbedtls_free(operation->data.inputs.peer);
8342 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008343 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008344 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008345
Przemek Stekield69dca92023-01-31 19:59:20 +01008346 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008347}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008348#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008349
Gilles Peskinee59236f2018-01-27 23:32:46 +01008350#endif /* MBEDTLS_PSA_CRYPTO_C */