blob: 3244831b23760ce7ab4795d829c9693db6ea66b0 [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 {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100109 unsigned drivers_initialized : 1;
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 Setti8bb57632023-05-26 13:48:07 +0200129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200131 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
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 Setti8bb57632023-05-26 13:48:07 +0200141#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200142 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200143 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
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) {
395 case MBEDTLS_ECP_DP_SECP192R1:
396 *bits = 192;
397 return PSA_ECC_FAMILY_SECP_R1;
398 case MBEDTLS_ECP_DP_SECP224R1:
399 *bits = 224;
400 return PSA_ECC_FAMILY_SECP_R1;
401 case MBEDTLS_ECP_DP_SECP256R1:
402 *bits = 256;
403 return PSA_ECC_FAMILY_SECP_R1;
404 case MBEDTLS_ECP_DP_SECP384R1:
405 *bits = 384;
406 return PSA_ECC_FAMILY_SECP_R1;
407 case MBEDTLS_ECP_DP_SECP521R1:
408 *bits = 521;
409 return PSA_ECC_FAMILY_SECP_R1;
410 case MBEDTLS_ECP_DP_BP256R1:
411 *bits = 256;
412 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
413 case MBEDTLS_ECP_DP_BP384R1:
414 *bits = 384;
415 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
416 case MBEDTLS_ECP_DP_BP512R1:
417 *bits = 512;
418 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
419 case MBEDTLS_ECP_DP_CURVE25519:
420 *bits = 255;
421 return PSA_ECC_FAMILY_MONTGOMERY;
422 case MBEDTLS_ECP_DP_SECP192K1:
423 *bits = 192;
424 return PSA_ECC_FAMILY_SECP_K1;
425 case MBEDTLS_ECP_DP_SECP224K1:
426 *bits = 224;
427 return PSA_ECC_FAMILY_SECP_K1;
428 case MBEDTLS_ECP_DP_SECP256K1:
429 *bits = 256;
430 return PSA_ECC_FAMILY_SECP_K1;
431 case MBEDTLS_ECP_DP_CURVE448:
432 *bits = 448;
433 return PSA_ECC_FAMILY_MONTGOMERY;
434 default:
435 *bits = 0;
436 return 0;
437 }
438}
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
441 size_t bits,
442 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200443{
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100445 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100447#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
451#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
455#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100456 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100458#endif
459#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100460 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100462#endif
463#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (bits_is_sloppy) {
468 return MBEDTLS_ECP_DP_SECP521R1;
469 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 }
473 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100474
Paul Elliott8ff510a2020-06-02 17:19:28 +0100475 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100477#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100478 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100480#endif
481#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100482 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100484#endif
485#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100486 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100488#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100489 }
490 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100491
Paul Elliott8ff510a2020-06-02 17:19:28 +0100492 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100494#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100495 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (bits_is_sloppy) {
499 return MBEDTLS_ECP_DP_CURVE25519;
500 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100502#endif
503#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100504 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100506#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100507 }
508 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100509
Paul Elliott8ff510a2020-06-02 17:19:28 +0100510 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100512#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100513 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100515#endif
516#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100517 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100519#endif
520#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100521 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100523#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100524 }
525 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200526 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100528 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200530}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200531#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
534 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535{
536 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200540 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200541 case PSA_KEY_TYPE_PASSWORD:
542 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200543 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100544#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (bits != 128 && bits != 192 && bits != 256) {
547 return PSA_ERROR_INVALID_ARGUMENT;
548 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549 break;
550#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200551#if defined(PSA_WANT_KEY_TYPE_ARIA)
552 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (bits != 128 && bits != 192 && bits != 256) {
554 return PSA_ERROR_INVALID_ARGUMENT;
555 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200556 break;
557#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100558#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200559 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (bits != 128 && bits != 192 && bits != 256) {
561 return PSA_ERROR_INVALID_ARGUMENT;
562 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200563 break;
564#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100565#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200566 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (bits != 64 && bits != 128 && bits != 192) {
568 return PSA_ERROR_INVALID_ARGUMENT;
569 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200570 break;
571#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100572#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200573 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (bits != 256) {
575 return PSA_ERROR_INVALID_ARGUMENT;
576 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200577 break;
578#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200579 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200581 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (bits % 8 != 0) {
583 return PSA_ERROR_INVALID_ARGUMENT;
584 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200587}
588
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100589/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100590 *
Steven Cooremancd640932021-03-08 12:00:27 +0100591 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
592 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100593 *
594 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100595 * \param[in] key_type The key type of the key to be used with the
596 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100597 *
598 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100599 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100600 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100601 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100602 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100603MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100604 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100606{
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (PSA_ALG_IS_HMAC(algorithm)) {
608 if (key_type == PSA_KEY_TYPE_HMAC) {
609 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100610 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100611 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
614 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
615 * key. */
616 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
617 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
618 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
619 * the block length (larger than 1) for block ciphers. */
620 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
621 return PSA_SUCCESS;
622 }
623 }
624 }
625
626 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100627}
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
630 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200631{
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (slot->key.data != NULL) {
633 return PSA_ERROR_ALREADY_EXISTS;
634 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 slot->key.data = mbedtls_calloc(1, buffer_length);
637 if (slot->key.data == NULL) {
638 return PSA_ERROR_INSUFFICIENT_MEMORY;
639 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200640
Ronald Cronea0f8a62020-11-25 17:52:23 +0100641 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200643}
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
646 const uint8_t *data,
647 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200648{
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 psa_status_t status = psa_allocate_buffer_to_slot(slot,
650 data_length);
651 if (status != PSA_SUCCESS) {
652 return status;
653 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 memcpy(slot->key.data, data, data_length);
656 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200657}
658
Ronald Crona0fe59f2020-11-29 11:14:53 +0100659psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100660 const psa_key_attributes_t *attributes,
661 const uint8_t *data, size_t data_length,
662 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
666 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200668 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (data_length == 0) {
670 return PSA_ERROR_NOT_SUPPORTED;
671 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (key_type_is_raw_bytes(type)) {
674 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
677 *bits);
678 if (status != PSA_SUCCESS) {
679 return status;
680 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200681
Ronald Crondd04d422020-11-28 15:14:42 +0100682 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100684 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return PSA_SUCCESS;
688 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200689#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200690 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100691 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200692 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100693 return PSA_ERROR_INVALID_ARGUMENT;
694 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200695 return mbedtls_psa_ffdh_import_key(attributes,
696 data, data_length,
697 key_buffer, key_buffer_size,
698 key_buffer_length,
699 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100700 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200701#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200702 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200703#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
705 if (PSA_KEY_TYPE_IS_ECC(type)) {
706 return mbedtls_psa_ecp_import_key(attributes,
707 data, data_length,
708 key_buffer, key_buffer_size,
709 key_buffer_length,
710 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100711 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200712#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800713 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200714#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
715 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
717 if (PSA_KEY_TYPE_IS_RSA(type)) {
718 return mbedtls_psa_rsa_import_key(attributes,
719 data, data_length,
720 key_buffer, key_buffer_size,
721 key_buffer_length,
722 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200723 }
Valerio Settife478902023-07-25 12:27:19 +0200724#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
725 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800726 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100727 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100730}
731
Gilles Peskinef603c712019-01-19 13:40:11 +0100732/** Calculate the intersection of two algorithm usage policies.
733 *
734 * Return 0 (which allows no operation) on incompatibility.
735 */
736static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100737 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100738 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100740{
Gilles Peskine549ea862019-05-22 11:45:59 +0200741 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (alg1 == alg2) {
743 return alg1;
744 }
Steven Cooreman03488022021-02-18 12:07:20 +0100745 /* If the policies are from the same hash-and-sign family, check
746 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
748 PSA_ALG_IS_SIGN_HASH(alg2) &&
749 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
750 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
751 return alg2;
752 }
753 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
754 return alg1;
755 }
Steven Cooreman03488022021-02-18 12:07:20 +0100756 }
757 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100758 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100759 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
761 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
762 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
763 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
764 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100765 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100766
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100767 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
769 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
770 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
771 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100772 }
773 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
775 (alg1_len <= alg2_len)) {
776 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100777 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
779 (alg2_len <= alg1_len)) {
780 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100781 }
782 }
783 /* If the policies are from the same MAC family, check whether one
784 * of them is a minimum-MAC-length policy. Calculate the most
785 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
787 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
788 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100789 /* Validate the combination of key type and algorithm. Since the base
790 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
792 return 0;
793 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100794
Steven Cooreman31a876d2021-03-03 20:47:40 +0100795 /* Get the (exact or at-least) output lengths for both sides of the
796 * requested intersection. None of the currently supported algorithms
797 * have an output length dependent on the actual key size, so setting it
798 * to a bogus value of 0 is currently OK.
799 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100800 * Note that for at-least-this-length wildcard algorithms, the output
801 * length is set to the shortest allowed length, which allows us to
802 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
804 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100805 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100806
Steven Cooremana1d83222021-02-25 10:20:29 +0100807 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
809 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
810 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100811 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100812
813 /* If only one is an at-least-this-length policy, the intersection would
814 * be the other (fixed-length) policy as long as said fixed length is
815 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
817 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100818 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
820 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100822
Steven Cooremancd640932021-03-08 12:00:27 +0100823 /* If none of them are wildcards, check whether they define the same tag
824 * length. This is still possible here when one is default-length and
825 * the other specific-length. Ensure to always return the
826 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (alg1_len == alg2_len) {
828 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
829 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100830 }
831 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100833}
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static int psa_key_algorithm_permits(psa_key_type_t key_type,
836 psa_algorithm_t policy_alg,
837 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838{
Gilles Peskine549ea862019-05-22 11:45:59 +0200839 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (requested_alg == policy_alg) {
841 return 1;
842 }
Steven Cooreman03488022021-02-18 12:07:20 +0100843 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
844 * and requested_alg is the same hash-and-sign family with any hash,
845 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
847 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
848 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
849 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100850 }
851 /* If policy_alg is a wildcard AEAD algorithm of the same base as
852 * the requested algorithm, check the requested tag length to be
853 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (PSA_ALG_IS_AEAD(policy_alg) &&
855 PSA_ALG_IS_AEAD(requested_alg) &&
856 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
857 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
858 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
859 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
860 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100861 }
Steven Cooremancd640932021-03-08 12:00:27 +0100862 /* If policy_alg is a MAC algorithm of the same base as the requested
863 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (PSA_ALG_IS_MAC(policy_alg) &&
865 PSA_ALG_IS_MAC(requested_alg) &&
866 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
867 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100868 /* Validate the combination of key type and algorithm. Since the policy
869 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
871 return 0;
872 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100873
Steven Cooreman31a876d2021-03-03 20:47:40 +0100874 /* Get both the requested output length for the algorithm which is to be
875 * verified, and the default output length for the base algorithm.
876 * Note that none of the currently supported algorithms have an output
877 * length dependent on actual key size, so setting it to a bogus value
878 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100879 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100881 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 key_type, 0,
883 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100884
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100885 /* If the policy is default-length, only allow an algorithm with
886 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
888 return requested_output_length == default_output_length;
889 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100890
891 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100892 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
894 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
895 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100896 }
897
Steven Cooremancd640932021-03-08 12:00:27 +0100898 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
899 * check for the requested MAC length to be equal to or longer than the
900 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
902 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
903 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100904 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200905 }
Steven Cooremance48e852020-10-05 16:02:45 +0200906 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200907 * a key derivation with that key agreement should also be allowed. This
908 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
910 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
911 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
912 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200913 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100914 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200916}
917
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918/** Test whether a policy permits an algorithm.
919 *
920 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100921 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100922 * \note This function requires providing the key type for which the policy is
923 * being validated, since some algorithm policy definitions (e.g. MAC)
924 * have different properties depending on what kind of cipher it is
925 * combined with.
926 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100927 * \retval PSA_SUCCESS When \p alg is a specific algorithm
928 * allowed by the \p policy.
929 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
930 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
931 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100932 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
934 psa_key_type_t key_type,
935 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100936{
Steven Cooremand788fab2021-02-25 11:29:17 +0100937 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (alg == 0) {
939 return PSA_ERROR_INVALID_ARGUMENT;
940 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100941
Steven Cooreman7e39f052021-02-23 14:18:32 +0100942 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (PSA_ALG_IS_WILDCARD(alg)) {
944 return PSA_ERROR_INVALID_ARGUMENT;
945 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
948 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
949 return PSA_SUCCESS;
950 } else {
951 return PSA_ERROR_NOT_PERMITTED;
952 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100953}
954
Gilles Peskinef603c712019-01-19 13:40:11 +0100955/** Restrict a key policy based on a constraint.
956 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100957 * \note This function requires providing the key type for which the policy is
958 * being restricted, since some algorithm policy definitions (e.g. MAC)
959 * have different properties depending on what kind of cipher it is
960 * combined with.
961 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100962 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100963 * \param[in,out] policy The policy to restrict.
964 * \param[in] constraint The policy constraint to apply.
965 *
966 * \retval #PSA_SUCCESS
967 * \c *policy contains the intersection of the original value of
968 * \c *policy and \c *constraint.
969 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100970 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100971 * \c *policy is unchanged.
972 */
973static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100974 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100975 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100977{
978 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 psa_key_policy_algorithm_intersection(key_type, policy->alg,
980 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
983 constraint->alg2);
984 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
985 return PSA_ERROR_INVALID_ARGUMENT;
986 }
987 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
988 return PSA_ERROR_INVALID_ARGUMENT;
989 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100990 policy->usage &= constraint->usage;
991 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200992 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100994}
995
Przemek Stekiel061f6942022-11-30 10:51:35 +0100996/** Get the description of a key given its identifier and policy constraints
997 * and lock it.
998 *
999 * The key must have allow all the usage flags set in \p usage. If \p alg is
1000 * nonzero, the key must allow operations with this algorithm. If \p alg is
1001 * zero, the algorithm is not checked.
1002 *
1003 * In case of a persistent key, the function loads the description of the key
1004 * into a key slot if not already done.
1005 *
1006 * On success, the returned key slot is locked. It is the responsibility of
1007 * the caller to unlock the key slot when it does not access it anymore.
1008 */
1009static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001010 mbedtls_svc_key_id_t key,
1011 psa_key_slot_t **p_slot,
1012 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001014{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001016 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 status = psa_get_and_lock_key_slot(key, p_slot);
1019 if (status != PSA_SUCCESS) {
1020 return status;
1021 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001022 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001023
1024 /* Enforce that usage policy for the key slot contains all the flags
1025 * required by the usage parameter. There is one exception: public
1026 * keys can always be exported, so we treat public key objects as
1027 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001029 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001033 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001034 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001035 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001036
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001037 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (alg != 0) {
1039 status = psa_key_policy_permits(&slot->attr.policy,
1040 slot->attr.type,
1041 alg);
1042 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001043 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001045 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001048
1049error:
1050 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001054}
Darryl Green940d72c2018-07-13 13:18:51 +01001055
Ronald Cron5c522922020-11-14 16:35:34 +01001056/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001057 *
1058 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001059 * available, as opposed to a key in a secure element and/or to be used
1060 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001061 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001062 * This is a temporary function that may be used instead of
1063 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1064 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001065 *
Ronald Cron5c522922020-11-14 16:35:34 +01001066 * On success, the returned key slot is locked. It is the responsibility of the
1067 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001068 */
Ronald Cron5c522922020-11-14 16:35:34 +01001069static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1070 mbedtls_svc_key_id_t key,
1071 psa_key_slot_t **p_slot,
1072 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001074{
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1076 usage, alg);
1077 if (status != PSA_SUCCESS) {
1078 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001079 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1082 psa_unlock_key_slot(*p_slot);
1083 *p_slot = NULL;
1084 return PSA_ERROR_NOT_SUPPORTED;
1085 }
1086
1087 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001088}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001091{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001092 /* Data pointer will always be either a valid pointer or NULL in an
1093 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (slot->key.data != NULL) {
1095 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1096 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001099 slot->key.data = NULL;
1100 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001103}
1104
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001105/** Completely wipe a slot in memory, including its policy.
1106 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001108{
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 /*
1112 * As the return error code may not be handled in case of multiple errors,
1113 * do our best to report an unexpected lock counter. Assert with
1114 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1115 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1116 * function is called as part of the execution of a test suite, the
1117 * execution of the test suite is stopped in error if the assertion fails.
1118 */
1119 if (slot->lock_count != 1) {
1120 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001121 status = PSA_ERROR_CORRUPTION_DETECTED;
1122 }
1123
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001124 /* Multipart operations may still be using the key. This is safe
1125 * because all multipart operation objects are independent from
1126 * the key slot: if they need to access the key after the setup
1127 * phase, they have a copy of the key. Note that this means that
1128 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001129 /* At this point, key material and other type-specific content has
1130 * been wiped. Clear remaining metadata. We can call memset and not
1131 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 memset(slot, 0, sizeof(*slot));
1133 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001134}
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001138 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001139 psa_status_t status; /* status of the last operation */
1140 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1142 psa_se_drv_table_entry_t *driver;
1143#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (mbedtls_svc_key_id_is_null(key)) {
1146 return PSA_SUCCESS;
1147 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001148
Ronald Cronf2911112020-10-29 17:51:10 +01001149 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001150 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001151 * key, this will load the key description from persistent memory if not
1152 * done yet. We cannot avoid this loading as without it we don't know if
1153 * the key is operated by an SE or not and this information is needed by
1154 * the current implementation.
1155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 status = psa_get_and_lock_key_slot(key, &slot);
1157 if (status != PSA_SUCCESS) {
1158 return status;
1159 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001160
Ronald Cronf2911112020-10-29 17:51:10 +01001161 /*
1162 * If the key slot containing the key description is under access by the
1163 * library (apart from the present access), the key cannot be destroyed
1164 * yet. For the time being, just return in error. Eventually (to be
1165 * implemented), the key should be destroyed when all accesses have
1166 * stopped.
1167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (slot->lock_count > 1) {
1169 psa_unlock_key_slot(slot);
1170 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001171 }
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001174 /* Refuse the destruction of a read-only key (which may or may not work
1175 * if we attempt it, depending on whether the key is merely read-only
1176 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001177 * Just do the best we can, which is to wipe the copy in memory
1178 * (done in this function's cleanup code). */
1179 overall_status = PSA_ERROR_NOT_PERMITTED;
1180 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001181 }
1182
Gilles Peskine354f7672019-07-12 23:46:38 +02001183#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1185 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001186 /* For a key in a secure element, we need to do three things:
1187 * remove the key file in internal storage, destroy the
1188 * key inside the secure element, and update the driver's
1189 * persistent data. Start a transaction that will encompass these
1190 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001192 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001194 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 status = psa_crypto_save_transaction();
1196 if (status != PSA_SUCCESS) {
1197 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198 /* We should still try to destroy the key in the secure
1199 * element and the key metadata in storage. This is especially
1200 * important if the error is that the storage is full.
1201 * But how to do it exactly without risking an inconsistent
1202 * state after a reset?
1203 * https://github.com/ARMmbed/mbed-crypto/issues/215
1204 */
1205 overall_status = status;
1206 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001207 }
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 status = psa_destroy_se_key(driver,
1210 psa_key_slot_get_slot_number(slot));
1211 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001212 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001214 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001215#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1216
Darryl Greend49a4992018-06-18 17:27:26 +01001217#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1219 status = psa_destroy_persistent_key(slot->attr.id);
1220 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001221 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001223
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001224 /* TODO: other slots may have a copy of the same key. We should
1225 * invalidate them.
1226 * https://github.com/ARMmbed/mbed-crypto/issues/214
1227 */
Darryl Greend49a4992018-06-18 17:27:26 +01001228 }
1229#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001230
Gilles Peskinefc762652019-07-22 19:30:34 +02001231#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (driver != NULL) {
1233 status = psa_save_se_persistent_data(driver);
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 }
1237 status = psa_crypto_stop_transaction();
1238 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001239 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001241 }
1242#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1243
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001246 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001248 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 }
1250 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251}
1252
Valerio Settib2bcedb2023-07-10 11:24:00 +02001253#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001254 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001255static psa_status_t psa_get_rsa_public_exponent(
1256 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001258{
1259 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 uint8_t *buffer = NULL;
1262 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1266 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 }
1269 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001270 /* It's the default value, which is reported as an empty string,
1271 * so there's nothing to do. */
1272 goto exit;
1273 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 buflen = mbedtls_mpi_size(&mpi);
1276 buffer = mbedtls_calloc(1, buflen);
1277 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001278 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1279 goto exit;
1280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1282 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001283 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001285 attributes->domain_parameters = buffer;
1286 attributes->domain_parameters_size = buflen;
1287
1288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 mbedtls_mpi_free(&mpi);
1290 if (ret != 0) {
1291 mbedtls_free(buffer);
1292 }
1293 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001294}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001295#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001296 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001297
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001298/** Retrieve all the publicly-accessible attributes of a key.
1299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1301 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001304 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001305 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1310 if (status != PSA_SUCCESS) {
1311 return status;
1312 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001313
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001314 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1316 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001317
1318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1320 psa_set_key_slot_number(attributes,
1321 psa_key_slot_get_slot_number(slot));
1322 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001326#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1327 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001328 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001329 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001330 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001331 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001332 * is not yet implemented.
1333 * https://github.com/ARMmbed/mbed-crypto/issues/216
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001336 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001337
Ronald Cron90857082020-11-25 14:58:33 +01001338 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 slot->attr.type,
1340 slot->key.data,
1341 slot->key.bytes,
1342 &rsa);
1343 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001344 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 status = psa_get_rsa_public_exponent(rsa,
1348 attributes);
1349 mbedtls_rsa_free(rsa);
1350 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001351 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001352 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001353#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1354 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001355 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001356 default:
1357 /* Nothing else to do. */
1358 break;
1359 }
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (status != PSA_SUCCESS) {
1362 psa_reset_key_attributes(attributes);
1363 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368}
1369
Gilles Peskinec8000c02019-08-02 20:15:51 +02001370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1371psa_status_t psa_get_key_slot_number(
1372 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001374{
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001376 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return PSA_SUCCESS;
1378 } else {
1379 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001380 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381}
1382#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1385 size_t key_buffer_size,
1386 uint8_t *data,
1387 size_t data_size,
1388 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001389{
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if (key_buffer_size > data_size) {
1391 return PSA_ERROR_BUFFER_TOO_SMALL;
1392 }
1393 memcpy(data, key_buffer, key_buffer_size);
1394 memset(data + key_buffer_size, 0,
1395 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001396 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001398}
1399
Ronald Cron7285cda2020-11-26 14:46:37 +01001400psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001401 const psa_key_attributes_t *attributes,
1402 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404{
Ronald Crond18b5f82020-11-25 16:46:05 +01001405 psa_key_type_t type = attributes->core.type;
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (key_type_is_raw_bytes(type) ||
1408 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001409 PSA_KEY_TYPE_IS_ECC(type) ||
1410 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return psa_export_key_buffer_internal(
1412 key_buffer, key_buffer_size,
1413 data, data_size, data_length);
1414 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415 /* This shouldn't happen in the reference implementation, but
1416 it is valid for a special-purpose implementation to omit
1417 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001419 }
1420}
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1423 uint8_t *data,
1424 size_t data_size,
1425 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426{
1427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1428 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1429 psa_key_slot_t *slot;
1430
Ronald Crone907e552021-01-18 13:22:38 +01001431 /* Reject a zero-length output buffer now, since this can never be a
1432 * valid key representation. This way we know that data must be a valid
1433 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (data_size == 0) {
1435 return PSA_ERROR_BUFFER_TOO_SMALL;
1436 }
Ronald Crone907e552021-01-18 13:22:38 +01001437
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 /* Set the key to empty now, so that even when there are errors, we always
1439 * set data_length to a value between 0 and data_size. On error, setting
1440 * the key to empty is a good choice because an empty key representation is
1441 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001442 *data_length = 0;
1443
Ronald Cron9486f9d2020-11-26 13:36:23 +01001444 /* Export requires the EXPORT flag. There is an exception for public keys,
1445 * which don't require any flag, but
1446 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1449 PSA_KEY_USAGE_EXPORT, 0);
1450 if (status != PSA_SUCCESS) {
1451 return status;
1452 }
mohammad160306e79202018-03-28 13:17:44 +03001453
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 psa_key_attributes_t attributes = {
1455 .core = slot->attr
1456 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 status = psa_driver_wrapper_export_key(&attributes,
1458 slot->key.data, slot->key.bytes,
1459 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001464}
1465
Ronald Cron7285cda2020-11-26 14:46:37 +01001466psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001467 const psa_key_attributes_t *attributes,
1468 const uint8_t *key_buffer,
1469 size_t key_buffer_size,
1470 uint8_t *data,
1471 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001473{
Ronald Crond18b5f82020-11-25 16:46:05 +01001474 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001475
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001476 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001477 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1478 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001479 /* Exporting public -> public */
1480 return psa_export_key_buffer_internal(
1481 key_buffer, key_buffer_size,
1482 data, data_size, data_length);
1483 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001484#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001485 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1486 return mbedtls_psa_rsa_export_public_key(attributes,
1487 key_buffer,
1488 key_buffer_size,
1489 data,
1490 data_size,
1491 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001492#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001493 /* We don't know how to convert a private RSA key to public. */
1494 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001495#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001496 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001497 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001499 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1500 return mbedtls_psa_ecp_export_public_key(attributes,
1501 key_buffer,
1502 key_buffer_size,
1503 data,
1504 data_size,
1505 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001506#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001507 /* We don't know how to convert a private ECC key to public */
1508 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001509#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001510 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001511 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001512#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001513 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001514 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001515 key_buffer,
1516 key_buffer_size,
1517 data, data_size,
1518 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001519#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001520 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001521#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001522 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001524 (void) key_buffer;
1525 (void) key_buffer_size;
1526 (void) data;
1527 (void) data_size;
1528 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001530 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531}
Ronald Crond18b5f82020-11-25 16:46:05 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1534 uint8_t *data,
1535 size_t data_size,
1536 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001537{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001539 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001540 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001541 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001542
Ronald Crone907e552021-01-18 13:22:38 +01001543 /* Reject a zero-length output buffer now, since this can never be a
1544 * valid key representation. This way we know that data must be a valid
1545 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (data_size == 0) {
1547 return PSA_ERROR_BUFFER_TOO_SMALL;
1548 }
Ronald Crone907e552021-01-18 13:22:38 +01001549
Darryl Greendd8fb772018-11-07 16:00:44 +00001550 /* Set the key to empty now, so that even when there are errors, we always
1551 * set data_length to a value between 0 and data_size. On error, setting
1552 * the key to empty is a good choice because an empty key representation is
1553 * unlikely to be accepted anywhere. */
1554 *data_length = 0;
1555
1556 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1558 if (status != PSA_SUCCESS) {
1559 return status;
1560 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1563 status = PSA_ERROR_INVALID_ARGUMENT;
1564 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001565 }
1566
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001567 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001568 .core = slot->attr
1569 };
Ronald Cron67227982020-11-26 15:16:05 +01001570 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001571 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001573
1574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001578}
1579
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001580MBEDTLS_STATIC_ASSERT(
1581 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1582 "One or more key attribute flag is listed as both external-only and dual-use")
1583MBEDTLS_STATIC_ASSERT(
1584 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1585 "One or more key attribute flag is listed as both internal-only and dual-use")
1586MBEDTLS_STATIC_ASSERT(
1587 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1588 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001589
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590/** Validate that a key policy is internally well-formed.
1591 *
1592 * This function only rejects invalid policies. It does not validate the
1593 * consistency of the policy with respect to other attributes of the key
1594 * such as the key type.
1595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001596static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001597{
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1599 PSA_KEY_USAGE_COPY |
1600 PSA_KEY_USAGE_ENCRYPT |
1601 PSA_KEY_USAGE_DECRYPT |
1602 PSA_KEY_USAGE_SIGN_MESSAGE |
1603 PSA_KEY_USAGE_VERIFY_MESSAGE |
1604 PSA_KEY_USAGE_SIGN_HASH |
1605 PSA_KEY_USAGE_VERIFY_HASH |
1606 PSA_KEY_USAGE_VERIFY_DERIVATION |
1607 PSA_KEY_USAGE_DERIVE)) != 0) {
1608 return PSA_ERROR_INVALID_ARGUMENT;
1609 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001612}
1613
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614/** Validate the internal consistency of key attributes.
1615 *
1616 * This function only rejects invalid attribute values. If does not
1617 * validate the consistency of the attributes with any key data that may
1618 * be involved in the creation of the key.
1619 *
1620 * Call this function early in the key creation process.
1621 *
1622 * \param[in] attributes Key attributes for the new key.
1623 * \param[out] p_drv On any return, the driver for the key, if any.
1624 * NULL for a transparent key.
1625 *
1626 */
1627static psa_status_t psa_validate_key_attributes(
1628 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001630{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001631 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1633 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 status = psa_validate_key_location(lifetime, p_drv);
1636 if (status != PSA_SUCCESS) {
1637 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001638 }
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 status = psa_validate_key_persistence(lifetime);
1641 if (status != PSA_SUCCESS) {
1642 return status;
1643 }
1644
1645 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1646 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1647 return PSA_ERROR_INVALID_ARGUMENT;
1648 }
1649 } else {
1650 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1651 return PSA_ERROR_INVALID_ARGUMENT;
1652 }
1653 }
1654
1655 status = psa_validate_key_policy(&attributes->core.policy);
1656 if (status != PSA_SUCCESS) {
1657 return status;
1658 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001659
1660 /* Refuse to create overly large keys.
1661 * Note that this doesn't trigger on import if the attributes don't
1662 * explicitly specify a size (so psa_get_key_bits returns 0), so
1663 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1665 return PSA_ERROR_NOT_SUPPORTED;
1666 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001667
Gilles Peskine91e8c332019-08-02 19:19:39 +02001668 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1670 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1671 return PSA_ERROR_INVALID_ARGUMENT;
1672 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001675}
1676
Gilles Peskine4747d192019-04-17 15:05:45 +02001677/** Prepare a key slot to receive key material.
1678 *
1679 * This function allocates a key slot and sets its metadata.
1680 *
1681 * If this function fails, call psa_fail_key_creation().
1682 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001683 * This function is intended to be used as follows:
1684 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001685 * it with the specified attributes, and in case of a volatile key assign it
1686 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001687 * -# Populate the slot with the key material.
1688 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1689 * In case of failure at any step, stop the sequence and call
1690 * psa_fail_key_creation().
1691 *
Ronald Cron5c522922020-11-14 16:35:34 +01001692 * On success, the key slot is locked. It is the responsibility of the caller
1693 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001694 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001695 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001696 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001697 * \param[out] p_slot On success, a pointer to the prepared slot.
1698 * \param[out] p_drv On any return, the driver for the key, if any.
1699 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001700 *
1701 * \retval #PSA_SUCCESS
1702 * The key slot is ready to receive key material.
1703 * \return If this function fails, the key slot is an invalid state.
1704 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 */
1706static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001707 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001708 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001709 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001711{
1712 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001713 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 psa_key_slot_t *slot;
1715
Gilles Peskinedf179142019-07-15 22:02:14 +02001716 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001717 *p_drv = NULL;
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 status = psa_validate_key_attributes(attributes, p_drv);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1725 if (status != PSA_SUCCESS) {
1726 return status;
1727 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 slot = *p_slot;
1729
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001730 /* We're storing the declared bit-size of the key. It's up to each
1731 * creation mechanism to verify that this information is correct.
1732 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001733 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001734 * is optional (import, copy). In case of a volatile key, assign it the
1735 * volatile key identifier associated to the slot returned to contain its
1736 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001737
1738 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001740#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1741 slot->attr.id = volatile_key_id;
1742#else
1743 slot->attr.id.key_id = volatile_key_id;
1744#endif
1745 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001746
Gilles Peskine91e8c332019-08-02 19:19:39 +02001747 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001748 * external-only flags, query `attributes`. Thanks to the check
1749 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001750 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001751 * may have set. */
1752 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001753
Gilles Peskinecbaff462019-07-12 23:46:04 +02001754#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001755 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001757 * create the key file in internal storage, create the
1758 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001759 * persistent data. This is done by starting a transaction that will
1760 * encompass these three actions.
1761 * For registering a volatile key, we just need to find an appropriate
1762 * slot number inside the SE. Since the key is designated volatile, creating
1763 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001764 /* The first thing to do is to find a slot number for the new key.
1765 * We save the slot number in persistent storage as part of the
1766 * transaction data. It will be needed to recover if the power
1767 * fails during the key creation process, to clean up on the secure
1768 * element side after restarting. Obtaining a slot number from the
1769 * secure element driver updates its persistent state, but we do not yet
1770 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001771 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001773 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1775 &slot_number);
1776 if (status != PSA_SUCCESS) {
1777 return status;
1778 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1781 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001782 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001783 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001784 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 status = psa_crypto_save_transaction();
1786 if (status != PSA_SUCCESS) {
1787 (void) psa_crypto_stop_transaction();
1788 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001789 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001790 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791
1792 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001794 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001797 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001799 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001803}
Gilles Peskine4747d192019-04-17 15:05:45 +02001804
1805/** Finalize the creation of a key once its key material has been set.
1806 *
1807 * This entails writing the key to persistent storage.
1808 *
1809 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001810 * See the documentation of psa_start_key_creation() for the intended use
1811 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 *
Ronald Cron5c522922020-11-14 16:35:34 +01001813 * If the finalization succeeds, the function unlocks the key slot (it was
1814 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1815 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001816 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001817 * \param[in,out] slot Pointer to the slot with key material.
1818 * \param[in] driver The secure element driver for the key,
1819 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001820 * \param[out] key On success, identifier of the key. Note that the
1821 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001822 *
1823 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001824 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1826 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1827 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1828 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1829 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1830 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001831 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001832 * \return If this function fails, the key slot is an invalid state.
1833 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001835static psa_status_t psa_finish_key_creation(
1836 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001837 psa_se_drv_table_entry_t *driver,
1838 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001839{
1840 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001841 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001843
1844#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001846#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001848 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001849 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001851
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001852 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1853 sizeof(data.slot_number),
1854 "Slot number size does not match psa_se_key_data_storage_t");
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1857 status = psa_save_persistent_key(&slot->attr,
1858 (uint8_t *) &data,
1859 sizeof(data));
1860 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001861#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1862 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001863 /* Key material is saved in export representation in the slot, so
1864 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 status = psa_save_persistent_key(&slot->attr,
1866 slot->key.data,
1867 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001868 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001870#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1871
Gilles Peskinecbaff462019-07-12 23:46:04 +02001872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001873 /* Finish the transaction for a key creation. This does not
1874 * happen when registering an existing key. Detect this case
1875 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001876 * creation of a persistent key in a secure element requires a transaction,
1877 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 if (driver != NULL &&
1879 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1880 status = psa_save_se_persistent_data(driver);
1881 if (status != PSA_SUCCESS) {
1882 psa_destroy_persistent_key(slot->attr.id);
1883 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001886 }
1887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001890 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 status = psa_unlock_key_slot(slot);
1892 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001893 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001895 }
Ronald Cron50972942020-11-14 11:28:25 +01001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001898}
1899
1900/** Abort the creation of a key.
1901 *
1902 * You may call this function after calling psa_start_key_creation(),
1903 * or after psa_finish_key_creation() fails. In other circumstances, this
1904 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001905 * See the documentation of psa_start_key_creation() for the intended use
1906 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001908 * \param[in,out] slot Pointer to the slot with key material.
1909 * \param[in] driver The secure element driver for the key,
1910 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001912static void psa_fail_key_creation(psa_key_slot_t *slot,
1913 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001914{
Gilles Peskine011e4282019-06-26 18:34:38 +02001915 (void) driver;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001918 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001920
1921#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001922 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001923 * element, and the failure happened later (when saving metadata
1924 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001925 * element.
1926 * https://github.com/ARMmbed/mbed-crypto/issues/217
1927 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001928
Gilles Peskined7729582019-08-05 15:55:54 +02001929 /* Abort the ongoing transaction if any (there may not be one if
1930 * the creation process failed before starting one, or if the
1931 * key creation is a registration of a key in a secure element).
1932 * Earlier functions must already have done what it takes to undo any
1933 * partial creation. All that's left is to update the transaction data
1934 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001936#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001939}
1940
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001941/** Validate optional attributes during key creation.
1942 *
1943 * Some key attributes are optional during key creation. If they are
1944 * specified in the attributes structure, check that they are consistent
1945 * with the data in the slot.
1946 *
1947 * This function should be called near the end of key creation, after
1948 * the slot in memory is fully populated but before saving persistent data.
1949 */
1950static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953{
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (attributes->core.type != 0) {
1955 if (attributes->core.type != slot->attr.type) {
1956 return PSA_ERROR_INVALID_ARGUMENT;
1957 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001961#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1962 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1964 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001965 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001966 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001968
Ronald Cron90857082020-11-25 14:58:33 +01001969 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 slot->attr.type,
1971 slot->key.data,
1972 slot->key.bytes,
1973 &rsa);
1974 if (status != PSA_SUCCESS) {
1975 return status;
1976 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 mbedtls_mpi_init(&actual);
1979 mbedtls_mpi_init(&required);
1980 ret = mbedtls_rsa_export(rsa,
1981 NULL, NULL, NULL, NULL, &actual);
1982 mbedtls_rsa_free(rsa);
1983 mbedtls_free(rsa);
1984 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001985 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
1987 ret = mbedtls_mpi_read_binary(&required,
1988 attributes->domain_parameters,
1989 attributes->domain_parameters_size);
1990 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001991 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
1993 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 }
1996rsa_exit:
1997 mbedtls_mpi_free(&actual);
1998 mbedtls_mpi_free(&required);
1999 if (ret != 0) {
2000 return mbedtls_to_psa_error(ret);
2001 }
2002 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002003#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2004 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002005 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 }
2009 }
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (attributes->core.bits != 0) {
2012 if (attributes->core.bits != slot->attr.bits) {
2013 return PSA_ERROR_INVALID_ARGUMENT;
2014 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 }
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018}
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2021 const uint8_t *data,
2022 size_t data_length,
2023 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002024{
2025 psa_status_t status;
2026 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002027 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302029 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002030
Ronald Cron81709fc2020-11-14 12:10:32 +01002031 *key = MBEDTLS_SVC_KEY_ID_INIT;
2032
Gilles Peskine0f84d622019-09-12 19:03:13 +02002033 /* Reject zero-length symmetric keys (including raw data key objects).
2034 * This also rejects any key which might be encoded as an empty string,
2035 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (data_length == 0) {
2037 return PSA_ERROR_INVALID_ARGUMENT;
2038 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002039
Archana449608b2021-09-08 15:36:05 +05302040 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 if (data_length > SIZE_MAX / 8) {
2042 return PSA_ERROR_NOT_SUPPORTED;
2043 }
Archana6ed4bda2021-08-04 10:47:15 +05302044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2046 &slot, &driver);
2047 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002051 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302052 * storage ( thus not in the case of importing a key in a secure element
2053 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2054 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (slot->key.data == NULL) {
2056 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302057 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 attributes, data, data_length, &storage_size);
2059 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302060 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Archanad8a83dc2021-06-14 10:04:16 +05302062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 status = psa_allocate_buffer_to_slot(slot, storage_size);
2064 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002067 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002068
2069 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_driver_wrapper_import_key(attributes,
2071 data, data_length,
2072 slot->key.data,
2073 slot->key.bytes,
2074 &slot->key.bytes, &bits);
2075 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002080 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002082 status = PSA_ERROR_INVALID_ARGUMENT;
2083 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002084 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002085
Archana6ed4bda2021-08-04 10:47:15 +05302086 /* Enforce a size limit, and in particular ensure that the bit
2087 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302089 status = PSA_ERROR_NOT_SUPPORTED;
2090 goto exit;
2091 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 status = psa_validate_optional_attributes(slot, attributes);
2093 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (status != PSA_SUCCESS) {
2100 psa_fail_key_creation(slot, driver);
2101 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002104}
2105
Gilles Peskined7729582019-08-05 15:55:54 +02002106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2107psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002109{
2110 psa_status_t status;
2111 psa_key_slot_t *slot = NULL;
2112 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002114
2115 /* Leaving attributes unspecified is not currently supported.
2116 * It could make sense to query the key type and size from the
2117 * secure element, but not all secure elements support this
2118 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2120 return PSA_ERROR_NOT_SUPPORTED;
2121 }
2122 if (psa_get_key_bits(attributes) == 0) {
2123 return PSA_ERROR_NOT_SUPPORTED;
2124 }
Gilles Peskined7729582019-08-05 15:55:54 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2127 &slot, &driver);
2128 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskined7729582019-08-05 15:55:54 +02002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002133
2134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (status != PSA_SUCCESS) {
2136 psa_fail_key_creation(slot, driver);
2137 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002138
Gilles Peskined7729582019-08-05 15:55:54 +02002139 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 psa_close_key(key);
2141 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002142}
2143#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2146 const psa_key_attributes_t *specified_attributes,
2147 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002148{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002150 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002151 psa_key_slot_t *source_slot = NULL;
2152 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002154 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302155 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002156
Ronald Cron81709fc2020-11-14 12:10:32 +01002157 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2158
Archana8a180362021-07-05 02:18:48 +05302159 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2161 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 status = psa_validate_optional_attributes(source_slot,
2166 specified_attributes);
2167 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002168 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002170
Archana9d17bf42021-09-10 06:22:44 +05302171 /* The target key type and number of bits have been validated by
2172 * psa_validate_optional_attributes() to be either equal to zero or
2173 * equal to the ones of the source key. So it is safe to inherit
2174 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302175 * */
Archana9d17bf42021-09-10 06:22:44 +05302176 actual_attributes.core.bits = source_slot->attr.bits;
2177 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302178
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_restrict_key_policy(source_slot->attr.type,
2181 &actual_attributes.core.policy,
2182 &source_slot->attr.policy);
2183 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2188 &target_slot, &driver);
2189 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 }
2192 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2193 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302194 /*
Archana9d17bf42021-09-10 06:22:44 +05302195 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302196 * the source key would need to be exported as plaintext and re-imported
2197 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302198 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302199 * appropriate API invocations from the application, if needed.
2200 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002201 status = PSA_ERROR_NOT_SUPPORTED;
2202 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203 }
Archana8a180362021-07-05 02:18:48 +05302204 /*
2205 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302206 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302207 * - For opaque keys this translates to an invocation of the drivers'
2208 * copy_key entry point through the dispatch layer.
2209 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2211 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2212 &storage_size);
2213 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 }
Archana374fe5b2021-09-08 15:50:28 +05302216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2218 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 }
Archana374fe5b2021-09-08 15:50:28 +05302221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 status = psa_driver_wrapper_copy_key(&actual_attributes,
2223 source_slot->key.data,
2224 source_slot->key.bytes,
2225 target_slot->key.data,
2226 target_slot->key.bytes,
2227 &target_slot->key.bytes);
2228 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
2231 } else {
2232 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302233 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 source_slot->key.bytes);
2235 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 }
Archana8a180362021-07-05 02:18:48 +05302238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (status != PSA_SUCCESS) {
2242 psa_fail_key_creation(target_slot, driver);
2243 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002248}
2249
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002250
2251
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002252/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002253/* Message digests */
2254/****************************************************************/
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002258 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (operation->id == 0) {
2260 return PSA_SUCCESS;
2261 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002264 operation->id = 0;
2265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267}
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2270 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002273
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002274 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002276 status = PSA_ERROR_BAD_STATE;
2277 goto exit;
2278 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002281 status = PSA_ERROR_INVALID_ARGUMENT;
2282 goto exit;
2283 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002285 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2286 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002290
2291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (status != PSA_SUCCESS) {
2293 psa_hash_abort(operation);
2294 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002295
2296 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297}
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2300 const uint8_t *input,
2301 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002306 status = PSA_ERROR_BAD_STATE;
2307 goto exit;
2308 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002309
Steven Cooremanc8288352021-03-04 14:02:19 +01002310 /* Don't require hash implementations to behave correctly on a
2311 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (input_length == 0) {
2313 return PSA_SUCCESS;
2314 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002317
2318exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (status != PSA_SUCCESS) {
2320 psa_hash_abort(operation);
2321 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324}
2325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2327 uint8_t *hash,
2328 size_t hash_size,
2329 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002331 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (operation->id == 0) {
2333 return PSA_ERROR_BAD_STATE;
2334 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335
Steven Cooreman1e582352021-02-18 17:24:37 +01002336 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 operation, hash, hash_size, hash_length);
2338 psa_hash_abort(operation);
2339 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340}
2341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2343 const uint8_t *hash,
2344 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345{
Ronald Cron69a63422021-10-18 09:47:58 +02002346 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002348 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 operation,
2350 actual_hash, sizeof(actual_hash),
2351 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358 status = PSA_ERROR_INVALID_SIGNATURE;
2359 goto exit;
2360 }
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002363 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002365
2366exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2368 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373}
2374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375psa_status_t psa_hash_compute(psa_algorithm_t alg,
2376 const uint8_t *input, size_t input_length,
2377 uint8_t *hash, size_t hash_size,
2378 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002379{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002380 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (!PSA_ALG_IS_HASH(alg)) {
2382 return PSA_ERROR_INVALID_ARGUMENT;
2383 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2386 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002387}
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_hash_compare(psa_algorithm_t alg,
2390 const uint8_t *input, size_t input_length,
2391 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002392{
Ronald Cron69a63422021-10-18 09:47:58 +02002393 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002394 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (!PSA_ALG_IS_HASH(alg)) {
2397 return PSA_ERROR_INVALID_ARGUMENT;
2398 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002399
Steven Cooreman1e582352021-02-18 17:24:37 +01002400 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 alg, input, input_length,
2402 actual_hash, sizeof(actual_hash),
2403 &actual_hash_length);
2404 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002405 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 }
2407 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002408 status = PSA_ERROR_INVALID_SIGNATURE;
2409 goto exit;
2410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002412 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002414
2415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2417 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002418}
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2421 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002422{
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (source_operation->id == 0 ||
2424 target_operation->id != 0) {
2425 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002426 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2429 target_operation);
2430 if (status != PSA_SUCCESS) {
2431 psa_hash_abort(target_operation);
2432 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002435}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436
2437
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439/* MAC */
2440/****************************************************************/
2441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002443{
Steven Cooremane6804192021-03-19 18:28:56 +01002444 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (operation->id == 0) {
2446 return PSA_SUCCESS;
2447 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002450 operation->mac_size = 0;
2451 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002452 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002455}
2456
Ronald Cron2dff3b22021-06-17 16:33:22 +02002457static psa_status_t psa_mac_finalize_alg_and_key_validation(
2458 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002459 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 psa_key_type_t key_type = psa_get_key_type(attributes);
2464 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (!PSA_ALG_IS_MAC(alg)) {
2467 return PSA_ERROR_INVALID_ARGUMENT;
2468 }
Steven Cooremane6804192021-03-19 18:28:56 +01002469
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002470 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 status = psa_mac_key_can_do(alg, key_type);
2472 if (status != PSA_SUCCESS) {
2473 return status;
2474 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002475
Steven Cooremand1a68f12021-05-10 11:13:41 +02002476 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002480 /* A very short MAC is too short for security since it can be
2481 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2482 * so we make this our minimum, even though 32 bits is still
2483 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002485 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2488 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002489 /* It's impossible to "truncate" to a larger length than the full length
2490 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002492 }
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002495 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2496 * that is disabled in the compile-time configuration. The result can
2497 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2498 * configuration into account. In this case, force a return of
2499 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2500 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2501 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2502 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2503 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002505 }
2506
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002508}
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2511 mbedtls_svc_key_id_t key,
2512 psa_algorithm_t alg,
2513 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002514{
2515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2516 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002517 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002518 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519
2520 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002522 status = PSA_ERROR_BAD_STATE;
2523 goto exit;
2524 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525
2526 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 key,
2528 &slot,
2529 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2530 alg);
2531 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002535 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 .core = slot->attr
2537 };
2538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2540 &operation->mac_size);
2541 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544
2545 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002546 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (is_sign) {
2548 status = psa_driver_wrapper_mac_sign_setup(operation,
2549 &attributes,
2550 slot->key.data,
2551 slot->key.bytes,
2552 alg);
2553 } else {
2554 status = psa_driver_wrapper_mac_verify_setup(operation,
2555 &attributes,
2556 slot->key.data,
2557 slot->key.bytes,
2558 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002560
Gilles Peskinefbfac682018-07-08 20:51:54 +02002561exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (status != PSA_SUCCESS) {
2563 psa_mac_abort(operation);
2564 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569}
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2572 mbedtls_svc_key_id_t key,
2573 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002574{
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002576}
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2579 mbedtls_svc_key_id_t key,
2580 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002581{
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002583}
2584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2586 const uint8_t *input,
2587 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588{
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (operation->id == 0) {
2590 return PSA_ERROR_BAD_STATE;
2591 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002592
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002593 /* Don't require hash implementations to behave correctly on a
2594 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (input_length == 0) {
2596 return PSA_SUCCESS;
2597 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2600 input, input_length);
2601 if (status != PSA_SUCCESS) {
2602 psa_mac_abort(operation);
2603 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606}
2607
Gilles Peskine449bd832023-01-11 14:50:10 +01002608psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2609 uint8_t *mac,
2610 size_t mac_size,
2611 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002612{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2614 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002617 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002618 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002619 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002622 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002623 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002624 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002625
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002626 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2627 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002629 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002630 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002631 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002634 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002635 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002636 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 status = psa_driver_wrapper_mac_sign_finish(operation,
2639 mac, operation->mac_size,
2640 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002641
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002642exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002643 /* In case of success, set the potential excess room in the output buffer
2644 * to an invalid value, to avoid potentially leaking a longer MAC.
2645 * In case of error, set the output length and content to a safe default,
2646 * such that in case the caller misses an error check, the output would be
2647 * an unachievable MAC.
2648 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002650 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002651 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002652 }
mohammad16036df908f2018-04-02 08:34:15 -07002653
Paul Elliottdc42ca82023-02-24 18:11:59 +00002654 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002659}
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2662 const uint8_t *mac,
2663 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2666 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002669 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002670 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002671 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002674 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002675 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002676 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002679 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002680 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002681 }
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_driver_wrapper_mac_verify_finish(operation,
2684 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002685
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002686exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690}
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2693 psa_algorithm_t alg,
2694 const uint8_t *input,
2695 size_t input_length,
2696 uint8_t *mac,
2697 size_t mac_size,
2698 size_t *mac_length,
2699 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002700{
2701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002702 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2703 psa_key_slot_t *slot;
2704 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002705 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706
Ronald Cron51131b52021-06-17 17:17:20 +02002707 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 key,
2709 &slot,
2710 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2711 alg);
2712 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002715
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002716 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002717 .core = slot->attr
2718 };
2719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2721 &operation_mac_size);
2722 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002727 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002728 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002729 }
2730
2731 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 &attributes,
2733 slot->key.data, slot->key.bytes,
2734 alg,
2735 input, input_length,
2736 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002737
2738exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002739 /* In case of success, set the potential excess room in the output buffer
2740 * to an invalid value, to avoid potentially leaking a longer MAC.
2741 * In case of error, set the output length and content to a safe default,
2742 * such that in case the caller misses an error check, the output would be
2743 * an unachievable MAC.
2744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002746 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002747 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002748 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002749
2750 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002755}
2756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002758 psa_algorithm_t alg,
2759 const uint8_t *input,
2760 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 uint8_t *mac,
2762 size_t mac_size,
2763 size_t *mac_length)
2764{
2765 return psa_mac_compute_internal(key, alg,
2766 input, input_length,
2767 mac, mac_size, mac_length, 1);
2768}
2769
2770psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2771 psa_algorithm_t alg,
2772 const uint8_t *input,
2773 size_t input_length,
2774 const uint8_t *mac,
2775 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776{
2777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002778 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2779 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 status = psa_mac_compute_internal(key, alg,
2782 input, input_length,
2783 actual_mac, sizeof(actual_mac),
2784 &actual_mac_length, 0);
2785 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002790 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002794 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002795 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002796 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002797
2798exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002802}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002803
Gilles Peskinee59236f2018-01-27 23:32:46 +01002804/****************************************************************/
2805/* Asymmetric cryptography */
2806/****************************************************************/
2807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2809 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002810{
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (input_is_message) {
2812 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2813 return PSA_ERROR_INVALID_ARGUMENT;
2814 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2817 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2818 return PSA_ERROR_INVALID_ARGUMENT;
2819 }
2820 }
2821 } else {
2822 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2823 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002824 }
2825 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002828}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2831 int input_is_message,
2832 psa_algorithm_t alg,
2833 const uint8_t *input,
2834 size_t input_length,
2835 uint8_t *signature,
2836 size_t signature_size,
2837 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002838{
2839 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2840 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2841 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002842 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002843
2844 *signature_length = 0;
2845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 status = psa_sign_verify_check_alg(input_is_message, alg);
2847 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002848 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002850
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002851 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002852 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002853 * buffer can in principle be empty since it doesn't actually have
2854 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if (signature_size == 0) {
2856 return PSA_ERROR_BUFFER_TOO_SMALL;
2857 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002858
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002859 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 key, &slot,
2861 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2862 PSA_KEY_USAGE_SIGN_HASH,
2863 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002866 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002870 status = PSA_ERROR_INVALID_ARGUMENT;
2871 goto exit;
2872 }
2873
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002874 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002876 };
2877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002879 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002880 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002881 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 signature, signature_size, signature_length);
2883 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884
2885 status = psa_driver_wrapper_sign_hash(
2886 &attributes, slot->key.data, slot->key.bytes,
2887 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002889 }
2890
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002891
2892exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002893 psa_wipe_tag_output_buffer(signature, status, signature_size,
2894 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899}
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2902 int input_is_message,
2903 psa_algorithm_t alg,
2904 const uint8_t *input,
2905 size_t input_length,
2906 const uint8_t *signature,
2907 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002908{
2909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2910 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2911 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 status = psa_sign_verify_check_alg(input_is_message, alg);
2914 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002917
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002918 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 key, &slot,
2920 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2921 PSA_KEY_USAGE_VERIFY_HASH,
2922 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002923
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (status != PSA_SUCCESS) {
2925 return status;
2926 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927
2928 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930 };
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002933 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002934 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002935 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 signature, signature_length);
2937 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002938 status = psa_driver_wrapper_verify_hash(
2939 &attributes, slot->key.data, slot->key.bytes,
2940 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002942 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947
2948}
2949
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002950psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002951 const psa_key_attributes_t *attributes,
2952 const uint8_t *key_buffer,
2953 size_t key_buffer_size,
2954 psa_algorithm_t alg,
2955 const uint8_t *input,
2956 size_t input_length,
2957 uint8_t *signature,
2958 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002960{
2961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002964 size_t hash_length;
2965 uint8_t hash[PSA_HASH_MAX_SIZE];
2966
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002967 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 PSA_ALG_SIGN_GET_HASH(alg),
2969 input, input_length,
2970 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002973 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002975
2976 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 attributes, key_buffer, key_buffer_size,
2978 alg, hash, hash_length,
2979 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002980 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002983}
2984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2986 psa_algorithm_t alg,
2987 const uint8_t *input,
2988 size_t input_length,
2989 uint8_t *signature,
2990 size_t signature_size,
2991 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002992{
2993 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002994 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002996}
2997
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002998psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002999 const psa_key_attributes_t *attributes,
3000 const uint8_t *key_buffer,
3001 size_t key_buffer_size,
3002 psa_algorithm_t alg,
3003 const uint8_t *input,
3004 size_t input_length,
3005 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003007{
3008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003011 size_t hash_length;
3012 uint8_t hash[PSA_HASH_MAX_SIZE];
3013
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003014 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 PSA_ALG_SIGN_GET_HASH(alg),
3016 input, input_length,
3017 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003020 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003022
3023 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 attributes, key_buffer, key_buffer_size,
3025 alg, hash, hash_length,
3026 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003027 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003030}
3031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3033 psa_algorithm_t alg,
3034 const uint8_t *input,
3035 size_t input_length,
3036 const uint8_t *signature,
3037 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003038{
3039 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003040 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003042}
3043
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003044psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003045 const psa_key_attributes_t *attributes,
3046 const uint8_t *key_buffer, size_t key_buffer_size,
3047 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003049{
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3051 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3052 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003053#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3055 return mbedtls_psa_rsa_sign_hash(
3056 attributes,
3057 key_buffer, key_buffer_size,
3058 alg, hash, hash_length,
3059 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003060#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3061 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 } else {
3063 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003064 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3066 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003067#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3069 return mbedtls_psa_ecdsa_sign_hash(
3070 attributes,
3071 key_buffer, key_buffer_size,
3072 alg, hash, hash_length,
3073 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003074#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3075 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 } else {
3077 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003078 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003079 }
Ronald Cron4501c982021-03-19 20:09:32 +01003080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 (void) key_buffer;
3082 (void) key_buffer_size;
3083 (void) hash;
3084 (void) hash_length;
3085 (void) signature;
3086 (void) signature_size;
3087 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003090}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3093 psa_algorithm_t alg,
3094 const uint8_t *hash,
3095 size_t hash_length,
3096 uint8_t *signature,
3097 size_t signature_size,
3098 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003099{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003100 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003101 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003103}
3104
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003105psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003106 const psa_key_attributes_t *attributes,
3107 const uint8_t *key_buffer, size_t key_buffer_size,
3108 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003110{
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3112 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3113 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003114#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3116 return mbedtls_psa_rsa_verify_hash(
3117 attributes,
3118 key_buffer, key_buffer_size,
3119 alg, hash, hash_length,
3120 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003121#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3122 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 } else {
3124 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3127 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003128#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3130 return mbedtls_psa_ecdsa_verify_hash(
3131 attributes,
3132 key_buffer, key_buffer_size,
3133 alg, hash, hash_length,
3134 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003135#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3136 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 } else {
3138 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003139 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003140 }
Ronald Cron4501c982021-03-19 20:09:32 +01003141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 (void) key_buffer;
3143 (void) key_buffer_size;
3144 (void) hash;
3145 (void) hash_length;
3146 (void) signature;
3147 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003150}
3151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3153 psa_algorithm_t alg,
3154 const uint8_t *hash,
3155 size_t hash_length,
3156 const uint8_t *signature,
3157 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003158{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003159 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003160 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003162}
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3165 psa_algorithm_t alg,
3166 const uint8_t *input,
3167 size_t input_length,
3168 const uint8_t *salt,
3169 size_t salt_length,
3170 uint8_t *output,
3171 size_t output_size,
3172 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003175 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003176 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003177 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003178
Darryl Green5cc689a2018-07-24 15:34:10 +01003179 (void) input;
3180 (void) input_length;
3181 (void) salt;
3182 (void) output;
3183 (void) output_size;
3184
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003185 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3188 return PSA_ERROR_INVALID_ARGUMENT;
3189 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003190
Ronald Cron5c522922020-11-14 16:35:34 +01003191 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3193 if (status != PSA_SUCCESS) {
3194 return status;
3195 }
3196 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3197 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003198 status = PSA_ERROR_INVALID_ARGUMENT;
3199 goto exit;
3200 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003202 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003204 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003205
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003206 status = psa_driver_wrapper_asymmetric_encrypt(
3207 &attributes, slot->key.data, slot->key.bytes,
3208 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003214}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *input,
3219 size_t input_length,
3220 const uint8_t *salt,
3221 size_t salt_length,
3222 uint8_t *output,
3223 size_t output_size,
3224 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003227 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003228 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003229 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003230
Darryl Green5cc689a2018-07-24 15:34:10 +01003231 (void) input;
3232 (void) input_length;
3233 (void) salt;
3234 (void) output;
3235 (void) output_size;
3236
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003237 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3240 return PSA_ERROR_INVALID_ARGUMENT;
3241 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003242
Ronald Cron5c522922020-11-14 16:35:34 +01003243 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3245 if (status != PSA_SUCCESS) {
3246 return status;
3247 }
3248 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003249 status = PSA_ERROR_INVALID_ARGUMENT;
3250 goto exit;
3251 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003253 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003255 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003257 status = psa_driver_wrapper_asymmetric_decrypt(
3258 &attributes, slot->key.data, slot->key.bytes,
3259 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003261
3262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003266}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003267
Paul Elliott9fe12f62022-11-30 19:16:02 +00003268/****************************************************************/
3269/* Asymmetric interruptible cryptography */
3270/****************************************************************/
3271
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003272static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3273
Paul Elliott9fe12f62022-11-30 19:16:02 +00003274void psa_interruptible_set_max_ops(uint32_t max_ops)
3275{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003276 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003277}
3278
3279uint32_t psa_interruptible_get_max_ops(void)
3280{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003281 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003282}
3283
Paul Elliott9fe12f62022-11-30 19:16:02 +00003284uint32_t psa_sign_hash_get_num_ops(
3285 const psa_sign_hash_interruptible_operation_t *operation)
3286{
Paul Elliott296ede92022-12-15 17:00:30 +00003287 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003288}
3289
3290uint32_t psa_verify_hash_get_num_ops(
3291 const psa_verify_hash_interruptible_operation_t *operation)
3292{
Paul Elliott296ede92022-12-15 17:00:30 +00003293 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003294}
3295
Paul Elliott59ad9452022-12-18 15:09:02 +00003296static psa_status_t psa_sign_hash_abort_internal(
3297 psa_sign_hash_interruptible_operation_t *operation)
3298{
3299 if (operation->id == 0) {
3300 /* The object has (apparently) been initialized but it is not (yet)
3301 * in use. It's ok to call abort on such an object, and there's
3302 * nothing to do. */
3303 return PSA_SUCCESS;
3304 }
3305
3306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3307
3308 status = psa_driver_wrapper_sign_hash_abort(operation);
3309
3310 operation->id = 0;
3311
Paul Elliotte6145dc2023-02-07 12:51:21 +00003312 /* Do not clear either the error_occurred or num_ops elements here as they
3313 * only want to be cleared by the application calling abort, not by abort
3314 * being called at completion of an operation. */
3315
Paul Elliott59ad9452022-12-18 15:09:02 +00003316 return status;
3317}
3318
Paul Elliott9fe12f62022-11-30 19:16:02 +00003319psa_status_t psa_sign_hash_start(
3320 psa_sign_hash_interruptible_operation_t *operation,
3321 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3322 const uint8_t *hash, size_t hash_length)
3323{
3324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3325 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3326 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003327 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003328
Paul Elliottc9774412023-02-06 15:14:07 +00003329 /* Check that start has not been previously called, or operation has not
3330 * previously errored. */
3331 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332 return PSA_ERROR_BAD_STATE;
3333 }
3334
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335 status = psa_sign_verify_check_alg(0, alg);
3336 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003337 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338 return status;
3339 }
3340
3341 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3342 PSA_KEY_USAGE_SIGN_HASH,
3343 alg);
3344
3345 if (status != PSA_SUCCESS) {
3346 goto exit;
3347 }
3348
3349 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3350 status = PSA_ERROR_INVALID_ARGUMENT;
3351 goto exit;
3352 }
3353
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003354 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355 .core = slot->attr
3356 };
3357
Paul Elliott296ede92022-12-15 17:00:30 +00003358 /* Ensure ops count gets reset, in case of operation re-use. */
3359 operation->num_ops = 0;
3360
Paul Elliott9fe12f62022-11-30 19:16:02 +00003361 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3362 slot->key.data,
3363 slot->key.bytes, alg,
3364 hash, hash_length);
3365exit:
3366
3367 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003368 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003369 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003370 }
3371
3372 unlock_status = psa_unlock_key_slot(slot);
3373
Paul Elliottc9774412023-02-06 15:14:07 +00003374 if (unlock_status != PSA_SUCCESS) {
3375 operation->error_occurred = 1;
3376 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003377
Paul Elliottc9774412023-02-06 15:14:07 +00003378 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003379}
3380
3381
3382psa_status_t psa_sign_hash_complete(
3383 psa_sign_hash_interruptible_operation_t *operation,
3384 uint8_t *signature, size_t signature_size,
3385 size_t *signature_length)
3386{
3387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3388
3389 *signature_length = 0;
3390
Paul Elliottc9774412023-02-06 15:14:07 +00003391 /* Check that start has been called first, and that operation has not
3392 * previously errored. */
3393 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003394 status = PSA_ERROR_BAD_STATE;
3395 goto exit;
3396 }
3397
Paul Elliott096abc42023-02-03 18:33:23 +00003398 /* Immediately reject a zero-length signature buffer. This guarantees that
3399 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003400 if (signature_size == 0) {
3401 status = PSA_ERROR_BUFFER_TOO_SMALL;
3402 goto exit;
3403 }
3404
3405 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3406 signature_size,
3407 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003408
Paul Elliott296ede92022-12-15 17:00:30 +00003409 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003410 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003411
Paul Elliottebe225c2023-02-10 14:32:53 +00003412exit:
3413
Paul Elliott59200a22023-02-21 15:07:40 +00003414 psa_wipe_tag_output_buffer(signature, status, signature_size,
3415 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416
Paul Elliott53bb3122023-02-10 14:22:22 +00003417 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003418 if (status != PSA_SUCCESS) {
3419 operation->error_occurred = 1;
3420 }
3421
Paul Elliott59ad9452022-12-18 15:09:02 +00003422 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003423 }
3424
3425 return status;
3426}
3427
3428psa_status_t psa_sign_hash_abort(
3429 psa_sign_hash_interruptible_operation_t *operation)
3430{
Paul Elliott59ad9452022-12-18 15:09:02 +00003431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3432
3433 status = psa_sign_hash_abort_internal(operation);
3434
3435 /* We clear the number of ops done here, so that it is not cleared when
3436 * the operation fails or succeeds, only on manual abort. */
3437 operation->num_ops = 0;
3438
Paul Elliottc9774412023-02-06 15:14:07 +00003439 /* Likewise, failure state. */
3440 operation->error_occurred = 0;
3441
Paul Elliott59ad9452022-12-18 15:09:02 +00003442 return status;
3443}
3444
3445static psa_status_t psa_verify_hash_abort_internal(
3446 psa_verify_hash_interruptible_operation_t *operation)
3447{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003448 if (operation->id == 0) {
3449 /* The object has (apparently) been initialized but it is not (yet)
3450 * in use. It's ok to call abort on such an object, and there's
3451 * nothing to do. */
3452 return PSA_SUCCESS;
3453 }
3454
Paul Elliott59ad9452022-12-18 15:09:02 +00003455 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3456
3457 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458
3459 operation->id = 0;
3460
Paul Elliotte6145dc2023-02-07 12:51:21 +00003461 /* Do not clear either the error_occurred or num_ops elements here as they
3462 * only want to be cleared by the application calling abort, not by abort
3463 * being called at completion of an operation. */
3464
Paul Elliott59ad9452022-12-18 15:09:02 +00003465 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003466}
3467
3468psa_status_t psa_verify_hash_start(
3469 psa_verify_hash_interruptible_operation_t *operation,
3470 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3471 const uint8_t *hash, size_t hash_length,
3472 const uint8_t *signature, size_t signature_length)
3473{
3474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3475 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3476 psa_key_slot_t *slot;
3477
Paul Elliottc9774412023-02-06 15:14:07 +00003478 /* Check that start has not been previously called, or operation has not
3479 * previously errored. */
3480 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481 return PSA_ERROR_BAD_STATE;
3482 }
3483
3484 status = psa_sign_verify_check_alg(0, alg);
3485 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003486 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 return status;
3488 }
3489
3490 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3491 PSA_KEY_USAGE_VERIFY_HASH,
3492 alg);
3493
3494 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003495 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003496 return status;
3497 }
3498
3499 psa_key_attributes_t attributes = {
3500 .core = slot->attr
3501 };
3502
Paul Elliott296ede92022-12-15 17:00:30 +00003503 /* Ensure ops count gets reset, in case of operation re-use. */
3504 operation->num_ops = 0;
3505
Paul Elliott9fe12f62022-11-30 19:16:02 +00003506 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3507 slot->key.data,
3508 slot->key.bytes,
3509 alg, hash, hash_length,
3510 signature, signature_length);
3511
3512 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003513 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003514 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003515 }
3516
3517 unlock_status = psa_unlock_key_slot(slot);
3518
Paul Elliottc9774412023-02-06 15:14:07 +00003519 if (unlock_status != PSA_SUCCESS) {
3520 operation->error_occurred = 1;
3521 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522
Paul Elliottc9774412023-02-06 15:14:07 +00003523 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003524}
3525
3526psa_status_t psa_verify_hash_complete(
3527 psa_verify_hash_interruptible_operation_t *operation)
3528{
3529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3530
Paul Elliottc9774412023-02-06 15:14:07 +00003531 /* Check that start has been called first, and that operation has not
3532 * previously errored. */
3533 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003534 status = PSA_ERROR_BAD_STATE;
3535 goto exit;
3536 }
3537
3538 status = psa_driver_wrapper_verify_hash_complete(operation);
3539
Paul Elliott296ede92022-12-15 17:00:30 +00003540 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003541 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003542 operation);
3543
Paul Elliottebe225c2023-02-10 14:32:53 +00003544exit:
3545
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003547 if (status != PSA_SUCCESS) {
3548 operation->error_occurred = 1;
3549 }
3550
Paul Elliott59ad9452022-12-18 15:09:02 +00003551 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003552 }
3553
3554 return status;
3555}
3556
3557psa_status_t psa_verify_hash_abort(
3558 psa_verify_hash_interruptible_operation_t *operation)
3559{
Paul Elliott59ad9452022-12-18 15:09:02 +00003560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003561
Paul Elliott59ad9452022-12-18 15:09:02 +00003562 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003563
Paul Elliott59ad9452022-12-18 15:09:02 +00003564 /* We clear the number of ops done here, so that it is not cleared when
3565 * the operation fails or succeeds, only on manual abort. */
3566 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003567
Paul Elliottc9774412023-02-06 15:14:07 +00003568 /* Likewise, failure state. */
3569 operation->error_occurred = 0;
3570
Paul Elliott59ad9452022-12-18 15:09:02 +00003571 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003572}
3573
Paul Elliott588f8ed2022-12-02 18:10:26 +00003574/****************************************************************/
3575/* Asymmetric interruptible cryptography internal */
3576/* implementations */
3577/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003578
Paul Elliott588f8ed2022-12-02 18:10:26 +00003579void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3580{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003581
Paul Elliott588f8ed2022-12-02 18:10:26 +00003582#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3583 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3584 defined(MBEDTLS_ECP_RESTARTABLE)
3585
3586 /* Internal implementation uses zero to indicate infinite number max ops,
3587 * therefore avoid this value, and set to minimum possible. */
3588 if (max_ops == 0) {
3589 max_ops = 1;
3590 }
3591
Paul Elliott588f8ed2022-12-02 18:10:26 +00003592 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003593#else
3594 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003595#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3596 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3597 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3598}
3599
Paul Elliott588f8ed2022-12-02 18:10:26 +00003600uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003601 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003602{
3603#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3604 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3605 defined(MBEDTLS_ECP_RESTARTABLE)
3606
Paul Elliott93d9ca82023-02-15 18:14:21 +00003607 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003608#else
3609 (void) operation;
3610 return 0;
3611#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3612 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3613 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3614}
3615
3616uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003617 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003618{
3619 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3620 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3621 defined(MBEDTLS_ECP_RESTARTABLE)
3622
Paul Elliott93d9ca82023-02-15 18:14:21 +00003623 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003624#else
3625 (void) operation;
3626 return 0;
3627#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3628 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3629 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3630}
3631
3632psa_status_t mbedtls_psa_sign_hash_start(
3633 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3634 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3635 size_t key_buffer_size, psa_algorithm_t alg,
3636 const uint8_t *hash, size_t hash_length)
3637{
3638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003639 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640
Paul Elliott068fe072023-01-16 13:59:15 +00003641 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3642 return PSA_ERROR_NOT_SUPPORTED;
3643 }
3644
3645 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003646 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003647 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648
3649#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003650 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3651 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652
Paul Elliotta1c94092023-02-15 16:38:04 +00003653 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3654
Paul Elliott93d9ca82023-02-15 18:14:21 +00003655 /* Ensure num_ops is zero'ed in case of context re-use. */
3656 operation->num_ops = 0;
3657
Paul Elliott068fe072023-01-16 13:59:15 +00003658 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3659 attributes->core.bits,
3660 key_buffer,
3661 key_buffer_size,
3662 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663
Paul Elliott068fe072023-01-16 13:59:15 +00003664 if (status != PSA_SUCCESS) {
3665 return status;
3666 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003667
Paul Elliott1bc59df2023-02-05 13:41:57 +00003668 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003669 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670
Paul Elliott068fe072023-01-16 13:59:15 +00003671 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003672 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003673 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674
Paul Elliott0290a762023-02-09 14:30:24 +00003675 /* We only need to store the same length of hash as the private key size
3676 * here, it would be truncated by the internal implementation anyway. */
3677 required_hash_length = (hash_length < operation->coordinate_bytes ?
3678 hash_length : operation->coordinate_bytes);
3679
Paul Elliottba70ad42023-02-15 18:23:53 +00003680 if (required_hash_length > sizeof(operation->hash)) {
3681 /* Shouldn't happen, but better safe than sorry. */
3682 return PSA_ERROR_CORRUPTION_DETECTED;
3683 }
3684
Paul Elliott0290a762023-02-09 14:30:24 +00003685 memcpy(operation->hash, hash, required_hash_length);
3686 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003687
3688 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003689
3690#else
Paul Elliott068fe072023-01-16 13:59:15 +00003691 (void) operation;
3692 (void) key_buffer;
3693 (void) key_buffer_size;
3694 (void) alg;
3695 (void) hash;
3696 (void) hash_length;
3697 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003698 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003699
Paul Elliott068fe072023-01-16 13:59:15 +00003700 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3702 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3703 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704}
3705
3706psa_status_t mbedtls_psa_sign_hash_complete(
3707 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3708 uint8_t *signature, size_t signature_size,
3709 size_t *signature_length)
3710{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3712 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3713 defined(MBEDTLS_ECP_RESTARTABLE)
3714
Paul Elliotta1c94092023-02-15 16:38:04 +00003715 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003716 mbedtls_mpi r;
3717 mbedtls_mpi s;
3718
Paul Elliott46845252023-02-03 14:59:11 +00003719 mbedtls_mpi_init(&r);
3720 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003721
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003722 /* Ensure max_ops is set to the current value (or default). */
3723 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3724
Paul Elliotta1c94092023-02-15 16:38:04 +00003725 if (signature_size < 2 * operation->coordinate_bytes) {
3726 status = PSA_ERROR_BUFFER_TOO_SMALL;
3727 goto exit;
3728 }
3729
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003731
Paul Elliott588f8ed2022-12-02 18:10:26 +00003732#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3733 status = mbedtls_to_psa_error(
3734 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003735 &r,
3736 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737 &operation->ctx->d,
3738 operation->hash,
3739 operation->hash_length,
3740 operation->md_alg,
3741 mbedtls_psa_get_random,
3742 MBEDTLS_PSA_RANDOM_STATE,
3743 &operation->restart_ctx));
3744#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003745 status = PSA_ERROR_NOT_SUPPORTED;
3746 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003747#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3748 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003749 status = mbedtls_to_psa_error(
3750 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003751 &r,
3752 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753 &operation->ctx->d,
3754 operation->hash,
3755 operation->hash_length,
3756 mbedtls_psa_get_random,
3757 MBEDTLS_PSA_RANDOM_STATE,
3758 mbedtls_psa_get_random,
3759 MBEDTLS_PSA_RANDOM_STATE,
3760 &operation->restart_ctx));
3761 }
3762
Paul Elliottf8e5b562023-02-19 18:43:45 +00003763 /* Hide the fact that the restart context only holds a delta of number of
3764 * ops done during the last operation, not an absolute value. */
3765 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3766
Paul Elliott724bd252023-02-08 12:35:08 +00003767 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003769 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003771 operation->coordinate_bytes)
3772 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003773
3774 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003775 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003776 }
3777
3778 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003779 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003780 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003781 operation->coordinate_bytes,
3782 operation->coordinate_bytes)
3783 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784
3785 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003786 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787 }
3788
Paul Elliott1bc59df2023-02-05 13:41:57 +00003789 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003790
Paul Elliott724bd252023-02-08 12:35:08 +00003791 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003792 }
Paul Elliott724bd252023-02-08 12:35:08 +00003793
3794exit:
3795
3796 mbedtls_mpi_free(&r);
3797 mbedtls_mpi_free(&s);
3798 return status;
3799
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800 #else
3801
3802 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803 (void) signature;
3804 (void) signature_size;
3805 (void) signature_length;
3806
3807 return PSA_ERROR_NOT_SUPPORTED;
3808
3809#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3810 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3811 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3812}
3813
3814psa_status_t mbedtls_psa_sign_hash_abort(
3815 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3816{
3817
3818#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3819 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3820 defined(MBEDTLS_ECP_RESTARTABLE)
3821
3822 if (operation->ctx) {
3823 mbedtls_ecdsa_free(operation->ctx);
3824 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003825 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003826 }
3827
3828 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3829
Paul Elliott93d9ca82023-02-15 18:14:21 +00003830 operation->num_ops = 0;
3831
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 return PSA_SUCCESS;
3833
3834#else
3835
3836 (void) operation;
3837
3838 return PSA_ERROR_NOT_SUPPORTED;
3839
3840#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3841 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3842 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3843}
3844
3845psa_status_t mbedtls_psa_verify_hash_start(
3846 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3847 const psa_key_attributes_t *attributes,
3848 const uint8_t *key_buffer, size_t key_buffer_size,
3849 psa_algorithm_t alg,
3850 const uint8_t *hash, size_t hash_length,
3851 const uint8_t *signature, size_t signature_length)
3852{
3853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003854 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003855 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliott068fe072023-01-16 13:59:15 +00003857 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3858 return PSA_ERROR_NOT_SUPPORTED;
3859 }
3860
3861 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003862 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003863 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003864
3865#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003866 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3867 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003868
Paul Elliotta1c94092023-02-15 16:38:04 +00003869 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3870 mbedtls_mpi_init(&operation->r);
3871 mbedtls_mpi_init(&operation->s);
3872
Paul Elliott93d9ca82023-02-15 18:14:21 +00003873 /* Ensure num_ops is zero'ed in case of context re-use. */
3874 operation->num_ops = 0;
3875
Paul Elliott068fe072023-01-16 13:59:15 +00003876 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3877 attributes->core.bits,
3878 key_buffer,
3879 key_buffer_size,
3880 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003881
Paul Elliott068fe072023-01-16 13:59:15 +00003882 if (status != PSA_SUCCESS) {
3883 return status;
3884 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003885
Paul Elliottc569fc22023-02-10 13:02:54 +00003886 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887
Paul Elliott1bc59df2023-02-05 13:41:57 +00003888 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003889 return PSA_ERROR_INVALID_SIGNATURE;
3890 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003891
Paul Elliott068fe072023-01-16 13:59:15 +00003892 status = mbedtls_to_psa_error(
3893 mbedtls_mpi_read_binary(&operation->r,
3894 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003895 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896
Paul Elliott068fe072023-01-16 13:59:15 +00003897 if (status != PSA_SUCCESS) {
3898 return status;
3899 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003900
Paul Elliott068fe072023-01-16 13:59:15 +00003901 status = mbedtls_to_psa_error(
3902 mbedtls_mpi_read_binary(&operation->s,
3903 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003904 coordinate_bytes,
3905 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003906
Paul Elliott068fe072023-01-16 13:59:15 +00003907 if (status != PSA_SUCCESS) {
3908 return status;
3909 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003910
Paul Elliott2c9843f2023-02-15 17:32:42 +00003911 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003912
Paul Elliott2c9843f2023-02-15 17:32:42 +00003913 if (status != PSA_SUCCESS) {
3914 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003915 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003916
Paul Elliott0290a762023-02-09 14:30:24 +00003917 /* We only need to store the same length of hash as the private key size
3918 * here, it would be truncated by the internal implementation anyway. */
3919 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3920 coordinate_bytes);
3921
Paul Elliottba70ad42023-02-15 18:23:53 +00003922 if (required_hash_length > sizeof(operation->hash)) {
3923 /* Shouldn't happen, but better safe than sorry. */
3924 return PSA_ERROR_CORRUPTION_DETECTED;
3925 }
3926
Paul Elliott0290a762023-02-09 14:30:24 +00003927 memcpy(operation->hash, hash, required_hash_length);
3928 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003929
3930 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931#else
Paul Elliott068fe072023-01-16 13:59:15 +00003932 (void) operation;
3933 (void) key_buffer;
3934 (void) key_buffer_size;
3935 (void) alg;
3936 (void) hash;
3937 (void) hash_length;
3938 (void) signature;
3939 (void) signature_length;
3940 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003941 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003942 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003943
Paul Elliott068fe072023-01-16 13:59:15 +00003944 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3946 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3947 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948}
3949
3950psa_status_t mbedtls_psa_verify_hash_complete(
3951 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3952{
3953
3954#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3955 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3956 defined(MBEDTLS_ECP_RESTARTABLE)
3957
Paul Elliottf8e5b562023-02-19 18:43:45 +00003958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3959
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003960 /* Ensure max_ops is set to the current value (or default). */
3961 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3962
Paul Elliottf8e5b562023-02-19 18:43:45 +00003963 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3965 operation->hash,
3966 operation->hash_length,
3967 &operation->ctx->Q,
3968 &operation->r,
3969 &operation->s,
3970 &operation->restart_ctx));
3971
Paul Elliottf8e5b562023-02-19 18:43:45 +00003972 /* Hide the fact that the restart context only holds a delta of number of
3973 * ops done during the last operation, not an absolute value. */
3974 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3975
3976 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003977#else
3978 (void) operation;
3979
3980 return PSA_ERROR_NOT_SUPPORTED;
3981
3982#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3983 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3984 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3985}
3986
3987psa_status_t mbedtls_psa_verify_hash_abort(
3988 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3989{
3990
3991#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3992 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3993 defined(MBEDTLS_ECP_RESTARTABLE)
3994
3995 if (operation->ctx) {
3996 mbedtls_ecdsa_free(operation->ctx);
3997 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003998 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003999 }
4000
4001 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4002
Paul Elliott93d9ca82023-02-15 18:14:21 +00004003 operation->num_ops = 0;
4004
Paul Elliott588f8ed2022-12-02 18:10:26 +00004005 mbedtls_mpi_free(&operation->r);
4006 mbedtls_mpi_free(&operation->s);
4007
4008 return PSA_SUCCESS;
4009
4010#else
4011 (void) operation;
4012
4013 return PSA_ERROR_NOT_SUPPORTED;
4014
4015#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4016 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4017 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4018}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004019
mohammad1603503973b2018-03-12 15:59:30 +02004020/****************************************************************/
4021/* Symmetric cryptography */
4022/****************************************************************/
4023
Gilles Peskine449bd832023-01-11 14:50:10 +01004024static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4025 mbedtls_svc_key_id_t key,
4026 psa_algorithm_t alg,
4027 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004028{
4029 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4030 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004031 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4033 PSA_KEY_USAGE_ENCRYPT :
4034 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004035 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004036
4037 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004039 status = PSA_ERROR_BAD_STATE;
4040 goto exit;
4041 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004042
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto exit;
4046 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4049 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004052
Ronald Cron49fafa92021-03-10 08:34:23 +01004053 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004054 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004055 * so we only set it (in the driver wrapper) after resources have been
4056 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004057 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004059 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004061 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 }
4063 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004064
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004065 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004067 };
4068
Ronald Cronab99ac22020-10-01 16:38:28 +02004069 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (cipher_operation == MBEDTLS_ENCRYPT) {
4071 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4072 &attributes,
4073 slot->key.data,
4074 slot->key.bytes,
4075 alg);
4076 } else {
4077 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4078 &attributes,
4079 slot->key.data,
4080 slot->key.bytes,
4081 alg);
4082 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004083
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (status != PSA_SUCCESS) {
4086 psa_cipher_abort(operation);
4087 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004088
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004092}
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4095 mbedtls_svc_key_id_t key,
4096 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004097{
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004099}
4100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4102 mbedtls_svc_key_id_t key,
4103 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004104{
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004106}
4107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4109 uint8_t *iv,
4110 size_t iv_size,
4111 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004112{
Ronald Cron6d051732020-10-01 14:10:20 +02004113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004114 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4115 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004118 status = PSA_ERROR_BAD_STATE;
4119 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004120 }
4121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004123 status = PSA_ERROR_BAD_STATE;
4124 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004125 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004126
Ronald Cron2fb90522021-07-05 12:31:44 +02004127 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004129 status = PSA_ERROR_BUFFER_TOO_SMALL;
4130 goto exit;
4131 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004134 status = PSA_ERROR_GENERIC_ERROR;
4135 goto exit;
4136 }
4137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 status = psa_generate_random(local_iv, default_iv_length);
4139 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 }
Ronald Cron5618a392021-03-26 09:52:26 +01004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 status = psa_driver_wrapper_cipher_set_iv(operation,
4144 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004145
4146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (status == PSA_SUCCESS) {
4148 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004149 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004150 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004152 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004154 }
Ronald Cron6d051732020-10-01 14:10:20 +02004155
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004157}
4158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4160 const uint8_t *iv,
4161 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004162{
Ronald Cron6d051732020-10-01 14:10:20 +02004163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004166 status = PSA_ERROR_BAD_STATE;
4167 goto exit;
4168 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004171 status = PSA_ERROR_BAD_STATE;
4172 goto exit;
4173 }
Ronald Crona0d68172021-03-26 10:15:08 +01004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004176 status = PSA_ERROR_INVALID_ARGUMENT;
4177 goto exit;
4178 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 status = psa_driver_wrapper_cipher_set_iv(operation,
4181 iv,
4182 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004183
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004186 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 } else {
4188 psa_cipher_abort(operation);
4189 }
4190 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004191}
4192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4194 const uint8_t *input,
4195 size_t input_length,
4196 uint8_t *output,
4197 size_t output_size,
4198 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004199{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004203 status = PSA_ERROR_BAD_STATE;
4204 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004205 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004208 status = PSA_ERROR_BAD_STATE;
4209 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004210 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 status = psa_driver_wrapper_cipher_update(operation,
4213 input,
4214 input_length,
4215 output,
4216 output_size,
4217 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004218
4219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 if (status != PSA_SUCCESS) {
4221 psa_cipher_abort(operation);
4222 }
Ronald Cron6d051732020-10-01 14:10:20 +02004223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004225}
4226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4228 uint8_t *output,
4229 size_t output_size,
4230 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004231{
David Saadab4ecc272019-02-14 13:48:10 +02004232 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235 status = PSA_ERROR_BAD_STATE;
4236 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004237 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004240 status = PSA_ERROR_BAD_STATE;
4241 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004242 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 status = psa_driver_wrapper_cipher_finish(operation,
4245 output,
4246 output_size,
4247 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004248
4249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 if (status == PSA_SUCCESS) {
4251 return psa_cipher_abort(operation);
4252 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004253 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004257 }
mohammad1603503973b2018-03-12 15:59:30 +02004258}
4259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004261{
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004263 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004264 * in use. It's ok to call abort on such an object, and there's
4265 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004267 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004270
Ronald Cron49fafa92021-03-10 08:34:23 +01004271 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004272 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004273 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004276}
4277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4279 psa_algorithm_t alg,
4280 const uint8_t *input,
4281 size_t input_length,
4282 uint8_t *output,
4283 size_t output_size,
4284 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004285{
4286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004287 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004288 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004289 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4290 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004291 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004294 status = PSA_ERROR_INVALID_ARGUMENT;
4295 goto exit;
4296 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4299 PSA_KEY_USAGE_ENCRYPT,
4300 alg);
4301 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004302 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004304
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004305 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004307 };
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4310 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004311 status = PSA_ERROR_GENERIC_ERROR;
4312 goto exit;
4313 }
4314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (default_iv_length > 0) {
4316 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004317 status = PSA_ERROR_BUFFER_TOO_SMALL;
4318 goto exit;
4319 }
4320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 status = psa_generate_random(local_iv, default_iv_length);
4322 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004323 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004325 }
4326
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004327 status = psa_driver_wrapper_cipher_encrypt(
4328 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004329 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004330 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004332
4333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 unlock_status = psa_unlock_key_slot(slot);
4335 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004336 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004337 }
Ronald Cron23919522021-07-08 19:00:07 +02004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (status == PSA_SUCCESS) {
4340 if (default_iv_length > 0) {
4341 memcpy(output, local_iv, default_iv_length);
4342 }
4343 *output_length += default_iv_length;
4344 } else {
4345 *output_length = 0;
4346 }
4347
4348 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004349}
4350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4352 psa_algorithm_t alg,
4353 const uint8_t *input,
4354 size_t input_length,
4355 uint8_t *output,
4356 size_t output_size,
4357 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004358{
4359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004360 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004361 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004362 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004365 status = PSA_ERROR_INVALID_ARGUMENT;
4366 goto exit;
4367 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4370 PSA_KEY_USAGE_DECRYPT,
4371 alg);
4372 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004375
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004376 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004378 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004379
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4381 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004382 status = PSA_ERROR_INVALID_ARGUMENT;
4383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004385 status = PSA_ERROR_INVALID_ARGUMENT;
4386 goto exit;
4387 }
4388
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004389 status = psa_driver_wrapper_cipher_decrypt(
4390 &attributes, slot->key.data, slot->key.bytes,
4391 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004393
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004394exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 unlock_status = psa_unlock_key_slot(slot);
4396 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004397 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004401 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 }
Ronald Cron23919522021-07-08 19:00:07 +02004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004405}
4406
4407
mohammad16035955c982018-04-26 00:53:03 +03004408/****************************************************************/
4409/* AEAD */
4410/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004411
Paul Elliottbaff51c2021-09-28 17:44:45 +01004412/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004413static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004414{
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004416}
4417
4418/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004419static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4420 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004421{
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004425#if defined(PSA_WANT_ALG_GCM)
4426 case PSA_ALG_GCM:
4427 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 * arbitrarily large nonces. Please note that we do not generally
4429 * recommend the usage of nonces of greater length than
4430 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4431 * size, which can then lead to collisions if you encrypt a very
4432 * large number of messages.*/
4433 if (nonce_length != 0) {
4434 return PSA_SUCCESS;
4435 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004436 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004437#endif /* PSA_WANT_ALG_GCM */
4438#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004439 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 if (nonce_length >= 7 && nonce_length <= 13) {
4441 return PSA_SUCCESS;
4442 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004443 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004444#endif /* PSA_WANT_ALG_CCM */
4445#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004446 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (nonce_length == 12) {
4448 return PSA_SUCCESS;
4449 } else if (nonce_length == 8) {
4450 return PSA_ERROR_NOT_SUPPORTED;
4451 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004452 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004453#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004454 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004455 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004457 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004460}
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004463{
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4465 return PSA_ERROR_INVALID_ARGUMENT;
4466 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004469}
4470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4472 psa_algorithm_t alg,
4473 const uint8_t *nonce,
4474 size_t nonce_length,
4475 const uint8_t *additional_data,
4476 size_t additional_data_length,
4477 const uint8_t *plaintext,
4478 size_t plaintext_length,
4479 uint8_t *ciphertext,
4480 size_t ciphertext_size,
4481 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004482{
Ronald Cron215633c2021-03-16 17:15:37 +01004483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4484 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004485
mohammad1603f08a5502018-06-03 15:05:47 +03004486 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 status = psa_aead_check_algorithm(alg);
4489 if (status != PSA_SUCCESS) {
4490 return status;
4491 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004492
Ronald Cron9a986162021-03-26 12:40:07 +01004493 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4495 if (status != PSA_SUCCESS) {
4496 return status;
4497 }
mohammad16035955c982018-04-26 00:53:03 +03004498
Ronald Cron215633c2021-03-16 17:15:37 +01004499 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004501 };
mohammad16035955c982018-04-26 00:53:03 +03004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 status = psa_aead_check_nonce_length(alg, nonce_length);
4504 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004505 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004507
Ronald Cronde822812021-03-17 16:08:20 +01004508 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004509 &attributes, slot->key.data, slot->key.bytes,
4510 alg,
4511 nonce, nonce_length,
4512 additional_data, additional_data_length,
4513 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4517 memset(ciphertext, 0, ciphertext_size);
4518 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004519
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004524}
4525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4527 psa_algorithm_t alg,
4528 const uint8_t *nonce,
4529 size_t nonce_length,
4530 const uint8_t *additional_data,
4531 size_t additional_data_length,
4532 const uint8_t *ciphertext,
4533 size_t ciphertext_length,
4534 uint8_t *plaintext,
4535 size_t plaintext_size,
4536 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004537{
Ronald Cron215633c2021-03-16 17:15:37 +01004538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4539 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004540
Gilles Peskineee652a32018-06-01 19:23:52 +02004541 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 status = psa_aead_check_algorithm(alg);
4544 if (status != PSA_SUCCESS) {
4545 return status;
4546 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004547
Ronald Cron9a986162021-03-26 12:40:07 +01004548 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4550 if (status != PSA_SUCCESS) {
4551 return status;
4552 }
mohammad16035955c982018-04-26 00:53:03 +03004553
Ronald Cron215633c2021-03-16 17:15:37 +01004554 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004556 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 status = psa_aead_check_nonce_length(alg, nonce_length);
4559 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004562
Ronald Cronde822812021-03-17 16:08:20 +01004563 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004564 &attributes, slot->key.data, slot->key.bytes,
4565 alg,
4566 nonce, nonce_length,
4567 additional_data, additional_data_length,
4568 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (status != PSA_SUCCESS && plaintext_size != 0) {
4572 memset(plaintext, 0, plaintext_size);
4573 }
mohammad160360a64d02018-06-03 17:20:42 +03004574
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004575exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 return status;
mohammad16035955c982018-04-26 00:53:03 +03004579}
4580
Gilles Peskine449bd832023-01-11 14:50:10 +01004581static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4582{
4583 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004586#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004588 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4590 return PSA_ERROR_INVALID_ARGUMENT;
4591 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004592 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004593#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004594
Przemek Stekiel86679c72022-10-06 17:06:56 +02004595#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004597 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4599 return PSA_ERROR_INVALID_ARGUMENT;
4600 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004601 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004602#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004603
Przemek Stekiel86679c72022-10-06 17:06:56 +02004604#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004606 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 if (tag_len != 16) {
4608 return PSA_ERROR_INVALID_ARGUMENT;
4609 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004610 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004611#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004612
4613 default:
4614 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004616 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004618}
4619
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004620/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004621static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4622 int is_encrypt,
4623 mbedtls_svc_key_id_t key,
4624 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004625{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4627 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4628 psa_key_slot_t *slot = NULL;
4629 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004630 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 status = psa_aead_check_algorithm(alg);
4633 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004638 status = PSA_ERROR_BAD_STATE;
4639 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004640 }
4641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if (operation->nonce_set || operation->lengths_set ||
4643 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004644 status = PSA_ERROR_BAD_STATE;
4645 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004646 }
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004649 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004651 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4655 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 Elliotte0a12bd2021-08-19 18:55:56 +01004659
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004660 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004661 .core = slot->attr
4662 };
4663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (is_encrypt) {
4669 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4670 &attributes,
4671 slot->key.data,
4672 slot->key.bytes,
4673 alg);
4674 } else {
4675 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4676 &attributes,
4677 slot->key.data,
4678 slot->key.bytes,
4679 alg);
4680 }
4681 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004682 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686
4687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004691 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004693 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 } else {
4695 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004696 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004699}
4700
Paul Elliott302ff6b2021-04-20 18:10:30 +01004701/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4703 mbedtls_svc_key_id_t key,
4704 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004705{
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004707}
4708
4709/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004710psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4711 mbedtls_svc_key_id_t key,
4712 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004713{
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004715}
4716
4717/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004718psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4719 uint8_t *nonce,
4720 size_t nonce_size,
4721 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004722{
4723 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004724 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004725 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004726
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004727 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004730 status = PSA_ERROR_BAD_STATE;
4731 goto exit;
4732 }
4733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004735 status = PSA_ERROR_BAD_STATE;
4736 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004737 }
4738
Paul Elliotte193ea82021-10-01 13:00:16 +01004739 /* For CCM, this size may not be correct according to the PSA
4740 * specification. The PSA Crypto 1.0.1 specification states:
4741 *
4742 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4743 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4744 *
4745 * However this restriction that L has to be the smallest integer is not
4746 * applied in practice, and it is not implementable here since the
4747 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4749 operation->alg);
4750 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004751 status = PSA_ERROR_BUFFER_TOO_SMALL;
4752 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004753 }
4754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 status = psa_generate_random(local_nonce, required_nonce_size);
4756 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004757 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004761
Paul Elliott39dc6b82021-05-11 19:16:09 +01004762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (status == PSA_SUCCESS) {
4764 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004765 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 } else {
4767 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004768 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
4772
4773/* Set the nonce for a multipart authenticated encryption or decryption
4774 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004775psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4776 const uint8_t *nonce,
4777 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004782 status = PSA_ERROR_BAD_STATE;
4783 goto exit;
4784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004787 status = PSA_ERROR_BAD_STATE;
4788 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789 }
4790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4792 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004793 status = PSA_ERROR_INVALID_ARGUMENT;
4794 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004795 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4798 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004799
Paul Elliott39dc6b82021-05-11 19:16:09 +01004800exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004802 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 } else {
4804 psa_aead_abort(operation);
4805 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004808}
4809
4810/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4812 size_t ad_length,
4813 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004815 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004818 status = PSA_ERROR_BAD_STATE;
4819 goto exit;
4820 }
4821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 if (operation->lengths_set || operation->ad_started ||
4823 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004824 status = PSA_ERROR_BAD_STATE;
4825 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004826 }
4827
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004829#if defined(PSA_WANT_ALG_GCM)
4830 case PSA_ALG_GCM:
4831 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 * bits. Without the guard this code will generate warnings on 32bit
4833 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004834#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (((uint64_t) ad_length) >> 61 != 0 ||
4836 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004837 status = PSA_ERROR_INVALID_ARGUMENT;
4838 goto exit;
4839 }
Paul Elliott325d3742021-09-27 17:56:28 +01004840#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004841 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004842#endif /* PSA_WANT_ALG_GCM */
4843#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004844 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004846 status = PSA_ERROR_INVALID_ARGUMENT;
4847 goto exit;
4848 }
4849 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004850#endif /* PSA_WANT_ALG_CCM */
4851#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004852 case PSA_ALG_CHACHA20_POLY1305:
4853 /* No length restrictions for ChaChaPoly. */
4854 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004855#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004856 default:
4857 break;
4858 }
Paul Elliott325d3742021-09-27 17:56:28 +01004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4861 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004862
Paul Elliott39dc6b82021-05-11 19:16:09 +01004863exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004865 operation->ad_remaining = ad_length;
4866 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004867 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 } else {
4869 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004870 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004873}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004874
4875/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004876psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4877 const uint8_t *input,
4878 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004879{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004883 status = PSA_ERROR_BAD_STATE;
4884 goto exit;
4885 }
4886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004888 status = PSA_ERROR_BAD_STATE;
4889 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004890 }
4891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (operation->lengths_set) {
4893 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004894 status = PSA_ERROR_INVALID_ARGUMENT;
4895 goto exit;
4896 }
4897
4898 operation->ad_remaining -= input_length;
4899 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004900#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004902 status = PSA_ERROR_BAD_STATE;
4903 goto exit;
4904 }
4905#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 status = psa_driver_wrapper_aead_update_ad(operation, input,
4908 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004909
Paul Elliott39dc6b82021-05-11 19:16:09 +01004910exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004912 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 } else {
4914 psa_aead_abort(operation);
4915 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004918}
4919
4920/* Encrypt or decrypt a message fragment in an active multipart AEAD
4921 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004922psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4923 const uint8_t *input,
4924 size_t input_length,
4925 uint8_t *output,
4926 size_t output_size,
4927 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004928{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004930
4931 *output_length = 0;
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004934 status = PSA_ERROR_BAD_STATE;
4935 goto exit;
4936 }
4937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004939 status = PSA_ERROR_BAD_STATE;
4940 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004941 }
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004944 /* Additional data length was supplied, but not all the additional
4945 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004947 status = PSA_ERROR_INVALID_ARGUMENT;
4948 goto exit;
4949 }
4950
4951 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004953 status = PSA_ERROR_INVALID_ARGUMENT;
4954 goto exit;
4955 }
4956
4957 operation->body_remaining -= input_length;
4958 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004959#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004961 status = PSA_ERROR_BAD_STATE;
4962 goto exit;
4963 }
4964#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004965
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4967 output, output_size,
4968 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004969
Paul Elliott39dc6b82021-05-11 19:16:09 +01004970exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004972 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 } else {
4974 psa_aead_abort(operation);
4975 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004978}
4979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004981{
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (operation->id == 0 || !operation->nonce_set) {
4983 return PSA_ERROR_BAD_STATE;
4984 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4987 operation->body_remaining != 0)) {
4988 return PSA_ERROR_INVALID_ARGUMENT;
4989 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004992}
4993
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004995psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4996 uint8_t *ciphertext,
4997 size_t ciphertext_size,
4998 size_t *ciphertext_length,
4999 uint8_t *tag,
5000 size_t tag_size,
5001 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005002{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5004
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005006 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 status = psa_aead_final_checks(operation);
5009 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005010 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005014 status = PSA_ERROR_BAD_STATE;
5015 goto exit;
5016 }
5017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5019 ciphertext_size,
5020 ciphertext_length,
5021 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005022
Paul Elliott39dc6b82021-05-11 19:16:09 +01005023exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005024
5025
Paul Elliottf47b0952021-05-21 18:02:33 +01005026 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005027 * the zero tag size, make sure the tag is set to something implausible.
5028 * Even if the operation succeeds, make sure we clear the rest of the
5029 * buffer to prevent potential leakage of anything previously placed in
5030 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005031 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005036}
5037
5038/* Finish authenticating and decrypting a message in a multipart AEAD
5039 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005040psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5041 uint8_t *plaintext,
5042 size_t plaintext_size,
5043 size_t *plaintext_length,
5044 const uint8_t *tag,
5045 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005046{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005047 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5048
Paul Elliott302ff6b2021-04-20 18:10:30 +01005049 *plaintext_length = 0;
5050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 status = psa_aead_final_checks(operation);
5052 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005057 status = PSA_ERROR_BAD_STATE;
5058 goto exit;
5059 }
5060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5062 plaintext_size,
5063 plaintext_length,
5064 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005065
5066exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005070}
5071
5072/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005073psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005074{
5075 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005078 /* The object has (apparently) been initialized but it is not (yet)
5079 * in use. It's ok to call abort on such an object, and there's
5080 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005082 }
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005089}
5090
Gilles Peskinee59236f2018-01-27 23:32:46 +01005091/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092/* Generators */
5093/****************************************************************/
5094
Przemek Stekiel69c46792022-06-10 12:59:51 +02005095#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005096 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005097 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305098 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5099 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005100#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005101#endif /* At least one builtin KDF */
5102
Przemek Stekiel69c46792022-06-10 12:59:51 +02005103#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005104 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5105 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5106static psa_status_t psa_key_derivation_start_hmac(
5107 psa_mac_operation_t *operation,
5108 psa_algorithm_t hash_alg,
5109 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005111{
5112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5115 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5116 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005117
Steven Cooreman72f736a2021-05-07 14:14:37 +02005118 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 status = psa_driver_wrapper_mac_sign_setup(operation,
5122 &attributes,
5123 hmac_key, hmac_key_length,
5124 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 psa_reset_key_attributes(&attributes);
5127 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005128}
5129#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005130
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005131#define HKDF_STATE_INIT 0 /* no input yet */
5132#define HKDF_STATE_STARTED 1 /* got salt */
5133#define HKDF_STATE_KEYED 2 /* got key */
5134#define HKDF_STATE_OUTPUT 3 /* output started */
5135
Gilles Peskinecbe66502019-05-16 16:59:18 +02005136static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005138{
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5140 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5141 } else {
5142 return operation->alg;
5143 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005144}
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147{
5148 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5150 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005151 /* The object has (apparently) been initialized but it is not
5152 * in use. It's ok to call abort on such an object, and there's
5153 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005155#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5157 mbedtls_free(operation->ctx.hkdf.info);
5158 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5159 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005160#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005161#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5164 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5165 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5166 if (operation->ctx.tls12_prf.secret != NULL) {
5167 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5168 operation->ctx.tls12_prf.secret_length);
5169 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005170 }
5171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (operation->ctx.tls12_prf.seed != NULL) {
5173 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5174 operation->ctx.tls12_prf.seed_length);
5175 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005176 }
5177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (operation->ctx.tls12_prf.label != NULL) {
5179 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5180 operation->ctx.tls12_prf.label_length);
5181 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005182 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005183#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 if (operation->ctx.tls12_prf.other_secret != NULL) {
5185 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5186 operation->ctx.tls12_prf.other_secret_length);
5187 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005188 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005189#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005190 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005191
5192 /* We leave the fields Ai and output_block to be erased safely by the
5193 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005195#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5196 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005197#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5199 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5200 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5201 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005202#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305203#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5204 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305205 if (operation->ctx.pbkdf2.salt != NULL) {
5206 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5207 operation->ctx.pbkdf2.salt_length);
5208 mbedtls_free(operation->ctx.pbkdf2.salt);
5209 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305210
5211 status = PSA_SUCCESS;
5212 } else
5213#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214 {
5215 status = PSA_ERROR_BAD_STATE;
5216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 mbedtls_platform_zeroize(operation, sizeof(*operation));
5218 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005219}
5220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223{
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005227 }
5228
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005231}
5232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5234 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235{
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (operation->alg == 0) {
5237 return PSA_ERROR_BAD_STATE;
5238 }
5239 if (capacity > operation->capacity) {
5240 return PSA_ERROR_INVALID_ARGUMENT;
5241 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005244}
5245
Przemek Stekiel69c46792022-06-10 12:59:51 +02005246#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005247/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005248static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5249 psa_algorithm_t kdf_alg,
5250 uint8_t *output,
5251 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005252{
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5254 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005255 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005257#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005259#else
5260 const uint8_t last_block = 0xff;
5261#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (hkdf->state < HKDF_STATE_KEYED ||
5264 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005265#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005267#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 )) {
5269 return PSA_ERROR_BAD_STATE;
5270 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271 hkdf->state = HKDF_STATE_OUTPUT;
5272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005274 /* Copy what remains of the current block */
5275 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005277 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 }
5279 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005280 output += n;
5281 output_length -= n;
5282 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005284 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005286 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005287 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005288 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005289 * object was corrupted or if this function is called directly
5290 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (hkdf->block_number == last_block) {
5292 return PSA_ERROR_BAD_STATE;
5293 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005294
5295 /* We need a new block */
5296 ++hkdf->block_number;
5297 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5300 hash_alg,
5301 hkdf->prk,
5302 hash_length);
5303 if (status != PSA_SUCCESS) {
5304 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005306
5307 if (hkdf->block_number != 1) {
5308 status = psa_mac_update(&hkdf->hmac,
5309 hkdf->output_block,
5310 hash_length);
5311 if (status != PSA_SUCCESS) {
5312 return status;
5313 }
5314 }
5315 status = psa_mac_update(&hkdf->hmac,
5316 hkdf->info,
5317 hkdf->info_length);
5318 if (status != PSA_SUCCESS) {
5319 return status;
5320 }
5321 status = psa_mac_update(&hkdf->hmac,
5322 &hkdf->block_number, 1);
5323 if (status != PSA_SUCCESS) {
5324 return status;
5325 }
5326 status = psa_mac_sign_finish(&hkdf->hmac,
5327 hkdf->output_block,
5328 sizeof(hkdf->output_block),
5329 &hmac_output_length);
5330 if (status != PSA_SUCCESS) {
5331 return status;
5332 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005333 }
5334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005336}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005337#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005338
John Durkop07cc04a2020-11-16 22:08:34 -08005339#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5340 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005341static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5342 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005344{
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5346 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005347 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5348 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005349 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005350
Janos Follath7742fee2019-06-17 12:58:10 +01005351 /* We can't be wanting more output after block 0xff, otherwise
5352 * the capacity check in psa_key_derivation_output_bytes() would have
5353 * prevented this call. It could happen only if the operation
5354 * object was corrupted or if this function is called directly
5355 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (tls12_prf->block_number == 0xff) {
5357 return PSA_ERROR_CORRUPTION_DETECTED;
5358 }
Janos Follath7742fee2019-06-17 12:58:10 +01005359
5360 /* We need a new block */
5361 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005362 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005363
5364 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5365 *
5366 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5367 *
5368 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5369 * HMAC_hash(secret, A(2) + seed) +
5370 * HMAC_hash(secret, A(3) + seed) + ...
5371 *
5372 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005373 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005374 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005375 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005376 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005377 * is currently extracted as `output_block` and where i is
5378 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005379 */
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 status = psa_key_derivation_start_hmac(&hmac,
5382 hash_alg,
5383 tls12_prf->secret,
5384 tls12_prf->secret_length);
5385 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005386 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005388
5389 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005391 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5392 * the variable seed and in this instance means it in the context of the
5393 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 status = psa_mac_update(&hmac,
5395 tls12_prf->label,
5396 tls12_prf->label_length);
5397 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005398 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 }
5400 status = psa_mac_update(&hmac,
5401 tls12_prf->seed,
5402 tls12_prf->seed_length);
5403 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005404 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 }
5406 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005407 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5409 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005410 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005412 }
5413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 status = psa_mac_sign_finish(&hmac,
5415 tls12_prf->Ai, hash_length,
5416 &hmac_output_length);
5417 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005418 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 }
5420 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005421 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005423
5424 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 status = psa_key_derivation_start_hmac(&hmac,
5426 hash_alg,
5427 tls12_prf->secret,
5428 tls12_prf->secret_length);
5429 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005430 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 }
5432 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5433 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 }
5436 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5437 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005438 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 }
5440 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5441 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005442 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 }
5444 status = psa_mac_sign_finish(&hmac,
5445 tls12_prf->output_block, hash_length,
5446 &hmac_output_length);
5447 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005448 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005450
Janos Follath7742fee2019-06-17 12:58:10 +01005451
5452cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 cleanup_status = psa_mac_abort(&hmac);
5454 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005455 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005459}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005460
Janos Follath844eb0e2019-06-19 12:10:49 +01005461static psa_status_t psa_key_derivation_tls12_prf_read(
5462 psa_tls12_prf_key_derivation_t *tls12_prf,
5463 psa_algorithm_t alg,
5464 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005466{
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5468 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005469 psa_status_t status;
5470 uint8_t offset, length;
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005473 case PSA_TLS12_PRF_STATE_LABEL_SET:
5474 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5475 break;
5476 case PSA_TLS12_PRF_STATE_OUTPUT:
5477 break;
5478 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005480 }
5481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005483 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 if (tls12_prf->left_in_block == 0) {
5485 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5486 alg);
5487 if (status != PSA_SUCCESS) {
5488 return status;
5489 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005490
5491 continue;
5492 }
5493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005495 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005497 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005499
5500 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005502 output += length;
5503 output_length -= length;
5504 tls12_prf->left_in_block -= length;
5505 }
5506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005508}
John Durkop07cc04a2020-11-16 22:08:34 -08005509#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5510 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005511
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005512#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5513static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5514 psa_tls12_ecjpake_to_pms_t *ecjpake,
5515 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005517{
5518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005519 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (output_length != 32) {
5522 return PSA_ERROR_INVALID_ARGUMENT;
5523 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5526 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5527 &output_size);
5528 if (status != PSA_SUCCESS) {
5529 return status;
5530 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 if (output_size != output_length) {
5533 return PSA_ERROR_GENERIC_ERROR;
5534 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005537}
5538#endif
5539
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305540#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5541static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5542 psa_pbkdf2_key_derivation_t *pbkdf2,
5543 psa_algorithm_t prf_alg,
5544 uint8_t prf_output_length,
5545 psa_key_attributes_t *attributes)
5546{
5547 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305548 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305549 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305550 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305551 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305552 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305553 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305554
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305555 mac_operation.is_sign = 1;
5556 mac_operation.mac_size = prf_output_length;
5557 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305558
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305559 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5560 attributes,
5561 pbkdf2->password,
5562 pbkdf2->password_length,
5563 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305564 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305565 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305566 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305567 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5568 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305569 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305570 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305571 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305572 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305573 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305574 }
5575 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5576 &mac_output_length);
5577 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305578 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305579 }
5580
5581 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305582 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305583 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305584 }
5585
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305586 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305587
5588 for (i = 1; i < pbkdf2->input_cost; i++) {
5589 status = psa_driver_wrapper_mac_compute(attributes,
5590 pbkdf2->password,
5591 pbkdf2->password_length,
5592 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305593 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305594 &mac_output_length);
5595 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305596 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305597 }
5598
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305599 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305600 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305601
5602cleanup:
5603 /* Zeroise buffers to clear sensitive data from memory. */
5604 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5605 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305606}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305607
5608static psa_status_t psa_key_derivation_pbkdf2_read(
5609 psa_pbkdf2_key_derivation_t *pbkdf2,
5610 psa_algorithm_t kdf_alg,
5611 uint8_t *output,
5612 size_t output_length)
5613{
5614 psa_status_t status;
5615 psa_algorithm_t prf_alg;
5616 uint8_t prf_output_length;
5617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5618 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5619 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5620
5621 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5622 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5623 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5624 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305625 } else {
5626 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305627 }
5628
5629 switch (pbkdf2->state) {
5630 case PSA_PBKDF2_STATE_PASSWORD_SET:
5631 /* Initially we need a new block so bytes_used is equal to block size*/
5632 pbkdf2->bytes_used = prf_output_length;
5633 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5634 break;
5635 case PSA_PBKDF2_STATE_OUTPUT:
5636 break;
5637 default:
5638 return PSA_ERROR_BAD_STATE;
5639 }
5640
5641 while (output_length != 0) {
5642 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5643 if (n > output_length) {
5644 n = (uint8_t) output_length;
5645 }
5646 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5647 output += n;
5648 output_length -= n;
5649 pbkdf2->bytes_used += n;
5650
5651 if (output_length == 0) {
5652 break;
5653 }
5654
5655 /* We need a new block */
5656 pbkdf2->bytes_used = 0;
5657 pbkdf2->block_number++;
5658
5659 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5660 prf_output_length,
5661 &attributes);
5662 if (status != PSA_SUCCESS) {
5663 return status;
5664 }
5665 }
5666
5667 return PSA_SUCCESS;
5668}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305669#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5670
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005671psa_status_t psa_key_derivation_output_bytes(
5672 psa_key_derivation_operation_t *operation,
5673 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675{
5676 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005680 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005682 }
5683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005685 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005686 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005688 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005689 goto exit;
5690 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005693 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005694 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5695 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005696 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005697 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005699 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005700 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005701
Przemek Stekiel69c46792022-06-10 12:59:51 +02005702#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5704 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5705 output, output_length);
5706 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005707#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005708#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5709 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5711 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5712 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5713 kdf_alg, output,
5714 output_length);
5715 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005716#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5717 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005718#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005720 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5722 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005723#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305724#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5725 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305726 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5727 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305728 } else
5729#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005730
Gilles Peskineeab56e42018-07-12 17:12:33 +02005731 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005732 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734 }
5735
5736exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005738 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 * This allows us to differentiate between exhausted operations and
5740 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5741 * operations. */
5742 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005748}
5749
David Brown7807bf72021-02-09 16:28:23 -07005750#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005751static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005752{
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (data_size >= 8) {
5754 mbedtls_des_key_set_parity(data);
5755 }
5756 if (data_size >= 16) {
5757 mbedtls_des_key_set_parity(data + 8);
5758 }
5759 if (data_size >= 24) {
5760 mbedtls_des_key_set_parity(data + 16);
5761 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005762}
David Brown7807bf72021-02-09 16:28:23 -07005763#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005764
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005765/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 * ECC keys on a Weierstrass elliptic curve require the generation
5767 * of a private key which is an integer
5768 * in the range [1, N - 1], where N is the boundary of the private key domain:
5769 * N is the prime p for Diffie-Hellman, or the order of the
5770 * curve’s base point for ECC.
5771 *
5772 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5773 * This function generates the private key using the following process:
5774 *
5775 * 1. Draw a byte string of length ceiling(m/8) bytes.
5776 * 2. If m is not a multiple of 8, set the most significant
5777 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5778 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5779 * 4. If k > N - 2, discard the result and return to step 1.
5780 * 5. Output k + 1 as the private key.
5781 *
5782 * This method allows compliance to NIST standards, specifically the methods titled
5783 * Key-Pair Generation by Testing Candidates in the following publications:
5784 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5785 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5786 * Diffie-Hellman keys.
5787 *
5788 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5789 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5790 *
5791 * Note: Function allocates memory for *data buffer, so given *data should be
5792 * always NULL.
5793 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005794#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5795#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005796static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005797 psa_key_slot_t *slot,
5798 size_t bits,
5799 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005800 uint8_t **data
5801 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005802{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005803 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005804 mbedtls_mpi k;
5805 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005807 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005808 size_t m;
5809 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 mbedtls_mpi_init(&k);
5812 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005813
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005814 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005816 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005820 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005821 goto cleanup;
5822 }
5823
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005824 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005828
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005829 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005830 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005831 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005832
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005833 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005834
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005835 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005837
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005838 /* Note: This function is always called with *data == NULL and it
5839 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 *data = mbedtls_calloc(1, m_bytes);
5841 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005842 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005843 goto cleanup;
5844 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005847 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005849 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005851
5852 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005854 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005855 * (8 * ceiling(m/8) - m) bits of the first byte in
5856 * the string to zero.
5857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005859 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005860 }
5861
5862 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 * big-endian byte string.
5864 */
5865 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005866
5867 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 * Result of comparison is returned. When it indicates error
5869 * then this function is called again.
5870 */
5871 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 }
5873
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5876 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005877cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (ret != 0) {
5879 status = mbedtls_to_psa_error(ret);
5880 }
5881 if (status != PSA_SUCCESS) {
5882 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883 *data = NULL;
5884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 mbedtls_mpi_free(&k);
5886 mbedtls_mpi_free(&diff_N_2);
5887 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005889
5890/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5891 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5892 *
5893 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5894 * draw a 32-byte string and process it as specified in
5895 * Elliptic Curves for Security [RFC7748] §5.
5896 *
5897 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5898 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5899 *
5900 * Note: Function allocates memory for *data buffer, so given *data should be
5901 * always NULL.
5902 */
5903
5904static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5905 size_t bits,
5906 psa_key_derivation_operation_t *operation,
5907 uint8_t **data
5908 )
5909{
5910 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005911 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005914 case 255:
5915 output_length = 32;
5916 break;
5917 case 448:
5918 output_length = 56;
5919 break;
5920 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005922 break;
5923 }
5924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 if (*data == NULL) {
5928 return PSA_ERROR_INSUFFICIENT_MEMORY;
5929 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005934 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005938 case 255:
5939 (*data)[0] &= 248;
5940 (*data)[31] &= 127;
5941 (*data)[31] |= 64;
5942 break;
5943 case 448:
5944 (*data)[0] &= 252;
5945 (*data)[55] |= 128;
5946 break;
5947 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005949 break;
5950 }
5951
5952 return status;
5953}
Valerio Setti86587ab2023-06-27 13:09:30 +02005954#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5955static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5956 psa_key_slot_t *slot, size_t bits,
5957 psa_key_derivation_operation_t *operation, uint8_t **data)
5958{
5959 (void) slot;
5960 (void) bits;
5961 (void) operation;
5962 (void) data;
5963 return PSA_ERROR_NOT_SUPPORTED;
5964}
5965
5966static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5967 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5968{
5969 (void) bits;
5970 (void) operation;
5971 (void) data;
5972 return PSA_ERROR_NOT_SUPPORTED;
5973}
5974#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5975#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005976
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005977static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005978 psa_key_slot_t *slot,
5979 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005981{
5982 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305984 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005986 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5989 return PSA_ERROR_INVALID_ARGUMENT;
5990 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005991
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005992#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005993 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5995 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5996 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005997 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
5999 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 }
6002 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006003 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6005 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006006 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006008 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006009 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006010#endif /* 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 (key_type_is_raw_bytes(slot->attr.type)) {
6013 if (bits % 8 != 0) {
6014 return PSA_ERROR_INVALID_ARGUMENT;
6015 }
6016 data = mbedtls_calloc(1, bytes);
6017 if (data == NULL) {
6018 return PSA_ERROR_INSUFFICIENT_MEMORY;
6019 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 status = psa_key_derivation_output_bytes(operation, data, bytes);
6022 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006023 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 }
David Brown12ca5032021-02-11 11:02:00 -07006025#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6027 psa_des_set_key_parity(data, bytes);
6028 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006029#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 } else {
6031 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006032 }
Ronald Crondd04d422020-11-28 15:14:42 +01006033
Ronald Cronbf33c932020-11-28 18:06:53 +01006034 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006035 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006037 };
6038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6040 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6041 &storage_size);
6042 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306043 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 }
Archanad8a83dc2021-06-14 10:04:16 +05306045 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 status = psa_allocate_buffer_to_slot(slot, storage_size);
6047 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 }
Archanad8a83dc2021-06-14 10:04:16 +05306050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 status = psa_driver_wrapper_import_key(&attributes,
6052 data, bytes,
6053 slot->key.data,
6054 slot->key.bytes,
6055 &slot->key.bytes, &bits);
6056 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006057 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006059
6060exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 mbedtls_free(data);
6062 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006063}
6064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6066 psa_key_derivation_operation_t *operation,
6067 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006068{
6069 psa_status_t status;
6070 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006071 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006072
Ronald Cron81709fc2020-11-14 12:10:32 +01006073 *key = MBEDTLS_SVC_KEY_ID_INIT;
6074
Gilles Peskine0f84d622019-09-12 19:03:13 +02006075 /* Reject any attempt to create a zero-length key so that we don't
6076 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (psa_get_key_bits(attributes) == 0) {
6078 return PSA_ERROR_INVALID_ARGUMENT;
6079 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (operation->alg == PSA_ALG_NONE) {
6082 return PSA_ERROR_BAD_STATE;
6083 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (!operation->can_output_key) {
6086 return PSA_ERROR_NOT_PERMITTED;
6087 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6090 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006093 /* Deriving a key in a secure element is not implemented yet. */
6094 status = PSA_ERROR_NOT_SUPPORTED;
6095 }
6096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (status == PSA_SUCCESS) {
6098 status = psa_generate_derived_key_internal(slot,
6099 attributes->core.bits,
6100 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 if (status == PSA_SUCCESS) {
6103 status = psa_finish_key_creation(slot, driver, key);
6104 }
6105 if (status != PSA_SUCCESS) {
6106 psa_fail_key_creation(slot, driver);
6107 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006110}
6111
Gilles Peskineeab56e42018-07-12 17:12:33 +02006112
6113
6114/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006115/* Key derivation */
6116/****************************************************************/
6117
Steven Cooreman932ffb72021-02-15 12:14:32 +01006118#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006119static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006120{
6121#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6123 return 1;
6124 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006125#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006126#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6128 return 1;
6129 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006130#endif
6131#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6133 return 1;
6134 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006135#endif
6136#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6138 return 1;
6139 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006140#endif
6141#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6143 return 1;
6144 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006145#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006146#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6148 return 1;
6149 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006150#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306151#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6152 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6153 return 1;
6154 }
6155#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006157}
6158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006160{
6161 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 psa_status_t status = psa_hash_setup(&operation, alg);
6163 psa_hash_abort(&operation);
6164 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006165}
6166
Gilles Peskine969c5d62019-01-16 15:53:06 +01006167static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006168 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006170{
Janos Follath5fe19732019-06-20 15:09:30 +01006171 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6172 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006174
Gilles Peskine969c5d62019-01-16 15:53:06 +01006175 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 if (!is_kdf_alg_supported(kdf_alg)) {
6177 return PSA_ERROR_NOT_SUPPORTED;
6178 }
John Durkop07cc04a2020-11-16 22:08:34 -08006179
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006180 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006181 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6183 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6184 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6185 if (hash_size == 0) {
6186 return PSA_ERROR_NOT_SUPPORTED;
6187 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006188
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006189 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6190 * we might fail later, which is somewhat unfriendly and potentially
6191 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 psa_status_t status = psa_hash_try_support(hash_alg);
6193 if (status != PSA_SUCCESS) {
6194 return status;
6195 }
6196 } else {
6197 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006198 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6201 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6202 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006204 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006205#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006206 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6208 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006209 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006211#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6212 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 operation->capacity = 255 * hash_size;
6214 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006215}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006218{
6219#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 if (alg == PSA_ALG_ECDH) {
6221 return PSA_SUCCESS;
6222 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006223#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006224#if defined(PSA_WANT_ALG_FFDH)
6225 if (alg == PSA_ALG_FFDH) {
6226 return PSA_SUCCESS;
6227 }
6228#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006229 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006231}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006232
6233static int psa_key_derivation_allows_free_form_secret_input(
6234 psa_algorithm_t kdf_alg)
6235{
6236#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6237 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6238 return 0;
6239 }
6240#endif
6241 (void) kdf_alg;
6242 return 1;
6243}
John Durkop07cc04a2020-11-16 22:08:34 -08006244#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6247 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006248{
6249 psa_status_t status;
6250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 if (operation->alg != 0) {
6252 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006253 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6256 return PSA_ERROR_INVALID_ARGUMENT;
6257 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6258#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6259 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6260 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6261 status = psa_key_agreement_try_support(ka_alg);
6262 if (status != PSA_SUCCESS) {
6263 return status;
6264 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006265 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6266 return PSA_ERROR_INVALID_ARGUMENT;
6267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6269#else
6270 return PSA_ERROR_NOT_SUPPORTED;
6271#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6272 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6273#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6274 status = psa_key_derivation_setup_kdf(operation, alg);
6275#else
6276 return PSA_ERROR_NOT_SUPPORTED;
6277#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6278 } else {
6279 return PSA_ERROR_INVALID_ARGUMENT;
6280 }
6281
6282 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006283 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 }
6285 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006286}
6287
Przemek Stekiel69c46792022-06-10 12:59:51 +02006288#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006289static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6290 psa_algorithm_t kdf_alg,
6291 psa_key_derivation_step_t step,
6292 const uint8_t *data,
6293 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006294{
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006296 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006298 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006299#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6301 return PSA_ERROR_INVALID_ARGUMENT;
6302 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006303#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 if (hkdf->state != HKDF_STATE_INIT) {
6305 return PSA_ERROR_BAD_STATE;
6306 } else {
6307 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6308 hash_alg,
6309 data, data_length);
6310 if (status != PSA_SUCCESS) {
6311 return status;
6312 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006313 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006315 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006316 case PSA_KEY_DERIVATION_INPUT_SECRET:
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)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006319 /* We shouldn't be in different state as HKDF_EXPAND only allows
6320 * two inputs: SECRET (this case) and INFO which does not modify
6321 * the state. It could happen only if the hkdf
6322 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 if (hkdf->state != HKDF_STATE_INIT) {
6324 return PSA_ERROR_BAD_STATE;
6325 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006326
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006327 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6329 return PSA_ERROR_INVALID_ARGUMENT;
6330 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 memcpy(hkdf->prk, data, data_length);
6333 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006334#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006335 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006336 /* HKDF: If no salt was provided, use an empty salt.
6337 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006339#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6341 return PSA_ERROR_BAD_STATE;
6342 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006343#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6345 hash_alg,
6346 NULL, 0);
6347 if (status != PSA_SUCCESS) {
6348 return status;
6349 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006350 hkdf->state = HKDF_STATE_STARTED;
6351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 if (hkdf->state != HKDF_STATE_STARTED) {
6353 return PSA_ERROR_BAD_STATE;
6354 }
6355 status = psa_mac_update(&hkdf->hmac,
6356 data, data_length);
6357 if (status != PSA_SUCCESS) {
6358 return status;
6359 }
6360 status = psa_mac_sign_finish(&hkdf->hmac,
6361 hkdf->prk,
6362 sizeof(hkdf->prk),
6363 &data_length);
6364 if (status != PSA_SUCCESS) {
6365 return status;
6366 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006367 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006368
Gilles Peskine2b522db2019-04-12 15:11:49 +02006369 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006370 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006371#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006373 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006375 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006377#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006378 {
6379 /* Block 0 is empty, and the next block will be
6380 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006382 }
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006385 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006386#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6388 return PSA_ERROR_INVALID_ARGUMENT;
6389 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006390#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006391#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6393 hkdf->state == HKDF_STATE_INIT) {
6394 return PSA_ERROR_BAD_STATE;
6395 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006396#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (hkdf->state == HKDF_STATE_OUTPUT) {
6398 return PSA_ERROR_BAD_STATE;
6399 }
6400 if (hkdf->info_set) {
6401 return PSA_ERROR_BAD_STATE;
6402 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006403 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 if (data_length != 0) {
6405 hkdf->info = mbedtls_calloc(1, data_length);
6406 if (hkdf->info == NULL) {
6407 return PSA_ERROR_INSUFFICIENT_MEMORY;
6408 }
6409 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006410 }
6411 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006413 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006415 }
6416}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006417#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006418
John Durkop07cc04a2020-11-16 22:08:34 -08006419#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6420 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006421static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6422 const uint8_t *data,
6423 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006424{
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6426 return PSA_ERROR_BAD_STATE;
6427 }
Janos Follathf08e2652019-06-13 09:05:41 +01006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (data_length != 0) {
6430 prf->seed = mbedtls_calloc(1, data_length);
6431 if (prf->seed == NULL) {
6432 return PSA_ERROR_INSUFFICIENT_MEMORY;
6433 }
Janos Follathf08e2652019-06-13 09:05:41 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006436 prf->seed_length = data_length;
6437 }
Janos Follathf08e2652019-06-13 09:05:41 +01006438
Gilles Peskine43f958b2020-12-13 14:55:14 +01006439 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006442}
6443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6445 const uint8_t *data,
6446 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006447{
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6449 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6450 return PSA_ERROR_BAD_STATE;
6451 }
Janos Follath81550542019-06-13 14:26:34 +01006452
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (data_length != 0) {
6454 prf->secret = mbedtls_calloc(1, data_length);
6455 if (prf->secret == NULL) {
6456 return PSA_ERROR_INSUFFICIENT_MEMORY;
6457 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006460 prf->secret_length = data_length;
6461 }
Janos Follath81550542019-06-13 14:26:34 +01006462
Gilles Peskine43f958b2020-12-13 14:55:14 +01006463 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006466}
6467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6469 const uint8_t *data,
6470 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006471{
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6473 return PSA_ERROR_BAD_STATE;
6474 }
Janos Follath63028dd2019-06-13 09:15:47 +01006475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 if (data_length != 0) {
6477 prf->label = mbedtls_calloc(1, data_length);
6478 if (prf->label == NULL) {
6479 return PSA_ERROR_INSUFFICIENT_MEMORY;
6480 }
Janos Follath63028dd2019-06-13 09:15:47 +01006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006483 prf->label_length = data_length;
6484 }
Janos Follath63028dd2019-06-13 09:15:47 +01006485
Gilles Peskine43f958b2020-12-13 14:55:14 +01006486 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006489}
6490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6492 psa_key_derivation_step_t step,
6493 const uint8_t *data,
6494 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006495{
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006497 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006499 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006501 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006503 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006505 }
6506}
John Durkop07cc04a2020-11-16 22:08:34 -08006507#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6508 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6509
6510#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6511static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6512 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006513 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006515{
6516 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6518 4 + data_length + prf->other_secret_length :
6519 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6522 return PSA_ERROR_INVALID_ARGUMENT;
6523 }
John Durkop07cc04a2020-11-16 22:08:34 -08006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 uint8_t *pms = mbedtls_calloc(1, pms_len);
6526 if (pms == NULL) {
6527 return PSA_ERROR_INSUFFICIENT_MEMORY;
6528 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006529 uint8_t *cur = pms;
6530
6531 /* pure-PSK:
6532 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006533 *
6534 * The premaster secret is formed as follows: if the PSK is N octets
6535 * long, concatenate a uint16 with the value N, N zero octets, a second
6536 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006537 *
6538 * mixed-PSK:
6539 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6540 * follows: concatenate a uint16 with the length of the other secret,
6541 * the other secret itself, uint16 with the length of PSK, and the
6542 * PSK itself.
6543 * For details please check:
6544 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6545 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6546 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006547 */
6548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6550 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6551 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6552 if (prf->other_secret_length != 0) {
6553 memcpy(cur, prf->other_secret, prf->other_secret_length);
6554 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006555 cur += prf->other_secret_length;
6556 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 } else {
6558 *cur++ = MBEDTLS_BYTE_1(data_length);
6559 *cur++ = MBEDTLS_BYTE_0(data_length);
6560 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006561 cur += data_length;
6562 }
6563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 *cur++ = MBEDTLS_BYTE_1(data_length);
6565 *cur++ = MBEDTLS_BYTE_0(data_length);
6566 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006567 cur += data_length;
6568
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 mbedtls_platform_zeroize(pms, pms_len);
6572 mbedtls_free(pms);
6573 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006574}
Janos Follath6660f0e2019-06-17 08:44:03 +01006575
Przemek Stekiele47201b2022-04-19 13:53:28 +02006576static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6577 psa_tls12_prf_key_derivation_t *prf,
6578 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006580{
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6582 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006583 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006584
6585 if (data_length != 0) {
6586 prf->other_secret = mbedtls_calloc(1, data_length);
6587 if (prf->other_secret == NULL) {
6588 return PSA_ERROR_INSUFFICIENT_MEMORY;
6589 }
6590
6591 memcpy(prf->other_secret, data, data_length);
6592 prf->other_secret_length = data_length;
6593 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006594 prf->other_secret_length = 0;
6595 }
6596
6597 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006600}
6601
Janos Follath6660f0e2019-06-17 08:44:03 +01006602static psa_status_t psa_tls12_prf_psk_to_ms_input(
6603 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006604 psa_key_derivation_step_t step,
6605 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006607{
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006609 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 return psa_tls12_prf_psk_to_ms_set_key(prf,
6611 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006612 break;
6613 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6615 data,
6616 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006617 break;
6618 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006620 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006621
Przemek Stekiele47201b2022-04-19 13:53:28 +02006622 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006623}
John Durkop07cc04a2020-11-16 22:08:34 -08006624#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006625
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006626#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6627static psa_status_t psa_tls12_ecjpake_to_pms_input(
6628 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006629 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006630 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006632{
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6634 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6635 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006636 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006637
6638 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 if (data[0] != 0x04) {
6640 return PSA_ERROR_INVALID_ARGUMENT;
6641 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006642
6643 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006647}
6648#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306649
6650#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6651static psa_status_t psa_pbkdf2_set_input_cost(
6652 psa_pbkdf2_key_derivation_t *pbkdf2,
6653 psa_key_derivation_step_t step,
6654 uint64_t data)
6655{
6656 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6657 return PSA_ERROR_INVALID_ARGUMENT;
6658 }
6659
6660 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6661 return PSA_ERROR_BAD_STATE;
6662 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306663
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306664 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306665 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306666 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306667
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306668 if (data == 0) {
6669 return PSA_ERROR_INVALID_ARGUMENT;
6670 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306671
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306672 pbkdf2->input_cost = data;
6673 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6674
6675 return PSA_SUCCESS;
6676}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306677
6678static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6679 const uint8_t *data,
6680 size_t data_length)
6681{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306682 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6683 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6684 return PSA_ERROR_BAD_STATE;
6685 }
6686
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306687 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6688 pbkdf2->salt = mbedtls_calloc(1, data_length);
6689 if (pbkdf2->salt == NULL) {
6690 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306691 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306692
6693 memcpy(pbkdf2->salt, data, data_length);
6694 pbkdf2->salt_length = data_length;
6695 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306696 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306697
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306698 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6699 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306700 return PSA_ERROR_INSUFFICIENT_MEMORY;
6701 }
6702
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306703 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6704 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306705 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306706 mbedtls_free(pbkdf2->salt);
6707 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306708 }
6709
6710 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6711
6712 return PSA_SUCCESS;
6713}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306714
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306715static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306716 const uint8_t *input,
6717 size_t input_len,
6718 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306719 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306720{
6721 psa_status_t status = PSA_SUCCESS;
6722 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6723 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306724 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306725 } else {
6726 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306727 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306728 }
6729 return status;
6730}
6731
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306732static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306733 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306734 const uint8_t *data,
6735 size_t data_length)
6736{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306737 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306738 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6739 return PSA_ERROR_BAD_STATE;
6740 }
6741
6742 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306743 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6744 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6745 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6746 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306747 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306748 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306749 }
6750
6751 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6752
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306753 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306754}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306755
6756static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306757 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306758 psa_key_derivation_step_t step,
6759 const uint8_t *data,
6760 size_t data_length)
6761{
6762 switch (step) {
6763 case PSA_KEY_DERIVATION_INPUT_SALT:
6764 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6765 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306766 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306767 default:
6768 return PSA_ERROR_INVALID_ARGUMENT;
6769 }
6770}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306771#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6772
Gilles Peskineb8965192019-09-24 16:21:10 +02006773/** Check whether the given key type is acceptable for the given
6774 * input step of a key derivation.
6775 *
6776 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6777 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6778 * Both secret and non-secret inputs can alternatively have the type
6779 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6780 * that the input was passed as a buffer rather than via a key object.
6781 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006782static int psa_key_derivation_check_input_type(
6783 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006785{
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006787 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 if (key_type == PSA_KEY_TYPE_DERIVE) {
6789 return PSA_SUCCESS;
6790 }
6791 if (key_type == PSA_KEY_TYPE_NONE) {
6792 return PSA_SUCCESS;
6793 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006794 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006795 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 if (key_type == PSA_KEY_TYPE_DERIVE) {
6797 return PSA_SUCCESS;
6798 }
6799 if (key_type == PSA_KEY_TYPE_NONE) {
6800 return PSA_SUCCESS;
6801 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006802 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006803 case PSA_KEY_DERIVATION_INPUT_LABEL:
6804 case PSA_KEY_DERIVATION_INPUT_SALT:
6805 case PSA_KEY_DERIVATION_INPUT_INFO:
6806 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6808 return PSA_SUCCESS;
6809 }
6810 if (key_type == PSA_KEY_TYPE_NONE) {
6811 return PSA_SUCCESS;
6812 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006813 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306814 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6815 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6816 return PSA_SUCCESS;
6817 }
6818 if (key_type == PSA_KEY_TYPE_DERIVE) {
6819 return PSA_SUCCESS;
6820 }
6821 if (key_type == PSA_KEY_TYPE_NONE) {
6822 return PSA_SUCCESS;
6823 }
6824 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006827}
6828
Janos Follathb80a94e2019-06-12 15:54:46 +01006829static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006830 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006831 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006832 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006833 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006835{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006836 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 status = psa_key_derivation_check_input_type(step, key_type);
6840 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006841 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006843
Przemek Stekiel69c46792022-06-10 12:59:51 +02006844#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6846 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6847 step, data, data_length);
6848 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006849#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006850#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6852 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6853 step, data, data_length);
6854 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006855#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6856#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6858 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6859 step, data, data_length);
6860 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006861#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006862#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006864 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6866 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006867#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306868#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6869 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306870 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6871 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306872 } else
6873#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006874 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006875 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006876 (void) data;
6877 (void) data_length;
6878 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006880 }
6881
Gilles Peskine224b0d62019-09-23 18:13:17 +02006882exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 if (status != PSA_SUCCESS) {
6884 psa_key_derivation_abort(operation);
6885 }
6886 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006887}
6888
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306889static psa_status_t psa_key_derivation_input_integer_internal(
6890 psa_key_derivation_operation_t *operation,
6891 psa_key_derivation_step_t step,
6892 uint64_t value)
6893{
6894 psa_status_t status;
6895 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6896
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306897#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6898 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6899 status = psa_pbkdf2_set_input_cost(
6900 &operation->ctx.pbkdf2, step, value);
6901 } else
6902#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306903 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306904 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306905 (void) value;
6906 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306907 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306908 }
6909
6910 if (status != PSA_SUCCESS) {
6911 psa_key_derivation_abort(operation);
6912 }
6913 return status;
6914}
6915
Janos Follath51f4a0f2019-06-14 11:35:55 +01006916psa_status_t psa_key_derivation_input_bytes(
6917 psa_key_derivation_operation_t *operation,
6918 psa_key_derivation_step_t step,
6919 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006921{
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 return psa_key_derivation_input_internal(operation, step,
6923 PSA_KEY_TYPE_NONE,
6924 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006925}
6926
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306927psa_status_t psa_key_derivation_input_integer(
6928 psa_key_derivation_operation_t *operation,
6929 psa_key_derivation_step_t step,
6930 uint64_t value)
6931{
6932 return psa_key_derivation_input_integer_internal(operation, step, value);
6933}
6934
Janos Follath51f4a0f2019-06-14 11:35:55 +01006935psa_status_t psa_key_derivation_input_key(
6936 psa_key_derivation_operation_t *operation,
6937 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006939{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006941 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006942 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006943
Ronald Cron5c522922020-11-14 16:35:34 +01006944 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6946 if (status != PSA_SUCCESS) {
6947 psa_key_derivation_abort(operation);
6948 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006949 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006950
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306951 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6952 * permission to output to a key object. */
6953 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6954 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006955 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006957
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 status = psa_key_derivation_input_internal(operation,
6959 step, slot->attr.type,
6960 slot->key.data,
6961 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006964
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006966}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006967
6968
6969
6970/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006971/* Key agreement */
6972/****************************************************************/
6973
Gilles Peskine449bd832023-01-11 14:50:10 +01006974psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6975 const uint8_t *key_buffer,
6976 size_t key_buffer_size,
6977 psa_algorithm_t alg,
6978 const uint8_t *peer_key,
6979 size_t peer_key_length,
6980 uint8_t *shared_secret,
6981 size_t shared_secret_size,
6982 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006983{
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006985#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006986 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6988 key_buffer_size, alg,
6989 peer_key, peer_key_length,
6990 shared_secret,
6991 shared_secret_size,
6992 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006993#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006994
6995#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6996 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006997 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006998 peer_key,
6999 peer_key_length,
7000 key_buffer,
7001 key_buffer_size,
7002 shared_secret,
7003 shared_secret_size,
7004 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007005#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7006
Gilles Peskine01d718c2018-09-18 12:01:02 +02007007 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007008 (void) attributes;
7009 (void) key_buffer;
7010 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007011 (void) peer_key;
7012 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007013 (void) shared_secret;
7014 (void) shared_secret_size;
7015 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007017 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007018}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007019
Aditya Deshpande17845b82022-10-13 17:21:01 +01007020/** Internal function for raw key agreement
7021 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007022 * to the driver's implementation if a driver is present.
7023 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007024 * (psa_key_agreement_raw_builtin).
7025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007026static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7027 psa_key_slot_t *private_key,
7028 const uint8_t *peer_key,
7029 size_t peer_key_length,
7030 uint8_t *shared_secret,
7031 size_t shared_secret_size,
7032 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007033{
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7035 return PSA_ERROR_NOT_SUPPORTED;
7036 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007037
7038 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007040 };
7041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 return psa_driver_wrapper_key_agreement(&attributes,
7043 private_key->key.data,
7044 private_key->key.bytes, alg,
7045 peer_key, peer_key_length,
7046 shared_secret,
7047 shared_secret_size,
7048 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007049}
7050
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007051/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007052 * to potentially free embedded data structures and wipe confidential data.
7053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7055 psa_key_derivation_step_t step,
7056 psa_key_slot_t *private_key,
7057 const uint8_t *peer_key,
7058 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007059{
7060 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007061 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007062 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007064
7065 /* Step 1: run the secret agreement algorithm to generate the shared
7066 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 status = psa_key_agreement_raw_internal(ka_alg,
7068 private_key,
7069 peer_key, peer_key_length,
7070 shared_secret,
7071 sizeof(shared_secret),
7072 &shared_secret_length);
7073 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007076
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007077 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007078 * the shared secret. A shared secret is permitted wherever a key
7079 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 status = psa_key_derivation_input_internal(operation, step,
7081 PSA_KEY_TYPE_DERIVE,
7082 shared_secret,
7083 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7086 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007087}
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7090 psa_key_derivation_step_t step,
7091 mbedtls_svc_key_id_t private_key,
7092 const uint8_t *peer_key,
7093 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007094{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007096 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007097 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7100 return PSA_ERROR_INVALID_ARGUMENT;
7101 }
Ronald Cron5c522922020-11-14 16:35:34 +01007102 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7104 if (status != PSA_SUCCESS) {
7105 return status;
7106 }
7107 status = psa_key_agreement_internal(operation, step,
7108 slot,
7109 peer_key, peer_key_length);
7110 if (status != PSA_SUCCESS) {
7111 psa_key_derivation_abort(operation);
7112 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007113 /* If a private key has been added as SECRET, we allow the derived
7114 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007116 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007118 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007121
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007123}
7124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7126 mbedtls_svc_key_id_t private_key,
7127 const uint8_t *peer_key,
7128 size_t peer_key_length,
7129 uint8_t *output,
7130 size_t output_size,
7131 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007132{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007133 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007134 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007135 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007136 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007139 status = PSA_ERROR_INVALID_ARGUMENT;
7140 goto exit;
7141 }
Ronald Cron5c522922020-11-14 16:35:34 +01007142 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7144 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007145 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007147
Gilles Peskine3e561302022-04-14 00:17:15 +02007148 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7149 * for the output size. The PSA specification only guarantees that this
7150 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7151 * but it might be nice to allow smaller buffers if the output fits.
7152 * At the time of writing this comment, with only ECDH implemented,
7153 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7154 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7155 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007156 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7158 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007159 status = PSA_ERROR_BUFFER_TOO_SMALL;
7160 goto exit;
7161 }
7162
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 status = psa_key_agreement_raw_internal(alg, slot,
7164 peer_key, peer_key_length,
7165 output, output_size,
7166 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007167
7168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007170 /* If an error happens and is not handled properly, the output
7171 * may be used as a key to protect sensitive data. Arrange for such
7172 * a key to be random, which is likely to result in decryption or
7173 * verification errors. This is better than filling the buffer with
7174 * some constant data such as zeros, which would result in the data
7175 * being protected with a reproducible, easily knowable key.
7176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007178 *output_length = output_size;
7179 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007180
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007184}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007185
7186
Gilles Peskine30524eb2020-11-13 17:02:26 +01007187
Gilles Peskine86a440b2018-11-12 18:39:40 +01007188/****************************************************************/
7189/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007190/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007191
Gilles Peskine672a7712023-04-28 21:00:28 +02007192#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7193#include "entropy_poll.h"
7194#endif
7195
Gilles Peskine30524eb2020-11-13 17:02:26 +01007196/** Initialize the PSA random generator.
7197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007198static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007199{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007200#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007202#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7203
Gilles Peskine30524eb2020-11-13 17:02:26 +01007204 /* Set default configuration if
7205 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007206 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007207 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 }
7209 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007210 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007214#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7215 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7216 /* The PSA entropy injection feature depends on using NV seed as an entropy
7217 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 mbedtls_entropy_add_source(&rng->entropy,
7219 mbedtls_nv_seed_poll, NULL,
7220 MBEDTLS_ENTROPY_BLOCK_SIZE,
7221 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007222#endif
7223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007225#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007226}
7227
7228/** Deinitialize the PSA random generator.
7229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007230static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007231{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007232#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007234#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7236 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007237#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007238}
7239
7240/** Seed the PSA random generator.
7241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007242static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007243{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007244#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7245 /* Do nothing: the external RNG seeds itself. */
7246 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007248#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007249 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7251 drbg_seed, sizeof(drbg_seed) - 1);
7252 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007253#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007254}
7255
Gilles Peskine449bd832023-01-11 14:50:10 +01007256psa_status_t psa_generate_random(uint8_t *output,
7257 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007258{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007259 GUARD_MODULE_INITIALIZED;
7260
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007261#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7262
7263 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7265 output, output_size,
7266 &output_length);
7267 if (status != PSA_SUCCESS) {
7268 return status;
7269 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007270 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007271 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 if (output_length != output_size) {
7273 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7274 }
7275 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007276
7277#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007280 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7282 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7283 output_size);
7284 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7285 output, request_size);
7286 if (ret != 0) {
7287 return mbedtls_to_psa_error(ret);
7288 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007289 output_size -= request_size;
7290 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007293#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007294}
7295
Gilles Peskine8814fc42020-12-14 15:33:44 +01007296/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007297 *
7298 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7299 * `psa_generate_random(...)`. The state parameter is ignored since the
7300 * PSA API doesn't support passing an explicit state.
7301 *
7302 * In the non-external case, psa_generate_random() calls an
7303 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7304 * and semantics as mbedtls_psa_get_random(). As an optimization,
7305 * instead of doing this back-and-forth between the PSA API and the
7306 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7307 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7310int mbedtls_psa_get_random(void *p_rng,
7311 unsigned char *output,
7312 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007313{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007314 /* This function takes a pointer to the RNG state because that's what
7315 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7316 * its own state internally and doesn't let the caller access that state.
7317 * So we just ignore the state parameter, and in practice we'll pass
7318 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007319 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 psa_status_t status = psa_generate_random(output, output_size);
7321 if (status == PSA_SUCCESS) {
7322 return 0;
7323 } else {
7324 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7325 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007326}
7327#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7328
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007329#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007330psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7331 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007332{
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 if (global_data.initialized) {
7334 return PSA_ERROR_NOT_PERMITTED;
7335 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7338 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7339 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7340 return PSA_ERROR_INVALID_ARGUMENT;
7341 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007344}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007345#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007346
Ronald Cron3772afe2021-02-08 16:10:05 +01007347/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007348 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007349 * \param type The key type
7350 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007351 *
7352 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007353 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007354 * \retval #PSA_ERROR_INVALID_ARGUMENT
7355 * The size in bits of the key is not valid.
7356 * \retval #PSA_ERROR_NOT_SUPPORTED
7357 * The type and/or the size in bits of the key or the combination of
7358 * the two is not supported.
7359 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007360static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007362{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007363 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (key_type_is_raw_bytes(type)) {
7366 status = psa_validate_unstructured_key_bit_size(type, bits);
7367 if (status != PSA_SUCCESS) {
7368 return status;
7369 }
7370 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007371#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7373 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7374 return PSA_ERROR_NOT_SUPPORTED;
7375 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007376
Ronald Cron01b2aba2020-10-05 09:42:02 +02007377 /* Accept only byte-aligned keys, for the same reasons as
7378 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 if (bits % 8 != 0) {
7380 return PSA_ERROR_NOT_SUPPORTED;
7381 }
7382 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007383#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007384
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007385#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007387 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 return PSA_SUCCESS;
7389 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007390#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007391
Valerio Setti8bb57632023-05-26 13:48:07 +02007392#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007393 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007394 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007395 return PSA_ERROR_NOT_SUPPORTED;
7396 }
7397 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007398#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007399 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007401 }
7402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007404}
7405
Ronald Cron55ed0592020-10-05 10:30:40 +02007406psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007407 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007409{
7410 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007411 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007412
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 if ((attributes->domain_parameters == NULL) &&
7414 (attributes->domain_parameters_size != 0)) {
7415 return PSA_ERROR_INVALID_ARGUMENT;
7416 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007417
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 if (key_type_is_raw_bytes(type)) {
7419 status = psa_generate_random(key_buffer, key_buffer_size);
7420 if (status != PSA_SUCCESS) {
7421 return status;
7422 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007423
David Brown12ca5032021-02-11 11:02:00 -07007424#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 if (type == PSA_KEY_TYPE_DES) {
7426 psa_des_set_key_parity(key_buffer, key_buffer_size);
7427 }
David Brown8107e312021-02-12 08:08:46 -07007428#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007430
Valerio Setti76df8c12023-07-11 14:11:28 +02007431#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7433 return mbedtls_psa_rsa_generate_key(attributes,
7434 key_buffer,
7435 key_buffer_size,
7436 key_buffer_length);
7437 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007438#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007439
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007440#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7442 return mbedtls_psa_ecp_generate_key(attributes,
7443 key_buffer,
7444 key_buffer_size,
7445 key_buffer_length);
7446 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007447#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007448
Valerio Setti8bb57632023-05-26 13:48:07 +02007449#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007450 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7451 return mbedtls_psa_ffdh_generate_key(attributes,
7452 key_buffer,
7453 key_buffer_size,
7454 key_buffer_length);
7455 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007456#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007457 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 (void) key_buffer_length;
7459 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007460 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007463}
Darryl Green0c6575a2018-11-07 16:05:30 +00007464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7466 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007467{
7468 psa_status_t status;
7469 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007470 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007471 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007472
Ronald Cron81709fc2020-11-14 12:10:32 +01007473 *key = MBEDTLS_SVC_KEY_ID_INIT;
7474
Gilles Peskine0f84d622019-09-12 19:03:13 +02007475 /* Reject any attempt to create a zero-length key so that we don't
7476 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (psa_get_key_bits(attributes) == 0) {
7478 return PSA_ERROR_INVALID_ARGUMENT;
7479 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007480
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007481 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7483 return PSA_ERROR_INVALID_ARGUMENT;
7484 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007485
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7487 &slot, &driver);
7488 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007489 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 }
Gilles Peskine11792082019-08-06 18:36:36 +02007491
Ronald Cron977c2472020-10-13 08:32:21 +02007492 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307493 * storage ( thus not in the case of generating a key in a secure element
7494 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7495 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (slot->key.data == NULL) {
7497 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7498 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007499 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 attributes->core.type, attributes->core.bits);
7501 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007502 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007504
7505 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 attributes->core.type,
7507 attributes->core.bits);
7508 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007509 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 attributes, &key_buffer_size);
7511 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007512 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 }
Ronald Cron977c2472020-10-13 08:32:21 +02007514 }
Ronald Cron977c2472020-10-13 08:32:21 +02007515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7517 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007518 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 }
Ronald Cron977c2472020-10-13 08:32:21 +02007520 }
7521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 status = psa_driver_wrapper_generate_key(attributes,
7523 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 if (status != PSA_SUCCESS) {
7526 psa_remove_key_data_from_memory(slot);
7527 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007528
Gilles Peskine11792082019-08-06 18:36:36 +02007529exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if (status == PSA_SUCCESS) {
7531 status = psa_finish_key_creation(slot, driver, key);
7532 }
7533 if (status != PSA_SUCCESS) {
7534 psa_fail_key_creation(slot, driver);
7535 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007536
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007538}
7539
Gilles Peskinee59236f2018-01-27 23:32:46 +01007540/****************************************************************/
7541/* Module setup */
7542/****************************************************************/
7543
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007544#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007545psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 void (* entropy_init)(mbedtls_entropy_context *ctx),
7547 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007548{
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7550 return PSA_ERROR_BAD_STATE;
7551 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007552 global_data.rng.entropy_init = entropy_init;
7553 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007555}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007556#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007559{
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 psa_wipe_all_key_slots();
7561 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7562 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007563 }
7564 /* Wipe all remaining data, including configuration.
7565 * In particular, this sets all state indicator to the value
7566 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007568
7569 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007571}
7572
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007573#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7574/** Recover a transaction that was interrupted by a power failure.
7575 *
7576 * This function is called during initialization, before psa_crypto_init()
7577 * returns. If this function returns a failure status, the initialization
7578 * fails.
7579 */
7580static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007582{
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007584 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7585 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 /* TODO - fall through to the failure case until this
7587 * is implemented.
7588 * https://github.com/ARMmbed/mbed-crypto/issues/218
7589 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007590 default:
7591 /* We found an unsupported transaction in the storage.
7592 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007594 }
7595}
7596#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007599{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007600 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007601
Gilles Peskinec6b69072018-11-20 21:42:52 +01007602 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (global_data.initialized != 0) {
7604 return PSA_SUCCESS;
7605 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007606
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007607 /* Init drivers */
7608 status = psa_driver_wrapper_init();
7609 if (status != PSA_SUCCESS) {
7610 goto exit;
7611 }
7612 global_data.drivers_initialized = 1;
7613
Gilles Peskine30524eb2020-11-13 17:02:26 +01007614 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007616 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 status = mbedtls_psa_random_seed(&global_data.rng);
7618 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007621 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 status = psa_initialize_key_slots();
7624 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007627
Gilles Peskine4b734222019-07-24 15:56:31 +02007628#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 status = psa_crypto_load_transaction();
7630 if (status == PSA_SUCCESS) {
7631 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7632 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 }
7635 status = psa_crypto_stop_transaction();
7636 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007637 /* There's no transaction to complete. It's all good. */
7638 status = PSA_SUCCESS;
7639 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007640#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007641
Gilles Peskinec6b69072018-11-20 21:42:52 +01007642 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007643 global_data.initialized = 1;
7644
7645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 if (status != PSA_SUCCESS) {
7647 mbedtls_psa_crypto_free();
7648 }
7649 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007650}
7651
Yanray Wangffc3c482023-07-11 12:01:04 +08007652#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007653psa_status_t psa_crypto_driver_pake_get_password_len(
7654 const psa_crypto_driver_pake_inputs_t *inputs,
7655 size_t *password_len)
7656{
7657 if (inputs->password_len == 0) {
7658 return PSA_ERROR_BAD_STATE;
7659 }
7660
7661 *password_len = inputs->password_len;
7662
7663 return PSA_SUCCESS;
7664}
7665
7666psa_status_t psa_crypto_driver_pake_get_password(
7667 const psa_crypto_driver_pake_inputs_t *inputs,
7668 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7669{
7670 if (inputs->password_len == 0) {
7671 return PSA_ERROR_BAD_STATE;
7672 }
7673
7674 if (buffer_size < inputs->password_len) {
7675 return PSA_ERROR_BUFFER_TOO_SMALL;
7676 }
7677
7678 memcpy(buffer, inputs->password, inputs->password_len);
7679 *buffer_length = inputs->password_len;
7680
7681 return PSA_SUCCESS;
7682}
7683
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007684psa_status_t psa_crypto_driver_pake_get_user_len(
7685 const psa_crypto_driver_pake_inputs_t *inputs,
7686 size_t *user_len)
7687{
7688 if (inputs->user_len == 0) {
7689 return PSA_ERROR_BAD_STATE;
7690 }
7691
7692 *user_len = inputs->user_len;
7693
7694 return PSA_SUCCESS;
7695}
7696
7697psa_status_t psa_crypto_driver_pake_get_user(
7698 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007699 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007700{
7701 if (inputs->user_len == 0) {
7702 return PSA_ERROR_BAD_STATE;
7703 }
7704
Przemek Stekielc0e62502023-03-14 11:49:36 +01007705 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007706 return PSA_ERROR_BUFFER_TOO_SMALL;
7707 }
7708
Przemek Stekielc0e62502023-03-14 11:49:36 +01007709 memcpy(user_id, inputs->user, inputs->user_len);
7710 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007711
7712 return PSA_SUCCESS;
7713}
7714
7715psa_status_t psa_crypto_driver_pake_get_peer_len(
7716 const psa_crypto_driver_pake_inputs_t *inputs,
7717 size_t *peer_len)
7718{
7719 if (inputs->peer_len == 0) {
7720 return PSA_ERROR_BAD_STATE;
7721 }
7722
7723 *peer_len = inputs->peer_len;
7724
7725 return PSA_SUCCESS;
7726}
7727
7728psa_status_t psa_crypto_driver_pake_get_peer(
7729 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007730 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007731{
7732 if (inputs->peer_len == 0) {
7733 return PSA_ERROR_BAD_STATE;
7734 }
7735
Przemek Stekielc0e62502023-03-14 11:49:36 +01007736 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007737 return PSA_ERROR_BUFFER_TOO_SMALL;
7738 }
7739
Przemek Stekielc0e62502023-03-14 11:49:36 +01007740 memcpy(peer_id, inputs->peer, inputs->peer_len);
7741 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007742
7743 return PSA_SUCCESS;
7744}
7745
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007746psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7747 const psa_crypto_driver_pake_inputs_t *inputs,
7748 psa_pake_cipher_suite_t *cipher_suite)
7749{
7750 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7751 return PSA_ERROR_BAD_STATE;
7752 }
7753
7754 *cipher_suite = inputs->cipher_suite;
7755
7756 return PSA_SUCCESS;
7757}
7758
Neil Armstronga7d08c32022-06-01 18:21:20 +02007759psa_status_t psa_pake_setup(
7760 psa_pake_operation_t *operation,
7761 const psa_pake_cipher_suite_t *cipher_suite)
7762{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007763 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007764
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007765 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007766 status = PSA_ERROR_BAD_STATE;
7767 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007768 }
7769
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007770 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007771 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007772 status = PSA_ERROR_INVALID_ARGUMENT;
7773 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007774 }
7775
Przemek Stekiel51eac532022-12-07 11:04:51 +01007776 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7777
Przemek Stekiele12ed362022-12-21 12:54:46 +01007778 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007779 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7780 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007781 operation->data.inputs.cipher_suite = *cipher_suite;
7782
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007783#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007784 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007785 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007786 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007787
David Horstmann16f01512023-06-14 17:21:07 +01007788 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007789 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007790 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007791#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007792 {
7793 status = PSA_ERROR_NOT_SUPPORTED;
7794 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007795 }
7796
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007797 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7798
Przemek Stekiel51eac532022-12-07 11:04:51 +01007799 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007800exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007801 psa_pake_abort(operation);
7802 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007803}
7804
7805psa_status_t psa_pake_set_password_key(
7806 psa_pake_operation_t *operation,
7807 mbedtls_svc_key_id_t password)
7808{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007809 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007810 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7811 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007812 psa_key_attributes_t attributes;
7813 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007814
Przemek Stekiel51eac532022-12-07 11:04:51 +01007815 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007816 status = PSA_ERROR_BAD_STATE;
7817 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007818 }
7819
Przemek Stekiel6c764412022-11-22 14:05:12 +01007820 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7821 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007822 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007823 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007825 }
7826
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007827 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007828 .core = slot->attr
7829 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007830
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007831 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007832
Przemek Stekiel51eac532022-12-07 11:04:51 +01007833 if (type != PSA_KEY_TYPE_PASSWORD &&
7834 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7835 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007836 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007837 }
7838
Przemek Stekiel51eac532022-12-07 11:04:51 +01007839 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7840 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007841 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007842 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007843 }
7844
7845 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7846 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007847 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007848exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007849 if (status != PSA_SUCCESS) {
7850 psa_pake_abort(operation);
7851 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007852 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007853 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007854}
7855
7856psa_status_t psa_pake_set_user(
7857 psa_pake_operation_t *operation,
7858 const uint8_t *user_id,
7859 size_t user_id_len)
7860{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007862
7863 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007864 status = PSA_ERROR_BAD_STATE;
7865 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007866 }
7867
Przemek Stekiel51eac532022-12-07 11:04:51 +01007868 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007869 status = PSA_ERROR_INVALID_ARGUMENT;
7870 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007871 }
7872
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007873 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007874 status = PSA_ERROR_BAD_STATE;
7875 goto exit;
7876 }
7877
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007878 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7879 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007880 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7881 goto exit;
7882 }
7883
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007884 memcpy(operation->data.inputs.user, user_id, user_id_len);
7885 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007886
7887 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007888exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007889 psa_pake_abort(operation);
7890 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007891}
7892
7893psa_status_t psa_pake_set_peer(
7894 psa_pake_operation_t *operation,
7895 const uint8_t *peer_id,
7896 size_t peer_id_len)
7897{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007898 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007899
7900 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007901 status = PSA_ERROR_BAD_STATE;
7902 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007903 }
7904
Przemek Stekiel51eac532022-12-07 11:04:51 +01007905 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007906 status = PSA_ERROR_INVALID_ARGUMENT;
7907 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007908 }
7909
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007910 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007911 status = PSA_ERROR_BAD_STATE;
7912 goto exit;
7913 }
7914
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007915 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7916 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007917 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7918 goto exit;
7919 }
7920
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007921 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7922 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007923
7924 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007926 psa_pake_abort(operation);
7927 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007928}
7929
7930psa_status_t psa_pake_set_role(
7931 psa_pake_operation_t *operation,
7932 psa_pake_role_t role)
7933{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007935
Przemek Stekiel51eac532022-12-07 11:04:51 +01007936 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007937 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007938 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007939 }
7940
Przemek Stekiel09104b82023-03-06 14:11:51 +01007941 switch (operation->alg) {
7942#if defined(PSA_WANT_ALG_JPAKE)
7943 case PSA_ALG_JPAKE:
7944 if (role == PSA_PAKE_ROLE_NONE) {
7945 return PSA_SUCCESS;
7946 }
7947 status = PSA_ERROR_INVALID_ARGUMENT;
7948 break;
7949#endif
7950 default:
7951 (void) role;
7952 status = PSA_ERROR_NOT_SUPPORTED;
7953 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007954 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007955exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007956 psa_pake_abort(operation);
7957 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007958}
7959
David Horstmanne7f21e62023-05-12 18:17:21 +01007960/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007961#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007962static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007963 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007964{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007965 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007966 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007967 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007968
David Horstmann024e5c52023-06-14 15:48:21 +01007969 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007970 is_x1 = (stage->outputs < 1);
7971 } else {
7972 is_x1 = (stage->inputs < 1);
7973 }
7974
David Horstmann74a3d8c2023-06-14 18:28:19 +01007975 key_share_step = is_x1 ?
7976 PSA_JPAKE_X1_STEP_KEY_SHARE :
7977 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007978 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007979 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7980 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7981 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7982 } else {
7983 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007984 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007985 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007986}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007987#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007988
Przemek Stekiel2797d372022-12-22 11:19:22 +01007989static psa_status_t psa_pake_complete_inputs(
7990 psa_pake_operation_t *operation)
7991{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007993 /* Create copy of the inputs on stack as inputs share memory
7994 with the driver context which will be setup by the driver. */
7995 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01007996
Przemek Stekielfde11282023-03-13 16:06:09 +01007997 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01007998 return PSA_ERROR_BAD_STATE;
7999 }
8000
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008001 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008002 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8003 return PSA_ERROR_BAD_STATE;
8004 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008005 }
8006
Przemek Stekiel18620a32023-01-17 16:34:52 +01008007 /* Clear driver context */
8008 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8009
8010 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008011
8012 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008013 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
8014 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008015
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008016 /* User and peer are translated to role. */
8017 mbedtls_free(inputs.user);
8018 mbedtls_free(inputs.peer);
8019
Przemek Stekiel2797d372022-12-22 11:19:22 +01008020 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008021#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008022 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008023 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008024 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008025#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008026 {
8027 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008028 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008029 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008030 return status;
8031}
8032
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008033#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008034static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008035 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008036 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008037 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008038{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008039 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8040 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8041 step != PSA_PAKE_STEP_ZK_PROOF) {
8042 return PSA_ERROR_INVALID_ARGUMENT;
8043 }
8044
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008045 psa_jpake_computation_stage_t *computation_stage =
8046 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008047
David Horstmann5da95602023-06-08 15:37:12 +01008048 if (computation_stage->round != PSA_JPAKE_FIRST &&
8049 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008050 return PSA_ERROR_BAD_STATE;
8051 }
8052
David Horstmanne7f21e62023-05-12 18:17:21 +01008053 /* Check that the step we are given is the one we were expecting */
8054 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008055 return PSA_ERROR_BAD_STATE;
8056 }
8057
David Horstmanne7f21e62023-05-12 18:17:21 +01008058 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8059 computation_stage->inputs == 0 &&
8060 computation_stage->outputs == 0) {
8061 /* Start of the round, so function decides whether we are inputting
8062 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008063 computation_stage->io_mode = io_mode;
8064 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008065 /* Middle of the round so the mode we are in must match the function
8066 * called by the user */
8067 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008068 }
8069
8070 return PSA_SUCCESS;
8071}
8072
David Horstmanne7f21e62023-05-12 18:17:21 +01008073static psa_status_t psa_jpake_epilogue(
8074 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008075 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008076{
David Horstmanne7f21e62023-05-12 18:17:21 +01008077 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008078 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008079
David Horstmanne7f21e62023-05-12 18:17:21 +01008080 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8081 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008082 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008083 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008084 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008085 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008086 }
8087 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008088 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008089 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008090 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008091 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008092 }
8093 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008094 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8095 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 /* End of a round, move to the next round */
8097 stage->inputs = 0;
8098 stage->outputs = 0;
8099 stage->round++;
8100 }
8101 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008102 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008103 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008104 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008105 return PSA_SUCCESS;
8106}
David Horstmanne7f21e62023-05-12 18:17:21 +01008107
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008108#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008109
Neil Armstronga7d08c32022-06-01 18:21:20 +02008110psa_status_t psa_pake_output(
8111 psa_pake_operation_t *operation,
8112 psa_pake_step_t step,
8113 uint8_t *output,
8114 size_t output_size,
8115 size_t *output_length)
8116{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008117 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008118 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008119 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008120
8121 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008122 status = psa_pake_complete_inputs(operation);
8123 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008124 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008125 }
8126 }
8127
8128 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008129 status = PSA_ERROR_BAD_STATE;
8130 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008131 }
8132
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008133 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008134 status = PSA_ERROR_INVALID_ARGUMENT;
8135 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008136 }
8137
Przemek Stekiele12ed362022-12-21 12:54:46 +01008138 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008139#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008140 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008141 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008143 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008145 driver_step = convert_jpake_computation_stage_to_driver_step(
8146 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008147 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008148#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008150 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008151 status = PSA_ERROR_NOT_SUPPORTED;
8152 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 }
8154
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008155 status = psa_driver_wrapper_pake_output(operation, driver_step,
8156 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157
8158 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008159 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160 }
8161
8162 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008163#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008165 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008167 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 }
8169 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008170#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008171 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008172 status = PSA_ERROR_NOT_SUPPORTED;
8173 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008174 }
8175
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008176 return PSA_SUCCESS;
8177exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008178 psa_pake_abort(operation);
8179 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008180}
8181
8182psa_status_t psa_pake_input(
8183 psa_pake_operation_t *operation,
8184 psa_pake_step_t step,
8185 const uint8_t *input,
8186 size_t input_length)
8187{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008189 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008190 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8191 operation->primitive,
8192 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008193
8194 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008195 status = psa_pake_complete_inputs(operation);
8196 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008197 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008198 }
8199 }
8200
8201 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008202 status = PSA_ERROR_BAD_STATE;
8203 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008204 }
8205
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008206 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008207 status = PSA_ERROR_INVALID_ARGUMENT;
8208 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008209 }
8210
Przemek Stekiele12ed362022-12-21 12:54:46 +01008211 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008212#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008213 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008214 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008216 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008218 driver_step = convert_jpake_computation_stage_to_driver_step(
8219 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008221#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008223 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008224 status = PSA_ERROR_NOT_SUPPORTED;
8225 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 }
8227
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008228 status = psa_driver_wrapper_pake_input(operation, driver_step,
8229 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230
8231 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008233 }
8234
8235 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008236#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008237 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008238 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008239 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 }
8242 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008243#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008244 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 status = PSA_ERROR_NOT_SUPPORTED;
8246 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008247 }
8248
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 return PSA_SUCCESS;
8250exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008251 psa_pake_abort(operation);
8252 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008253}
8254
8255psa_status_t psa_pake_get_implicit_key(
8256 psa_pake_operation_t *operation,
8257 psa_key_derivation_operation_t *output)
8258{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008260 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008261 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008262 size_t shared_key_len = 0;
8263
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008264 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 status = PSA_ERROR_BAD_STATE;
8266 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008267 }
8268
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008269#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008271 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008272 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008273 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008274 status = PSA_ERROR_BAD_STATE;
8275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008277 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008278#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008279 {
8280 status = PSA_ERROR_NOT_SUPPORTED;
8281 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 }
8283
Przemek Stekiel0c781802022-11-29 14:53:13 +01008284 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8285 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008286 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008287 &shared_key_len);
8288
8289 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008290 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008291 }
8292
8293 status = psa_key_derivation_input_bytes(output,
8294 PSA_KEY_DERIVATION_INPUT_SECRET,
8295 shared_key,
8296 shared_key_len);
8297
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008298 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008299exit:
8300 abort_status = psa_pake_abort(operation);
8301 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008302}
8303
8304psa_status_t psa_pake_abort(
8305 psa_pake_operation_t *operation)
8306{
Przemek Stekield69dca92023-01-31 19:59:20 +01008307 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008308
Przemek Stekield69dca92023-01-31 19:59:20 +01008309 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008310 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008311 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008312
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008313 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8314 if (operation->data.inputs.password != NULL) {
8315 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008316 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008317 mbedtls_free(operation->data.inputs.password);
8318 }
8319 if (operation->data.inputs.user != NULL) {
8320 mbedtls_free(operation->data.inputs.user);
8321 }
8322 if (operation->data.inputs.peer != NULL) {
8323 mbedtls_free(operation->data.inputs.peer);
8324 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008325 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008326 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008327
Przemek Stekield69dca92023-01-31 19:59:20 +01008328 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008329}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008330#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008331
Gilles Peskinee59236f2018-01-27 23:32:46 +01008332#endif /* MBEDTLS_PSA_CRYPTO_C */