blob: 94139afaa44c8c8384102fff429407d9aeaf9b49 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000076#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020084#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020086#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
87 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020089#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020090#endif
91
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010092/****************************************************************/
93/* Global data, support functions and library management */
94/****************************************************************/
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099}
100
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100101/* Values for psa_global_data_t::rng_state */
102#define RNG_NOT_INITIALIZED 0
103#define RNG_INITIALIZED 1
104#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100107 uint8_t initialized;
108 uint8_t rng_state;
109 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100110 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100111} psa_global_data_t;
112
113static psa_global_data_t global_data;
114
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100115#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
116mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
117 &global_data.rng.drbg;
118#endif
119
itayzafrir0adf0fc2018-09-06 16:24:41 +0300120#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (global_data.initialized == 0) \
122 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100124int psa_can_do_hash(psa_algorithm_t hash_alg)
125{
126 (void) hash_alg;
127 return global_data.drivers_initialized;
128}
Valerio Settia55f0422023-07-10 15:34:41 +0200129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200131 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200132static int psa_is_dh_key_size_valid(size_t bits)
133{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200134 if (bits != 2048 && bits != 3072 && bits != 4096 &&
135 bits != 6144 && bits != 8192) {
136 return 0;
137 }
138
139 return 1;
140}
Valerio Settia55f0422023-07-10 15:34:41 +0200141#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200142 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200143 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100147 /* Mbed TLS error codes can combine a high-level error code and a
148 * low-level error code. The low-level error usually reflects the
149 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 int low_level_ret = -(-ret & 0x007f);
151 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100152 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100154
Dave Rodgman509b5672023-08-16 19:26:23 +0100155#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskinea5905292018-02-07 20:59:33 +0100156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100159#endif
160
161#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200162 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
163 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
164 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
165 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
166 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200168 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100172#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200173
Dave Rodgman509b5672023-08-16 19:26:23 +0100174#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000176 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000177#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100180#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Dave Rodgman509b5672023-08-16 19:26:23 +0100182#if defined(PSA_WANT_ALG_CCM)
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100187#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100188
Dave Rodgman509b5672023-08-16 19:26:23 +0100189#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200190 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100192#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200193
Dave Rodgman509b5672023-08-16 19:26:23 +0100194#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100199#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200200
Dave Rodgman509b5672023-08-16 19:26:23 +0100201#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100216#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
219 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100220 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
221 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100222 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
225 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100229#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
Dave Rodgman509b5672023-08-16 19:26:23 +0100231#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskinea5905292018-02-07 20:59:33 +0100232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
Gilles Peskinee59236f2018-01-27 23:32:46 +0100236 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
237 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
238 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100240
Dave Rodgman509b5672023-08-16 19:26:23 +0100241#if defined(PSA_WANT_ALG_GCM)
Gilles Peskinea5905292018-02-07 20:59:33 +0100242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200244 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100248#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100249
Gilles Peskine82e57d12020-11-13 21:31:17 +0100250#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100252 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
253 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100254 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
257 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100261#endif
262
Dave Rodgman509b5672023-08-16 19:26:23 +0100263#if defined(MBEDTLS_MD_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100270#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#endif
274#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100275
Dave Rodgman509b5672023-08-16 19:26:23 +0100276#if defined(MBEDTLS_BIGNUM_C)
277#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100278 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100280#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100287 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100291 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100293 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100295#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100296
Dave Rodgman509b5672023-08-16 19:26:23 +0100297#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100298 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
301 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100303#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
304 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100307#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
309 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100313 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
314 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318 case MBEDTLS_ERR_PK_INVALID_ALG:
319 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
320 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200324 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100326#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100327
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200330 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200332
Dave Rodgman509b5672023-08-16 19:26:23 +0100333#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
343 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100349 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100351#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200352
Dave Rodgman509b5672023-08-16 19:26:23 +0100353#if defined(MBEDTLS_ECP_C)
itayzafrir5c753392018-05-08 11:18:38 +0300354 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
362 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100366 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100368
Dave Rodgman509b5672023-08-16 19:26:23 +0100369#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000370 case MBEDTLS_ERR_ECP_IN_PROGRESS:
371 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100372#endif
373#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000374
Janos Follatha13b9052019-11-22 12:48:59 +0000375 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300377
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100380 }
381}
382
Paul Elliottdc42ca82023-02-24 18:11:59 +0000383/**
384 * \brief For output buffers which contain "tags"
385 * (outputs that may be checked for validity like
386 * hashes, MACs and signatures), fill the unused
387 * part of the output buffer (the whole buffer on
388 * error, the trailing part on success) with
389 * something that isn't a valid tag (barring an
390 * attack on the tag and deliberately-crafted
391 * input), in case the caller doesn't check the
392 * return status properly.
393 *
394 * \param output_buffer Pointer to buffer to wipe. May not be NULL
395 * unless \p output_buffer_size is zero.
396 * \param status Status of function called to generate
397 * output_buffer originally
398 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
399 * could be NULL.
400 * \param output_buffer_length Length of data written to output_buffer, must be
401 * less than \p output_buffer_size
402 */
403static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000404 size_t output_buffer_size, size_t output_buffer_length)
405{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000406 size_t offset = 0;
407
408 if (output_buffer_size == 0) {
409 /* If output_buffer_size is 0 then we have nothing to do. We must not
410 call memset because output_buffer may be NULL in this case */
411 return;
412 }
413
414 if (status == PSA_SUCCESS) {
415 offset = output_buffer_length;
416 }
417
418 memset(output_buffer + offset, '!', output_buffer_size - offset);
419}
420
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200421
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200422
423
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100424/****************************************************************/
425/* Key management */
426/****************************************************************/
427
Valerio Settia9aab1a2023-06-19 13:39:54 +0200428#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200429psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
430 size_t *bits)
431{
432 switch (grpid) {
Dave Rodgman6f682032023-08-16 18:44:32 +0100433#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200434 case MBEDTLS_ECP_DP_SECP192R1:
435 *bits = 192;
436 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100437#endif
438#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200439 case MBEDTLS_ECP_DP_SECP224R1:
440 *bits = 224;
441 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100442#endif
443#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200444 case MBEDTLS_ECP_DP_SECP256R1:
445 *bits = 256;
446 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100447#endif
448#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200449 case MBEDTLS_ECP_DP_SECP384R1:
450 *bits = 384;
451 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100452#endif
453#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200454 case MBEDTLS_ECP_DP_SECP521R1:
455 *bits = 521;
456 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100457#endif
458#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200459 case MBEDTLS_ECP_DP_BP256R1:
460 *bits = 256;
461 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100462#endif
463#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200464 case MBEDTLS_ECP_DP_BP384R1:
465 *bits = 384;
466 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100467#endif
468#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200469 case MBEDTLS_ECP_DP_BP512R1:
470 *bits = 512;
471 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100472#endif
473#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200474 case MBEDTLS_ECP_DP_CURVE25519:
475 *bits = 255;
476 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100477#endif
478#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200479 case MBEDTLS_ECP_DP_SECP192K1:
480 *bits = 192;
481 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100482#endif
483#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200484 case MBEDTLS_ECP_DP_SECP224K1:
485 *bits = 224;
486 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100487#endif
488#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200489 case MBEDTLS_ECP_DP_SECP256K1:
490 *bits = 256;
491 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100492#endif
493#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200494 case MBEDTLS_ECP_DP_CURVE448:
495 *bits = 448;
496 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100497#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200498 default:
499 *bits = 0;
500 return 0;
501 }
502}
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
505 size_t bits,
506 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100509 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100511#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100512 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#endif
515#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100516 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100518#endif
519#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100520 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100522#endif
523#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100524 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100526#endif
527#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100530 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (bits_is_sloppy) {
532 return MBEDTLS_ECP_DP_SECP521R1;
533 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100534 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100535#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100536 }
537 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100538
Paul Elliott8ff510a2020-06-02 17:19:28 +0100539 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100541#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100542 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100544#endif
545#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100546 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100548#endif
549#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100550 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100552#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100553 }
554 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100555
Paul Elliott8ff510a2020-06-02 17:19:28 +0100556 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100558#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100559 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100561 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (bits_is_sloppy) {
563 return MBEDTLS_ECP_DP_CURVE25519;
564 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100565 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100566#endif
567#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100568 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100570#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100571 }
572 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100573
Paul Elliott8ff510a2020-06-02 17:19:28 +0100574 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100576#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100577 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100579#endif
580#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100581 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100583#endif
584#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100585 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100587#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100588 }
589 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200590 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100591
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100592 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200594}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200595#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
598 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200599{
600 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200603 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200604 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200605 case PSA_KEY_TYPE_PASSWORD:
606 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200607 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100608#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200609 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (bits != 128 && bits != 192 && bits != 256) {
611 return PSA_ERROR_INVALID_ARGUMENT;
612 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200613 break;
614#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200615#if defined(PSA_WANT_KEY_TYPE_ARIA)
616 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (bits != 128 && bits != 192 && bits != 256) {
618 return PSA_ERROR_INVALID_ARGUMENT;
619 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200620 break;
621#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100622#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200623 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (bits != 128 && bits != 192 && bits != 256) {
625 return PSA_ERROR_INVALID_ARGUMENT;
626 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 break;
628#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100629#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (bits != 64 && bits != 128 && bits != 192) {
632 return PSA_ERROR_INVALID_ARGUMENT;
633 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200634 break;
635#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100636#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200637 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (bits != 256) {
639 return PSA_ERROR_INVALID_ARGUMENT;
640 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200641 break;
642#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200645 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (bits % 8 != 0) {
647 return PSA_ERROR_INVALID_ARGUMENT;
648 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200651}
652
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100653/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100654 *
Steven Cooremancd640932021-03-08 12:00:27 +0100655 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
656 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100657 *
658 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100659 * \param[in] key_type The key type of the key to be used with the
660 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100661 *
662 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100663 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100666 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100667MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100668 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if (PSA_ALG_IS_HMAC(algorithm)) {
672 if (key_type == PSA_KEY_TYPE_HMAC) {
673 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100674 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100675 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
678 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
679 * key. */
680 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
681 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
682 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
683 * the block length (larger than 1) for block ciphers. */
684 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
685 return PSA_SUCCESS;
686 }
687 }
688 }
689
690 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100691}
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
694 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200695{
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (slot->key.data != NULL) {
697 return PSA_ERROR_ALREADY_EXISTS;
698 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 slot->key.data = mbedtls_calloc(1, buffer_length);
701 if (slot->key.data == NULL) {
702 return PSA_ERROR_INSUFFICIENT_MEMORY;
703 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200704
Ronald Cronea0f8a62020-11-25 17:52:23 +0100705 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200707}
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
710 const uint8_t *data,
711 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200712{
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 psa_status_t status = psa_allocate_buffer_to_slot(slot,
714 data_length);
715 if (status != PSA_SUCCESS) {
716 return status;
717 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 memcpy(slot->key.data, data, data_length);
720 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200721}
722
Ronald Crona0fe59f2020-11-29 11:14:53 +0100723psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100724 const psa_key_attributes_t *attributes,
725 const uint8_t *data, size_t data_length,
726 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
730 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200732 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (data_length == 0) {
734 return PSA_ERROR_NOT_SUPPORTED;
735 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 if (key_type_is_raw_bytes(type)) {
738 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
741 *bits);
742 if (status != PSA_SUCCESS) {
743 return status;
744 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Ronald Crondd04d422020-11-28 15:14:42 +0100746 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100748 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return PSA_SUCCESS;
752 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200753#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200754 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100755 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200756 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100757 return PSA_ERROR_INVALID_ARGUMENT;
758 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200759 return mbedtls_psa_ffdh_import_key(attributes,
760 data, data_length,
761 key_buffer, key_buffer_size,
762 key_buffer_length,
763 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100764 }
Valerio Settia55f0422023-07-10 15:34:41 +0200765#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200766 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200767#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
769 if (PSA_KEY_TYPE_IS_ECC(type)) {
770 return mbedtls_psa_ecp_import_key(attributes,
771 data, data_length,
772 key_buffer, key_buffer_size,
773 key_buffer_length,
774 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100775 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200776#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800777 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200778#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
779 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
781 if (PSA_KEY_TYPE_IS_RSA(type)) {
782 return mbedtls_psa_rsa_import_key(attributes,
783 data, data_length,
784 key_buffer, key_buffer_size,
785 key_buffer_length,
786 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200787 }
Valerio Settife478902023-07-25 12:27:19 +0200788#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
789 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800790 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100791 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100794}
795
Gilles Peskinef603c712019-01-19 13:40:11 +0100796/** Calculate the intersection of two algorithm usage policies.
797 *
798 * Return 0 (which allows no operation) on incompatibility.
799 */
800static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100801 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100802 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100804{
Gilles Peskine549ea862019-05-22 11:45:59 +0200805 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (alg1 == alg2) {
807 return alg1;
808 }
Steven Cooreman03488022021-02-18 12:07:20 +0100809 /* If the policies are from the same hash-and-sign family, check
810 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
812 PSA_ALG_IS_SIGN_HASH(alg2) &&
813 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
814 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
815 return alg2;
816 }
817 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
818 return alg1;
819 }
Steven Cooreman03488022021-02-18 12:07:20 +0100820 }
821 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100822 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100823 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
825 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
826 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
827 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
828 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100829 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100830
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100831 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
833 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
834 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
835 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100836 }
837 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
839 (alg1_len <= alg2_len)) {
840 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100841 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
843 (alg2_len <= alg1_len)) {
844 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100845 }
846 }
847 /* If the policies are from the same MAC family, check whether one
848 * of them is a minimum-MAC-length policy. Calculate the most
849 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
851 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
852 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100853 /* Validate the combination of key type and algorithm. Since the base
854 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
856 return 0;
857 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100858
Steven Cooreman31a876d2021-03-03 20:47:40 +0100859 /* Get the (exact or at-least) output lengths for both sides of the
860 * requested intersection. None of the currently supported algorithms
861 * have an output length dependent on the actual key size, so setting it
862 * to a bogus value of 0 is currently OK.
863 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100864 * Note that for at-least-this-length wildcard algorithms, the output
865 * length is set to the shortest allowed length, which allows us to
866 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
868 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100869 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100870
Steven Cooremana1d83222021-02-25 10:20:29 +0100871 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
873 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
874 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100875 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100876
877 /* If only one is an at-least-this-length policy, the intersection would
878 * be the other (fixed-length) policy as long as said fixed length is
879 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
881 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100882 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
884 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100886
Steven Cooremancd640932021-03-08 12:00:27 +0100887 /* If none of them are wildcards, check whether they define the same tag
888 * length. This is still possible here when one is default-length and
889 * the other specific-length. Ensure to always return the
890 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (alg1_len == alg2_len) {
892 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
893 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100894 }
895 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100897}
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899static int psa_key_algorithm_permits(psa_key_type_t key_type,
900 psa_algorithm_t policy_alg,
901 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902{
Gilles Peskine549ea862019-05-22 11:45:59 +0200903 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (requested_alg == policy_alg) {
905 return 1;
906 }
Steven Cooreman03488022021-02-18 12:07:20 +0100907 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
908 * and requested_alg is the same hash-and-sign family with any hash,
909 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
911 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
912 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
913 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100914 }
915 /* If policy_alg is a wildcard AEAD algorithm of the same base as
916 * the requested algorithm, check the requested tag length to be
917 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (PSA_ALG_IS_AEAD(policy_alg) &&
919 PSA_ALG_IS_AEAD(requested_alg) &&
920 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
921 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
922 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
923 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
924 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100925 }
Steven Cooremancd640932021-03-08 12:00:27 +0100926 /* If policy_alg is a MAC algorithm of the same base as the requested
927 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (PSA_ALG_IS_MAC(policy_alg) &&
929 PSA_ALG_IS_MAC(requested_alg) &&
930 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
931 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100932 /* Validate the combination of key type and algorithm. Since the policy
933 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
935 return 0;
936 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100937
Steven Cooreman31a876d2021-03-03 20:47:40 +0100938 /* Get both the requested output length for the algorithm which is to be
939 * verified, and the default output length for the base algorithm.
940 * Note that none of the currently supported algorithms have an output
941 * length dependent on actual key size, so setting it to a bogus value
942 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100943 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100945 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 key_type, 0,
947 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100948
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100949 /* If the policy is default-length, only allow an algorithm with
950 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
952 return requested_output_length == default_output_length;
953 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100954
955 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100956 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
958 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
959 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100960 }
961
Steven Cooremancd640932021-03-08 12:00:27 +0100962 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
963 * check for the requested MAC length to be equal to or longer than the
964 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
966 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
967 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100968 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200969 }
Steven Cooremance48e852020-10-05 16:02:45 +0200970 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200971 * a key derivation with that key agreement should also be allowed. This
972 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
974 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
975 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
976 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200977 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100978 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200980}
981
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100982/** Test whether a policy permits an algorithm.
983 *
984 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100985 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100986 * \note This function requires providing the key type for which the policy is
987 * being validated, since some algorithm policy definitions (e.g. MAC)
988 * have different properties depending on what kind of cipher it is
989 * combined with.
990 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 * \retval PSA_SUCCESS When \p alg is a specific algorithm
992 * allowed by the \p policy.
993 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
994 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
995 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100996 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
998 psa_key_type_t key_type,
999 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001000{
Steven Cooremand788fab2021-02-25 11:29:17 +01001001 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (alg == 0) {
1003 return PSA_ERROR_INVALID_ARGUMENT;
1004 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001005
Steven Cooreman7e39f052021-02-23 14:18:32 +01001006 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (PSA_ALG_IS_WILDCARD(alg)) {
1008 return PSA_ERROR_INVALID_ARGUMENT;
1009 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1012 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1013 return PSA_SUCCESS;
1014 } else {
1015 return PSA_ERROR_NOT_PERMITTED;
1016 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001017}
1018
Gilles Peskinef603c712019-01-19 13:40:11 +01001019/** Restrict a key policy based on a constraint.
1020 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001021 * \note This function requires providing the key type for which the policy is
1022 * being restricted, since some algorithm policy definitions (e.g. MAC)
1023 * have different properties depending on what kind of cipher it is
1024 * combined with.
1025 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001026 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001027 * \param[in,out] policy The policy to restrict.
1028 * \param[in] constraint The policy constraint to apply.
1029 *
1030 * \retval #PSA_SUCCESS
1031 * \c *policy contains the intersection of the original value of
1032 * \c *policy and \c *constraint.
1033 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001034 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001035 * \c *policy is unchanged.
1036 */
1037static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001038 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001039 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001041{
1042 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1044 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001045 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1047 constraint->alg2);
1048 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1049 return PSA_ERROR_INVALID_ARGUMENT;
1050 }
1051 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1052 return PSA_ERROR_INVALID_ARGUMENT;
1053 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001054 policy->usage &= constraint->usage;
1055 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001056 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001058}
1059
Przemek Stekiel061f6942022-11-30 10:51:35 +01001060/** Get the description of a key given its identifier and policy constraints
1061 * and lock it.
1062 *
1063 * The key must have allow all the usage flags set in \p usage. If \p alg is
1064 * nonzero, the key must allow operations with this algorithm. If \p alg is
1065 * zero, the algorithm is not checked.
1066 *
1067 * In case of a persistent key, the function loads the description of the key
1068 * into a key slot if not already done.
1069 *
1070 * On success, the returned key slot is locked. It is the responsibility of
1071 * the caller to unlock the key slot when it does not access it anymore.
1072 */
1073static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001074 mbedtls_svc_key_id_t key,
1075 psa_key_slot_t **p_slot,
1076 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001078{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001080 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 status = psa_get_and_lock_key_slot(key, p_slot);
1083 if (status != PSA_SUCCESS) {
1084 return status;
1085 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001086 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001087
1088 /* Enforce that usage policy for the key slot contains all the flags
1089 * required by the usage parameter. There is one exception: public
1090 * keys can always be exported, so we treat public key objects as
1091 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001093 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001097 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001098 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001099 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001100
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001101 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (alg != 0) {
1103 status = psa_key_policy_permits(&slot->attr.policy,
1104 slot->attr.type,
1105 alg);
1106 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001107 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001109 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001112
1113error:
1114 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001118}
Darryl Green940d72c2018-07-13 13:18:51 +01001119
Ronald Cron5c522922020-11-14 16:35:34 +01001120/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001121 *
1122 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001123 * available, as opposed to a key in a secure element and/or to be used
1124 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001125 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001126 * This is a temporary function that may be used instead of
1127 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1128 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001129 *
Ronald Cron5c522922020-11-14 16:35:34 +01001130 * On success, the returned key slot is locked. It is the responsibility of the
1131 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 */
Ronald Cron5c522922020-11-14 16:35:34 +01001133static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1134 mbedtls_svc_key_id_t key,
1135 psa_key_slot_t **p_slot,
1136 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001138{
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1140 usage, alg);
1141 if (status != PSA_SUCCESS) {
1142 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001143 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1146 psa_unlock_key_slot(*p_slot);
1147 *p_slot = NULL;
1148 return PSA_ERROR_NOT_SUPPORTED;
1149 }
1150
1151 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001152}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001155{
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001157 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001159
Ronald Cronea0f8a62020-11-25 17:52:23 +01001160 slot->key.data = NULL;
1161 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001164}
1165
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001166/** Completely wipe a slot in memory, including its policy.
1167 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001169{
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 /*
1173 * As the return error code may not be handled in case of multiple errors,
1174 * do our best to report an unexpected lock counter. Assert with
1175 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1176 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1177 * function is called as part of the execution of a test suite, the
1178 * execution of the test suite is stopped in error if the assertion fails.
1179 */
1180 if (slot->lock_count != 1) {
1181 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001182 status = PSA_ERROR_CORRUPTION_DETECTED;
1183 }
1184
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001185 /* Multipart operations may still be using the key. This is safe
1186 * because all multipart operation objects are independent from
1187 * the key slot: if they need to access the key after the setup
1188 * phase, they have a copy of the key. Note that this means that
1189 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001190 /* At this point, key material and other type-specific content has
1191 * been wiped. Clear remaining metadata. We can call memset and not
1192 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 memset(slot, 0, sizeof(*slot));
1194 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001195}
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001199 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 psa_status_t status; /* status of the last operation */
1201 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001202#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1203 psa_se_drv_table_entry_t *driver;
1204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (mbedtls_svc_key_id_is_null(key)) {
1207 return PSA_SUCCESS;
1208 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001211 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001212 * key, this will load the key description from persistent memory if not
1213 * done yet. We cannot avoid this loading as without it we don't know if
1214 * the key is operated by an SE or not and this information is needed by
1215 * the current implementation.
1216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 status = psa_get_and_lock_key_slot(key, &slot);
1218 if (status != PSA_SUCCESS) {
1219 return status;
1220 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001221
Ronald Cronf2911112020-10-29 17:51:10 +01001222 /*
1223 * If the key slot containing the key description is under access by the
1224 * library (apart from the present access), the key cannot be destroyed
1225 * yet. For the time being, just return in error. Eventually (to be
1226 * implemented), the key should be destroyed when all accesses have
1227 * stopped.
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (slot->lock_count > 1) {
1230 psa_unlock_key_slot(slot);
1231 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001232 }
1233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001235 /* Refuse the destruction of a read-only key (which may or may not work
1236 * if we attempt it, depending on whether the key is merely read-only
1237 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001238 * Just do the best we can, which is to wipe the copy in memory
1239 * (done in this function's cleanup code). */
1240 overall_status = PSA_ERROR_NOT_PERMITTED;
1241 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001242 }
1243
Gilles Peskine354f7672019-07-12 23:46:38 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1246 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001247 /* For a key in a secure element, we need to do three things:
1248 * remove the key file in internal storage, destroy the
1249 * key inside the secure element, and update the driver's
1250 * persistent data. Start a transaction that will encompass these
1251 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001253 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001255 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 status = psa_crypto_save_transaction();
1257 if (status != PSA_SUCCESS) {
1258 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001259 /* We should still try to destroy the key in the secure
1260 * element and the key metadata in storage. This is especially
1261 * important if the error is that the storage is full.
1262 * But how to do it exactly without risking an inconsistent
1263 * state after a reset?
1264 * https://github.com/ARMmbed/mbed-crypto/issues/215
1265 */
1266 overall_status = status;
1267 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001268 }
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 status = psa_destroy_se_key(driver,
1271 psa_key_slot_get_slot_number(slot));
1272 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001273 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001275 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001276#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1277
Darryl Greend49a4992018-06-18 17:27:26 +01001278#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1280 status = psa_destroy_persistent_key(slot->attr.id);
1281 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001285 /* TODO: other slots may have a copy of the same key. We should
1286 * invalidate them.
1287 * https://github.com/ARMmbed/mbed-crypto/issues/214
1288 */
Darryl Greend49a4992018-06-18 17:27:26 +01001289 }
1290#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001291
Gilles Peskinefc762652019-07-22 19:30:34 +02001292#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (driver != NULL) {
1294 status = psa_save_se_persistent_data(driver);
1295 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001296 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 }
1298 status = psa_crypto_stop_transaction();
1299 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001300 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001302 }
1303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1304
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001309 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 }
1311 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312}
1313
Valerio Settib2bcedb2023-07-10 11:24:00 +02001314#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001315 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001316static psa_status_t psa_get_rsa_public_exponent(
1317 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001319{
1320 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001322 uint8_t *buffer = NULL;
1323 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1327 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001328 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 }
1330 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001331 /* It's the default value, which is reported as an empty string,
1332 * so there's nothing to do. */
1333 goto exit;
1334 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 buflen = mbedtls_mpi_size(&mpi);
1337 buffer = mbedtls_calloc(1, buflen);
1338 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001339 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1340 goto exit;
1341 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1343 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001344 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001346 attributes->domain_parameters = buffer;
1347 attributes->domain_parameters_size = buflen;
1348
1349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 mbedtls_mpi_free(&mpi);
1351 if (ret != 0) {
1352 mbedtls_free(buffer);
1353 }
1354 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001355}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001356#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001357 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001358
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001359/** Retrieve all the publicly-accessible attributes of a key.
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1362 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001363{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001366 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1371 if (status != PSA_SUCCESS) {
1372 return status;
1373 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001374
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001375 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1377 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001378
1379#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1381 psa_set_key_slot_number(attributes,
1382 psa_key_slot_get_slot_number(slot));
1383 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001387#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1388 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001389 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001390 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001391 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001392 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001393 * is not yet implemented.
1394 * https://github.com/ARMmbed/mbed-crypto/issues/216
1395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001397 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001398
Ronald Cron90857082020-11-25 14:58:33 +01001399 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 slot->attr.type,
1401 slot->key.data,
1402 slot->key.bytes,
1403 &rsa);
1404 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001405 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 status = psa_get_rsa_public_exponent(rsa,
1409 attributes);
1410 mbedtls_rsa_free(rsa);
1411 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001412 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001413 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001414#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1415 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001416 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001417 default:
1418 /* Nothing else to do. */
1419 break;
1420 }
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (status != PSA_SUCCESS) {
1423 psa_reset_key_attributes(attributes);
1424 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429}
1430
Gilles Peskinec8000c02019-08-02 20:15:51 +02001431#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1432psa_status_t psa_get_key_slot_number(
1433 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001435{
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001437 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return PSA_SUCCESS;
1439 } else {
1440 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001441 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001442}
1443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1446 size_t key_buffer_size,
1447 uint8_t *data,
1448 size_t data_size,
1449 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001450{
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (key_buffer_size > data_size) {
1452 return PSA_ERROR_BUFFER_TOO_SMALL;
1453 }
1454 memcpy(data, key_buffer, key_buffer_size);
1455 memset(data + key_buffer_size, 0,
1456 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001457 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001459}
1460
Ronald Cron7285cda2020-11-26 14:46:37 +01001461psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001462 const psa_key_attributes_t *attributes,
1463 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001465{
Ronald Crond18b5f82020-11-25 16:46:05 +01001466 psa_key_type_t type = attributes->core.type;
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (key_type_is_raw_bytes(type) ||
1469 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001470 PSA_KEY_TYPE_IS_ECC(type) ||
1471 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return psa_export_key_buffer_internal(
1473 key_buffer, key_buffer_size,
1474 data, data_size, data_length);
1475 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476 /* This shouldn't happen in the reference implementation, but
1477 it is valid for a special-purpose implementation to omit
1478 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001480 }
1481}
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1484 uint8_t *data,
1485 size_t data_size,
1486 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001487{
1488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1489 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1490 psa_key_slot_t *slot;
1491
Ronald Crone907e552021-01-18 13:22:38 +01001492 /* Reject a zero-length output buffer now, since this can never be a
1493 * valid key representation. This way we know that data must be a valid
1494 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (data_size == 0) {
1496 return PSA_ERROR_BUFFER_TOO_SMALL;
1497 }
Ronald Crone907e552021-01-18 13:22:38 +01001498
Ronald Cron9486f9d2020-11-26 13:36:23 +01001499 /* Set the key to empty now, so that even when there are errors, we always
1500 * set data_length to a value between 0 and data_size. On error, setting
1501 * the key to empty is a good choice because an empty key representation is
1502 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001503 *data_length = 0;
1504
Ronald Cron9486f9d2020-11-26 13:36:23 +01001505 /* Export requires the EXPORT flag. There is an exception for public keys,
1506 * which don't require any flag, but
1507 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1510 PSA_KEY_USAGE_EXPORT, 0);
1511 if (status != PSA_SUCCESS) {
1512 return status;
1513 }
mohammad160306e79202018-03-28 13:17:44 +03001514
Ronald Crond18b5f82020-11-25 16:46:05 +01001515 psa_key_attributes_t attributes = {
1516 .core = slot->attr
1517 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 status = psa_driver_wrapper_export_key(&attributes,
1519 slot->key.data, slot->key.bytes,
1520 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525}
1526
Ronald Cron7285cda2020-11-26 14:46:37 +01001527psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001528 const psa_key_attributes_t *attributes,
1529 const uint8_t *key_buffer,
1530 size_t key_buffer_size,
1531 uint8_t *data,
1532 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001534{
Ronald Crond18b5f82020-11-25 16:46:05 +01001535 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001536
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001537 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001538 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1539 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001540 /* Exporting public -> public */
1541 return psa_export_key_buffer_internal(
1542 key_buffer, key_buffer_size,
1543 data, data_size, data_length);
1544 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001545#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001546 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1547 return mbedtls_psa_rsa_export_public_key(attributes,
1548 key_buffer,
1549 key_buffer_size,
1550 data,
1551 data_size,
1552 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001553#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001554 /* We don't know how to convert a private RSA key to public. */
1555 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001556#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001557 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001558 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001559#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001560 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1561 return mbedtls_psa_ecp_export_public_key(attributes,
1562 key_buffer,
1563 key_buffer_size,
1564 data,
1565 data_size,
1566 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001567#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001568 /* We don't know how to convert a private ECC key to public */
1569 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001570#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001571 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001572 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001573#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001574 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001575 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001576 key_buffer,
1577 key_buffer_size,
1578 data, data_size,
1579 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001580#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001581 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001582#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001583 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001585 (void) key_buffer;
1586 (void) key_buffer_size;
1587 (void) data;
1588 (void) data_size;
1589 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001591 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592}
Ronald Crond18b5f82020-11-25 16:46:05 +01001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1595 uint8_t *data,
1596 size_t data_size,
1597 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001598{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001600 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001601 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001602 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001603
Ronald Crone907e552021-01-18 13:22:38 +01001604 /* Reject a zero-length output buffer now, since this can never be a
1605 * valid key representation. This way we know that data must be a valid
1606 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (data_size == 0) {
1608 return PSA_ERROR_BUFFER_TOO_SMALL;
1609 }
Ronald Crone907e552021-01-18 13:22:38 +01001610
Darryl Greendd8fb772018-11-07 16:00:44 +00001611 /* Set the key to empty now, so that even when there are errors, we always
1612 * set data_length to a value between 0 and data_size. On error, setting
1613 * the key to empty is a good choice because an empty key representation is
1614 * unlikely to be accepted anywhere. */
1615 *data_length = 0;
1616
1617 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1619 if (status != PSA_SUCCESS) {
1620 return status;
1621 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1624 status = PSA_ERROR_INVALID_ARGUMENT;
1625 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001626 }
1627
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001628 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001629 .core = slot->attr
1630 };
Ronald Cron67227982020-11-26 15:16:05 +01001631 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001632 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001634
1635exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001639}
1640
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001641MBEDTLS_STATIC_ASSERT(
1642 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1643 "One or more key attribute flag is listed as both external-only and dual-use")
1644MBEDTLS_STATIC_ASSERT(
1645 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1646 "One or more key attribute flag is listed as both internal-only and dual-use")
1647MBEDTLS_STATIC_ASSERT(
1648 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1649 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001650
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001651/** Validate that a key policy is internally well-formed.
1652 *
1653 * This function only rejects invalid policies. It does not validate the
1654 * consistency of the policy with respect to other attributes of the key
1655 * such as the key type.
1656 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001657static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1660 PSA_KEY_USAGE_COPY |
1661 PSA_KEY_USAGE_ENCRYPT |
1662 PSA_KEY_USAGE_DECRYPT |
1663 PSA_KEY_USAGE_SIGN_MESSAGE |
1664 PSA_KEY_USAGE_VERIFY_MESSAGE |
1665 PSA_KEY_USAGE_SIGN_HASH |
1666 PSA_KEY_USAGE_VERIFY_HASH |
1667 PSA_KEY_USAGE_VERIFY_DERIVATION |
1668 PSA_KEY_USAGE_DERIVE)) != 0) {
1669 return PSA_ERROR_INVALID_ARGUMENT;
1670 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001673}
1674
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001675/** Validate the internal consistency of key attributes.
1676 *
1677 * This function only rejects invalid attribute values. If does not
1678 * validate the consistency of the attributes with any key data that may
1679 * be involved in the creation of the key.
1680 *
1681 * Call this function early in the key creation process.
1682 *
1683 * \param[in] attributes Key attributes for the new key.
1684 * \param[out] p_drv On any return, the driver for the key, if any.
1685 * NULL for a transparent key.
1686 *
1687 */
1688static psa_status_t psa_validate_key_attributes(
1689 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001691{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001692 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1694 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 status = psa_validate_key_location(lifetime, p_drv);
1697 if (status != PSA_SUCCESS) {
1698 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001699 }
1700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 status = psa_validate_key_persistence(lifetime);
1702 if (status != PSA_SUCCESS) {
1703 return status;
1704 }
1705
1706 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1707 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1708 return PSA_ERROR_INVALID_ARGUMENT;
1709 }
1710 } else {
1711 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1712 return PSA_ERROR_INVALID_ARGUMENT;
1713 }
1714 }
1715
1716 status = psa_validate_key_policy(&attributes->core.policy);
1717 if (status != PSA_SUCCESS) {
1718 return status;
1719 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001720
1721 /* Refuse to create overly large keys.
1722 * Note that this doesn't trigger on import if the attributes don't
1723 * explicitly specify a size (so psa_get_key_bits returns 0), so
1724 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1726 return PSA_ERROR_NOT_SUPPORTED;
1727 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001728
Gilles Peskine91e8c332019-08-02 19:19:39 +02001729 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1731 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1732 return PSA_ERROR_INVALID_ARGUMENT;
1733 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001736}
1737
Gilles Peskine4747d192019-04-17 15:05:45 +02001738/** Prepare a key slot to receive key material.
1739 *
1740 * This function allocates a key slot and sets its metadata.
1741 *
1742 * If this function fails, call psa_fail_key_creation().
1743 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001744 * This function is intended to be used as follows:
1745 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001746 * it with the specified attributes, and in case of a volatile key assign it
1747 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001748 * -# Populate the slot with the key material.
1749 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1750 * In case of failure at any step, stop the sequence and call
1751 * psa_fail_key_creation().
1752 *
Ronald Cron5c522922020-11-14 16:35:34 +01001753 * On success, the key slot is locked. It is the responsibility of the caller
1754 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001755 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001756 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001757 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001758 * \param[out] p_slot On success, a pointer to the prepared slot.
1759 * \param[out] p_drv On any return, the driver for the key, if any.
1760 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001761 *
1762 * \retval #PSA_SUCCESS
1763 * The key slot is ready to receive key material.
1764 * \return If this function fails, the key slot is an invalid state.
1765 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 */
1767static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001768 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001770 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001772{
1773 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001774 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 psa_key_slot_t *slot;
1776
Gilles Peskinedf179142019-07-15 22:02:14 +02001777 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001778 *p_drv = NULL;
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 status = psa_validate_key_attributes(attributes, p_drv);
1781 if (status != PSA_SUCCESS) {
1782 return status;
1783 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1786 if (status != PSA_SUCCESS) {
1787 return status;
1788 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 slot = *p_slot;
1790
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001791 /* We're storing the declared bit-size of the key. It's up to each
1792 * creation mechanism to verify that this information is correct.
1793 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001794 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001795 * is optional (import, copy). In case of a volatile key, assign it the
1796 * volatile key identifier associated to the slot returned to contain its
1797 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001798
1799 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001801#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1802 slot->attr.id = volatile_key_id;
1803#else
1804 slot->attr.id.key_id = volatile_key_id;
1805#endif
1806 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001807
Gilles Peskine91e8c332019-08-02 19:19:39 +02001808 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001809 * external-only flags, query `attributes`. Thanks to the check
1810 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001811 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001812 * may have set. */
1813 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001814
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001816 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001817 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001818 * create the key file in internal storage, create the
1819 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820 * persistent data. This is done by starting a transaction that will
1821 * encompass these three actions.
1822 * For registering a volatile key, we just need to find an appropriate
1823 * slot number inside the SE. Since the key is designated volatile, creating
1824 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001825 /* The first thing to do is to find a slot number for the new key.
1826 * We save the slot number in persistent storage as part of the
1827 * transaction data. It will be needed to recover if the power
1828 * fails during the key creation process, to clean up on the secure
1829 * element side after restarting. Obtaining a slot number from the
1830 * secure element driver updates its persistent state, but we do not yet
1831 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001832 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001834 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1836 &slot_number);
1837 if (status != PSA_SUCCESS) {
1838 return status;
1839 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1842 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001844 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 status = psa_crypto_save_transaction();
1847 if (status != PSA_SUCCESS) {
1848 (void) psa_crypto_stop_transaction();
1849 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001850 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001851 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001852
1853 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001855 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001858 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001860 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001861#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001864}
Gilles Peskine4747d192019-04-17 15:05:45 +02001865
1866/** Finalize the creation of a key once its key material has been set.
1867 *
1868 * This entails writing the key to persistent storage.
1869 *
1870 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001871 * See the documentation of psa_start_key_creation() for the intended use
1872 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001873 *
Ronald Cron5c522922020-11-14 16:35:34 +01001874 * If the finalization succeeds, the function unlocks the key slot (it was
1875 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1876 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001877 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 * \param[in,out] slot Pointer to the slot with key material.
1879 * \param[in] driver The secure element driver for the key,
1880 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001881 * \param[out] key On success, identifier of the key. Note that the
1882 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001883 *
1884 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001885 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1887 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1888 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1889 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1890 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1891 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001892 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001893 * \return If this function fails, the key slot is an invalid state.
1894 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001895 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001896static psa_status_t psa_finish_key_creation(
1897 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001898 psa_se_drv_table_entry_t *driver,
1899 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001900{
1901 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001902 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001903 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001904
1905#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001909 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001910 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001912
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001913 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1914 sizeof(data.slot_number),
1915 "Slot number size does not match psa_se_key_data_storage_t");
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1918 status = psa_save_persistent_key(&slot->attr,
1919 (uint8_t *) &data,
1920 sizeof(data));
1921 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001922#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1923 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001924 /* Key material is saved in export representation in the slot, so
1925 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 status = psa_save_persistent_key(&slot->attr,
1927 slot->key.data,
1928 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001929 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001930 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001931#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1932
Gilles Peskinecbaff462019-07-12 23:46:04 +02001933#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001934 /* Finish the transaction for a key creation. This does not
1935 * happen when registering an existing key. Detect this case
1936 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001937 * creation of a persistent key in a secure element requires a transaction,
1938 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (driver != NULL &&
1940 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1941 status = psa_save_se_persistent_data(driver);
1942 if (status != PSA_SUCCESS) {
1943 psa_destroy_persistent_key(slot->attr.id);
1944 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001945 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 }
1948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001951 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_unlock_key_slot(slot);
1953 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001954 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001956 }
Ronald Cron50972942020-11-14 11:28:25 +01001957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001959}
1960
1961/** Abort the creation of a key.
1962 *
1963 * You may call this function after calling psa_start_key_creation(),
1964 * or after psa_finish_key_creation() fails. In other circumstances, this
1965 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001966 * See the documentation of psa_start_key_creation() for the intended use
1967 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001969 * \param[in,out] slot Pointer to the slot with key material.
1970 * \param[in] driver The secure element driver for the key,
1971 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001972 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973static void psa_fail_key_creation(psa_key_slot_t *slot,
1974 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001975{
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 (void) driver;
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001979 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001981
1982#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001983 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001984 * element, and the failure happened later (when saving metadata
1985 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001986 * element.
1987 * https://github.com/ARMmbed/mbed-crypto/issues/217
1988 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001989
Gilles Peskined7729582019-08-05 15:55:54 +02001990 /* Abort the ongoing transaction if any (there may not be one if
1991 * the creation process failed before starting one, or if the
1992 * key creation is a registration of a key in a secure element).
1993 * Earlier functions must already have done what it takes to undo any
1994 * partial creation. All that's left is to update the transaction data
1995 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001997#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002000}
2001
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002002/** Validate optional attributes during key creation.
2003 *
2004 * Some key attributes are optional during key creation. If they are
2005 * specified in the attributes structure, check that they are consistent
2006 * with the data in the slot.
2007 *
2008 * This function should be called near the end of key creation, after
2009 * the slot in memory is fully populated but before saving persistent data.
2010 */
2011static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014{
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (attributes->core.type != 0) {
2016 if (attributes->core.type != slot->attr.type) {
2017 return PSA_ERROR_INVALID_ARGUMENT;
2018 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019 }
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002022#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2023 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2025 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002026 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002027 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002029
Ronald Cron90857082020-11-25 14:58:33 +01002030 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 slot->attr.type,
2032 slot->key.data,
2033 slot->key.bytes,
2034 &rsa);
2035 if (status != PSA_SUCCESS) {
2036 return status;
2037 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 mbedtls_mpi_init(&actual);
2040 mbedtls_mpi_init(&required);
2041 ret = mbedtls_rsa_export(rsa,
2042 NULL, NULL, NULL, NULL, &actual);
2043 mbedtls_rsa_free(rsa);
2044 mbedtls_free(rsa);
2045 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002046 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 }
2048 ret = mbedtls_mpi_read_binary(&required,
2049 attributes->domain_parameters,
2050 attributes->domain_parameters_size);
2051 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002052 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
2054 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002055 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 }
2057rsa_exit:
2058 mbedtls_mpi_free(&actual);
2059 mbedtls_mpi_free(&required);
2060 if (ret != 0) {
2061 return mbedtls_to_psa_error(ret);
2062 }
2063 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002064#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2065 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002066 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002069 }
2070 }
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if (attributes->core.bits != 0) {
2073 if (attributes->core.bits != slot->attr.bits) {
2074 return PSA_ERROR_INVALID_ARGUMENT;
2075 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 }
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079}
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2082 const uint8_t *data,
2083 size_t data_length,
2084 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002085{
2086 psa_status_t status;
2087 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002088 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002089 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302090 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002091
Ronald Cron81709fc2020-11-14 12:10:32 +01002092 *key = MBEDTLS_SVC_KEY_ID_INIT;
2093
Gilles Peskine0f84d622019-09-12 19:03:13 +02002094 /* Reject zero-length symmetric keys (including raw data key objects).
2095 * This also rejects any key which might be encoded as an empty string,
2096 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (data_length == 0) {
2098 return PSA_ERROR_INVALID_ARGUMENT;
2099 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002100
Archana449608b2021-09-08 15:36:05 +05302101 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (data_length > SIZE_MAX / 8) {
2103 return PSA_ERROR_NOT_SUPPORTED;
2104 }
Archana6ed4bda2021-08-04 10:47:15 +05302105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2107 &slot, &driver);
2108 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002112 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302113 * storage ( thus not in the case of importing a key in a secure element
2114 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2115 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (slot->key.data == NULL) {
2117 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302118 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 attributes, data, data_length, &storage_size);
2120 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 }
Archanad8a83dc2021-06-14 10:04:16 +05302123 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 status = psa_allocate_buffer_to_slot(slot, storage_size);
2125 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002128 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002129
2130 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 status = psa_driver_wrapper_import_key(attributes,
2132 data, data_length,
2133 slot->key.data,
2134 slot->key.bytes,
2135 &slot->key.bytes, &bits);
2136 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002141 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002143 status = PSA_ERROR_INVALID_ARGUMENT;
2144 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002145 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002146
Archana6ed4bda2021-08-04 10:47:15 +05302147 /* Enforce a size limit, and in particular ensure that the bit
2148 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302150 status = PSA_ERROR_NOT_SUPPORTED;
2151 goto exit;
2152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 status = psa_validate_optional_attributes(slot, attributes);
2154 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (status != PSA_SUCCESS) {
2161 psa_fail_key_creation(slot, driver);
2162 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002165}
2166
Gilles Peskined7729582019-08-05 15:55:54 +02002167#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2168psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002170{
2171 psa_status_t status;
2172 psa_key_slot_t *slot = NULL;
2173 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002175
2176 /* Leaving attributes unspecified is not currently supported.
2177 * It could make sense to query the key type and size from the
2178 * secure element, but not all secure elements support this
2179 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2181 return PSA_ERROR_NOT_SUPPORTED;
2182 }
2183 if (psa_get_key_bits(attributes) == 0) {
2184 return PSA_ERROR_NOT_SUPPORTED;
2185 }
Gilles Peskined7729582019-08-05 15:55:54 +02002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2188 &slot, &driver);
2189 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002190 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 }
Gilles Peskined7729582019-08-05 15:55:54 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002194
2195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (status != PSA_SUCCESS) {
2197 psa_fail_key_creation(slot, driver);
2198 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002199
Gilles Peskined7729582019-08-05 15:55:54 +02002200 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 psa_close_key(key);
2202 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002203}
2204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2207 const psa_key_attributes_t *specified_attributes,
2208 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002209{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002211 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002212 psa_key_slot_t *source_slot = NULL;
2213 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002214 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002215 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302216 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002217
Ronald Cron81709fc2020-11-14 12:10:32 +01002218 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2219
Archana8a180362021-07-05 02:18:48 +05302220 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2222 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 status = psa_validate_optional_attributes(source_slot,
2227 specified_attributes);
2228 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002231
Archana9d17bf42021-09-10 06:22:44 +05302232 /* The target key type and number of bits have been validated by
2233 * psa_validate_optional_attributes() to be either equal to zero or
2234 * equal to the ones of the source key. So it is safe to inherit
2235 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302236 * */
Archana9d17bf42021-09-10 06:22:44 +05302237 actual_attributes.core.bits = source_slot->attr.bits;
2238 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302239
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 status = psa_restrict_key_policy(source_slot->attr.type,
2242 &actual_attributes.core.policy,
2243 &source_slot->attr.policy);
2244 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002245 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2249 &target_slot, &driver);
2250 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 }
2253 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2254 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302255 /*
Archana9d17bf42021-09-10 06:22:44 +05302256 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302257 * the source key would need to be exported as plaintext and re-imported
2258 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302259 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302260 * appropriate API invocations from the application, if needed.
2261 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002262 status = PSA_ERROR_NOT_SUPPORTED;
2263 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002264 }
Archana8a180362021-07-05 02:18:48 +05302265 /*
2266 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302267 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302268 * - For opaque keys this translates to an invocation of the drivers'
2269 * copy_key entry point through the dispatch layer.
2270 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2272 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2273 &storage_size);
2274 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302275 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 }
Archana374fe5b2021-09-08 15:50:28 +05302277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2279 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 }
Archana374fe5b2021-09-08 15:50:28 +05302282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_driver_wrapper_copy_key(&actual_attributes,
2284 source_slot->key.data,
2285 source_slot->key.bytes,
2286 target_slot->key.data,
2287 target_slot->key.bytes,
2288 &target_slot->key.bytes);
2289 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
2292 } else {
2293 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302294 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 source_slot->key.bytes);
2296 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302297 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 }
Archana8a180362021-07-05 02:18:48 +05302299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (status != PSA_SUCCESS) {
2303 psa_fail_key_creation(target_slot, driver);
2304 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002309}
2310
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002311
2312
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002313/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002314/* Message digests */
2315/****************************************************************/
2316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002319 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (operation->id == 0) {
2321 return PSA_SUCCESS;
2322 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325 operation->id = 0;
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328}
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2331 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002334
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002335 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002337 status = PSA_ERROR_BAD_STATE;
2338 goto exit;
2339 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002342 status = PSA_ERROR_INVALID_ARGUMENT;
2343 goto exit;
2344 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002345
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002346 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2347 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002351
2352exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (status != PSA_SUCCESS) {
2354 psa_hash_abort(operation);
2355 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002356
2357 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358}
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2361 const uint8_t *input,
2362 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367 status = PSA_ERROR_BAD_STATE;
2368 goto exit;
2369 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002370
Steven Cooremanc8288352021-03-04 14:02:19 +01002371 /* Don't require hash implementations to behave correctly on a
2372 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (input_length == 0) {
2374 return PSA_SUCCESS;
2375 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002378
2379exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (status != PSA_SUCCESS) {
2381 psa_hash_abort(operation);
2382 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385}
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2388 uint8_t *hash,
2389 size_t hash_size,
2390 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002392 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (operation->id == 0) {
2394 return PSA_ERROR_BAD_STATE;
2395 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396
Steven Cooreman1e582352021-02-18 17:24:37 +01002397 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 operation, hash, hash_size, hash_length);
2399 psa_hash_abort(operation);
2400 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401}
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2404 const uint8_t *hash,
2405 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406{
Ronald Cron69a63422021-10-18 09:47:58 +02002407 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002409 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 operation,
2411 actual_hash, sizeof(actual_hash),
2412 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419 status = PSA_ERROR_INVALID_SIGNATURE;
2420 goto exit;
2421 }
2422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002424 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002426
2427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2429 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002430 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002434}
2435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436psa_status_t psa_hash_compute(psa_algorithm_t alg,
2437 const uint8_t *input, size_t input_length,
2438 uint8_t *hash, size_t hash_size,
2439 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002440{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002441 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (!PSA_ALG_IS_HASH(alg)) {
2443 return PSA_ERROR_INVALID_ARGUMENT;
2444 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2447 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002448}
2449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450psa_status_t psa_hash_compare(psa_algorithm_t alg,
2451 const uint8_t *input, size_t input_length,
2452 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002453{
Ronald Cron69a63422021-10-18 09:47:58 +02002454 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002455 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (!PSA_ALG_IS_HASH(alg)) {
2458 return PSA_ERROR_INVALID_ARGUMENT;
2459 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002460
Steven Cooreman1e582352021-02-18 17:24:37 +01002461 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 alg, input, input_length,
2463 actual_hash, sizeof(actual_hash),
2464 &actual_hash_length);
2465 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 }
2468 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002469 status = PSA_ERROR_INVALID_SIGNATURE;
2470 goto exit;
2471 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002473 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002475
2476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2478 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002479}
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2482 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002483{
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (source_operation->id == 0 ||
2485 target_operation->id != 0) {
2486 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002487 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2490 target_operation);
2491 if (status != PSA_SUCCESS) {
2492 psa_hash_abort(target_operation);
2493 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002496}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497
2498
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500/* MAC */
2501/****************************************************************/
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002504{
Steven Cooremane6804192021-03-19 18:28:56 +01002505 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (operation->id == 0) {
2507 return PSA_SUCCESS;
2508 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002511 operation->mac_size = 0;
2512 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002513 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002516}
2517
Ronald Cron2dff3b22021-06-17 16:33:22 +02002518static psa_status_t psa_mac_finalize_alg_and_key_validation(
2519 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002520 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 psa_key_type_t key_type = psa_get_key_type(attributes);
2525 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (!PSA_ALG_IS_MAC(alg)) {
2528 return PSA_ERROR_INVALID_ARGUMENT;
2529 }
Steven Cooremane6804192021-03-19 18:28:56 +01002530
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002531 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 status = psa_mac_key_can_do(alg, key_type);
2533 if (status != PSA_SUCCESS) {
2534 return status;
2535 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002536
Steven Cooremand1a68f12021-05-10 11:13:41 +02002537 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002541 /* A very short MAC is too short for security since it can be
2542 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2543 * so we make this our minimum, even though 32 bits is still
2544 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002546 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2549 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002550 /* It's impossible to "truncate" to a larger length than the full length
2551 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002553 }
2554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002556 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2557 * that is disabled in the compile-time configuration. The result can
2558 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2559 * configuration into account. In this case, force a return of
2560 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2561 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2562 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2563 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2564 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002566 }
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002569}
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2572 mbedtls_svc_key_id_t key,
2573 psa_algorithm_t alg,
2574 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002575{
2576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2577 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002578 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002579 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580
2581 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002583 status = PSA_ERROR_BAD_STATE;
2584 goto exit;
2585 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586
2587 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 key,
2589 &slot,
2590 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2591 alg);
2592 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002596 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597 .core = slot->attr
2598 };
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2601 &operation->mac_size);
2602 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605
2606 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002607 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (is_sign) {
2609 status = psa_driver_wrapper_mac_sign_setup(operation,
2610 &attributes,
2611 slot->key.data,
2612 slot->key.bytes,
2613 alg);
2614 } else {
2615 status = psa_driver_wrapper_mac_verify_setup(operation,
2616 &attributes,
2617 slot->key.data,
2618 slot->key.bytes,
2619 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002621
Gilles Peskinefbfac682018-07-08 20:51:54 +02002622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (status != PSA_SUCCESS) {
2624 psa_mac_abort(operation);
2625 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002630}
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2633 mbedtls_svc_key_id_t key,
2634 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002635{
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637}
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2640 mbedtls_svc_key_id_t key,
2641 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002642{
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002644}
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2647 const uint8_t *input,
2648 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649{
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (operation->id == 0) {
2651 return PSA_ERROR_BAD_STATE;
2652 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002654 /* Don't require hash implementations to behave correctly on a
2655 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (input_length == 0) {
2657 return PSA_SUCCESS;
2658 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2661 input, input_length);
2662 if (status != PSA_SUCCESS) {
2663 psa_mac_abort(operation);
2664 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667}
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2670 uint8_t *mac,
2671 size_t mac_size,
2672 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002673{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2675 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002678 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002679 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002680 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002684 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002685 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002686
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002687 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2688 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002690 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002691 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002692 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002695 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002696 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002697 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 status = psa_driver_wrapper_mac_sign_finish(operation,
2700 mac, operation->mac_size,
2701 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002702
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002703exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002704 /* In case of success, set the potential excess room in the output buffer
2705 * to an invalid value, to avoid potentially leaking a longer MAC.
2706 * In case of error, set the output length and content to a safe default,
2707 * such that in case the caller misses an error check, the output would be
2708 * an unachievable MAC.
2709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002711 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002712 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002713 }
mohammad16036df908f2018-04-02 08:34:15 -07002714
Paul Elliottdc42ca82023-02-24 18:11:59 +00002715 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002720}
2721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2723 const uint8_t *mac,
2724 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002726 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2727 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002730 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002731 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002732 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002735 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002736 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002737 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002740 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002741 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002742 }
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 status = psa_driver_wrapper_mac_verify_finish(operation,
2745 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002746
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751}
2752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2754 psa_algorithm_t alg,
2755 const uint8_t *input,
2756 size_t input_length,
2757 uint8_t *mac,
2758 size_t mac_size,
2759 size_t *mac_length,
2760 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002761{
2762 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002763 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2764 psa_key_slot_t *slot;
2765 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002766 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767
Ronald Cron51131b52021-06-17 17:17:20 +02002768 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 key,
2770 &slot,
2771 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2772 alg);
2773 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002777 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002778 .core = slot->attr
2779 };
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2782 &operation_mac_size);
2783 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002788 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002790 }
2791
2792 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 &attributes,
2794 slot->key.data, slot->key.bytes,
2795 alg,
2796 input, input_length,
2797 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002798
2799exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002800 /* In case of success, set the potential excess room in the output buffer
2801 * to an invalid value, to avoid potentially leaking a longer MAC.
2802 * In case of error, set the output length and content to a safe default,
2803 * such that in case the caller misses an error check, the output would be
2804 * an unachievable MAC.
2805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002807 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002808 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002809 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002810
2811 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002816}
2817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002819 psa_algorithm_t alg,
2820 const uint8_t *input,
2821 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 uint8_t *mac,
2823 size_t mac_size,
2824 size_t *mac_length)
2825{
2826 return psa_mac_compute_internal(key, alg,
2827 input, input_length,
2828 mac, mac_size, mac_length, 1);
2829}
2830
2831psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2832 psa_algorithm_t alg,
2833 const uint8_t *input,
2834 size_t input_length,
2835 const uint8_t *mac,
2836 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002837{
2838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002839 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2840 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 status = psa_mac_compute_internal(key, alg,
2843 input, input_length,
2844 actual_mac, sizeof(actual_mac),
2845 &actual_mac_length, 0);
2846 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002851 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002855 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002856 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002857 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858
2859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002863}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002864
Gilles Peskinee59236f2018-01-27 23:32:46 +01002865/****************************************************************/
2866/* Asymmetric cryptography */
2867/****************************************************************/
2868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2870 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002871{
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (input_is_message) {
2873 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2874 return PSA_ERROR_INVALID_ARGUMENT;
2875 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2878 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2879 return PSA_ERROR_INVALID_ARGUMENT;
2880 }
2881 }
2882 } else {
2883 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2884 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002885 }
2886 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002889}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2892 int input_is_message,
2893 psa_algorithm_t alg,
2894 const uint8_t *input,
2895 size_t input_length,
2896 uint8_t *signature,
2897 size_t signature_size,
2898 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002899{
2900 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2901 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2902 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002903 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002904
2905 *signature_length = 0;
2906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 status = psa_sign_verify_check_alg(input_is_message, alg);
2908 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002909 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002911
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002912 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002913 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914 * buffer can in principle be empty since it doesn't actually have
2915 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (signature_size == 0) {
2917 return PSA_ERROR_BUFFER_TOO_SMALL;
2918 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002919
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002920 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 key, &slot,
2922 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2923 PSA_KEY_USAGE_SIGN_HASH,
2924 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002927 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931 status = PSA_ERROR_INVALID_ARGUMENT;
2932 goto exit;
2933 }
2934
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002935 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002937 };
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002940 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002941 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 signature, signature_size, signature_length);
2944 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002945
2946 status = psa_driver_wrapper_sign_hash(
2947 &attributes, slot->key.data, slot->key.bytes,
2948 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002950 }
2951
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002952
2953exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002954 psa_wipe_tag_output_buffer(signature, status, signature_size,
2955 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002960}
2961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2963 int input_is_message,
2964 psa_algorithm_t alg,
2965 const uint8_t *input,
2966 size_t input_length,
2967 const uint8_t *signature,
2968 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002969{
2970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2971 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2972 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 status = psa_sign_verify_check_alg(input_is_message, alg);
2975 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002976 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002978
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002979 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 key, &slot,
2981 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2982 PSA_KEY_USAGE_VERIFY_HASH,
2983 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 if (status != PSA_SUCCESS) {
2986 return status;
2987 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002988
2989 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002991 };
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002994 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002995 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 signature, signature_length);
2998 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002999 status = psa_driver_wrapper_verify_hash(
3000 &attributes, slot->key.data, slot->key.bytes,
3001 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003003 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003008
3009}
3010
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003011psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003012 const psa_key_attributes_t *attributes,
3013 const uint8_t *key_buffer,
3014 size_t key_buffer_size,
3015 psa_algorithm_t alg,
3016 const uint8_t *input,
3017 size_t input_length,
3018 uint8_t *signature,
3019 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003021{
3022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003025 size_t hash_length;
3026 uint8_t hash[PSA_HASH_MAX_SIZE];
3027
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003028 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 PSA_ALG_SIGN_GET_HASH(alg),
3030 input, input_length,
3031 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003034 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003036
3037 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 attributes, key_buffer, key_buffer_size,
3039 alg, hash, hash_length,
3040 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003041 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003044}
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3047 psa_algorithm_t alg,
3048 const uint8_t *input,
3049 size_t input_length,
3050 uint8_t *signature,
3051 size_t signature_size,
3052 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003053{
3054 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003055 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003057}
3058
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003059psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003060 const psa_key_attributes_t *attributes,
3061 const uint8_t *key_buffer,
3062 size_t key_buffer_size,
3063 psa_algorithm_t alg,
3064 const uint8_t *input,
3065 size_t input_length,
3066 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003068{
3069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003072 size_t hash_length;
3073 uint8_t hash[PSA_HASH_MAX_SIZE];
3074
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003075 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 PSA_ALG_SIGN_GET_HASH(alg),
3077 input, input_length,
3078 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003081 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003083
3084 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 attributes, key_buffer, key_buffer_size,
3086 alg, hash, hash_length,
3087 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003088 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003091}
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3094 psa_algorithm_t alg,
3095 const uint8_t *input,
3096 size_t input_length,
3097 const uint8_t *signature,
3098 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003099{
3100 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003101 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003103}
3104
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003105psa_status_t psa_sign_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 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003110{
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
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_sign_hash(
3117 attributes,
3118 key_buffer, key_buffer_size,
3119 alg, hash, hash_length,
3120 signature, signature_size, 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_sign_hash(
3131 attributes,
3132 key_buffer, key_buffer_size,
3133 alg, hash, hash_length,
3134 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +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_size;
3148 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003151}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3154 psa_algorithm_t alg,
3155 const uint8_t *hash,
3156 size_t hash_length,
3157 uint8_t *signature,
3158 size_t signature_size,
3159 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003160{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003161 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003162 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003164}
3165
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003166psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003167 const psa_key_attributes_t *attributes,
3168 const uint8_t *key_buffer, size_t key_buffer_size,
3169 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003171{
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3173 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3174 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003175#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3177 return mbedtls_psa_rsa_verify_hash(
3178 attributes,
3179 key_buffer, key_buffer_size,
3180 alg, hash, hash_length,
3181 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003182#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3183 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 } else {
3185 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003186 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3188 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003189#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3191 return mbedtls_psa_ecdsa_verify_hash(
3192 attributes,
3193 key_buffer, key_buffer_size,
3194 alg, hash, hash_length,
3195 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003196#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3197 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 } else {
3199 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003200 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003201 }
Ronald Cron4501c982021-03-19 20:09:32 +01003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 (void) key_buffer;
3204 (void) key_buffer_size;
3205 (void) hash;
3206 (void) hash_length;
3207 (void) signature;
3208 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003211}
3212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3214 psa_algorithm_t alg,
3215 const uint8_t *hash,
3216 size_t hash_length,
3217 const uint8_t *signature,
3218 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003219{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003220 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003221 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003223}
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3226 psa_algorithm_t alg,
3227 const uint8_t *input,
3228 size_t input_length,
3229 const uint8_t *salt,
3230 size_t salt_length,
3231 uint8_t *output,
3232 size_t output_size,
3233 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003234{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003236 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003237 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003238 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003239
Darryl Green5cc689a2018-07-24 15:34:10 +01003240 (void) input;
3241 (void) input_length;
3242 (void) salt;
3243 (void) output;
3244 (void) output_size;
3245
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003246 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3249 return PSA_ERROR_INVALID_ARGUMENT;
3250 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003251
Ronald Cron5c522922020-11-14 16:35:34 +01003252 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3254 if (status != PSA_SUCCESS) {
3255 return status;
3256 }
3257 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3258 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003259 status = PSA_ERROR_INVALID_ARGUMENT;
3260 goto exit;
3261 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003263 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003265 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003266
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003267 status = psa_driver_wrapper_asymmetric_encrypt(
3268 &attributes, slot->key.data, slot->key.bytes,
3269 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003275}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3278 psa_algorithm_t alg,
3279 const uint8_t *input,
3280 size_t input_length,
3281 const uint8_t *salt,
3282 size_t salt_length,
3283 uint8_t *output,
3284 size_t output_size,
3285 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003288 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003289 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003290 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003291
Darryl Green5cc689a2018-07-24 15:34:10 +01003292 (void) input;
3293 (void) input_length;
3294 (void) salt;
3295 (void) output;
3296 (void) output_size;
3297
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003298 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3301 return PSA_ERROR_INVALID_ARGUMENT;
3302 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003303
Ronald Cron5c522922020-11-14 16:35:34 +01003304 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3306 if (status != PSA_SUCCESS) {
3307 return status;
3308 }
3309 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003310 status = PSA_ERROR_INVALID_ARGUMENT;
3311 goto exit;
3312 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003314 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003316 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003317
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003318 status = psa_driver_wrapper_asymmetric_decrypt(
3319 &attributes, slot->key.data, slot->key.bytes,
3320 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003322
3323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003325
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003327}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003328
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329/****************************************************************/
3330/* Asymmetric interruptible cryptography */
3331/****************************************************************/
3332
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003333static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3334
Paul Elliott9fe12f62022-11-30 19:16:02 +00003335void psa_interruptible_set_max_ops(uint32_t max_ops)
3336{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003337 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338}
3339
3340uint32_t psa_interruptible_get_max_ops(void)
3341{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003342 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003343}
3344
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345uint32_t psa_sign_hash_get_num_ops(
3346 const psa_sign_hash_interruptible_operation_t *operation)
3347{
Paul Elliott296ede92022-12-15 17:00:30 +00003348 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003349}
3350
3351uint32_t psa_verify_hash_get_num_ops(
3352 const psa_verify_hash_interruptible_operation_t *operation)
3353{
Paul Elliott296ede92022-12-15 17:00:30 +00003354 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003355}
3356
Paul Elliott59ad9452022-12-18 15:09:02 +00003357static psa_status_t psa_sign_hash_abort_internal(
3358 psa_sign_hash_interruptible_operation_t *operation)
3359{
3360 if (operation->id == 0) {
3361 /* The object has (apparently) been initialized but it is not (yet)
3362 * in use. It's ok to call abort on such an object, and there's
3363 * nothing to do. */
3364 return PSA_SUCCESS;
3365 }
3366
3367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3368
3369 status = psa_driver_wrapper_sign_hash_abort(operation);
3370
3371 operation->id = 0;
3372
Paul Elliotte6145dc2023-02-07 12:51:21 +00003373 /* Do not clear either the error_occurred or num_ops elements here as they
3374 * only want to be cleared by the application calling abort, not by abort
3375 * being called at completion of an operation. */
3376
Paul Elliott59ad9452022-12-18 15:09:02 +00003377 return status;
3378}
3379
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380psa_status_t psa_sign_hash_start(
3381 psa_sign_hash_interruptible_operation_t *operation,
3382 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3383 const uint8_t *hash, size_t hash_length)
3384{
3385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3386 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3387 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003388 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003389
Paul Elliottc9774412023-02-06 15:14:07 +00003390 /* Check that start has not been previously called, or operation has not
3391 * previously errored. */
3392 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003393 return PSA_ERROR_BAD_STATE;
3394 }
3395
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396 status = psa_sign_verify_check_alg(0, alg);
3397 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003398 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 return status;
3400 }
3401
3402 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3403 PSA_KEY_USAGE_SIGN_HASH,
3404 alg);
3405
3406 if (status != PSA_SUCCESS) {
3407 goto exit;
3408 }
3409
3410 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3411 status = PSA_ERROR_INVALID_ARGUMENT;
3412 goto exit;
3413 }
3414
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003415 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003416 .core = slot->attr
3417 };
3418
Paul Elliott296ede92022-12-15 17:00:30 +00003419 /* Ensure ops count gets reset, in case of operation re-use. */
3420 operation->num_ops = 0;
3421
Paul Elliott9fe12f62022-11-30 19:16:02 +00003422 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3423 slot->key.data,
3424 slot->key.bytes, alg,
3425 hash, hash_length);
3426exit:
3427
3428 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003429 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003430 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003431 }
3432
3433 unlock_status = psa_unlock_key_slot(slot);
3434
Paul Elliottc9774412023-02-06 15:14:07 +00003435 if (unlock_status != PSA_SUCCESS) {
3436 operation->error_occurred = 1;
3437 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003438
Paul Elliottc9774412023-02-06 15:14:07 +00003439 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003440}
3441
3442
3443psa_status_t psa_sign_hash_complete(
3444 psa_sign_hash_interruptible_operation_t *operation,
3445 uint8_t *signature, size_t signature_size,
3446 size_t *signature_length)
3447{
3448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3449
3450 *signature_length = 0;
3451
Paul Elliottc9774412023-02-06 15:14:07 +00003452 /* Check that start has been called first, and that operation has not
3453 * previously errored. */
3454 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003455 status = PSA_ERROR_BAD_STATE;
3456 goto exit;
3457 }
3458
Paul Elliott096abc42023-02-03 18:33:23 +00003459 /* Immediately reject a zero-length signature buffer. This guarantees that
3460 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003461 if (signature_size == 0) {
3462 status = PSA_ERROR_BUFFER_TOO_SMALL;
3463 goto exit;
3464 }
3465
3466 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3467 signature_size,
3468 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003469
Paul Elliott296ede92022-12-15 17:00:30 +00003470 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003471 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003472
Paul Elliottebe225c2023-02-10 14:32:53 +00003473exit:
3474
Paul Elliott59200a22023-02-21 15:07:40 +00003475 psa_wipe_tag_output_buffer(signature, status, signature_size,
3476 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003477
Paul Elliott53bb3122023-02-10 14:22:22 +00003478 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003479 if (status != PSA_SUCCESS) {
3480 operation->error_occurred = 1;
3481 }
3482
Paul Elliott59ad9452022-12-18 15:09:02 +00003483 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003484 }
3485
3486 return status;
3487}
3488
3489psa_status_t psa_sign_hash_abort(
3490 psa_sign_hash_interruptible_operation_t *operation)
3491{
Paul Elliott59ad9452022-12-18 15:09:02 +00003492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3493
3494 status = psa_sign_hash_abort_internal(operation);
3495
3496 /* We clear the number of ops done here, so that it is not cleared when
3497 * the operation fails or succeeds, only on manual abort. */
3498 operation->num_ops = 0;
3499
Paul Elliottc9774412023-02-06 15:14:07 +00003500 /* Likewise, failure state. */
3501 operation->error_occurred = 0;
3502
Paul Elliott59ad9452022-12-18 15:09:02 +00003503 return status;
3504}
3505
3506static psa_status_t psa_verify_hash_abort_internal(
3507 psa_verify_hash_interruptible_operation_t *operation)
3508{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003509 if (operation->id == 0) {
3510 /* The object has (apparently) been initialized but it is not (yet)
3511 * in use. It's ok to call abort on such an object, and there's
3512 * nothing to do. */
3513 return PSA_SUCCESS;
3514 }
3515
Paul Elliott59ad9452022-12-18 15:09:02 +00003516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3517
3518 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003519
3520 operation->id = 0;
3521
Paul Elliotte6145dc2023-02-07 12:51:21 +00003522 /* Do not clear either the error_occurred or num_ops elements here as they
3523 * only want to be cleared by the application calling abort, not by abort
3524 * being called at completion of an operation. */
3525
Paul Elliott59ad9452022-12-18 15:09:02 +00003526 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003527}
3528
3529psa_status_t psa_verify_hash_start(
3530 psa_verify_hash_interruptible_operation_t *operation,
3531 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3532 const uint8_t *hash, size_t hash_length,
3533 const uint8_t *signature, size_t signature_length)
3534{
3535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3536 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3537 psa_key_slot_t *slot;
3538
Paul Elliottc9774412023-02-06 15:14:07 +00003539 /* Check that start has not been previously called, or operation has not
3540 * previously errored. */
3541 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003542 return PSA_ERROR_BAD_STATE;
3543 }
3544
3545 status = psa_sign_verify_check_alg(0, alg);
3546 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003547 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 return status;
3549 }
3550
3551 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3552 PSA_KEY_USAGE_VERIFY_HASH,
3553 alg);
3554
3555 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003556 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003557 return status;
3558 }
3559
3560 psa_key_attributes_t attributes = {
3561 .core = slot->attr
3562 };
3563
Paul Elliott296ede92022-12-15 17:00:30 +00003564 /* Ensure ops count gets reset, in case of operation re-use. */
3565 operation->num_ops = 0;
3566
Paul Elliott9fe12f62022-11-30 19:16:02 +00003567 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3568 slot->key.data,
3569 slot->key.bytes,
3570 alg, hash, hash_length,
3571 signature, signature_length);
3572
3573 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003574 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003575 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003576 }
3577
3578 unlock_status = psa_unlock_key_slot(slot);
3579
Paul Elliottc9774412023-02-06 15:14:07 +00003580 if (unlock_status != PSA_SUCCESS) {
3581 operation->error_occurred = 1;
3582 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003583
Paul Elliottc9774412023-02-06 15:14:07 +00003584 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585}
3586
3587psa_status_t psa_verify_hash_complete(
3588 psa_verify_hash_interruptible_operation_t *operation)
3589{
3590 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3591
Paul Elliottc9774412023-02-06 15:14:07 +00003592 /* Check that start has been called first, and that operation has not
3593 * previously errored. */
3594 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003595 status = PSA_ERROR_BAD_STATE;
3596 goto exit;
3597 }
3598
3599 status = psa_driver_wrapper_verify_hash_complete(operation);
3600
Paul Elliott296ede92022-12-15 17:00:30 +00003601 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003602 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003603 operation);
3604
Paul Elliottebe225c2023-02-10 14:32:53 +00003605exit:
3606
Paul Elliott9fe12f62022-11-30 19:16:02 +00003607 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003608 if (status != PSA_SUCCESS) {
3609 operation->error_occurred = 1;
3610 }
3611
Paul Elliott59ad9452022-12-18 15:09:02 +00003612 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003613 }
3614
3615 return status;
3616}
3617
3618psa_status_t psa_verify_hash_abort(
3619 psa_verify_hash_interruptible_operation_t *operation)
3620{
Paul Elliott59ad9452022-12-18 15:09:02 +00003621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622
Paul Elliott59ad9452022-12-18 15:09:02 +00003623 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624
Paul Elliott59ad9452022-12-18 15:09:02 +00003625 /* We clear the number of ops done here, so that it is not cleared when
3626 * the operation fails or succeeds, only on manual abort. */
3627 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003628
Paul Elliottc9774412023-02-06 15:14:07 +00003629 /* Likewise, failure state. */
3630 operation->error_occurred = 0;
3631
Paul Elliott59ad9452022-12-18 15:09:02 +00003632 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003633}
3634
Paul Elliott588f8ed2022-12-02 18:10:26 +00003635/****************************************************************/
3636/* Asymmetric interruptible cryptography internal */
3637/* implementations */
3638/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003639
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3641{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003642
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3644 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3645 defined(MBEDTLS_ECP_RESTARTABLE)
3646
3647 /* Internal implementation uses zero to indicate infinite number max ops,
3648 * therefore avoid this value, and set to minimum possible. */
3649 if (max_ops == 0) {
3650 max_ops = 1;
3651 }
3652
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003654#else
3655 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3657 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3658 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3659}
3660
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003662 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663{
3664#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3665 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3666 defined(MBEDTLS_ECP_RESTARTABLE)
3667
Paul Elliott93d9ca82023-02-15 18:14:21 +00003668 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003669#else
3670 (void) operation;
3671 return 0;
3672#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3673 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3674 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3675}
3676
3677uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003678 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003679{
3680 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3681 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3682 defined(MBEDTLS_ECP_RESTARTABLE)
3683
Paul Elliott93d9ca82023-02-15 18:14:21 +00003684 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003685#else
3686 (void) operation;
3687 return 0;
3688#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3689 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3690 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3691}
3692
3693psa_status_t mbedtls_psa_sign_hash_start(
3694 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3695 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3696 size_t key_buffer_size, psa_algorithm_t alg,
3697 const uint8_t *hash, size_t hash_length)
3698{
3699 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003700 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003701
Paul Elliott068fe072023-01-16 13:59:15 +00003702 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3703 return PSA_ERROR_NOT_SUPPORTED;
3704 }
3705
3706 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003707 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003708 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003709
3710#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003711 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3712 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003713
Paul Elliotta1c94092023-02-15 16:38:04 +00003714 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3715
Paul Elliott93d9ca82023-02-15 18:14:21 +00003716 /* Ensure num_ops is zero'ed in case of context re-use. */
3717 operation->num_ops = 0;
3718
Paul Elliott068fe072023-01-16 13:59:15 +00003719 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3720 attributes->core.bits,
3721 key_buffer,
3722 key_buffer_size,
3723 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003724
Paul Elliott068fe072023-01-16 13:59:15 +00003725 if (status != PSA_SUCCESS) {
3726 return status;
3727 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003728
Paul Elliott1bc59df2023-02-05 13:41:57 +00003729 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003730 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott068fe072023-01-16 13:59:15 +00003732 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003733 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003734 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003735
Paul Elliott0290a762023-02-09 14:30:24 +00003736 /* We only need to store the same length of hash as the private key size
3737 * here, it would be truncated by the internal implementation anyway. */
3738 required_hash_length = (hash_length < operation->coordinate_bytes ?
3739 hash_length : operation->coordinate_bytes);
3740
Paul Elliottba70ad42023-02-15 18:23:53 +00003741 if (required_hash_length > sizeof(operation->hash)) {
3742 /* Shouldn't happen, but better safe than sorry. */
3743 return PSA_ERROR_CORRUPTION_DETECTED;
3744 }
3745
Paul Elliott0290a762023-02-09 14:30:24 +00003746 memcpy(operation->hash, hash, required_hash_length);
3747 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003748
3749 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750
3751#else
Paul Elliott068fe072023-01-16 13:59:15 +00003752 (void) operation;
3753 (void) key_buffer;
3754 (void) key_buffer_size;
3755 (void) alg;
3756 (void) hash;
3757 (void) hash_length;
3758 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003759 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003760
Paul Elliott068fe072023-01-16 13:59:15 +00003761 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3763 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3764 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765}
3766
3767psa_status_t mbedtls_psa_sign_hash_complete(
3768 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3769 uint8_t *signature, size_t signature_size,
3770 size_t *signature_length)
3771{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003772#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3773 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3774 defined(MBEDTLS_ECP_RESTARTABLE)
3775
Paul Elliotta1c94092023-02-15 16:38:04 +00003776 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003777 mbedtls_mpi r;
3778 mbedtls_mpi s;
3779
Paul Elliott46845252023-02-03 14:59:11 +00003780 mbedtls_mpi_init(&r);
3781 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003782
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003783 /* Ensure max_ops is set to the current value (or default). */
3784 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3785
Paul Elliotta1c94092023-02-15 16:38:04 +00003786 if (signature_size < 2 * operation->coordinate_bytes) {
3787 status = PSA_ERROR_BUFFER_TOO_SMALL;
3788 goto exit;
3789 }
3790
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003792
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3794 status = mbedtls_to_psa_error(
3795 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003796 &r,
3797 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798 &operation->ctx->d,
3799 operation->hash,
3800 operation->hash_length,
3801 operation->md_alg,
3802 mbedtls_psa_get_random,
3803 MBEDTLS_PSA_RANDOM_STATE,
3804 &operation->restart_ctx));
3805#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003806 status = PSA_ERROR_NOT_SUPPORTED;
3807 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003808#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3809 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810 status = mbedtls_to_psa_error(
3811 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003812 &r,
3813 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003814 &operation->ctx->d,
3815 operation->hash,
3816 operation->hash_length,
3817 mbedtls_psa_get_random,
3818 MBEDTLS_PSA_RANDOM_STATE,
3819 mbedtls_psa_get_random,
3820 MBEDTLS_PSA_RANDOM_STATE,
3821 &operation->restart_ctx));
3822 }
3823
Paul Elliottf8e5b562023-02-19 18:43:45 +00003824 /* Hide the fact that the restart context only holds a delta of number of
3825 * ops done during the last operation, not an absolute value. */
3826 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3827
Paul Elliott724bd252023-02-08 12:35:08 +00003828 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003829 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003830 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003832 operation->coordinate_bytes)
3833 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834
3835 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003836 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837 }
3838
3839 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003840 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003841 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003842 operation->coordinate_bytes,
3843 operation->coordinate_bytes)
3844 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003845
3846 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003847 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848 }
3849
Paul Elliott1bc59df2023-02-05 13:41:57 +00003850 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851
Paul Elliott724bd252023-02-08 12:35:08 +00003852 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 }
Paul Elliott724bd252023-02-08 12:35:08 +00003854
3855exit:
3856
3857 mbedtls_mpi_free(&r);
3858 mbedtls_mpi_free(&s);
3859 return status;
3860
Paul Elliott588f8ed2022-12-02 18:10:26 +00003861 #else
3862
3863 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003864 (void) signature;
3865 (void) signature_size;
3866 (void) signature_length;
3867
3868 return PSA_ERROR_NOT_SUPPORTED;
3869
3870#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3871 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3872 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3873}
3874
3875psa_status_t mbedtls_psa_sign_hash_abort(
3876 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3877{
3878
3879#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3880 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3881 defined(MBEDTLS_ECP_RESTARTABLE)
3882
3883 if (operation->ctx) {
3884 mbedtls_ecdsa_free(operation->ctx);
3885 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003886 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003887 }
3888
3889 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3890
Paul Elliott93d9ca82023-02-15 18:14:21 +00003891 operation->num_ops = 0;
3892
Paul Elliott588f8ed2022-12-02 18:10:26 +00003893 return PSA_SUCCESS;
3894
3895#else
3896
3897 (void) operation;
3898
3899 return PSA_ERROR_NOT_SUPPORTED;
3900
3901#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3902 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3903 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3904}
3905
3906psa_status_t mbedtls_psa_verify_hash_start(
3907 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3908 const psa_key_attributes_t *attributes,
3909 const uint8_t *key_buffer, size_t key_buffer_size,
3910 psa_algorithm_t alg,
3911 const uint8_t *hash, size_t hash_length,
3912 const uint8_t *signature, size_t signature_length)
3913{
3914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003915 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003916 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003917
Paul Elliott068fe072023-01-16 13:59:15 +00003918 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3919 return PSA_ERROR_NOT_SUPPORTED;
3920 }
3921
3922 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003923 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003924 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003925
3926#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003927 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3928 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003929
Paul Elliotta1c94092023-02-15 16:38:04 +00003930 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3931 mbedtls_mpi_init(&operation->r);
3932 mbedtls_mpi_init(&operation->s);
3933
Paul Elliott93d9ca82023-02-15 18:14:21 +00003934 /* Ensure num_ops is zero'ed in case of context re-use. */
3935 operation->num_ops = 0;
3936
Paul Elliott068fe072023-01-16 13:59:15 +00003937 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3938 attributes->core.bits,
3939 key_buffer,
3940 key_buffer_size,
3941 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003942
Paul Elliott068fe072023-01-16 13:59:15 +00003943 if (status != PSA_SUCCESS) {
3944 return status;
3945 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946
Paul Elliottc569fc22023-02-10 13:02:54 +00003947 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
Paul Elliott1bc59df2023-02-05 13:41:57 +00003949 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003950 return PSA_ERROR_INVALID_SIGNATURE;
3951 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003952
Paul Elliott068fe072023-01-16 13:59:15 +00003953 status = mbedtls_to_psa_error(
3954 mbedtls_mpi_read_binary(&operation->r,
3955 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003956 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957
Paul Elliott068fe072023-01-16 13:59:15 +00003958 if (status != PSA_SUCCESS) {
3959 return status;
3960 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961
Paul Elliott068fe072023-01-16 13:59:15 +00003962 status = mbedtls_to_psa_error(
3963 mbedtls_mpi_read_binary(&operation->s,
3964 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003965 coordinate_bytes,
3966 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003967
Paul Elliott068fe072023-01-16 13:59:15 +00003968 if (status != PSA_SUCCESS) {
3969 return status;
3970 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003971
Paul Elliott2c9843f2023-02-15 17:32:42 +00003972 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003973
Paul Elliott2c9843f2023-02-15 17:32:42 +00003974 if (status != PSA_SUCCESS) {
3975 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003976 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003977
Paul Elliott0290a762023-02-09 14:30:24 +00003978 /* We only need to store the same length of hash as the private key size
3979 * here, it would be truncated by the internal implementation anyway. */
3980 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3981 coordinate_bytes);
3982
Paul Elliottba70ad42023-02-15 18:23:53 +00003983 if (required_hash_length > sizeof(operation->hash)) {
3984 /* Shouldn't happen, but better safe than sorry. */
3985 return PSA_ERROR_CORRUPTION_DETECTED;
3986 }
3987
Paul Elliott0290a762023-02-09 14:30:24 +00003988 memcpy(operation->hash, hash, required_hash_length);
3989 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003990
3991 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003992#else
Paul Elliott068fe072023-01-16 13:59:15 +00003993 (void) operation;
3994 (void) key_buffer;
3995 (void) key_buffer_size;
3996 (void) alg;
3997 (void) hash;
3998 (void) hash_length;
3999 (void) signature;
4000 (void) signature_length;
4001 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004002 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004003 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004004
Paul Elliott068fe072023-01-16 13:59:15 +00004005 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004006#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4007 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4008 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009}
4010
4011psa_status_t mbedtls_psa_verify_hash_complete(
4012 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4013{
4014
4015#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4016 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4017 defined(MBEDTLS_ECP_RESTARTABLE)
4018
Paul Elliottf8e5b562023-02-19 18:43:45 +00004019 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4020
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004021 /* Ensure max_ops is set to the current value (or default). */
4022 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4023
Paul Elliottf8e5b562023-02-19 18:43:45 +00004024 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004025 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4026 operation->hash,
4027 operation->hash_length,
4028 &operation->ctx->Q,
4029 &operation->r,
4030 &operation->s,
4031 &operation->restart_ctx));
4032
Paul Elliottf8e5b562023-02-19 18:43:45 +00004033 /* Hide the fact that the restart context only holds a delta of number of
4034 * ops done during the last operation, not an absolute value. */
4035 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4036
4037 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004038#else
4039 (void) operation;
4040
4041 return PSA_ERROR_NOT_SUPPORTED;
4042
4043#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4044 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4045 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4046}
4047
4048psa_status_t mbedtls_psa_verify_hash_abort(
4049 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4050{
4051
4052#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4053 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4054 defined(MBEDTLS_ECP_RESTARTABLE)
4055
4056 if (operation->ctx) {
4057 mbedtls_ecdsa_free(operation->ctx);
4058 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004059 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004060 }
4061
4062 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4063
Paul Elliott93d9ca82023-02-15 18:14:21 +00004064 operation->num_ops = 0;
4065
Paul Elliott588f8ed2022-12-02 18:10:26 +00004066 mbedtls_mpi_free(&operation->r);
4067 mbedtls_mpi_free(&operation->s);
4068
4069 return PSA_SUCCESS;
4070
4071#else
4072 (void) operation;
4073
4074 return PSA_ERROR_NOT_SUPPORTED;
4075
4076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4077 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4078 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4079}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004080
mohammad1603503973b2018-03-12 15:59:30 +02004081/****************************************************************/
4082/* Symmetric cryptography */
4083/****************************************************************/
4084
Gilles Peskine449bd832023-01-11 14:50:10 +01004085static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4086 mbedtls_svc_key_id_t key,
4087 psa_algorithm_t alg,
4088 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004089{
4090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4091 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004092 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4094 PSA_KEY_USAGE_ENCRYPT :
4095 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004096 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004097
4098 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004100 status = PSA_ERROR_BAD_STATE;
4101 goto exit;
4102 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004105 status = PSA_ERROR_INVALID_ARGUMENT;
4106 goto exit;
4107 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4110 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004113
Ronald Cron49fafa92021-03-10 08:34:23 +01004114 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004115 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004116 * so we only set it (in the driver wrapper) after resources have been
4117 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004118 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004122 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 }
4124 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004125
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004126 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004128 };
4129
Ronald Cronab99ac22020-10-01 16:38:28 +02004130 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 if (cipher_operation == MBEDTLS_ENCRYPT) {
4132 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4133 &attributes,
4134 slot->key.data,
4135 slot->key.bytes,
4136 alg);
4137 } else {
4138 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4139 &attributes,
4140 slot->key.data,
4141 slot->key.bytes,
4142 alg);
4143 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004144
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004145exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 if (status != PSA_SUCCESS) {
4147 psa_cipher_abort(operation);
4148 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004153}
4154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4156 mbedtls_svc_key_id_t key,
4157 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004158{
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004160}
4161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4163 mbedtls_svc_key_id_t key,
4164 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004165{
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004167}
4168
Gilles Peskine449bd832023-01-11 14:50:10 +01004169psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4170 uint8_t *iv,
4171 size_t iv_size,
4172 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004173{
Ronald Cron6d051732020-10-01 14:10:20 +02004174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004175 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4176 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004179 status = PSA_ERROR_BAD_STATE;
4180 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004181 }
4182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004184 status = PSA_ERROR_BAD_STATE;
4185 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004186 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004187
Ronald Cron2fb90522021-07-05 12:31:44 +02004188 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004190 status = PSA_ERROR_BUFFER_TOO_SMALL;
4191 goto exit;
4192 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004195 status = PSA_ERROR_GENERIC_ERROR;
4196 goto exit;
4197 }
4198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 status = psa_generate_random(local_iv, default_iv_length);
4200 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004201 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 }
Ronald Cron5618a392021-03-26 09:52:26 +01004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 status = psa_driver_wrapper_cipher_set_iv(operation,
4205 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004206
4207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (status == PSA_SUCCESS) {
4209 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004210 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004211 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004213 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004215 }
Ronald Cron6d051732020-10-01 14:10:20 +02004216
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004218}
4219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4221 const uint8_t *iv,
4222 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004223{
Ronald Cron6d051732020-10-01 14:10:20 +02004224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004227 status = PSA_ERROR_BAD_STATE;
4228 goto exit;
4229 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004232 status = PSA_ERROR_BAD_STATE;
4233 goto exit;
4234 }
Ronald Crona0d68172021-03-26 10:15:08 +01004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237 status = PSA_ERROR_INVALID_ARGUMENT;
4238 goto exit;
4239 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 status = psa_driver_wrapper_cipher_set_iv(operation,
4242 iv,
4243 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004244
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004247 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 } else {
4249 psa_cipher_abort(operation);
4250 }
4251 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004252}
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4255 const uint8_t *input,
4256 size_t input_length,
4257 uint8_t *output,
4258 size_t output_size,
4259 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004260{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004264 status = PSA_ERROR_BAD_STATE;
4265 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004266 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269 status = PSA_ERROR_BAD_STATE;
4270 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004271 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 status = psa_driver_wrapper_cipher_update(operation,
4274 input,
4275 input_length,
4276 output,
4277 output_size,
4278 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004279
4280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (status != PSA_SUCCESS) {
4282 psa_cipher_abort(operation);
4283 }
Ronald Cron6d051732020-10-01 14:10:20 +02004284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004286}
4287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4289 uint8_t *output,
4290 size_t output_size,
4291 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004292{
David Saadab4ecc272019-02-14 13:48:10 +02004293 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004296 status = PSA_ERROR_BAD_STATE;
4297 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004298 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301 status = PSA_ERROR_BAD_STATE;
4302 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004303 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 status = psa_driver_wrapper_cipher_finish(operation,
4306 output,
4307 output_size,
4308 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004309
4310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 if (status == PSA_SUCCESS) {
4312 return psa_cipher_abort(operation);
4313 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004314 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004316
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004318 }
mohammad1603503973b2018-03-12 15:59:30 +02004319}
4320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004322{
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004324 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004325 * in use. It's ok to call abort on such an object, and there's
4326 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004328 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004331
Ronald Cron49fafa92021-03-10 08:34:23 +01004332 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004333 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004334 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004337}
4338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4340 psa_algorithm_t alg,
4341 const uint8_t *input,
4342 size_t input_length,
4343 uint8_t *output,
4344 size_t output_size,
4345 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004346{
4347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004348 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004349 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004350 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4351 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004352 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004355 status = PSA_ERROR_INVALID_ARGUMENT;
4356 goto exit;
4357 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4360 PSA_KEY_USAGE_ENCRYPT,
4361 alg);
4362 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004363 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004365
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004366 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004368 };
4369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4371 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004372 status = PSA_ERROR_GENERIC_ERROR;
4373 goto exit;
4374 }
4375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (default_iv_length > 0) {
4377 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004378 status = PSA_ERROR_BUFFER_TOO_SMALL;
4379 goto exit;
4380 }
4381
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 status = psa_generate_random(local_iv, default_iv_length);
4383 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004384 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004386 }
4387
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004388 status = psa_driver_wrapper_cipher_encrypt(
4389 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004390 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004391 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004393
4394exit:
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;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004398 }
Ronald Cron23919522021-07-08 19:00:07 +02004399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (status == PSA_SUCCESS) {
4401 if (default_iv_length > 0) {
4402 memcpy(output, local_iv, default_iv_length);
4403 }
4404 *output_length += default_iv_length;
4405 } else {
4406 *output_length = 0;
4407 }
4408
4409 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004410}
4411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4413 psa_algorithm_t alg,
4414 const uint8_t *input,
4415 size_t input_length,
4416 uint8_t *output,
4417 size_t output_size,
4418 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004419{
4420 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004421 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004422 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004423 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004426 status = PSA_ERROR_INVALID_ARGUMENT;
4427 goto exit;
4428 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4431 PSA_KEY_USAGE_DECRYPT,
4432 alg);
4433 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004434 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004436
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004437 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004439 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4442 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004443 status = PSA_ERROR_INVALID_ARGUMENT;
4444 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004446 status = PSA_ERROR_INVALID_ARGUMENT;
4447 goto exit;
4448 }
4449
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004450 status = psa_driver_wrapper_cipher_decrypt(
4451 &attributes, slot->key.data, slot->key.bytes,
4452 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004454
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 unlock_status = psa_unlock_key_slot(slot);
4457 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004458 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004462 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 }
Ronald Cron23919522021-07-08 19:00:07 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004466}
4467
4468
mohammad16035955c982018-04-26 00:53:03 +03004469/****************************************************************/
4470/* AEAD */
4471/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004472
Paul Elliottbaff51c2021-09-28 17:44:45 +01004473/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004474static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004475{
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004477}
4478
4479/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4481 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004482{
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004486#if defined(PSA_WANT_ALG_GCM)
4487 case PSA_ALG_GCM:
4488 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 * arbitrarily large nonces. Please note that we do not generally
4490 * recommend the usage of nonces of greater length than
4491 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4492 * size, which can then lead to collisions if you encrypt a very
4493 * large number of messages.*/
4494 if (nonce_length != 0) {
4495 return PSA_SUCCESS;
4496 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004497 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004498#endif /* PSA_WANT_ALG_GCM */
4499#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004500 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 if (nonce_length >= 7 && nonce_length <= 13) {
4502 return PSA_SUCCESS;
4503 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004504 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004505#endif /* PSA_WANT_ALG_CCM */
4506#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004507 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 if (nonce_length == 12) {
4509 return PSA_SUCCESS;
4510 } else if (nonce_length == 8) {
4511 return PSA_ERROR_NOT_SUPPORTED;
4512 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004513 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004514#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004515 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004516 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004518 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004521}
4522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004524{
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4526 return PSA_ERROR_INVALID_ARGUMENT;
4527 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004530}
4531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4533 psa_algorithm_t alg,
4534 const uint8_t *nonce,
4535 size_t nonce_length,
4536 const uint8_t *additional_data,
4537 size_t additional_data_length,
4538 const uint8_t *plaintext,
4539 size_t plaintext_length,
4540 uint8_t *ciphertext,
4541 size_t ciphertext_size,
4542 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004543{
Ronald Cron215633c2021-03-16 17:15:37 +01004544 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4545 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004546
mohammad1603f08a5502018-06-03 15:05:47 +03004547 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 status = psa_aead_check_algorithm(alg);
4550 if (status != PSA_SUCCESS) {
4551 return status;
4552 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004553
Ronald Cron9a986162021-03-26 12:40:07 +01004554 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4556 if (status != PSA_SUCCESS) {
4557 return status;
4558 }
mohammad16035955c982018-04-26 00:53:03 +03004559
Ronald Cron215633c2021-03-16 17:15:37 +01004560 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004562 };
mohammad16035955c982018-04-26 00:53:03 +03004563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 status = psa_aead_check_nonce_length(alg, nonce_length);
4565 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004568
Ronald Cronde822812021-03-17 16:08:20 +01004569 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004570 &attributes, slot->key.data, slot->key.bytes,
4571 alg,
4572 nonce, nonce_length,
4573 additional_data, additional_data_length,
4574 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4578 memset(ciphertext, 0, ciphertext_size);
4579 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004580
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004585}
4586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4588 psa_algorithm_t alg,
4589 const uint8_t *nonce,
4590 size_t nonce_length,
4591 const uint8_t *additional_data,
4592 size_t additional_data_length,
4593 const uint8_t *ciphertext,
4594 size_t ciphertext_length,
4595 uint8_t *plaintext,
4596 size_t plaintext_size,
4597 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004598{
Ronald Cron215633c2021-03-16 17:15:37 +01004599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4600 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004601
Gilles Peskineee652a32018-06-01 19:23:52 +02004602 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 status = psa_aead_check_algorithm(alg);
4605 if (status != PSA_SUCCESS) {
4606 return status;
4607 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004608
Ronald Cron9a986162021-03-26 12:40:07 +01004609 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4611 if (status != PSA_SUCCESS) {
4612 return status;
4613 }
mohammad16035955c982018-04-26 00:53:03 +03004614
Ronald Cron215633c2021-03-16 17:15:37 +01004615 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004617 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 status = psa_aead_check_nonce_length(alg, nonce_length);
4620 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004621 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004623
Ronald Cronde822812021-03-17 16:08:20 +01004624 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004625 &attributes, slot->key.data, slot->key.bytes,
4626 alg,
4627 nonce, nonce_length,
4628 additional_data, additional_data_length,
4629 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 if (status != PSA_SUCCESS && plaintext_size != 0) {
4633 memset(plaintext, 0, plaintext_size);
4634 }
mohammad160360a64d02018-06-03 17:20:42 +03004635
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004636exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 return status;
mohammad16035955c982018-04-26 00:53:03 +03004640}
4641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4643{
4644 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004645
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004647#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004649 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4651 return PSA_ERROR_INVALID_ARGUMENT;
4652 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004653 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004654#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004655
Przemek Stekiel86679c72022-10-06 17:06:56 +02004656#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4660 return PSA_ERROR_INVALID_ARGUMENT;
4661 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004663#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664
Przemek Stekiel86679c72022-10-06 17:06:56 +02004665#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (tag_len != 16) {
4669 return PSA_ERROR_INVALID_ARGUMENT;
4670 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004671 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004672#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004673
4674 default:
4675 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004679}
4680
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004681/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004682static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4683 int is_encrypt,
4684 mbedtls_svc_key_id_t key,
4685 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004686{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4688 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4689 psa_key_slot_t *slot = NULL;
4690 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004691 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 status = psa_aead_check_algorithm(alg);
4694 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004695 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004699 status = PSA_ERROR_BAD_STATE;
4700 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004701 }
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (operation->nonce_set || operation->lengths_set ||
4704 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004705 status = PSA_ERROR_BAD_STATE;
4706 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004707 }
4708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004710 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4716 alg);
4717 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004718 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004720
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004721 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004722 .core = slot->attr
4723 };
4724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004726 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (is_encrypt) {
4730 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4731 &attributes,
4732 slot->key.data,
4733 slot->key.bytes,
4734 alg);
4735 } else {
4736 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4737 &attributes,
4738 slot->key.data,
4739 slot->key.bytes,
4740 alg);
4741 }
4742 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004743 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747
4748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004754 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 } else {
4756 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004760}
4761
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004763psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4764 mbedtls_svc_key_id_t key,
4765 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004766{
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768}
4769
4770/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4772 mbedtls_svc_key_id_t key,
4773 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004774{
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776}
4777
4778/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004779psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4780 uint8_t *nonce,
4781 size_t nonce_size,
4782 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004783{
4784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004785 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004786 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004787
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004788 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004791 status = PSA_ERROR_BAD_STATE;
4792 goto exit;
4793 }
4794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004796 status = PSA_ERROR_BAD_STATE;
4797 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798 }
4799
Paul Elliotte193ea82021-10-01 13:00:16 +01004800 /* For CCM, this size may not be correct according to the PSA
4801 * specification. The PSA Crypto 1.0.1 specification states:
4802 *
4803 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4804 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4805 *
4806 * However this restriction that L has to be the smallest integer is not
4807 * applied in practice, and it is not implementable here since the
4808 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4810 operation->alg);
4811 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004812 status = PSA_ERROR_BUFFER_TOO_SMALL;
4813 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004814 }
4815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 status = psa_generate_random(local_nonce, required_nonce_size);
4817 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004818 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (status == PSA_SUCCESS) {
4825 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004826 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 } else {
4828 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004829 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004832}
4833
4834/* Set the nonce for a multipart authenticated encryption or decryption
4835 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004836psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4837 const uint8_t *nonce,
4838 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004839{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004843 status = PSA_ERROR_BAD_STATE;
4844 goto exit;
4845 }
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004848 status = PSA_ERROR_BAD_STATE;
4849 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4853 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004854 status = PSA_ERROR_INVALID_ARGUMENT;
4855 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004856 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004857
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4859 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004860
Paul Elliott39dc6b82021-05-11 19:16:09 +01004861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004863 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 } else {
4865 psa_aead_abort(operation);
4866 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004869}
4870
4871/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004872psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4873 size_t ad_length,
4874 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004875{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004879 status = PSA_ERROR_BAD_STATE;
4880 goto exit;
4881 }
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->lengths_set || operation->ad_started ||
4884 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004885 status = PSA_ERROR_BAD_STATE;
4886 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004887 }
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004890#if defined(PSA_WANT_ALG_GCM)
4891 case PSA_ALG_GCM:
4892 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 * bits. Without the guard this code will generate warnings on 32bit
4894 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004895#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (((uint64_t) ad_length) >> 61 != 0 ||
4897 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004898 status = PSA_ERROR_INVALID_ARGUMENT;
4899 goto exit;
4900 }
Paul Elliott325d3742021-09-27 17:56:28 +01004901#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004902 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004903#endif /* PSA_WANT_ALG_GCM */
4904#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004905 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 status = PSA_ERROR_INVALID_ARGUMENT;
4908 goto exit;
4909 }
4910 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004911#endif /* PSA_WANT_ALG_CCM */
4912#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004913 case PSA_ALG_CHACHA20_POLY1305:
4914 /* No length restrictions for ChaChaPoly. */
4915 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004916#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004917 default:
4918 break;
4919 }
Paul Elliott325d3742021-09-27 17:56:28 +01004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4922 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004923
Paul Elliott39dc6b82021-05-11 19:16:09 +01004924exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004926 operation->ad_remaining = ad_length;
4927 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004928 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 } else {
4930 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004931 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004934}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004935
4936/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004937psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4938 const uint8_t *input,
4939 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004940{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4942
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004944 status = PSA_ERROR_BAD_STATE;
4945 goto exit;
4946 }
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (operation->lengths_set) {
4954 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004955 status = PSA_ERROR_INVALID_ARGUMENT;
4956 goto exit;
4957 }
4958
4959 operation->ad_remaining -= input_length;
4960 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004961#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004963 status = PSA_ERROR_BAD_STATE;
4964 goto exit;
4965 }
4966#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004967
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 status = psa_driver_wrapper_aead_update_ad(operation, input,
4969 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004970
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 } else {
4975 psa_aead_abort(operation);
4976 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979}
4980
4981/* Encrypt or decrypt a message fragment in an active multipart AEAD
4982 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004983psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4984 const uint8_t *input,
4985 size_t input_length,
4986 uint8_t *output,
4987 size_t output_size,
4988 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004989{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004990 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991
4992 *output_length = 0;
4993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004995 status = PSA_ERROR_BAD_STATE;
4996 goto exit;
4997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005000 status = PSA_ERROR_BAD_STATE;
5001 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005002 }
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005005 /* Additional data length was supplied, but not all the additional
5006 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005008 status = PSA_ERROR_INVALID_ARGUMENT;
5009 goto exit;
5010 }
5011
5012 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005014 status = PSA_ERROR_INVALID_ARGUMENT;
5015 goto exit;
5016 }
5017
5018 operation->body_remaining -= input_length;
5019 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005020#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005022 status = PSA_ERROR_BAD_STATE;
5023 goto exit;
5024 }
5025#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5028 output, output_size,
5029 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005030
Paul Elliott39dc6b82021-05-11 19:16:09 +01005031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005033 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 } else {
5035 psa_aead_abort(operation);
5036 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005039}
5040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005042{
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (operation->id == 0 || !operation->nonce_set) {
5044 return PSA_ERROR_BAD_STATE;
5045 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5048 operation->body_remaining != 0)) {
5049 return PSA_ERROR_INVALID_ARGUMENT;
5050 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005053}
5054
Paul Elliott302ff6b2021-04-20 18:10:30 +01005055/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005056psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5057 uint8_t *ciphertext,
5058 size_t ciphertext_size,
5059 size_t *ciphertext_length,
5060 uint8_t *tag,
5061 size_t tag_size,
5062 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005063{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5065
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005067 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 status = psa_aead_final_checks(operation);
5070 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005075 status = PSA_ERROR_BAD_STATE;
5076 goto exit;
5077 }
5078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5080 ciphertext_size,
5081 ciphertext_length,
5082 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005083
Paul Elliott39dc6b82021-05-11 19:16:09 +01005084exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005085
5086
Paul Elliottf47b0952021-05-21 18:02:33 +01005087 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005088 * the zero tag size, make sure the tag is set to something implausible.
5089 * Even if the operation succeeds, make sure we clear the rest of the
5090 * buffer to prevent potential leakage of anything previously placed in
5091 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005092 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005097}
5098
5099/* Finish authenticating and decrypting a message in a multipart AEAD
5100 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005101psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5102 uint8_t *plaintext,
5103 size_t plaintext_size,
5104 size_t *plaintext_length,
5105 const uint8_t *tag,
5106 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005107{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005108 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5109
Paul Elliott302ff6b2021-04-20 18:10:30 +01005110 *plaintext_length = 0;
5111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 status = psa_aead_final_checks(operation);
5113 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005118 status = PSA_ERROR_BAD_STATE;
5119 goto exit;
5120 }
5121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5123 plaintext_size,
5124 plaintext_length,
5125 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005126
5127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005131}
5132
5133/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005135{
5136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005139 /* The object has (apparently) been initialized but it is not (yet)
5140 * in use. It's ok to call abort on such an object, and there's
5141 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005143 }
5144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005150}
5151
Gilles Peskinee59236f2018-01-27 23:32:46 +01005152/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153/* Generators */
5154/****************************************************************/
5155
Przemek Stekiel69c46792022-06-10 12:59:51 +02005156#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005157 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005158 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5160 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005161#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005162#endif /* At least one builtin KDF */
5163
Przemek Stekiel69c46792022-06-10 12:59:51 +02005164#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005165 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5166 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5167static psa_status_t psa_key_derivation_start_hmac(
5168 psa_mac_operation_t *operation,
5169 psa_algorithm_t hash_alg,
5170 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005172{
5173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5176 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5177 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005178
Steven Cooreman72f736a2021-05-07 14:14:37 +02005179 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 status = psa_driver_wrapper_mac_sign_setup(operation,
5183 &attributes,
5184 hmac_key, hmac_key_length,
5185 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 psa_reset_key_attributes(&attributes);
5188 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005189}
5190#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005191
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005192#define HKDF_STATE_INIT 0 /* no input yet */
5193#define HKDF_STATE_STARTED 1 /* got salt */
5194#define HKDF_STATE_KEYED 2 /* got key */
5195#define HKDF_STATE_OUTPUT 3 /* output started */
5196
Gilles Peskinecbe66502019-05-16 16:59:18 +02005197static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005199{
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5201 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5202 } else {
5203 return operation->alg;
5204 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205}
5206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005208{
5209 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5211 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005212 /* The object has (apparently) been initialized but it is not
5213 * in use. It's ok to call abort on such an object, and there's
5214 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005216#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5218 mbedtls_free(operation->ctx.hkdf.info);
5219 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5220 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005221#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005222#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5223 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5225 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5226 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5227 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005228 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005230 }
5231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005233 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005235 }
5236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005238 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005240 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005241#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005243 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005245 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005246#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005247 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005248
5249 /* We leave the fields Ai and output_block to be erased safely by the
5250 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005252#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5253 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005254#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5256 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5257 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5258 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005259#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305260#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5261 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305262 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005263 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305264 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305265 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305266
5267 status = PSA_SUCCESS;
5268 } else
5269#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270 {
5271 status = PSA_ERROR_BAD_STATE;
5272 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 mbedtls_platform_zeroize(operation, sizeof(*operation));
5274 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005275}
5276
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279{
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005283 }
5284
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287}
5288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5290 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005291{
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (operation->alg == 0) {
5293 return PSA_ERROR_BAD_STATE;
5294 }
5295 if (capacity > operation->capacity) {
5296 return PSA_ERROR_INVALID_ARGUMENT;
5297 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005300}
5301
Przemek Stekiel69c46792022-06-10 12:59:51 +02005302#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005303/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5305 psa_algorithm_t kdf_alg,
5306 uint8_t *output,
5307 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005308{
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5310 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005311 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005312 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005313#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005315#else
5316 const uint8_t last_block = 0xff;
5317#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 if (hkdf->state < HKDF_STATE_KEYED ||
5320 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005321#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005323#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 )) {
5325 return PSA_ERROR_BAD_STATE;
5326 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005327 hkdf->state = HKDF_STATE_OUTPUT;
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005330 /* Copy what remains of the current block */
5331 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005333 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 }
5335 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005336 output += n;
5337 output_length -= n;
5338 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005340 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005342 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005343 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005345 * object was corrupted or if this function is called directly
5346 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 if (hkdf->block_number == last_block) {
5348 return PSA_ERROR_BAD_STATE;
5349 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005350
5351 /* We need a new block */
5352 ++hkdf->block_number;
5353 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5356 hash_alg,
5357 hkdf->prk,
5358 hash_length);
5359 if (status != PSA_SUCCESS) {
5360 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005361 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005362
5363 if (hkdf->block_number != 1) {
5364 status = psa_mac_update(&hkdf->hmac,
5365 hkdf->output_block,
5366 hash_length);
5367 if (status != PSA_SUCCESS) {
5368 return status;
5369 }
5370 }
5371 status = psa_mac_update(&hkdf->hmac,
5372 hkdf->info,
5373 hkdf->info_length);
5374 if (status != PSA_SUCCESS) {
5375 return status;
5376 }
5377 status = psa_mac_update(&hkdf->hmac,
5378 &hkdf->block_number, 1);
5379 if (status != PSA_SUCCESS) {
5380 return status;
5381 }
5382 status = psa_mac_sign_finish(&hkdf->hmac,
5383 hkdf->output_block,
5384 sizeof(hkdf->output_block),
5385 &hmac_output_length);
5386 if (status != PSA_SUCCESS) {
5387 return status;
5388 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005389 }
5390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005392}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005393#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005394
John Durkop07cc04a2020-11-16 22:08:34 -08005395#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5396 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005397static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5398 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005400{
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5402 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005403 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5404 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005405 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005406
Janos Follath7742fee2019-06-17 12:58:10 +01005407 /* We can't be wanting more output after block 0xff, otherwise
5408 * the capacity check in psa_key_derivation_output_bytes() would have
5409 * prevented this call. It could happen only if the operation
5410 * object was corrupted or if this function is called directly
5411 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if (tls12_prf->block_number == 0xff) {
5413 return PSA_ERROR_CORRUPTION_DETECTED;
5414 }
Janos Follath7742fee2019-06-17 12:58:10 +01005415
5416 /* We need a new block */
5417 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005418 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005419
5420 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5421 *
5422 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5423 *
5424 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5425 * HMAC_hash(secret, A(2) + seed) +
5426 * HMAC_hash(secret, A(3) + seed) + ...
5427 *
5428 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005429 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005430 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005432 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005433 * is currently extracted as `output_block` and where i is
5434 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005435 */
5436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 status = psa_key_derivation_start_hmac(&hmac,
5438 hash_alg,
5439 tls12_prf->secret,
5440 tls12_prf->secret_length);
5441 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005442 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005444
5445 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5448 * the variable seed and in this instance means it in the context of the
5449 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 status = psa_mac_update(&hmac,
5451 tls12_prf->label,
5452 tls12_prf->label_length);
5453 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005454 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 }
5456 status = psa_mac_update(&hmac,
5457 tls12_prf->seed,
5458 tls12_prf->seed_length);
5459 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 }
5462 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5465 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 }
5469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 status = psa_mac_sign_finish(&hmac,
5471 tls12_prf->Ai, hash_length,
5472 &hmac_output_length);
5473 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005474 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 }
5476 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005477 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005479
5480 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 status = psa_key_derivation_start_hmac(&hmac,
5482 hash_alg,
5483 tls12_prf->secret,
5484 tls12_prf->secret_length);
5485 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005486 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 }
5488 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5489 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005490 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 }
5492 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5493 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005494 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 }
5496 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5497 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005498 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 }
5500 status = psa_mac_sign_finish(&hmac,
5501 tls12_prf->output_block, hash_length,
5502 &hmac_output_length);
5503 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005504 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005506
Janos Follath7742fee2019-06-17 12:58:10 +01005507
5508cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 cleanup_status = psa_mac_abort(&hmac);
5510 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005511 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005513
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005515}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005516
Janos Follath844eb0e2019-06-19 12:10:49 +01005517static psa_status_t psa_key_derivation_tls12_prf_read(
5518 psa_tls12_prf_key_derivation_t *tls12_prf,
5519 psa_algorithm_t alg,
5520 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005522{
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5524 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005525 psa_status_t status;
5526 uint8_t offset, length;
5527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005529 case PSA_TLS12_PRF_STATE_LABEL_SET:
5530 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5531 break;
5532 case PSA_TLS12_PRF_STATE_OUTPUT:
5533 break;
5534 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005536 }
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005539 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 if (tls12_prf->left_in_block == 0) {
5541 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5542 alg);
5543 if (status != PSA_SUCCESS) {
5544 return status;
5545 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005546
5547 continue;
5548 }
5549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005551 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005553 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005555
5556 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005558 output += length;
5559 output_length -= length;
5560 tls12_prf->left_in_block -= length;
5561 }
5562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005564}
John Durkop07cc04a2020-11-16 22:08:34 -08005565#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5566 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005567
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005568#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5569static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5570 psa_tls12_ecjpake_to_pms_t *ecjpake,
5571 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005573{
5574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005575 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (output_length != 32) {
5578 return PSA_ERROR_INVALID_ARGUMENT;
5579 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5582 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5583 &output_size);
5584 if (status != PSA_SUCCESS) {
5585 return status;
5586 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 if (output_size != output_length) {
5589 return PSA_ERROR_GENERIC_ERROR;
5590 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005593}
5594#endif
5595
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305596#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5597static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5598 psa_pbkdf2_key_derivation_t *pbkdf2,
5599 psa_algorithm_t prf_alg,
5600 uint8_t prf_output_length,
5601 psa_key_attributes_t *attributes)
5602{
5603 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305604 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305605 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305606 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305607 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305608 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305609 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305610
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305611 mac_operation.is_sign = 1;
5612 mac_operation.mac_size = prf_output_length;
5613 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305614
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305615 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5616 attributes,
5617 pbkdf2->password,
5618 pbkdf2->password_length,
5619 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305620 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305621 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305622 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305623 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5624 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305625 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305626 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305627 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305628 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305629 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305630 }
5631 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5632 &mac_output_length);
5633 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305634 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 }
5636
5637 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305638 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305639 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305640 }
5641
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305642 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305643
5644 for (i = 1; i < pbkdf2->input_cost; i++) {
5645 status = psa_driver_wrapper_mac_compute(attributes,
5646 pbkdf2->password,
5647 pbkdf2->password_length,
5648 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305649 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305650 &mac_output_length);
5651 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305652 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653 }
5654
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305655 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305657
5658cleanup:
5659 /* Zeroise buffers to clear sensitive data from memory. */
5660 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5661 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305662}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305663
5664static psa_status_t psa_key_derivation_pbkdf2_read(
5665 psa_pbkdf2_key_derivation_t *pbkdf2,
5666 psa_algorithm_t kdf_alg,
5667 uint8_t *output,
5668 size_t output_length)
5669{
5670 psa_status_t status;
5671 psa_algorithm_t prf_alg;
5672 uint8_t prf_output_length;
5673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5674 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5675 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5676
5677 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5678 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5679 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5680 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305681 } else {
5682 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305683 }
5684
5685 switch (pbkdf2->state) {
5686 case PSA_PBKDF2_STATE_PASSWORD_SET:
5687 /* Initially we need a new block so bytes_used is equal to block size*/
5688 pbkdf2->bytes_used = prf_output_length;
5689 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5690 break;
5691 case PSA_PBKDF2_STATE_OUTPUT:
5692 break;
5693 default:
5694 return PSA_ERROR_BAD_STATE;
5695 }
5696
5697 while (output_length != 0) {
5698 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5699 if (n > output_length) {
5700 n = (uint8_t) output_length;
5701 }
5702 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5703 output += n;
5704 output_length -= n;
5705 pbkdf2->bytes_used += n;
5706
5707 if (output_length == 0) {
5708 break;
5709 }
5710
5711 /* We need a new block */
5712 pbkdf2->bytes_used = 0;
5713 pbkdf2->block_number++;
5714
5715 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5716 prf_output_length,
5717 &attributes);
5718 if (status != PSA_SUCCESS) {
5719 return status;
5720 }
5721 }
5722
5723 return PSA_SUCCESS;
5724}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305725#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5726
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005727psa_status_t psa_key_derivation_output_bytes(
5728 psa_key_derivation_operation_t *operation,
5729 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005731{
5732 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005736 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005738 }
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005742 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005744 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005745 goto exit;
5746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005749 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5751 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005756 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757
Przemek Stekiel69c46792022-06-10 12:59:51 +02005758#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5760 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5761 output, output_length);
5762 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005763#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005764#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5765 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5767 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5768 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5769 kdf_alg, output,
5770 output_length);
5771 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005772#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5773 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005774#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005776 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5778 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005779#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305780#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5781 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305782 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5783 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305784 } else
5785#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005786
Gilles Peskineeab56e42018-07-12 17:12:33 +02005787 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005788 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005790 }
5791
5792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005794 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 * This allows us to differentiate between exhausted operations and
5796 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5797 * operations. */
5798 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005800 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005804}
5805
David Brown7807bf72021-02-09 16:28:23 -07005806#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005807static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005808{
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (data_size >= 8) {
5810 mbedtls_des_key_set_parity(data);
5811 }
5812 if (data_size >= 16) {
5813 mbedtls_des_key_set_parity(data + 8);
5814 }
5815 if (data_size >= 24) {
5816 mbedtls_des_key_set_parity(data + 16);
5817 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005818}
David Brown7807bf72021-02-09 16:28:23 -07005819#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005820
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005821/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 * ECC keys on a Weierstrass elliptic curve require the generation
5823 * of a private key which is an integer
5824 * in the range [1, N - 1], where N is the boundary of the private key domain:
5825 * N is the prime p for Diffie-Hellman, or the order of the
5826 * curve’s base point for ECC.
5827 *
5828 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5829 * This function generates the private key using the following process:
5830 *
5831 * 1. Draw a byte string of length ceiling(m/8) bytes.
5832 * 2. If m is not a multiple of 8, set the most significant
5833 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5834 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5835 * 4. If k > N - 2, discard the result and return to step 1.
5836 * 5. Output k + 1 as the private key.
5837 *
5838 * This method allows compliance to NIST standards, specifically the methods titled
5839 * Key-Pair Generation by Testing Candidates in the following publications:
5840 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5841 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5842 * Diffie-Hellman keys.
5843 *
5844 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5845 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5846 *
5847 * Note: Function allocates memory for *data buffer, so given *data should be
5848 * always NULL.
5849 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005850#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5851#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005852static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005853 psa_key_slot_t *slot,
5854 size_t bits,
5855 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005856 uint8_t **data
5857 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005858{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005859 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005860 mbedtls_mpi k;
5861 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005864 size_t m;
5865 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 mbedtls_mpi_init(&k);
5868 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005869
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005876 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005877 goto cleanup;
5878 }
5879
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005880 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005885 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005887 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005889 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005890
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005891 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005893
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005894 /* Note: This function is always called with *data == NULL and it
5895 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 *data = mbedtls_calloc(1, m_bytes);
5897 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005898 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005899 goto cleanup;
5900 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005903 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005905 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005907
5908 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005910 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005911 * (8 * ceiling(m/8) - m) bits of the first byte in
5912 * the string to zero.
5913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005915 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005916 }
5917
5918 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 * big-endian byte string.
5920 */
5921 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005922
5923 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 * Result of comparison is returned. When it indicates error
5925 * then this function is called again.
5926 */
5927 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005928 }
5929
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005930 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5932 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005933cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if (ret != 0) {
5935 status = mbedtls_to_psa_error(ret);
5936 }
5937 if (status != PSA_SUCCESS) {
5938 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005939 *data = NULL;
5940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 mbedtls_mpi_free(&k);
5942 mbedtls_mpi_free(&diff_N_2);
5943 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005944}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005945
5946/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5947 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5948 *
5949 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5950 * draw a 32-byte string and process it as specified in
5951 * Elliptic Curves for Security [RFC7748] §5.
5952 *
5953 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5954 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5955 *
5956 * Note: Function allocates memory for *data buffer, so given *data should be
5957 * always NULL.
5958 */
5959
5960static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5961 size_t bits,
5962 psa_key_derivation_operation_t *operation,
5963 uint8_t **data
5964 )
5965{
5966 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005970 case 255:
5971 output_length = 32;
5972 break;
5973 case 448:
5974 output_length = 56;
5975 break;
5976 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005978 break;
5979 }
5980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (*data == NULL) {
5984 return PSA_ERROR_INSUFFICIENT_MEMORY;
5985 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994 case 255:
5995 (*data)[0] &= 248;
5996 (*data)[31] &= 127;
5997 (*data)[31] |= 64;
5998 break;
5999 case 448:
6000 (*data)[0] &= 252;
6001 (*data)[55] |= 128;
6002 break;
6003 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006005 break;
6006 }
6007
6008 return status;
6009}
Valerio Setti86587ab2023-06-27 13:09:30 +02006010#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6011static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6012 psa_key_slot_t *slot, size_t bits,
6013 psa_key_derivation_operation_t *operation, uint8_t **data)
6014{
6015 (void) slot;
6016 (void) bits;
6017 (void) operation;
6018 (void) data;
6019 return PSA_ERROR_NOT_SUPPORTED;
6020}
6021
6022static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6023 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6024{
6025 (void) bits;
6026 (void) operation;
6027 (void) data;
6028 return PSA_ERROR_NOT_SUPPORTED;
6029}
6030#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6031#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006032
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006033static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006034 psa_key_slot_t *slot,
6035 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006037{
6038 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306040 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006042 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6045 return PSA_ERROR_INVALID_ARGUMENT;
6046 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006047
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006048#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006049 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6051 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6052 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006053 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6055 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006056 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 }
6058 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006059 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6061 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006062 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006064 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006065 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006066#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006067 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (key_type_is_raw_bytes(slot->attr.type)) {
6069 if (bits % 8 != 0) {
6070 return PSA_ERROR_INVALID_ARGUMENT;
6071 }
6072 data = mbedtls_calloc(1, bytes);
6073 if (data == NULL) {
6074 return PSA_ERROR_INSUFFICIENT_MEMORY;
6075 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 status = psa_key_derivation_output_bytes(operation, data, bytes);
6078 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 }
David Brown12ca5032021-02-11 11:02:00 -07006081#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6083 psa_des_set_key_parity(data, bytes);
6084 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006085#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 } else {
6087 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006088 }
Ronald Crondd04d422020-11-28 15:14:42 +01006089
Ronald Cronbf33c932020-11-28 18:06:53 +01006090 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006091 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006093 };
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6096 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6097 &storage_size);
6098 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 }
Archanad8a83dc2021-06-14 10:04:16 +05306101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 status = psa_allocate_buffer_to_slot(slot, storage_size);
6103 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 }
Archanad8a83dc2021-06-14 10:04:16 +05306106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 status = psa_driver_wrapper_import_key(&attributes,
6108 data, bytes,
6109 slot->key.data,
6110 slot->key.bytes,
6111 &slot->key.bytes, &bits);
6112 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006113 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006115
6116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 mbedtls_free(data);
6118 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006119}
6120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6122 psa_key_derivation_operation_t *operation,
6123 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006124{
6125 psa_status_t status;
6126 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006127 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006128
Ronald Cron81709fc2020-11-14 12:10:32 +01006129 *key = MBEDTLS_SVC_KEY_ID_INIT;
6130
Gilles Peskine0f84d622019-09-12 19:03:13 +02006131 /* Reject any attempt to create a zero-length key so that we don't
6132 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (psa_get_key_bits(attributes) == 0) {
6134 return PSA_ERROR_INVALID_ARGUMENT;
6135 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 if (operation->alg == PSA_ALG_NONE) {
6138 return PSA_ERROR_BAD_STATE;
6139 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (!operation->can_output_key) {
6142 return PSA_ERROR_NOT_PERMITTED;
6143 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6146 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006147#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006149 /* Deriving a key in a secure element is not implemented yet. */
6150 status = PSA_ERROR_NOT_SUPPORTED;
6151 }
6152#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (status == PSA_SUCCESS) {
6154 status = psa_generate_derived_key_internal(slot,
6155 attributes->core.bits,
6156 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (status == PSA_SUCCESS) {
6159 status = psa_finish_key_creation(slot, driver, key);
6160 }
6161 if (status != PSA_SUCCESS) {
6162 psa_fail_key_creation(slot, driver);
6163 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006166}
6167
Gilles Peskineeab56e42018-07-12 17:12:33 +02006168
6169
6170/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006171/* Key derivation */
6172/****************************************************************/
6173
Steven Cooreman932ffb72021-02-15 12:14:32 +01006174#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006175static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006176{
6177#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6179 return 1;
6180 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006181#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006182#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6184 return 1;
6185 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006186#endif
6187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6189 return 1;
6190 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006191#endif
6192#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6194 return 1;
6195 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006196#endif
6197#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6199 return 1;
6200 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006201#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006202#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6204 return 1;
6205 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006206#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306207#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6208 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6209 return 1;
6210 }
6211#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006213}
6214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006216{
6217 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 psa_status_t status = psa_hash_setup(&operation, alg);
6219 psa_hash_abort(&operation);
6220 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006221}
6222
Gilles Peskine969c5d62019-01-16 15:53:06 +01006223static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006224 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006226{
Janos Follath5fe19732019-06-20 15:09:30 +01006227 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6228 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006230
Gilles Peskine969c5d62019-01-16 15:53:06 +01006231 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 if (!is_kdf_alg_supported(kdf_alg)) {
6233 return PSA_ERROR_NOT_SUPPORTED;
6234 }
John Durkop07cc04a2020-11-16 22:08:34 -08006235
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006236 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006237 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6239 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6240 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6241 if (hash_size == 0) {
6242 return PSA_ERROR_NOT_SUPPORTED;
6243 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006244
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006245 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6246 * we might fail later, which is somewhat unfriendly and potentially
6247 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 psa_status_t status = psa_hash_try_support(hash_alg);
6249 if (status != PSA_SUCCESS) {
6250 return status;
6251 }
6252 } else {
6253 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006254 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6257 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6258 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6259 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006260 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006261#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006262 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6264 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006265 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006267#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6268 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 operation->capacity = 255 * hash_size;
6270 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006271}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006274{
6275#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 if (alg == PSA_ALG_ECDH) {
6277 return PSA_SUCCESS;
6278 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006279#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006280#if defined(PSA_WANT_ALG_FFDH)
6281 if (alg == PSA_ALG_FFDH) {
6282 return PSA_SUCCESS;
6283 }
6284#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006285 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006287}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006288
6289static int psa_key_derivation_allows_free_form_secret_input(
6290 psa_algorithm_t kdf_alg)
6291{
6292#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6293 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6294 return 0;
6295 }
6296#endif
6297 (void) kdf_alg;
6298 return 1;
6299}
John Durkop07cc04a2020-11-16 22:08:34 -08006300#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6303 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006304{
6305 psa_status_t status;
6306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 if (operation->alg != 0) {
6308 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006309 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6312 return PSA_ERROR_INVALID_ARGUMENT;
6313 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6314#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6315 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6316 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6317 status = psa_key_agreement_try_support(ka_alg);
6318 if (status != PSA_SUCCESS) {
6319 return status;
6320 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006321 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6322 return PSA_ERROR_INVALID_ARGUMENT;
6323 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6325#else
6326 return PSA_ERROR_NOT_SUPPORTED;
6327#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6328 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6329#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6330 status = psa_key_derivation_setup_kdf(operation, alg);
6331#else
6332 return PSA_ERROR_NOT_SUPPORTED;
6333#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6334 } else {
6335 return PSA_ERROR_INVALID_ARGUMENT;
6336 }
6337
6338 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006339 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 }
6341 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006342}
6343
Przemek Stekiel69c46792022-06-10 12:59:51 +02006344#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006345static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6346 psa_algorithm_t kdf_alg,
6347 psa_key_derivation_step_t step,
6348 const uint8_t *data,
6349 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006350{
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006352 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006354 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006355#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6357 return PSA_ERROR_INVALID_ARGUMENT;
6358 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006359#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 if (hkdf->state != HKDF_STATE_INIT) {
6361 return PSA_ERROR_BAD_STATE;
6362 } else {
6363 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6364 hash_alg,
6365 data, data_length);
6366 if (status != PSA_SUCCESS) {
6367 return status;
6368 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006369 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006371 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006372 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006373#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006375 /* We shouldn't be in different state as HKDF_EXPAND only allows
6376 * two inputs: SECRET (this case) and INFO which does not modify
6377 * the state. It could happen only if the hkdf
6378 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (hkdf->state != HKDF_STATE_INIT) {
6380 return PSA_ERROR_BAD_STATE;
6381 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006382
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006383 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6385 return PSA_ERROR_INVALID_ARGUMENT;
6386 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 memcpy(hkdf->prk, data, data_length);
6389 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006390#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006391 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006392 /* HKDF: If no salt was provided, use an empty salt.
6393 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006395#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6397 return PSA_ERROR_BAD_STATE;
6398 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006399#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6401 hash_alg,
6402 NULL, 0);
6403 if (status != PSA_SUCCESS) {
6404 return status;
6405 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006406 hkdf->state = HKDF_STATE_STARTED;
6407 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 if (hkdf->state != HKDF_STATE_STARTED) {
6409 return PSA_ERROR_BAD_STATE;
6410 }
6411 status = psa_mac_update(&hkdf->hmac,
6412 data, data_length);
6413 if (status != PSA_SUCCESS) {
6414 return status;
6415 }
6416 status = psa_mac_sign_finish(&hkdf->hmac,
6417 hkdf->prk,
6418 sizeof(hkdf->prk),
6419 &data_length);
6420 if (status != PSA_SUCCESS) {
6421 return status;
6422 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006423 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006424
Gilles Peskine2b522db2019-04-12 15:11:49 +02006425 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006426 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006427#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006429 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006431 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006433#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006434 {
6435 /* Block 0 is empty, and the next block will be
6436 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006438 }
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006441 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006442#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6444 return PSA_ERROR_INVALID_ARGUMENT;
6445 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006446#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006447#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6449 hkdf->state == HKDF_STATE_INIT) {
6450 return PSA_ERROR_BAD_STATE;
6451 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006452#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 if (hkdf->state == HKDF_STATE_OUTPUT) {
6454 return PSA_ERROR_BAD_STATE;
6455 }
6456 if (hkdf->info_set) {
6457 return PSA_ERROR_BAD_STATE;
6458 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006459 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (data_length != 0) {
6461 hkdf->info = mbedtls_calloc(1, data_length);
6462 if (hkdf->info == NULL) {
6463 return PSA_ERROR_INSUFFICIENT_MEMORY;
6464 }
6465 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006466 }
6467 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006471 }
6472}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006473#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006474
John Durkop07cc04a2020-11-16 22:08:34 -08006475#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6476 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006477static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6478 const uint8_t *data,
6479 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006480{
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6482 return PSA_ERROR_BAD_STATE;
6483 }
Janos Follathf08e2652019-06-13 09:05:41 +01006484
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 if (data_length != 0) {
6486 prf->seed = mbedtls_calloc(1, data_length);
6487 if (prf->seed == NULL) {
6488 return PSA_ERROR_INSUFFICIENT_MEMORY;
6489 }
Janos Follathf08e2652019-06-13 09:05:41 +01006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006492 prf->seed_length = data_length;
6493 }
Janos Follathf08e2652019-06-13 09:05:41 +01006494
Gilles Peskine43f958b2020-12-13 14:55:14 +01006495 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006498}
6499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6501 const uint8_t *data,
6502 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006503{
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6505 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6506 return PSA_ERROR_BAD_STATE;
6507 }
Janos Follath81550542019-06-13 14:26:34 +01006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 if (data_length != 0) {
6510 prf->secret = mbedtls_calloc(1, data_length);
6511 if (prf->secret == NULL) {
6512 return PSA_ERROR_INSUFFICIENT_MEMORY;
6513 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006516 prf->secret_length = data_length;
6517 }
Janos Follath81550542019-06-13 14:26:34 +01006518
Gilles Peskine43f958b2020-12-13 14:55:14 +01006519 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006522}
6523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6525 const uint8_t *data,
6526 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006527{
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6529 return PSA_ERROR_BAD_STATE;
6530 }
Janos Follath63028dd2019-06-13 09:15:47 +01006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (data_length != 0) {
6533 prf->label = mbedtls_calloc(1, data_length);
6534 if (prf->label == NULL) {
6535 return PSA_ERROR_INSUFFICIENT_MEMORY;
6536 }
Janos Follath63028dd2019-06-13 09:15:47 +01006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006539 prf->label_length = data_length;
6540 }
Janos Follath63028dd2019-06-13 09:15:47 +01006541
Gilles Peskine43f958b2020-12-13 14:55:14 +01006542 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006545}
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6548 psa_key_derivation_step_t step,
6549 const uint8_t *data,
6550 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006551{
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006553 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006555 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006557 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006559 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006561 }
6562}
John Durkop07cc04a2020-11-16 22:08:34 -08006563#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6564 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6565
6566#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6567static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6568 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006569 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006571{
6572 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6574 4 + data_length + prf->other_secret_length :
6575 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006576
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6578 return PSA_ERROR_INVALID_ARGUMENT;
6579 }
John Durkop07cc04a2020-11-16 22:08:34 -08006580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 uint8_t *pms = mbedtls_calloc(1, pms_len);
6582 if (pms == NULL) {
6583 return PSA_ERROR_INSUFFICIENT_MEMORY;
6584 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006585 uint8_t *cur = pms;
6586
6587 /* pure-PSK:
6588 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006589 *
6590 * The premaster secret is formed as follows: if the PSK is N octets
6591 * long, concatenate a uint16 with the value N, N zero octets, a second
6592 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006593 *
6594 * mixed-PSK:
6595 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6596 * follows: concatenate a uint16 with the length of the other secret,
6597 * the other secret itself, uint16 with the length of PSK, and the
6598 * PSK itself.
6599 * For details please check:
6600 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6601 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6602 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006603 */
6604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6606 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6607 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6608 if (prf->other_secret_length != 0) {
6609 memcpy(cur, prf->other_secret, prf->other_secret_length);
6610 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006611 cur += prf->other_secret_length;
6612 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 } else {
6614 *cur++ = MBEDTLS_BYTE_1(data_length);
6615 *cur++ = MBEDTLS_BYTE_0(data_length);
6616 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006617 cur += data_length;
6618 }
6619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 *cur++ = MBEDTLS_BYTE_1(data_length);
6621 *cur++ = MBEDTLS_BYTE_0(data_length);
6622 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006623 cur += data_length;
6624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006626
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006627 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006629}
Janos Follath6660f0e2019-06-17 08:44:03 +01006630
Przemek Stekiele47201b2022-04-19 13:53:28 +02006631static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6632 psa_tls12_prf_key_derivation_t *prf,
6633 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006635{
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6637 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006639
6640 if (data_length != 0) {
6641 prf->other_secret = mbedtls_calloc(1, data_length);
6642 if (prf->other_secret == NULL) {
6643 return PSA_ERROR_INSUFFICIENT_MEMORY;
6644 }
6645
6646 memcpy(prf->other_secret, data, data_length);
6647 prf->other_secret_length = data_length;
6648 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006649 prf->other_secret_length = 0;
6650 }
6651
6652 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6653
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006655}
6656
Janos Follath6660f0e2019-06-17 08:44:03 +01006657static psa_status_t psa_tls12_prf_psk_to_ms_input(
6658 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006659 psa_key_derivation_step_t step,
6660 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006662{
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006664 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 return psa_tls12_prf_psk_to_ms_set_key(prf,
6666 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006667 break;
6668 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6670 data,
6671 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006672 break;
6673 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006676
Przemek Stekiele47201b2022-04-19 13:53:28 +02006677 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006678}
John Durkop07cc04a2020-11-16 22:08:34 -08006679#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006680
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006681#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6682static psa_status_t psa_tls12_ecjpake_to_pms_input(
6683 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006684 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006685 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006687{
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6689 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6690 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006691 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006692
6693 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if (data[0] != 0x04) {
6695 return PSA_ERROR_INVALID_ARGUMENT;
6696 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006697
6698 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006702}
6703#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306704
6705#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6706static psa_status_t psa_pbkdf2_set_input_cost(
6707 psa_pbkdf2_key_derivation_t *pbkdf2,
6708 psa_key_derivation_step_t step,
6709 uint64_t data)
6710{
6711 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6712 return PSA_ERROR_INVALID_ARGUMENT;
6713 }
6714
6715 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6716 return PSA_ERROR_BAD_STATE;
6717 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306718
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306719 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306720 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306721 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306722
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306723 if (data == 0) {
6724 return PSA_ERROR_INVALID_ARGUMENT;
6725 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306726
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306727 pbkdf2->input_cost = data;
6728 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6729
6730 return PSA_SUCCESS;
6731}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306732
6733static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6734 const uint8_t *data,
6735 size_t data_length)
6736{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306737 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6738 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6739 return PSA_ERROR_BAD_STATE;
6740 }
6741
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306742 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6743 pbkdf2->salt = mbedtls_calloc(1, data_length);
6744 if (pbkdf2->salt == NULL) {
6745 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306746 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306747
6748 memcpy(pbkdf2->salt, data, data_length);
6749 pbkdf2->salt_length = data_length;
6750 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306751 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306752
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306753 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6754 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306755 return PSA_ERROR_INSUFFICIENT_MEMORY;
6756 }
6757
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306758 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6759 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306760 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306761 mbedtls_free(pbkdf2->salt);
6762 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306763 }
6764
6765 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6766
6767 return PSA_SUCCESS;
6768}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306769
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306770static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306771 const uint8_t *input,
6772 size_t input_len,
6773 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306774 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306775{
6776 psa_status_t status = PSA_SUCCESS;
6777 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6778 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306779 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306780 } else {
6781 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306782 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783 }
6784 return status;
6785}
6786
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306787static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306788 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306789 const uint8_t *data,
6790 size_t data_length)
6791{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306792 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306793 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6794 return PSA_ERROR_BAD_STATE;
6795 }
6796
6797 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306798 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6799 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6800 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6801 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306802 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306803 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306804 }
6805
6806 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6807
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306808 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306809}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306810
6811static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306812 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306813 psa_key_derivation_step_t step,
6814 const uint8_t *data,
6815 size_t data_length)
6816{
6817 switch (step) {
6818 case PSA_KEY_DERIVATION_INPUT_SALT:
6819 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6820 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306821 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306822 default:
6823 return PSA_ERROR_INVALID_ARGUMENT;
6824 }
6825}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306826#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6827
Gilles Peskineb8965192019-09-24 16:21:10 +02006828/** Check whether the given key type is acceptable for the given
6829 * input step of a key derivation.
6830 *
6831 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6832 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6833 * Both secret and non-secret inputs can alternatively have the type
6834 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6835 * that the input was passed as a buffer rather than via a key object.
6836 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006837static int psa_key_derivation_check_input_type(
6838 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006840{
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006842 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 if (key_type == PSA_KEY_TYPE_DERIVE) {
6844 return PSA_SUCCESS;
6845 }
6846 if (key_type == PSA_KEY_TYPE_NONE) {
6847 return PSA_SUCCESS;
6848 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006849 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006850 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (key_type == PSA_KEY_TYPE_DERIVE) {
6852 return PSA_SUCCESS;
6853 }
6854 if (key_type == PSA_KEY_TYPE_NONE) {
6855 return PSA_SUCCESS;
6856 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006857 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006858 case PSA_KEY_DERIVATION_INPUT_LABEL:
6859 case PSA_KEY_DERIVATION_INPUT_SALT:
6860 case PSA_KEY_DERIVATION_INPUT_INFO:
6861 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6863 return PSA_SUCCESS;
6864 }
6865 if (key_type == PSA_KEY_TYPE_NONE) {
6866 return PSA_SUCCESS;
6867 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006868 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306869 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6870 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6871 return PSA_SUCCESS;
6872 }
6873 if (key_type == PSA_KEY_TYPE_DERIVE) {
6874 return PSA_SUCCESS;
6875 }
6876 if (key_type == PSA_KEY_TYPE_NONE) {
6877 return PSA_SUCCESS;
6878 }
6879 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006880 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006882}
6883
Janos Follathb80a94e2019-06-12 15:54:46 +01006884static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006885 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006886 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006887 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006888 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006890{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006891 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 status = psa_key_derivation_check_input_type(step, key_type);
6895 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006896 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006898
Przemek Stekiel69c46792022-06-10 12:59:51 +02006899#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6901 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6902 step, data, data_length);
6903 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006904#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006905#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6907 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6908 step, data, data_length);
6909 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006910#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6911#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6913 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6914 step, data, data_length);
6915 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006916#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006917#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006919 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6921 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006922#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306923#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6924 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306925 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6926 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306927 } else
6928#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006929 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006930 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006931 (void) data;
6932 (void) data_length;
6933 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006935 }
6936
Gilles Peskine224b0d62019-09-23 18:13:17 +02006937exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 if (status != PSA_SUCCESS) {
6939 psa_key_derivation_abort(operation);
6940 }
6941 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006942}
6943
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306944static psa_status_t psa_key_derivation_input_integer_internal(
6945 psa_key_derivation_operation_t *operation,
6946 psa_key_derivation_step_t step,
6947 uint64_t value)
6948{
6949 psa_status_t status;
6950 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6951
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306952#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6953 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6954 status = psa_pbkdf2_set_input_cost(
6955 &operation->ctx.pbkdf2, step, value);
6956 } else
6957#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306958 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306959 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306960 (void) value;
6961 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306962 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306963 }
6964
6965 if (status != PSA_SUCCESS) {
6966 psa_key_derivation_abort(operation);
6967 }
6968 return status;
6969}
6970
Janos Follath51f4a0f2019-06-14 11:35:55 +01006971psa_status_t psa_key_derivation_input_bytes(
6972 psa_key_derivation_operation_t *operation,
6973 psa_key_derivation_step_t step,
6974 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006976{
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 return psa_key_derivation_input_internal(operation, step,
6978 PSA_KEY_TYPE_NONE,
6979 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006980}
6981
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306982psa_status_t psa_key_derivation_input_integer(
6983 psa_key_derivation_operation_t *operation,
6984 psa_key_derivation_step_t step,
6985 uint64_t value)
6986{
6987 return psa_key_derivation_input_integer_internal(operation, step, value);
6988}
6989
Janos Follath51f4a0f2019-06-14 11:35:55 +01006990psa_status_t psa_key_derivation_input_key(
6991 psa_key_derivation_operation_t *operation,
6992 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006994{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006996 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006997 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006998
Ronald Cron5c522922020-11-14 16:35:34 +01006999 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7001 if (status != PSA_SUCCESS) {
7002 psa_key_derivation_abort(operation);
7003 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007004 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007005
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307006 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7007 * permission to output to a key object. */
7008 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7009 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007010 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 status = psa_key_derivation_input_internal(operation,
7014 step, slot->attr.type,
7015 slot->key.data,
7016 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007019
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007021}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007022
7023
7024
7025/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007026/* Key agreement */
7027/****************************************************************/
7028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7030 const uint8_t *key_buffer,
7031 size_t key_buffer_size,
7032 psa_algorithm_t alg,
7033 const uint8_t *peer_key,
7034 size_t peer_key_length,
7035 uint8_t *shared_secret,
7036 size_t shared_secret_size,
7037 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007038{
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007040#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007041 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7043 key_buffer_size, alg,
7044 peer_key, peer_key_length,
7045 shared_secret,
7046 shared_secret_size,
7047 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007048#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007049
7050#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7051 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007052 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007053 peer_key,
7054 peer_key_length,
7055 key_buffer,
7056 key_buffer_size,
7057 shared_secret,
7058 shared_secret_size,
7059 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007060#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7061
Gilles Peskine01d718c2018-09-18 12:01:02 +02007062 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007063 (void) attributes;
7064 (void) key_buffer;
7065 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007066 (void) peer_key;
7067 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007068 (void) shared_secret;
7069 (void) shared_secret_size;
7070 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007072 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007073}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007074
Aditya Deshpande17845b82022-10-13 17:21:01 +01007075/** Internal function for raw key agreement
7076 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007077 * to the driver's implementation if a driver is present.
7078 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007079 * (psa_key_agreement_raw_builtin).
7080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7082 psa_key_slot_t *private_key,
7083 const uint8_t *peer_key,
7084 size_t peer_key_length,
7085 uint8_t *shared_secret,
7086 size_t shared_secret_size,
7087 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007088{
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7090 return PSA_ERROR_NOT_SUPPORTED;
7091 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007092
7093 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007095 };
7096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 return psa_driver_wrapper_key_agreement(&attributes,
7098 private_key->key.data,
7099 private_key->key.bytes, alg,
7100 peer_key, peer_key_length,
7101 shared_secret,
7102 shared_secret_size,
7103 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007104}
7105
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007106/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007107 * to potentially free embedded data structures and wipe confidential data.
7108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007109static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7110 psa_key_derivation_step_t step,
7111 psa_key_slot_t *private_key,
7112 const uint8_t *peer_key,
7113 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007114{
7115 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007116 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007117 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007119
7120 /* Step 1: run the secret agreement algorithm to generate the shared
7121 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 status = psa_key_agreement_raw_internal(ka_alg,
7123 private_key,
7124 peer_key, peer_key_length,
7125 shared_secret,
7126 sizeof(shared_secret),
7127 &shared_secret_length);
7128 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007131
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007132 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007133 * the shared secret. A shared secret is permitted wherever a key
7134 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 status = psa_key_derivation_input_internal(operation, step,
7136 PSA_KEY_TYPE_DERIVE,
7137 shared_secret,
7138 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007139exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7141 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007142}
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7145 psa_key_derivation_step_t step,
7146 mbedtls_svc_key_id_t private_key,
7147 const uint8_t *peer_key,
7148 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007149{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007151 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007152 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7155 return PSA_ERROR_INVALID_ARGUMENT;
7156 }
Ronald Cron5c522922020-11-14 16:35:34 +01007157 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7159 if (status != PSA_SUCCESS) {
7160 return status;
7161 }
7162 status = psa_key_agreement_internal(operation, step,
7163 slot,
7164 peer_key, peer_key_length);
7165 if (status != PSA_SUCCESS) {
7166 psa_key_derivation_abort(operation);
7167 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007168 /* If a private key has been added as SECRET, we allow the derived
7169 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007171 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007173 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007178}
7179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7181 mbedtls_svc_key_id_t private_key,
7182 const uint8_t *peer_key,
7183 size_t peer_key_length,
7184 uint8_t *output,
7185 size_t output_size,
7186 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007187{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007189 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007190 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007191 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007192
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007194 status = PSA_ERROR_INVALID_ARGUMENT;
7195 goto exit;
7196 }
Ronald Cron5c522922020-11-14 16:35:34 +01007197 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7199 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007202
Gilles Peskine3e561302022-04-14 00:17:15 +02007203 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7204 * for the output size. The PSA specification only guarantees that this
7205 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7206 * but it might be nice to allow smaller buffers if the output fits.
7207 * At the time of writing this comment, with only ECDH implemented,
7208 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7209 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7210 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007211 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7213 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007214 status = PSA_ERROR_BUFFER_TOO_SMALL;
7215 goto exit;
7216 }
7217
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 status = psa_key_agreement_raw_internal(alg, slot,
7219 peer_key, peer_key_length,
7220 output, output_size,
7221 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007222
7223exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007225 /* If an error happens and is not handled properly, the output
7226 * may be used as a key to protect sensitive data. Arrange for such
7227 * a key to be random, which is likely to result in decryption or
7228 * verification errors. This is better than filling the buffer with
7229 * some constant data such as zeros, which would result in the data
7230 * being protected with a reproducible, easily knowable key.
7231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007233 *output_length = output_size;
7234 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007237
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007239}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007240
7241
Gilles Peskine30524eb2020-11-13 17:02:26 +01007242
Gilles Peskine86a440b2018-11-12 18:39:40 +01007243/****************************************************************/
7244/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007245/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007246
Gilles Peskine672a7712023-04-28 21:00:28 +02007247#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7248#include "entropy_poll.h"
7249#endif
7250
Gilles Peskine30524eb2020-11-13 17:02:26 +01007251/** Initialize the PSA random generator.
7252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007253static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007254{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007255#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007257#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7258
Gilles Peskine30524eb2020-11-13 17:02:26 +01007259 /* Set default configuration if
7260 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007262 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 }
7264 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007265 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007269#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7270 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7271 /* The PSA entropy injection feature depends on using NV seed as an entropy
7272 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 mbedtls_entropy_add_source(&rng->entropy,
7274 mbedtls_nv_seed_poll, NULL,
7275 MBEDTLS_ENTROPY_BLOCK_SIZE,
7276 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007277#endif
7278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007280#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007281}
7282
7283/** Deinitialize the PSA random generator.
7284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007285static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007286{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007287#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007289#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7291 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007292#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007293}
7294
7295/** Seed the PSA random generator.
7296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007297static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007298{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007299#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7300 /* Do nothing: the external RNG seeds itself. */
7301 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007303#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007304 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7306 drbg_seed, sizeof(drbg_seed) - 1);
7307 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007308#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007309}
7310
Gilles Peskine449bd832023-01-11 14:50:10 +01007311psa_status_t psa_generate_random(uint8_t *output,
7312 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007313{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007314 GUARD_MODULE_INITIALIZED;
7315
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007316#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7317
7318 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7320 output, output_size,
7321 &output_length);
7322 if (status != PSA_SUCCESS) {
7323 return status;
7324 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007325 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007326 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 if (output_length != output_size) {
7328 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7329 }
7330 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007331
7332#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7333
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007335 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7337 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7338 output_size);
7339 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7340 output, request_size);
7341 if (ret != 0) {
7342 return mbedtls_to_psa_error(ret);
7343 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007344 output_size -= request_size;
7345 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007346 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007349}
7350
Gilles Peskine8814fc42020-12-14 15:33:44 +01007351/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007352 *
7353 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7354 * `psa_generate_random(...)`. The state parameter is ignored since the
7355 * PSA API doesn't support passing an explicit state.
7356 *
7357 * In the non-external case, psa_generate_random() calls an
7358 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7359 * and semantics as mbedtls_psa_get_random(). As an optimization,
7360 * instead of doing this back-and-forth between the PSA API and the
7361 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7362 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007364#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7365int mbedtls_psa_get_random(void *p_rng,
7366 unsigned char *output,
7367 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007368{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007369 /* This function takes a pointer to the RNG state because that's what
7370 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7371 * its own state internally and doesn't let the caller access that state.
7372 * So we just ignore the state parameter, and in practice we'll pass
7373 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007374 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 psa_status_t status = psa_generate_random(output, output_size);
7376 if (status == PSA_SUCCESS) {
7377 return 0;
7378 } else {
7379 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7380 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007381}
7382#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7383
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007384#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007385psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7386 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007387{
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 if (global_data.initialized) {
7389 return PSA_ERROR_NOT_PERMITTED;
7390 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007391
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7393 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7394 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7395 return PSA_ERROR_INVALID_ARGUMENT;
7396 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007399}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007400#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007401
Ronald Cron3772afe2021-02-08 16:10:05 +01007402/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007403 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007404 * \param type The key type
7405 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007406 *
7407 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007408 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007409 * \retval #PSA_ERROR_INVALID_ARGUMENT
7410 * The size in bits of the key is not valid.
7411 * \retval #PSA_ERROR_NOT_SUPPORTED
7412 * The type and/or the size in bits of the key or the combination of
7413 * the two is not supported.
7414 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007415static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007417{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (key_type_is_raw_bytes(type)) {
7421 status = psa_validate_unstructured_key_bit_size(type, bits);
7422 if (status != PSA_SUCCESS) {
7423 return status;
7424 }
7425 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007426#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7428 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7429 return PSA_ERROR_NOT_SUPPORTED;
7430 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007431 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007432 return PSA_ERROR_NOT_SUPPORTED;
7433 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007434
Ronald Cron01b2aba2020-10-05 09:42:02 +02007435 /* Accept only byte-aligned keys, for the same reasons as
7436 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (bits % 8 != 0) {
7438 return PSA_ERROR_NOT_SUPPORTED;
7439 }
7440 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007441#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007442
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007443#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007445 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 return PSA_SUCCESS;
7447 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007448#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007449
Valerio Settia55f0422023-07-10 15:34:41 +02007450#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007451 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007452 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007453 return PSA_ERROR_NOT_SUPPORTED;
7454 }
7455 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007456#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007457 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007459 }
7460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462}
7463
Ronald Cron55ed0592020-10-05 10:30:40 +02007464psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007465 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007467{
7468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007469 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007470
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if ((attributes->domain_parameters == NULL) &&
7472 (attributes->domain_parameters_size != 0)) {
7473 return PSA_ERROR_INVALID_ARGUMENT;
7474 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (key_type_is_raw_bytes(type)) {
7477 status = psa_generate_random(key_buffer, key_buffer_size);
7478 if (status != PSA_SUCCESS) {
7479 return status;
7480 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007481
David Brown12ca5032021-02-11 11:02:00 -07007482#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 if (type == PSA_KEY_TYPE_DES) {
7484 psa_des_set_key_parity(key_buffer, key_buffer_size);
7485 }
David Brown8107e312021-02-12 08:08:46 -07007486#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007488
Valerio Setti76df8c12023-07-11 14:11:28 +02007489#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7491 return mbedtls_psa_rsa_generate_key(attributes,
7492 key_buffer,
7493 key_buffer_size,
7494 key_buffer_length);
7495 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007496#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007497
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7500 return mbedtls_psa_ecp_generate_key(attributes,
7501 key_buffer,
7502 key_buffer_size,
7503 key_buffer_length);
7504 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007505#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007506
Valerio Settia55f0422023-07-10 15:34:41 +02007507#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007508 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7509 return mbedtls_psa_ffdh_generate_key(attributes,
7510 key_buffer,
7511 key_buffer_size,
7512 key_buffer_length);
7513 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007514#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007515 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 (void) key_buffer_length;
7517 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007518 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007521}
Darryl Green0c6575a2018-11-07 16:05:30 +00007522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7524 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007525{
7526 psa_status_t status;
7527 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007528 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007529 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007530
Ronald Cron81709fc2020-11-14 12:10:32 +01007531 *key = MBEDTLS_SVC_KEY_ID_INIT;
7532
Gilles Peskine0f84d622019-09-12 19:03:13 +02007533 /* Reject any attempt to create a zero-length key so that we don't
7534 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (psa_get_key_bits(attributes) == 0) {
7536 return PSA_ERROR_INVALID_ARGUMENT;
7537 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007538
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007539 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7541 return PSA_ERROR_INVALID_ARGUMENT;
7542 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7545 &slot, &driver);
7546 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007547 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 }
Gilles Peskine11792082019-08-06 18:36:36 +02007549
Ronald Cron977c2472020-10-13 08:32:21 +02007550 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307551 * storage ( thus not in the case of generating a key in a secure element
7552 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7553 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 if (slot->key.data == NULL) {
7555 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7556 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007557 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 attributes->core.type, attributes->core.bits);
7559 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007560 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007562
7563 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 attributes->core.type,
7565 attributes->core.bits);
7566 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007567 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 attributes, &key_buffer_size);
7569 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007570 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 }
Ronald Cron977c2472020-10-13 08:32:21 +02007572 }
Ronald Cron977c2472020-10-13 08:32:21 +02007573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7575 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 }
Ronald Cron977c2472020-10-13 08:32:21 +02007578 }
7579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 status = psa_driver_wrapper_generate_key(attributes,
7581 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 if (status != PSA_SUCCESS) {
7584 psa_remove_key_data_from_memory(slot);
7585 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007586
Gilles Peskine11792082019-08-06 18:36:36 +02007587exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 if (status == PSA_SUCCESS) {
7589 status = psa_finish_key_creation(slot, driver, key);
7590 }
7591 if (status != PSA_SUCCESS) {
7592 psa_fail_key_creation(slot, driver);
7593 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007596}
7597
Gilles Peskinee59236f2018-01-27 23:32:46 +01007598/****************************************************************/
7599/* Module setup */
7600/****************************************************************/
7601
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007602#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007603psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 void (* entropy_init)(mbedtls_entropy_context *ctx),
7605 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007606{
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7608 return PSA_ERROR_BAD_STATE;
7609 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007610 global_data.rng.entropy_init = entropy_init;
7611 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007613}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007614#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007615
Gilles Peskine449bd832023-01-11 14:50:10 +01007616void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007617{
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 psa_wipe_all_key_slots();
7619 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7620 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007621 }
7622 /* Wipe all remaining data, including configuration.
7623 * In particular, this sets all state indicator to the value
7624 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007626
7627 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007629}
7630
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007631#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7632/** Recover a transaction that was interrupted by a power failure.
7633 *
7634 * This function is called during initialization, before psa_crypto_init()
7635 * returns. If this function returns a failure status, the initialization
7636 * fails.
7637 */
7638static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007640{
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007642 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7643 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 /* TODO - fall through to the failure case until this
7645 * is implemented.
7646 * https://github.com/ARMmbed/mbed-crypto/issues/218
7647 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007648 default:
7649 /* We found an unsupported transaction in the storage.
7650 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007652 }
7653}
7654#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007657{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007658 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007659
Gilles Peskinec6b69072018-11-20 21:42:52 +01007660 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 if (global_data.initialized != 0) {
7662 return PSA_SUCCESS;
7663 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007664
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007665 /* Init drivers */
7666 status = psa_driver_wrapper_init();
7667 if (status != PSA_SUCCESS) {
7668 goto exit;
7669 }
7670 global_data.drivers_initialized = 1;
7671
Gilles Peskine30524eb2020-11-13 17:02:26 +01007672 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007674 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007675 status = mbedtls_psa_random_seed(&global_data.rng);
7676 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007677 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007679 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007680
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 status = psa_initialize_key_slots();
7682 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007685
Gilles Peskine4b734222019-07-24 15:56:31 +02007686#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 status = psa_crypto_load_transaction();
7688 if (status == PSA_SUCCESS) {
7689 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7690 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007691 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 }
7693 status = psa_crypto_stop_transaction();
7694 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007695 /* There's no transaction to complete. It's all good. */
7696 status = PSA_SUCCESS;
7697 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007698#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007699
Gilles Peskinec6b69072018-11-20 21:42:52 +01007700 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007701 global_data.initialized = 1;
7702
7703exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 if (status != PSA_SUCCESS) {
7705 mbedtls_psa_crypto_free();
7706 }
7707 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007708}
7709
Yanray Wangffc3c482023-07-11 12:01:04 +08007710#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007711psa_status_t psa_crypto_driver_pake_get_password_len(
7712 const psa_crypto_driver_pake_inputs_t *inputs,
7713 size_t *password_len)
7714{
7715 if (inputs->password_len == 0) {
7716 return PSA_ERROR_BAD_STATE;
7717 }
7718
7719 *password_len = inputs->password_len;
7720
7721 return PSA_SUCCESS;
7722}
7723
7724psa_status_t psa_crypto_driver_pake_get_password(
7725 const psa_crypto_driver_pake_inputs_t *inputs,
7726 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7727{
7728 if (inputs->password_len == 0) {
7729 return PSA_ERROR_BAD_STATE;
7730 }
7731
7732 if (buffer_size < inputs->password_len) {
7733 return PSA_ERROR_BUFFER_TOO_SMALL;
7734 }
7735
7736 memcpy(buffer, inputs->password, inputs->password_len);
7737 *buffer_length = inputs->password_len;
7738
7739 return PSA_SUCCESS;
7740}
7741
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007742psa_status_t psa_crypto_driver_pake_get_user_len(
7743 const psa_crypto_driver_pake_inputs_t *inputs,
7744 size_t *user_len)
7745{
7746 if (inputs->user_len == 0) {
7747 return PSA_ERROR_BAD_STATE;
7748 }
7749
7750 *user_len = inputs->user_len;
7751
7752 return PSA_SUCCESS;
7753}
7754
7755psa_status_t psa_crypto_driver_pake_get_user(
7756 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007757 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007758{
7759 if (inputs->user_len == 0) {
7760 return PSA_ERROR_BAD_STATE;
7761 }
7762
Przemek Stekielc0e62502023-03-14 11:49:36 +01007763 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007764 return PSA_ERROR_BUFFER_TOO_SMALL;
7765 }
7766
Przemek Stekielc0e62502023-03-14 11:49:36 +01007767 memcpy(user_id, inputs->user, inputs->user_len);
7768 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007769
7770 return PSA_SUCCESS;
7771}
7772
7773psa_status_t psa_crypto_driver_pake_get_peer_len(
7774 const psa_crypto_driver_pake_inputs_t *inputs,
7775 size_t *peer_len)
7776{
7777 if (inputs->peer_len == 0) {
7778 return PSA_ERROR_BAD_STATE;
7779 }
7780
7781 *peer_len = inputs->peer_len;
7782
7783 return PSA_SUCCESS;
7784}
7785
7786psa_status_t psa_crypto_driver_pake_get_peer(
7787 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007788 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007789{
7790 if (inputs->peer_len == 0) {
7791 return PSA_ERROR_BAD_STATE;
7792 }
7793
Przemek Stekielc0e62502023-03-14 11:49:36 +01007794 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007795 return PSA_ERROR_BUFFER_TOO_SMALL;
7796 }
7797
Przemek Stekielc0e62502023-03-14 11:49:36 +01007798 memcpy(peer_id, inputs->peer, inputs->peer_len);
7799 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007800
7801 return PSA_SUCCESS;
7802}
7803
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007804psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7805 const psa_crypto_driver_pake_inputs_t *inputs,
7806 psa_pake_cipher_suite_t *cipher_suite)
7807{
7808 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7809 return PSA_ERROR_BAD_STATE;
7810 }
7811
7812 *cipher_suite = inputs->cipher_suite;
7813
7814 return PSA_SUCCESS;
7815}
7816
Neil Armstronga7d08c32022-06-01 18:21:20 +02007817psa_status_t psa_pake_setup(
7818 psa_pake_operation_t *operation,
7819 const psa_pake_cipher_suite_t *cipher_suite)
7820{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007821 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007822
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007823 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824 status = PSA_ERROR_BAD_STATE;
7825 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007826 }
7827
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007828 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007830 status = PSA_ERROR_INVALID_ARGUMENT;
7831 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007832 }
7833
Przemek Stekiel51eac532022-12-07 11:04:51 +01007834 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7835
Przemek Stekiele12ed362022-12-21 12:54:46 +01007836 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007837 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7838 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007839 operation->data.inputs.cipher_suite = *cipher_suite;
7840
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007841#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007842 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007843 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007844 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007845
David Horstmann16f01512023-06-14 17:21:07 +01007846 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007847 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007848 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007849#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007850 {
7851 status = PSA_ERROR_NOT_SUPPORTED;
7852 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007853 }
7854
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007855 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7856
Przemek Stekiel51eac532022-12-07 11:04:51 +01007857 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007858exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007859 psa_pake_abort(operation);
7860 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007861}
7862
7863psa_status_t psa_pake_set_password_key(
7864 psa_pake_operation_t *operation,
7865 mbedtls_svc_key_id_t password)
7866{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007867 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007868 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7869 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007870 psa_key_attributes_t attributes;
7871 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007872
Przemek Stekiel51eac532022-12-07 11:04:51 +01007873 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007874 status = PSA_ERROR_BAD_STATE;
7875 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007876 }
7877
Przemek Stekiel6c764412022-11-22 14:05:12 +01007878 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7879 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007880 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007881 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007882 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007883 }
7884
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007885 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007886 .core = slot->attr
7887 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007888
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007889 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007890
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 if (type != PSA_KEY_TYPE_PASSWORD &&
7892 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7893 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007894 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 }
7896
Przemek Stekiel51eac532022-12-07 11:04:51 +01007897 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7898 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007900 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007901 }
7902
7903 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7904 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007905 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007906exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007907 if (status != PSA_SUCCESS) {
7908 psa_pake_abort(operation);
7909 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007910 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007911 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007912}
7913
7914psa_status_t psa_pake_set_user(
7915 psa_pake_operation_t *operation,
7916 const uint8_t *user_id,
7917 size_t user_id_len)
7918{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007920
7921 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007922 status = PSA_ERROR_BAD_STATE;
7923 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007924 }
7925
Przemek Stekiel51eac532022-12-07 11:04:51 +01007926 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007927 status = PSA_ERROR_INVALID_ARGUMENT;
7928 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007929 }
7930
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007931 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007932 status = PSA_ERROR_BAD_STATE;
7933 goto exit;
7934 }
7935
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007936 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7937 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007938 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7939 goto exit;
7940 }
7941
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007942 memcpy(operation->data.inputs.user, user_id, user_id_len);
7943 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007944
7945 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007946exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007947 psa_pake_abort(operation);
7948 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007949}
7950
7951psa_status_t psa_pake_set_peer(
7952 psa_pake_operation_t *operation,
7953 const uint8_t *peer_id,
7954 size_t peer_id_len)
7955{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007956 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007957
7958 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 status = PSA_ERROR_BAD_STATE;
7960 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007961 }
7962
Przemek Stekiel51eac532022-12-07 11:04:51 +01007963 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007964 status = PSA_ERROR_INVALID_ARGUMENT;
7965 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007966 }
7967
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007968 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007969 status = PSA_ERROR_BAD_STATE;
7970 goto exit;
7971 }
7972
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007973 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7974 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007975 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7976 goto exit;
7977 }
7978
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007979 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7980 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007981
7982 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007983exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007984 psa_pake_abort(operation);
7985 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007986}
7987
7988psa_status_t psa_pake_set_role(
7989 psa_pake_operation_t *operation,
7990 psa_pake_role_t role)
7991{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007993
Przemek Stekiel51eac532022-12-07 11:04:51 +01007994 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007995 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007996 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007997 }
7998
Przemek Stekiel09104b82023-03-06 14:11:51 +01007999 switch (operation->alg) {
8000#if defined(PSA_WANT_ALG_JPAKE)
8001 case PSA_ALG_JPAKE:
8002 if (role == PSA_PAKE_ROLE_NONE) {
8003 return PSA_SUCCESS;
8004 }
8005 status = PSA_ERROR_INVALID_ARGUMENT;
8006 break;
8007#endif
8008 default:
8009 (void) role;
8010 status = PSA_ERROR_NOT_SUPPORTED;
8011 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008012 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008013exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008014 psa_pake_abort(operation);
8015 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008016}
8017
David Horstmanne7f21e62023-05-12 18:17:21 +01008018/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008019#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008020static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008021 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008022{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008023 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008024 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008025 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008026
David Horstmann024e5c52023-06-14 15:48:21 +01008027 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008028 is_x1 = (stage->outputs < 1);
8029 } else {
8030 is_x1 = (stage->inputs < 1);
8031 }
8032
David Horstmann74a3d8c2023-06-14 18:28:19 +01008033 key_share_step = is_x1 ?
8034 PSA_JPAKE_X1_STEP_KEY_SHARE :
8035 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008036 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008037 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8038 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8039 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8040 } else {
8041 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008042 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008043 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008044}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008045#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008046
Przemek Stekiel2797d372022-12-22 11:19:22 +01008047static psa_status_t psa_pake_complete_inputs(
8048 psa_pake_operation_t *operation)
8049{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008051 /* Create copy of the inputs on stack as inputs share memory
8052 with the driver context which will be setup by the driver. */
8053 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008054
Przemek Stekielfde11282023-03-13 16:06:09 +01008055 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008056 return PSA_ERROR_BAD_STATE;
8057 }
8058
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008059 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008060 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8061 return PSA_ERROR_BAD_STATE;
8062 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008063 }
8064
Przemek Stekiel18620a32023-01-17 16:34:52 +01008065 /* Clear driver context */
8066 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8067
8068 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008069
8070 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008071 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008072
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008073 /* User and peer are translated to role. */
8074 mbedtls_free(inputs.user);
8075 mbedtls_free(inputs.peer);
8076
Przemek Stekiel2797d372022-12-22 11:19:22 +01008077 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008078#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008079 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008080 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008081 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008082#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008083 {
8084 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008085 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008086 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008087 return status;
8088}
8089
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008090#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008091static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008092 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008093 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008094 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008096 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8097 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8098 step != PSA_PAKE_STEP_ZK_PROOF) {
8099 return PSA_ERROR_INVALID_ARGUMENT;
8100 }
8101
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008102 psa_jpake_computation_stage_t *computation_stage =
8103 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008104
David Horstmann5da95602023-06-08 15:37:12 +01008105 if (computation_stage->round != PSA_JPAKE_FIRST &&
8106 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008107 return PSA_ERROR_BAD_STATE;
8108 }
8109
David Horstmanne7f21e62023-05-12 18:17:21 +01008110 /* Check that the step we are given is the one we were expecting */
8111 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008112 return PSA_ERROR_BAD_STATE;
8113 }
8114
David Horstmanne7f21e62023-05-12 18:17:21 +01008115 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8116 computation_stage->inputs == 0 &&
8117 computation_stage->outputs == 0) {
8118 /* Start of the round, so function decides whether we are inputting
8119 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008120 computation_stage->io_mode = io_mode;
8121 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008122 /* Middle of the round so the mode we are in must match the function
8123 * called by the user */
8124 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008125 }
8126
8127 return PSA_SUCCESS;
8128}
8129
David Horstmanne7f21e62023-05-12 18:17:21 +01008130static psa_status_t psa_jpake_epilogue(
8131 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008132 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008133{
David Horstmanne7f21e62023-05-12 18:17:21 +01008134 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008135 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136
David Horstmanne7f21e62023-05-12 18:17:21 +01008137 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8138 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008139 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008140 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008141 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008142 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008143 }
8144 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008145 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008146 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008147 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008148 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008149 }
8150 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008151 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8152 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008153 /* End of a round, move to the next round */
8154 stage->inputs = 0;
8155 stage->outputs = 0;
8156 stage->round++;
8157 }
8158 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008159 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008160 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008162 return PSA_SUCCESS;
8163}
David Horstmanne7f21e62023-05-12 18:17:21 +01008164
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008165#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008166
Neil Armstronga7d08c32022-06-01 18:21:20 +02008167psa_status_t psa_pake_output(
8168 psa_pake_operation_t *operation,
8169 psa_pake_step_t step,
8170 uint8_t *output,
8171 size_t output_size,
8172 size_t *output_length)
8173{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008174 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008175 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008176 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008177
8178 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008179 status = psa_pake_complete_inputs(operation);
8180 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008181 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008182 }
8183 }
8184
8185 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008186 status = PSA_ERROR_BAD_STATE;
8187 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008188 }
8189
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008190 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008191 status = PSA_ERROR_INVALID_ARGUMENT;
8192 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008193 }
8194
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008196#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008197 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008198 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008199 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008200 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008201 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008202 driver_step = convert_jpake_computation_stage_to_driver_step(
8203 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008205#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008206 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008207 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008208 status = PSA_ERROR_NOT_SUPPORTED;
8209 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008210 }
8211
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008212 status = psa_driver_wrapper_pake_output(operation, driver_step,
8213 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008214
8215 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008216 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 }
8218
8219 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008220#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008222 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008224 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225 }
8226 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008227#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008229 status = PSA_ERROR_NOT_SUPPORTED;
8230 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008231 }
8232
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008233 return PSA_SUCCESS;
8234exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008235 psa_pake_abort(operation);
8236 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008237}
8238
8239psa_status_t psa_pake_input(
8240 psa_pake_operation_t *operation,
8241 psa_pake_step_t step,
8242 const uint8_t *input,
8243 size_t input_length)
8244{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008246 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008247 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8248 operation->primitive,
8249 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008250
8251 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008252 status = psa_pake_complete_inputs(operation);
8253 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008255 }
8256 }
8257
8258 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 status = PSA_ERROR_BAD_STATE;
8260 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008261 }
8262
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008263 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 status = PSA_ERROR_INVALID_ARGUMENT;
8265 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008266 }
8267
Przemek Stekiele12ed362022-12-21 12:54:46 +01008268 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008269#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008271 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008272 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008273 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008275 driver_step = convert_jpake_computation_stage_to_driver_step(
8276 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008278#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008280 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008281 status = PSA_ERROR_NOT_SUPPORTED;
8282 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008283 }
8284
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008285 status = psa_driver_wrapper_pake_input(operation, driver_step,
8286 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287
8288 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008289 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 }
8291
8292 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008293#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008295 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008296 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008297 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008298 }
8299 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008300#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008301 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008302 status = PSA_ERROR_NOT_SUPPORTED;
8303 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008304 }
8305
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008306 return PSA_SUCCESS;
8307exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008308 psa_pake_abort(operation);
8309 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008310}
8311
8312psa_status_t psa_pake_get_implicit_key(
8313 psa_pake_operation_t *operation,
8314 psa_key_derivation_operation_t *output)
8315{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008317 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008318 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008319 size_t shared_key_len = 0;
8320
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008321 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008322 status = PSA_ERROR_BAD_STATE;
8323 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008324 }
8325
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008326#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008328 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008329 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008330 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008331 status = PSA_ERROR_BAD_STATE;
8332 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008334 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008335#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008336 {
8337 status = PSA_ERROR_NOT_SUPPORTED;
8338 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 }
8340
Przemek Stekiel0c781802022-11-29 14:53:13 +01008341 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8342 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008343 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008344 &shared_key_len);
8345
8346 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008347 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008348 }
8349
8350 status = psa_key_derivation_input_bytes(output,
8351 PSA_KEY_DERIVATION_INPUT_SECRET,
8352 shared_key,
8353 shared_key_len);
8354
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008355 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008356exit:
8357 abort_status = psa_pake_abort(operation);
8358 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008359}
8360
8361psa_status_t psa_pake_abort(
8362 psa_pake_operation_t *operation)
8363{
Przemek Stekield69dca92023-01-31 19:59:20 +01008364 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008365
Przemek Stekield69dca92023-01-31 19:59:20 +01008366 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008367 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008368 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008369
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008370 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8371 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008372 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008373 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008374 }
8375 if (operation->data.inputs.user != NULL) {
8376 mbedtls_free(operation->data.inputs.user);
8377 }
8378 if (operation->data.inputs.peer != NULL) {
8379 mbedtls_free(operation->data.inputs.peer);
8380 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008381 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008382 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008383
Przemek Stekield69dca92023-01-31 19:59:20 +01008384 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008385}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008386#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008387
Gilles Peskinee59236f2018-01-27 23:32:46 +01008388#endif /* MBEDTLS_PSA_CRYPTO_C */