blob: 2ada2eb7203fe177000867b93140d02ee2f18b20 [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"
Xiaokang Qiane9c39c42023-08-09 08:50:19 +000036#include "psa_crypto_driver_wrappers.h"
Xiaokang Qianfe9666b2023-09-11 10:36:20 +000037#include "psa_crypto_driver_wrappers_no_static.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010039#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010040#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010041#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010042#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010043#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020044#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020045#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020046#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010047#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010048/* Include internal declarations that are useful for implementing persistently
49 * stored keys. */
50#include "psa_crypto_storage.h"
51
Gilles Peskineb2b64d32020-12-14 16:43:58 +010052#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010053
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include <stdlib.h>
55#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010057
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010058#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020063#include "mbedtls/chacha20.h"
64#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/cipher.h"
66#include "mbedtls/ccm.h"
67#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010068#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010076#include "mbedtls/md.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000078#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020086#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010087
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
90 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020091#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020092#endif
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/****************************************************************/
95/* Global data, support functions and library management */
96/****************************************************************/
97
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200101}
102
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100103/* Values for psa_global_data_t::rng_state */
104#define RNG_NOT_INITIALIZED 0
105#define RNG_INITIALIZED 1
106#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100109 uint8_t initialized;
110 uint8_t rng_state;
111 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100112 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113} psa_global_data_t;
114
115static psa_global_data_t global_data;
116
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100117#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
118mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
119 &global_data.rng.drbg;
120#endif
121
itayzafrir0adf0fc2018-09-06 16:24:41 +0300122#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (global_data.initialized == 0) \
124 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100126int psa_can_do_hash(psa_algorithm_t hash_alg)
127{
128 (void) hash_alg;
129 return global_data.drivers_initialized;
130}
Valerio Settia55f0422023-07-10 15:34:41 +0200131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200132 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200133 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200134static int psa_is_dh_key_size_valid(size_t bits)
135{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200136 if (bits != 2048 && bits != 3072 && bits != 4096 &&
137 bits != 6144 && bits != 8192) {
138 return 0;
139 }
140
141 return 1;
142}
Valerio Settia55f0422023-07-10 15:34:41 +0200143#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200144 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200145 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100149 /* Mbed TLS error codes can combine a high-level error code and a
150 * low-level error code. The low-level error usually reflects the
151 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 int low_level_ret = -(-ret & 0x007f);
153 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100154 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100156
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100157#if defined(MBEDTLS_AES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100158 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
159 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800160 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100162 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
163 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100164#endif
165
166#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
168 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
169 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
170 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
171 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200173 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200175 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100177#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200178
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100179#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000180 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100183#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100184
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100185#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100191
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100192#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100195#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200196
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100197#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100202#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200203
Dave Rodgman509b5672023-08-16 19:26:23 +0100204#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100219#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100234#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskinee59236f2018-01-27 23:32:46 +0100239 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
240 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
241 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100244#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200247 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100251#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100264#endif
265
Dave Rodgmandea266f2023-08-31 11:52:43 +0100266#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100274 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100276#endif
277#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100278
Dave Rodgman509b5672023-08-16 19:26:23 +0100279#if defined(MBEDTLS_BIGNUM_C)
280#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100283#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100298#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100299
Dave Rodgman509b5672023-08-16 19:26:23 +0100300#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
304 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100306#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
307 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100310#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
312 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
317 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 case MBEDTLS_ERR_PK_INVALID_ALG:
322 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
323 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200327 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100329#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100330
Gilles Peskineff2d2002019-05-06 15:26:23 +0200331 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200333 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200335
Dave Rodgman509b5672023-08-16 19:26:23 +0100336#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
346 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100354#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200355
Dave Rodgman1dab4452023-09-01 09:59:51 +0100356#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300357 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300358 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300360 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
365 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300367 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100369 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100371
Dave Rodgman509b5672023-08-16 19:26:23 +0100372#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000373 case MBEDTLS_ERR_ECP_IN_PROGRESS:
374 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100375#endif
376#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000377
Janos Follatha13b9052019-11-22 12:48:59 +0000378 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300380
Gilles Peskinee59236f2018-01-27 23:32:46 +0100381 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100383 }
384}
385
Paul Elliottdc42ca82023-02-24 18:11:59 +0000386/**
387 * \brief For output buffers which contain "tags"
388 * (outputs that may be checked for validity like
389 * hashes, MACs and signatures), fill the unused
390 * part of the output buffer (the whole buffer on
391 * error, the trailing part on success) with
392 * something that isn't a valid tag (barring an
393 * attack on the tag and deliberately-crafted
394 * input), in case the caller doesn't check the
395 * return status properly.
396 *
397 * \param output_buffer Pointer to buffer to wipe. May not be NULL
398 * unless \p output_buffer_size is zero.
399 * \param status Status of function called to generate
400 * output_buffer originally
401 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
402 * could be NULL.
403 * \param output_buffer_length Length of data written to output_buffer, must be
404 * less than \p output_buffer_size
405 */
406static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000407 size_t output_buffer_size, size_t output_buffer_length)
408{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000409 size_t offset = 0;
410
411 if (output_buffer_size == 0) {
412 /* If output_buffer_size is 0 then we have nothing to do. We must not
413 call memset because output_buffer may be NULL in this case */
414 return;
415 }
416
417 if (status == PSA_SUCCESS) {
418 offset = output_buffer_length;
419 }
420
421 memset(output_buffer + offset, '!', output_buffer_size - offset);
422}
423
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200424
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200425
426
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427/****************************************************************/
428/* Key management */
429/****************************************************************/
430
Valerio Settia9aab1a2023-06-19 13:39:54 +0200431#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200432psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
433 size_t *bits)
434{
435 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200436#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200437 case MBEDTLS_ECP_DP_SECP192R1:
438 *bits = 192;
439 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100440#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200441#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200442 case MBEDTLS_ECP_DP_SECP224R1:
443 *bits = 224;
444 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100445#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200446#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200447 case MBEDTLS_ECP_DP_SECP256R1:
448 *bits = 256;
449 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100450#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200451#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200452 case MBEDTLS_ECP_DP_SECP384R1:
453 *bits = 384;
454 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100455#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200456#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200457 case MBEDTLS_ECP_DP_SECP521R1:
458 *bits = 521;
459 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100460#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200461#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200462 case MBEDTLS_ECP_DP_BP256R1:
463 *bits = 256;
464 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100465#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200466#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200467 case MBEDTLS_ECP_DP_BP384R1:
468 *bits = 384;
469 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100470#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200471#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200472 case MBEDTLS_ECP_DP_BP512R1:
473 *bits = 512;
474 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100475#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200476#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200477 case MBEDTLS_ECP_DP_CURVE25519:
478 *bits = 255;
479 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100480#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200481#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200482 case MBEDTLS_ECP_DP_SECP192K1:
483 *bits = 192;
484 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100485#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200486#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 case MBEDTLS_ECP_DP_SECP224K1:
488 *bits = 224;
489 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100490#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200491#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200492 case MBEDTLS_ECP_DP_SECP256K1:
493 *bits = 256;
494 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100495#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200496#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200497 case MBEDTLS_ECP_DP_CURVE448:
498 *bits = 448;
499 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100500#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200501 default:
502 *bits = 0;
503 return 0;
504 }
505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
508 size_t bits,
509 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200510{
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100512 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100517#endif
518#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100521#endif
522#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
530#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100533 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (bits_is_sloppy) {
535 return MBEDTLS_ECP_DP_SECP521R1;
536 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100537 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100538#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100539 }
540 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100541
Paul Elliott8ff510a2020-06-02 17:19:28 +0100542 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100544#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100545 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#endif
548#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100549 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#endif
552#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100553 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100556 }
557 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100558
Paul Elliott8ff510a2020-06-02 17:19:28 +0100559 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100561#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100564 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (bits_is_sloppy) {
566 return MBEDTLS_ECP_DP_CURVE25519;
567 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100568 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100569#endif
570#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100571 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100573#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100574 }
575 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100576
Paul Elliott8ff510a2020-06-02 17:19:28 +0100577 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100579#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100580 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100582#endif
583#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100584 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100586#endif
587#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100588 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100590#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100591 }
592 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200593 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100594
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100595 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200597}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200598#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
601 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602{
603 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200607 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200608 case PSA_KEY_TYPE_PASSWORD:
609 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200610 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100611#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (bits != 128 && bits != 192 && bits != 256) {
614 return PSA_ERROR_INVALID_ARGUMENT;
615 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 break;
617#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200618#if defined(PSA_WANT_KEY_TYPE_ARIA)
619 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (bits != 128 && bits != 192 && bits != 256) {
621 return PSA_ERROR_INVALID_ARGUMENT;
622 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200623 break;
624#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100625#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200626 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (bits != 128 && bits != 192 && bits != 256) {
628 return PSA_ERROR_INVALID_ARGUMENT;
629 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 break;
631#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100632#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200633 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (bits != 64 && bits != 128 && bits != 192) {
635 return PSA_ERROR_INVALID_ARGUMENT;
636 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 break;
638#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100639#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200640 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (bits != 256) {
642 return PSA_ERROR_INVALID_ARGUMENT;
643 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200644 break;
645#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200646 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200648 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (bits % 8 != 0) {
650 return PSA_ERROR_INVALID_ARGUMENT;
651 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200654}
655
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100656/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100657 *
Steven Cooremancd640932021-03-08 12:00:27 +0100658 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
659 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100660 *
661 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100662 * \param[in] key_type The key type of the key to be used with the
663 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 *
665 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100667 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100668 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100669 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100670MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100671 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (PSA_ALG_IS_HMAC(algorithm)) {
675 if (key_type == PSA_KEY_TYPE_HMAC) {
676 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100677 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100678 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
681 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
682 * key. */
683 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
684 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
685 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
686 * the block length (larger than 1) for block ciphers. */
687 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
688 return PSA_SUCCESS;
689 }
690 }
691 }
692
693 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100694}
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
697 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200698{
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (slot->key.data != NULL) {
700 return PSA_ERROR_ALREADY_EXISTS;
701 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 slot->key.data = mbedtls_calloc(1, buffer_length);
704 if (slot->key.data == NULL) {
705 return PSA_ERROR_INSUFFICIENT_MEMORY;
706 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200707
Ronald Cronea0f8a62020-11-25 17:52:23 +0100708 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200710}
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
713 const uint8_t *data,
714 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200715{
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 psa_status_t status = psa_allocate_buffer_to_slot(slot,
717 data_length);
718 if (status != PSA_SUCCESS) {
719 return status;
720 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 memcpy(slot->key.data, data, data_length);
723 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200724}
725
Ronald Crona0fe59f2020-11-29 11:14:53 +0100726psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100727 const psa_key_attributes_t *attributes,
728 const uint8_t *data, size_t data_length,
729 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
733 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200735 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (data_length == 0) {
737 return PSA_ERROR_NOT_SUPPORTED;
738 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (key_type_is_raw_bytes(type)) {
741 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
744 *bits);
745 if (status != PSA_SUCCESS) {
746 return status;
747 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200748
Ronald Crondd04d422020-11-28 15:14:42 +0100749 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100751 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return PSA_SUCCESS;
755 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100758 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200759 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100760 return PSA_ERROR_INVALID_ARGUMENT;
761 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200762 return mbedtls_psa_ffdh_import_key(attributes,
763 data, data_length,
764 key_buffer, key_buffer_size,
765 key_buffer_length,
766 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100767 }
Valerio Settia55f0422023-07-10 15:34:41 +0200768#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200769 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200770#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
772 if (PSA_KEY_TYPE_IS_ECC(type)) {
773 return mbedtls_psa_ecp_import_key(attributes,
774 data, data_length,
775 key_buffer, key_buffer_size,
776 key_buffer_length,
777 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100778 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200779#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800780 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200781#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
784 if (PSA_KEY_TYPE_IS_RSA(type)) {
785 return mbedtls_psa_rsa_import_key(attributes,
786 data, data_length,
787 key_buffer, key_buffer_size,
788 key_buffer_length,
789 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200790 }
Valerio Settife478902023-07-25 12:27:19 +0200791#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
792 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800793 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100794 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Calculate the intersection of two algorithm usage policies.
800 *
801 * Return 0 (which allows no operation) on incompatibility.
802 */
803static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100804 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100807{
Gilles Peskine549ea862019-05-22 11:45:59 +0200808 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (alg1 == alg2) {
810 return alg1;
811 }
Steven Cooreman03488022021-02-18 12:07:20 +0100812 /* If the policies are from the same hash-and-sign family, check
813 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
815 PSA_ALG_IS_SIGN_HASH(alg2) &&
816 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
817 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
818 return alg2;
819 }
820 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
821 return alg1;
822 }
Steven Cooreman03488022021-02-18 12:07:20 +0100823 }
824 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100825 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100826 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
828 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
829 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
830 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
831 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100832 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100834 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
836 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
837 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
838 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100839 }
840 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
842 (alg1_len <= alg2_len)) {
843 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100844 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
846 (alg2_len <= alg1_len)) {
847 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100848 }
849 }
850 /* If the policies are from the same MAC family, check whether one
851 * of them is a minimum-MAC-length policy. Calculate the most
852 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
854 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
855 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100856 /* Validate the combination of key type and algorithm. Since the base
857 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
859 return 0;
860 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861
Steven Cooreman31a876d2021-03-03 20:47:40 +0100862 /* Get the (exact or at-least) output lengths for both sides of the
863 * requested intersection. None of the currently supported algorithms
864 * have an output length dependent on the actual key size, so setting it
865 * to a bogus value of 0 is currently OK.
866 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100867 * Note that for at-least-this-length wildcard algorithms, the output
868 * length is set to the shortest allowed length, which allows us to
869 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
871 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100872 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873
Steven Cooremana1d83222021-02-25 10:20:29 +0100874 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
876 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
877 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100878 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879
880 /* If only one is an at-least-this-length policy, the intersection would
881 * be the other (fixed-length) policy as long as said fixed length is
882 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
884 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100885 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
887 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100889
Steven Cooremancd640932021-03-08 12:00:27 +0100890 /* If none of them are wildcards, check whether they define the same tag
891 * length. This is still possible here when one is default-length and
892 * the other specific-length. Ensure to always return the
893 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (alg1_len == alg2_len) {
895 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
896 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 }
898 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100900}
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902static int psa_key_algorithm_permits(psa_key_type_t key_type,
903 psa_algorithm_t policy_alg,
904 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200905{
Gilles Peskine549ea862019-05-22 11:45:59 +0200906 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (requested_alg == policy_alg) {
908 return 1;
909 }
Steven Cooreman03488022021-02-18 12:07:20 +0100910 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
911 * and requested_alg is the same hash-and-sign family with any hash,
912 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
914 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
915 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
916 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100917 }
918 /* If policy_alg is a wildcard AEAD algorithm of the same base as
919 * the requested algorithm, check the requested tag length to be
920 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (PSA_ALG_IS_AEAD(policy_alg) &&
922 PSA_ALG_IS_AEAD(requested_alg) &&
923 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
924 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
925 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
926 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
927 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100928 }
Steven Cooremancd640932021-03-08 12:00:27 +0100929 /* If policy_alg is a MAC algorithm of the same base as the requested
930 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (PSA_ALG_IS_MAC(policy_alg) &&
932 PSA_ALG_IS_MAC(requested_alg) &&
933 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
934 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100935 /* Validate the combination of key type and algorithm. Since the policy
936 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
938 return 0;
939 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100940
Steven Cooreman31a876d2021-03-03 20:47:40 +0100941 /* Get both the requested output length for the algorithm which is to be
942 * verified, and the default output length for the base algorithm.
943 * Note that none of the currently supported algorithms have an output
944 * length dependent on actual key size, so setting it to a bogus value
945 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100946 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100948 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 key_type, 0,
950 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100951
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 /* If the policy is default-length, only allow an algorithm with
953 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
955 return requested_output_length == default_output_length;
956 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957
958 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100959 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
961 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
962 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100963 }
964
Steven Cooremancd640932021-03-08 12:00:27 +0100965 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
966 * check for the requested MAC length to be equal to or longer than the
967 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
969 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
970 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100971 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200972 }
Steven Cooremance48e852020-10-05 16:02:45 +0200973 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200974 * a key derivation with that key agreement should also be allowed. This
975 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
977 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
978 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
979 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200980 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200983}
984
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985/** Test whether a policy permits an algorithm.
986 *
987 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100988 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100989 * \note This function requires providing the key type for which the policy is
990 * being validated, since some algorithm policy definitions (e.g. MAC)
991 * have different properties depending on what kind of cipher it is
992 * combined with.
993 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994 * \retval PSA_SUCCESS When \p alg is a specific algorithm
995 * allowed by the \p policy.
996 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
997 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
998 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1001 psa_key_type_t key_type,
1002 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001003{
Steven Cooremand788fab2021-02-25 11:29:17 +01001004 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (alg == 0) {
1006 return PSA_ERROR_INVALID_ARGUMENT;
1007 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001008
Steven Cooreman7e39f052021-02-23 14:18:32 +01001009 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (PSA_ALG_IS_WILDCARD(alg)) {
1011 return PSA_ERROR_INVALID_ARGUMENT;
1012 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1015 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1016 return PSA_SUCCESS;
1017 } else {
1018 return PSA_ERROR_NOT_PERMITTED;
1019 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020}
1021
Gilles Peskinef603c712019-01-19 13:40:11 +01001022/** Restrict a key policy based on a constraint.
1023 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001024 * \note This function requires providing the key type for which the policy is
1025 * being restricted, since some algorithm policy definitions (e.g. MAC)
1026 * have different properties depending on what kind of cipher it is
1027 * combined with.
1028 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001029 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001030 * \param[in,out] policy The policy to restrict.
1031 * \param[in] constraint The policy constraint to apply.
1032 *
1033 * \retval #PSA_SUCCESS
1034 * \c *policy contains the intersection of the original value of
1035 * \c *policy and \c *constraint.
1036 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001037 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 * \c *policy is unchanged.
1039 */
1040static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001041 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001042 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001044{
1045 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1047 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001048 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1050 constraint->alg2);
1051 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1052 return PSA_ERROR_INVALID_ARGUMENT;
1053 }
1054 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1055 return PSA_ERROR_INVALID_ARGUMENT;
1056 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001057 policy->usage &= constraint->usage;
1058 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001059 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001061}
1062
Przemek Stekiel061f6942022-11-30 10:51:35 +01001063/** Get the description of a key given its identifier and policy constraints
1064 * and lock it.
1065 *
1066 * The key must have allow all the usage flags set in \p usage. If \p alg is
1067 * nonzero, the key must allow operations with this algorithm. If \p alg is
1068 * zero, the algorithm is not checked.
1069 *
1070 * In case of a persistent key, the function loads the description of the key
1071 * into a key slot if not already done.
1072 *
1073 * On success, the returned key slot is locked. It is the responsibility of
1074 * the caller to unlock the key slot when it does not access it anymore.
1075 */
1076static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001077 mbedtls_svc_key_id_t key,
1078 psa_key_slot_t **p_slot,
1079 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001083 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 status = psa_get_and_lock_key_slot(key, p_slot);
1086 if (status != PSA_SUCCESS) {
1087 return status;
1088 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001089 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001090
1091 /* Enforce that usage policy for the key slot contains all the flags
1092 * required by the usage parameter. There is one exception: public
1093 * keys can always be exported, so we treat public key objects as
1094 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001096 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001100 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001101 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001102 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001103
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001104 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (alg != 0) {
1106 status = psa_key_policy_permits(&slot->attr.policy,
1107 slot->attr.type,
1108 alg);
1109 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001110 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001112 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001115
1116error:
1117 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001121}
Darryl Green940d72c2018-07-13 13:18:51 +01001122
Ronald Cron5c522922020-11-14 16:35:34 +01001123/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001124 *
1125 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001126 * available, as opposed to a key in a secure element and/or to be used
1127 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001128 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001129 * This is a temporary function that may be used instead of
1130 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1131 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001132 *
Ronald Cron5c522922020-11-14 16:35:34 +01001133 * On success, the returned key slot is locked. It is the responsibility of the
1134 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001135 */
Ronald Cron5c522922020-11-14 16:35:34 +01001136static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1137 mbedtls_svc_key_id_t key,
1138 psa_key_slot_t **p_slot,
1139 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141{
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1143 usage, alg);
1144 if (status != PSA_SUCCESS) {
1145 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1149 psa_unlock_key_slot(*p_slot);
1150 *p_slot = NULL;
1151 return PSA_ERROR_NOT_SUPPORTED;
1152 }
1153
1154 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001155}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001158{
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001160 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001162
Ronald Cronea0f8a62020-11-25 17:52:23 +01001163 slot->key.data = NULL;
1164 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001167}
1168
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001169/** Completely wipe a slot in memory, including its policy.
1170 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001172{
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 /*
1176 * As the return error code may not be handled in case of multiple errors,
1177 * do our best to report an unexpected lock counter. Assert with
1178 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1179 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1180 * function is called as part of the execution of a test suite, the
1181 * execution of the test suite is stopped in error if the assertion fails.
1182 */
1183 if (slot->lock_count != 1) {
1184 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001185 status = PSA_ERROR_CORRUPTION_DETECTED;
1186 }
1187
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001188 /* Multipart operations may still be using the key. This is safe
1189 * because all multipart operation objects are independent from
1190 * the key slot: if they need to access the key after the setup
1191 * phase, they have a copy of the key. Note that this means that
1192 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001193 /* At this point, key material and other type-specific content has
1194 * been wiped. Clear remaining metadata. We can call memset and not
1195 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 memset(slot, 0, sizeof(*slot));
1197 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001198}
1199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001202 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001203 psa_status_t status; /* status of the last operation */
1204 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1206 psa_se_drv_table_entry_t *driver;
1207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_svc_key_id_is_null(key)) {
1210 return PSA_SUCCESS;
1211 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001212
Ronald Cronf2911112020-10-29 17:51:10 +01001213 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001214 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001215 * key, this will load the key description from persistent memory if not
1216 * done yet. We cannot avoid this loading as without it we don't know if
1217 * the key is operated by an SE or not and this information is needed by
1218 * the current implementation.
1219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 status = psa_get_and_lock_key_slot(key, &slot);
1221 if (status != PSA_SUCCESS) {
1222 return status;
1223 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001224
Ronald Cronf2911112020-10-29 17:51:10 +01001225 /*
1226 * If the key slot containing the key description is under access by the
1227 * library (apart from the present access), the key cannot be destroyed
1228 * yet. For the time being, just return in error. Eventually (to be
1229 * implemented), the key should be destroyed when all accesses have
1230 * stopped.
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (slot->lock_count > 1) {
1233 psa_unlock_key_slot(slot);
1234 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001238 /* Refuse the destruction of a read-only key (which may or may not work
1239 * if we attempt it, depending on whether the key is merely read-only
1240 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001241 * Just do the best we can, which is to wipe the copy in memory
1242 * (done in this function's cleanup code). */
1243 overall_status = PSA_ERROR_NOT_PERMITTED;
1244 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001245 }
1246
Gilles Peskine354f7672019-07-12 23:46:38 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1249 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001250 /* For a key in a secure element, we need to do three things:
1251 * remove the key file in internal storage, destroy the
1252 * key inside the secure element, and update the driver's
1253 * persistent data. Start a transaction that will encompass these
1254 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001258 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 status = psa_crypto_save_transaction();
1260 if (status != PSA_SUCCESS) {
1261 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 /* We should still try to destroy the key in the secure
1263 * element and the key metadata in storage. This is especially
1264 * important if the error is that the storage is full.
1265 * But how to do it exactly without risking an inconsistent
1266 * state after a reset?
1267 * https://github.com/ARMmbed/mbed-crypto/issues/215
1268 */
1269 overall_status = status;
1270 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 }
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 status = psa_destroy_se_key(driver,
1274 psa_key_slot_get_slot_number(slot));
1275 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001276 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001278 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1280
Darryl Greend49a4992018-06-18 17:27:26 +01001281#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1283 status = psa_destroy_persistent_key(slot->attr.id);
1284 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001285 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001288 /* TODO: other slots may have a copy of the same key. We should
1289 * invalidate them.
1290 * https://github.com/ARMmbed/mbed-crypto/issues/214
1291 */
Darryl Greend49a4992018-06-18 17:27:26 +01001292 }
1293#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001294
Gilles Peskinefc762652019-07-22 19:30:34 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (driver != NULL) {
1297 status = psa_save_se_persistent_data(driver);
1298 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 }
1301 status = psa_crypto_stop_transaction();
1302 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001305 }
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001310 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 }
1314 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315}
1316
Valerio Settib2bcedb2023-07-10 11:24:00 +02001317#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001318 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001319static psa_status_t psa_get_rsa_public_exponent(
1320 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001322{
1323 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001325 uint8_t *buffer = NULL;
1326 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1330 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 }
1333 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001334 /* It's the default value, which is reported as an empty string,
1335 * so there's nothing to do. */
1336 goto exit;
1337 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 buflen = mbedtls_mpi_size(&mpi);
1340 buffer = mbedtls_calloc(1, buflen);
1341 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001342 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1343 goto exit;
1344 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1346 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001349 attributes->domain_parameters = buffer;
1350 attributes->domain_parameters_size = buflen;
1351
1352exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 mbedtls_mpi_free(&mpi);
1354 if (ret != 0) {
1355 mbedtls_free(buffer);
1356 }
1357 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001358}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001359#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001360 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001361
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001362/** Retrieve all the publicly-accessible attributes of a key.
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1365 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001368 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001369 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1374 if (status != PSA_SUCCESS) {
1375 return status;
1376 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001377
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001378 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1380 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381
1382#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1384 psa_set_key_slot_number(attributes,
1385 psa_key_slot_get_slot_number(slot));
1386 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001387#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001390#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1391 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001392 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001393 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001394 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001395 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001396 * is not yet implemented.
1397 * https://github.com/ARMmbed/mbed-crypto/issues/216
1398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001400 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001401
Ronald Cron90857082020-11-25 14:58:33 +01001402 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 slot->attr.type,
1404 slot->key.data,
1405 slot->key.bytes,
1406 &rsa);
1407 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001408 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 status = psa_get_rsa_public_exponent(rsa,
1412 attributes);
1413 mbedtls_rsa_free(rsa);
1414 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001415 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001416 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001417#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1418 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001420 default:
1421 /* Nothing else to do. */
1422 break;
1423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (status != PSA_SUCCESS) {
1426 psa_reset_key_attributes(attributes);
1427 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
1433
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1435psa_status_t psa_get_key_slot_number(
1436 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438{
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001440 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return PSA_SUCCESS;
1442 } else {
1443 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001444 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445}
1446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1449 size_t key_buffer_size,
1450 uint8_t *data,
1451 size_t data_size,
1452 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001453{
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (key_buffer_size > data_size) {
1455 return PSA_ERROR_BUFFER_TOO_SMALL;
1456 }
1457 memcpy(data, key_buffer, key_buffer_size);
1458 memset(data + key_buffer_size, 0,
1459 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001462}
1463
Ronald Cron7285cda2020-11-26 14:46:37 +01001464psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 const psa_key_attributes_t *attributes,
1466 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468{
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 psa_key_type_t type = attributes->core.type;
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (key_type_is_raw_bytes(type) ||
1472 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001473 PSA_KEY_TYPE_IS_ECC(type) ||
1474 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return psa_export_key_buffer_internal(
1476 key_buffer, key_buffer_size,
1477 data, data_size, data_length);
1478 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479 /* This shouldn't happen in the reference implementation, but
1480 it is valid for a special-purpose implementation to omit
1481 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483 }
1484}
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1487 uint8_t *data,
1488 size_t data_size,
1489 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490{
1491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1492 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1493 psa_key_slot_t *slot;
1494
Ronald Crone907e552021-01-18 13:22:38 +01001495 /* Reject a zero-length output buffer now, since this can never be a
1496 * valid key representation. This way we know that data must be a valid
1497 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (data_size == 0) {
1499 return PSA_ERROR_BUFFER_TOO_SMALL;
1500 }
Ronald Crone907e552021-01-18 13:22:38 +01001501
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 /* Set the key to empty now, so that even when there are errors, we always
1503 * set data_length to a value between 0 and data_size. On error, setting
1504 * the key to empty is a good choice because an empty key representation is
1505 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001506 *data_length = 0;
1507
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 /* Export requires the EXPORT flag. There is an exception for public keys,
1509 * which don't require any flag, but
1510 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1513 PSA_KEY_USAGE_EXPORT, 0);
1514 if (status != PSA_SUCCESS) {
1515 return status;
1516 }
mohammad160306e79202018-03-28 13:17:44 +03001517
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 psa_key_attributes_t attributes = {
1519 .core = slot->attr
1520 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 status = psa_driver_wrapper_export_key(&attributes,
1522 slot->key.data, slot->key.bytes,
1523 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528}
1529
Ronald Cron7285cda2020-11-26 14:46:37 +01001530psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001531 const psa_key_attributes_t *attributes,
1532 const uint8_t *key_buffer,
1533 size_t key_buffer_size,
1534 uint8_t *data,
1535 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001537{
Ronald Crond18b5f82020-11-25 16:46:05 +01001538 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001539
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001540 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001541 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1542 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001543 /* Exporting public -> public */
1544 return psa_export_key_buffer_internal(
1545 key_buffer, key_buffer_size,
1546 data, data_size, data_length);
1547 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1550 return mbedtls_psa_rsa_export_public_key(attributes,
1551 key_buffer,
1552 key_buffer_size,
1553 data,
1554 data_size,
1555 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001556#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001557 /* We don't know how to convert a private RSA key to public. */
1558 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001560 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001561 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001563 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1564 return mbedtls_psa_ecp_export_public_key(attributes,
1565 key_buffer,
1566 key_buffer_size,
1567 data,
1568 data_size,
1569 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001571 /* We don't know how to convert a private ECC key to public */
1572 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001574 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001575 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001578 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001579 key_buffer,
1580 key_buffer_size,
1581 data, data_size,
1582 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001583#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001584 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001585#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001586 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001588 (void) key_buffer;
1589 (void) key_buffer_size;
1590 (void) data;
1591 (void) data_size;
1592 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595}
Ronald Crond18b5f82020-11-25 16:46:05 +01001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1598 uint8_t *data,
1599 size_t data_size,
1600 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001601{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001603 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001604 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001605 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001606
Ronald Crone907e552021-01-18 13:22:38 +01001607 /* Reject a zero-length output buffer now, since this can never be a
1608 * valid key representation. This way we know that data must be a valid
1609 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (data_size == 0) {
1611 return PSA_ERROR_BUFFER_TOO_SMALL;
1612 }
Ronald Crone907e552021-01-18 13:22:38 +01001613
Darryl Greendd8fb772018-11-07 16:00:44 +00001614 /* Set the key to empty now, so that even when there are errors, we always
1615 * set data_length to a value between 0 and data_size. On error, setting
1616 * the key to empty is a good choice because an empty key representation is
1617 * unlikely to be accepted anywhere. */
1618 *data_length = 0;
1619
1620 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1622 if (status != PSA_SUCCESS) {
1623 return status;
1624 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1627 status = PSA_ERROR_INVALID_ARGUMENT;
1628 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001629 }
1630
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001631 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001632 .core = slot->attr
1633 };
Ronald Cron67227982020-11-26 15:16:05 +01001634 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001635 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001637
1638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001642}
1643
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001644MBEDTLS_STATIC_ASSERT(
1645 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1646 "One or more key attribute flag is listed as both external-only and dual-use")
1647MBEDTLS_STATIC_ASSERT(
1648 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1649 "One or more key attribute flag is listed as both internal-only and dual-use")
1650MBEDTLS_STATIC_ASSERT(
1651 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1652 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate that a key policy is internally well-formed.
1655 *
1656 * This function only rejects invalid policies. It does not validate the
1657 * consistency of the policy with respect to other attributes of the key
1658 * such as the key type.
1659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001661{
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1663 PSA_KEY_USAGE_COPY |
1664 PSA_KEY_USAGE_ENCRYPT |
1665 PSA_KEY_USAGE_DECRYPT |
1666 PSA_KEY_USAGE_SIGN_MESSAGE |
1667 PSA_KEY_USAGE_VERIFY_MESSAGE |
1668 PSA_KEY_USAGE_SIGN_HASH |
1669 PSA_KEY_USAGE_VERIFY_HASH |
1670 PSA_KEY_USAGE_VERIFY_DERIVATION |
1671 PSA_KEY_USAGE_DERIVE)) != 0) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676}
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate the internal consistency of key attributes.
1679 *
1680 * This function only rejects invalid attribute values. If does not
1681 * validate the consistency of the attributes with any key data that may
1682 * be involved in the creation of the key.
1683 *
1684 * Call this function early in the key creation process.
1685 *
1686 * \param[in] attributes Key attributes for the new key.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
1689 *
1690 */
1691static psa_status_t psa_validate_key_attributes(
1692 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001694{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1697 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 status = psa_validate_key_location(lifetime, p_drv);
1700 if (status != PSA_SUCCESS) {
1701 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 status = psa_validate_key_persistence(lifetime);
1705 if (status != PSA_SUCCESS) {
1706 return status;
1707 }
1708
1709 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1710 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
1713 } else {
1714 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1715 return PSA_ERROR_INVALID_ARGUMENT;
1716 }
1717 }
1718
1719 status = psa_validate_key_policy(&attributes->core.policy);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
1724 /* Refuse to create overly large keys.
1725 * Note that this doesn't trigger on import if the attributes don't
1726 * explicitly specify a size (so psa_get_key_bits returns 0), so
1727 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1729 return PSA_ERROR_NOT_SUPPORTED;
1730 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1734 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1735 return PSA_ERROR_INVALID_ARGUMENT;
1736 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001739}
1740
Gilles Peskine4747d192019-04-17 15:05:45 +02001741/** Prepare a key slot to receive key material.
1742 *
1743 * This function allocates a key slot and sets its metadata.
1744 *
1745 * If this function fails, call psa_fail_key_creation().
1746 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001747 * This function is intended to be used as follows:
1748 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001749 * it with the specified attributes, and in case of a volatile key assign it
1750 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 * -# Populate the slot with the key material.
1752 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1753 * In case of failure at any step, stop the sequence and call
1754 * psa_fail_key_creation().
1755 *
Ronald Cron5c522922020-11-14 16:35:34 +01001756 * On success, the key slot is locked. It is the responsibility of the caller
1757 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001758 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001759 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[out] p_slot On success, a pointer to the prepared slot.
1762 * \param[out] p_drv On any return, the driver for the key, if any.
1763 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 *
1765 * \retval #PSA_SUCCESS
1766 * The key slot is ready to receive key material.
1767 * \return If this function fails, the key slot is an invalid state.
1768 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 */
1770static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001771 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001775{
1776 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001777 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_key_slot_t *slot;
1779
Gilles Peskinedf179142019-07-15 22:02:14 +02001780 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 *p_drv = NULL;
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 status = psa_validate_key_attributes(attributes, p_drv);
1784 if (status != PSA_SUCCESS) {
1785 return status;
1786 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1789 if (status != PSA_SUCCESS) {
1790 return status;
1791 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 slot = *p_slot;
1793
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001794 /* We're storing the declared bit-size of the key. It's up to each
1795 * creation mechanism to verify that this information is correct.
1796 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001798 * is optional (import, copy). In case of a volatile key, assign it the
1799 * volatile key identifier associated to the slot returned to contain its
1800 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001801
1802 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001804#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1805 slot->attr.id = volatile_key_id;
1806#else
1807 slot->attr.id.key_id = volatile_key_id;
1808#endif
1809 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001810
Gilles Peskine91e8c332019-08-02 19:19:39 +02001811 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001812 * external-only flags, query `attributes`. Thanks to the check
1813 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001814 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001815 * may have set. */
1816 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001817
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001819 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001821 * create the key file in internal storage, create the
1822 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * persistent data. This is done by starting a transaction that will
1824 * encompass these three actions.
1825 * For registering a volatile key, we just need to find an appropriate
1826 * slot number inside the SE. Since the key is designated volatile, creating
1827 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001828 /* The first thing to do is to find a slot number for the new key.
1829 * We save the slot number in persistent storage as part of the
1830 * transaction data. It will be needed to recover if the power
1831 * fails during the key creation process, to clean up on the secure
1832 * element side after restarting. Obtaining a slot number from the
1833 * secure element driver updates its persistent state, but we do not yet
1834 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001835 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001837 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1839 &slot_number);
1840 if (status != PSA_SUCCESS) {
1841 return status;
1842 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1845 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 status = psa_crypto_save_transaction();
1850 if (status != PSA_SUCCESS) {
1851 (void) psa_crypto_stop_transaction();
1852 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001853 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001854 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001855
1856 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001858 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001864#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001867}
Gilles Peskine4747d192019-04-17 15:05:45 +02001868
1869/** Finalize the creation of a key once its key material has been set.
1870 *
1871 * This entails writing the key to persistent storage.
1872 *
1873 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001874 * See the documentation of psa_start_key_creation() for the intended use
1875 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001876 *
Ronald Cron5c522922020-11-14 16:35:34 +01001877 * If the finalization succeeds, the function unlocks the key slot (it was
1878 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1879 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001880 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 * \param[in,out] slot Pointer to the slot with key material.
1882 * \param[in] driver The secure element driver for the key,
1883 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 * \param[out] key On success, identifier of the key. Note that the
1885 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001886 *
1887 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001888 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1890 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1891 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1892 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1893 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1894 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001895 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001896 * \return If this function fails, the key slot is an invalid state.
1897 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001899static psa_status_t psa_finish_key_creation(
1900 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001901 psa_se_drv_table_entry_t *driver,
1902 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001903{
1904 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001905 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001906 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001907
1908#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001912 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001913 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001915
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001916 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1917 sizeof(data.slot_number),
1918 "Slot number size does not match psa_se_key_data_storage_t");
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1921 status = psa_save_persistent_key(&slot->attr,
1922 (uint8_t *) &data,
1923 sizeof(data));
1924 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001925#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1926 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001927 /* Key material is saved in export representation in the slot, so
1928 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 status = psa_save_persistent_key(&slot->attr,
1930 slot->key.data,
1931 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001932 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001933 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001934#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1935
Gilles Peskinecbaff462019-07-12 23:46:04 +02001936#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001937 /* Finish the transaction for a key creation. This does not
1938 * happen when registering an existing key. Detect this case
1939 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001940 * creation of a persistent key in a secure element requires a transaction,
1941 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (driver != NULL &&
1943 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1944 status = psa_save_se_persistent_data(driver);
1945 if (status != PSA_SUCCESS) {
1946 psa_destroy_persistent_key(slot->attr.id);
1947 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001950 }
1951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001954 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_unlock_key_slot(slot);
1956 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001959 }
Ronald Cron50972942020-11-14 11:28:25 +01001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001962}
1963
1964/** Abort the creation of a key.
1965 *
1966 * You may call this function after calling psa_start_key_creation(),
1967 * or after psa_finish_key_creation() fails. In other circumstances, this
1968 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001969 * See the documentation of psa_start_key_creation() for the intended use
1970 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 * \param[in,out] slot Pointer to the slot with key material.
1973 * \param[in] driver The secure element driver for the key,
1974 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976static void psa_fail_key_creation(psa_key_slot_t *slot,
1977 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001978{
Gilles Peskine011e4282019-06-26 18:34:38 +02001979 (void) driver;
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001984
1985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001986 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001987 * element, and the failure happened later (when saving metadata
1988 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001989 * element.
1990 * https://github.com/ARMmbed/mbed-crypto/issues/217
1991 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001992
Gilles Peskined7729582019-08-05 15:55:54 +02001993 /* Abort the ongoing transaction if any (there may not be one if
1994 * the creation process failed before starting one, or if the
1995 * key creation is a registration of a key in a secure element).
1996 * Earlier functions must already have done what it takes to undo any
1997 * partial creation. All that's left is to update the transaction data
1998 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002003}
2004
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002005/** Validate optional attributes during key creation.
2006 *
2007 * Some key attributes are optional during key creation. If they are
2008 * specified in the attributes structure, check that they are consistent
2009 * with the data in the slot.
2010 *
2011 * This function should be called near the end of key creation, after
2012 * the slot in memory is fully populated but before saving persistent data.
2013 */
2014static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017{
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (attributes->core.type != 0) {
2019 if (attributes->core.type != slot->attr.type) {
2020 return PSA_ERROR_INVALID_ARGUMENT;
2021 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 }
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002025#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2026 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2028 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002029 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002030 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002032
Ronald Cron90857082020-11-25 14:58:33 +01002033 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 slot->attr.type,
2035 slot->key.data,
2036 slot->key.bytes,
2037 &rsa);
2038 if (status != PSA_SUCCESS) {
2039 return status;
2040 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 mbedtls_mpi_init(&actual);
2043 mbedtls_mpi_init(&required);
2044 ret = mbedtls_rsa_export(rsa,
2045 NULL, NULL, NULL, NULL, &actual);
2046 mbedtls_rsa_free(rsa);
2047 mbedtls_free(rsa);
2048 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
2051 ret = mbedtls_mpi_read_binary(&required,
2052 attributes->domain_parameters,
2053 attributes->domain_parameters_size);
2054 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002055 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 }
2057 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 }
2060rsa_exit:
2061 mbedtls_mpi_free(&actual);
2062 mbedtls_mpi_free(&required);
2063 if (ret != 0) {
2064 return mbedtls_to_psa_error(ret);
2065 }
2066 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002067#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2068 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002069 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 }
2073 }
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (attributes->core.bits != 0) {
2076 if (attributes->core.bits != slot->attr.bits) {
2077 return PSA_ERROR_INVALID_ARGUMENT;
2078 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079 }
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082}
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2085 const uint8_t *data,
2086 size_t data_length,
2087 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002088{
2089 psa_status_t status;
2090 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002091 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002092 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302093 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002094
Ronald Cron81709fc2020-11-14 12:10:32 +01002095 *key = MBEDTLS_SVC_KEY_ID_INIT;
2096
Gilles Peskine0f84d622019-09-12 19:03:13 +02002097 /* Reject zero-length symmetric keys (including raw data key objects).
2098 * This also rejects any key which might be encoded as an empty string,
2099 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (data_length == 0) {
2101 return PSA_ERROR_INVALID_ARGUMENT;
2102 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002103
Archana449608b2021-09-08 15:36:05 +05302104 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (data_length > SIZE_MAX / 8) {
2106 return PSA_ERROR_NOT_SUPPORTED;
2107 }
Archana6ed4bda2021-08-04 10:47:15 +05302108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2110 &slot, &driver);
2111 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002115 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302116 * storage ( thus not in the case of importing a key in a secure element
2117 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2118 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (slot->key.data == NULL) {
2120 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302121 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 attributes, data, data_length, &storage_size);
2123 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
Archanad8a83dc2021-06-14 10:04:16 +05302126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_allocate_buffer_to_slot(slot, storage_size);
2128 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002131 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002132
2133 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 status = psa_driver_wrapper_import_key(attributes,
2135 data, data_length,
2136 slot->key.data,
2137 slot->key.bytes,
2138 &slot->key.bytes, &bits);
2139 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002144 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002146 status = PSA_ERROR_INVALID_ARGUMENT;
2147 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002148 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002149
Archana6ed4bda2021-08-04 10:47:15 +05302150 /* Enforce a size limit, and in particular ensure that the bit
2151 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302153 status = PSA_ERROR_NOT_SUPPORTED;
2154 goto exit;
2155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 status = psa_validate_optional_attributes(slot, attributes);
2157 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (status != PSA_SUCCESS) {
2164 psa_fail_key_creation(slot, driver);
2165 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002168}
2169
Gilles Peskined7729582019-08-05 15:55:54 +02002170#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2171psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002173{
2174 psa_status_t status;
2175 psa_key_slot_t *slot = NULL;
2176 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002178
2179 /* Leaving attributes unspecified is not currently supported.
2180 * It could make sense to query the key type and size from the
2181 * secure element, but not all secure elements support this
2182 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2184 return PSA_ERROR_NOT_SUPPORTED;
2185 }
2186 if (psa_get_key_bits(attributes) == 0) {
2187 return PSA_ERROR_NOT_SUPPORTED;
2188 }
Gilles Peskined7729582019-08-05 15:55:54 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2191 &slot, &driver);
2192 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 }
Gilles Peskined7729582019-08-05 15:55:54 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002197
2198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (status != PSA_SUCCESS) {
2200 psa_fail_key_creation(slot, driver);
2201 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002202
Gilles Peskined7729582019-08-05 15:55:54 +02002203 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_close_key(key);
2205 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002206}
2207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2210 const psa_key_attributes_t *specified_attributes,
2211 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002212{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002214 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002215 psa_key_slot_t *source_slot = NULL;
2216 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002218 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302219 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002220
Ronald Cron81709fc2020-11-14 12:10:32 +01002221 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2222
Archana8a180362021-07-05 02:18:48 +05302223 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2225 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 status = psa_validate_optional_attributes(source_slot,
2230 specified_attributes);
2231 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234
Archana9d17bf42021-09-10 06:22:44 +05302235 /* The target key type and number of bits have been validated by
2236 * psa_validate_optional_attributes() to be either equal to zero or
2237 * equal to the ones of the source key. So it is safe to inherit
2238 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302239 * */
Archana9d17bf42021-09-10 06:22:44 +05302240 actual_attributes.core.bits = source_slot->attr.bits;
2241 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302242
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 status = psa_restrict_key_policy(source_slot->attr.type,
2245 &actual_attributes.core.policy,
2246 &source_slot->attr.policy);
2247 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2252 &target_slot, &driver);
2253 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 }
2256 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2257 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302258 /*
Archana9d17bf42021-09-10 06:22:44 +05302259 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302260 * the source key would need to be exported as plaintext and re-imported
2261 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302262 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302263 * appropriate API invocations from the application, if needed.
2264 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002265 status = PSA_ERROR_NOT_SUPPORTED;
2266 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002267 }
Archana8a180362021-07-05 02:18:48 +05302268 /*
2269 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302270 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302271 * - For opaque keys this translates to an invocation of the drivers'
2272 * copy_key entry point through the dispatch layer.
2273 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2275 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2276 &storage_size);
2277 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302278 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 }
Archana374fe5b2021-09-08 15:50:28 +05302280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2282 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302283 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 }
Archana374fe5b2021-09-08 15:50:28 +05302285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 status = psa_driver_wrapper_copy_key(&actual_attributes,
2287 source_slot->key.data,
2288 source_slot->key.bytes,
2289 target_slot->key.data,
2290 target_slot->key.bytes,
2291 &target_slot->key.bytes);
2292 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
2295 } else {
2296 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302297 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 source_slot->key.bytes);
2299 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302300 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 }
Archana8a180362021-07-05 02:18:48 +05302302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (status != PSA_SUCCESS) {
2306 psa_fail_key_creation(target_slot, driver);
2307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002312}
2313
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002314
2315
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002316/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002317/* Message digests */
2318/****************************************************************/
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (operation->id == 0) {
2324 return PSA_SUCCESS;
2325 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002328 operation->id = 0;
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2334 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002338 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002340 status = PSA_ERROR_BAD_STATE;
2341 goto exit;
2342 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002345 status = PSA_ERROR_INVALID_ARGUMENT;
2346 goto exit;
2347 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002349 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2350 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354
2355exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (status != PSA_SUCCESS) {
2357 psa_hash_abort(operation);
2358 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359
2360 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361}
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2364 const uint8_t *input,
2365 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002366{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370 status = PSA_ERROR_BAD_STATE;
2371 goto exit;
2372 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002373
Steven Cooremanc8288352021-03-04 14:02:19 +01002374 /* Don't require hash implementations to behave correctly on a
2375 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (input_length == 0) {
2377 return PSA_SUCCESS;
2378 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002381
2382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (status != PSA_SUCCESS) {
2384 psa_hash_abort(operation);
2385 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388}
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2391 uint8_t *hash,
2392 size_t hash_size,
2393 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002395 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (operation->id == 0) {
2397 return PSA_ERROR_BAD_STATE;
2398 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399
Steven Cooreman1e582352021-02-18 17:24:37 +01002400 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 operation, hash, hash_size, hash_length);
2402 psa_hash_abort(operation);
2403 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404}
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2407 const uint8_t *hash,
2408 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409{
Ronald Cron69a63422021-10-18 09:47:58 +02002410 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002412 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 operation,
2414 actual_hash, sizeof(actual_hash),
2415 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002422 status = PSA_ERROR_INVALID_SIGNATURE;
2423 goto exit;
2424 }
2425
Dave Rodgman78701152023-08-29 14:20:18 +01002426 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002429
2430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2432 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002433 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437}
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439psa_status_t psa_hash_compute(psa_algorithm_t alg,
2440 const uint8_t *input, size_t input_length,
2441 uint8_t *hash, size_t hash_size,
2442 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002443{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002444 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (!PSA_ALG_IS_HASH(alg)) {
2446 return PSA_ERROR_INVALID_ARGUMENT;
2447 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2450 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002451}
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453psa_status_t psa_hash_compare(psa_algorithm_t alg,
2454 const uint8_t *input, size_t input_length,
2455 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002456{
Ronald Cron69a63422021-10-18 09:47:58 +02002457 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002458 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (!PSA_ALG_IS_HASH(alg)) {
2461 return PSA_ERROR_INVALID_ARGUMENT;
2462 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002463
Steven Cooreman1e582352021-02-18 17:24:37 +01002464 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 alg, input, input_length,
2466 actual_hash, sizeof(actual_hash),
2467 &actual_hash_length);
2468 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002469 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 }
2471 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002472 status = PSA_ERROR_INVALID_SIGNATURE;
2473 goto exit;
2474 }
Dave Rodgman78701152023-08-29 14:20:18 +01002475 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002476 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002478
2479exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2481 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002482}
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2485 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002486{
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (source_operation->id == 0 ||
2488 target_operation->id != 0) {
2489 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002490 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2493 target_operation);
2494 if (status != PSA_SUCCESS) {
2495 psa_hash_abort(target_operation);
2496 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002499}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500
2501
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503/* MAC */
2504/****************************************************************/
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002507{
Steven Cooremane6804192021-03-19 18:28:56 +01002508 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (operation->id == 0) {
2510 return PSA_SUCCESS;
2511 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002514 operation->mac_size = 0;
2515 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002516 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002519}
2520
Ronald Cron2dff3b22021-06-17 16:33:22 +02002521static psa_status_t psa_mac_finalize_alg_and_key_validation(
2522 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002523 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 psa_key_type_t key_type = psa_get_key_type(attributes);
2528 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (!PSA_ALG_IS_MAC(alg)) {
2531 return PSA_ERROR_INVALID_ARGUMENT;
2532 }
Steven Cooremane6804192021-03-19 18:28:56 +01002533
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002534 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 status = psa_mac_key_can_do(alg, key_type);
2536 if (status != PSA_SUCCESS) {
2537 return status;
2538 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002539
Steven Cooremand1a68f12021-05-10 11:13:41 +02002540 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002544 /* A very short MAC is too short for security since it can be
2545 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2546 * so we make this our minimum, even though 32 bits is still
2547 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002549 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2552 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002553 /* It's impossible to "truncate" to a larger length than the full length
2554 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002556 }
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002559 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2560 * that is disabled in the compile-time configuration. The result can
2561 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2562 * configuration into account. In this case, force a return of
2563 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2564 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2565 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2566 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2567 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002569 }
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002572}
2573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2575 mbedtls_svc_key_id_t key,
2576 psa_algorithm_t alg,
2577 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002578{
2579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2580 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002581 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002582 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583
2584 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002586 status = PSA_ERROR_BAD_STATE;
2587 goto exit;
2588 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
2590 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 key,
2592 &slot,
2593 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2594 alg);
2595 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002599 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 .core = slot->attr
2601 };
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2604 &operation->mac_size);
2605 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
2609 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002610 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (is_sign) {
2612 status = psa_driver_wrapper_mac_sign_setup(operation,
2613 &attributes,
2614 slot->key.data,
2615 slot->key.bytes,
2616 alg);
2617 } else {
2618 status = psa_driver_wrapper_mac_verify_setup(operation,
2619 &attributes,
2620 slot->key.data,
2621 slot->key.bytes,
2622 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002624
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (status != PSA_SUCCESS) {
2627 psa_mac_abort(operation);
2628 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633}
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2636 mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002638{
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640}
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2643 mbedtls_svc_key_id_t key,
2644 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002645{
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002647}
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2650 const uint8_t *input,
2651 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652{
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (operation->id == 0) {
2654 return PSA_ERROR_BAD_STATE;
2655 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002657 /* Don't require hash implementations to behave correctly on a
2658 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (input_length == 0) {
2660 return PSA_SUCCESS;
2661 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2664 input, input_length);
2665 if (status != PSA_SUCCESS) {
2666 psa_mac_abort(operation);
2667 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670}
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2673 uint8_t *mac,
2674 size_t mac_size,
2675 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002676{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2678 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002689
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002690 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2691 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002693 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002694 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002695 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002698 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002699 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002700 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 status = psa_driver_wrapper_mac_sign_finish(operation,
2703 mac, operation->mac_size,
2704 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002705
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002706exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707 /* In case of success, set the potential excess room in the output buffer
2708 * to an invalid value, to avoid potentially leaking a longer MAC.
2709 * In case of error, set the output length and content to a safe default,
2710 * such that in case the caller misses an error check, the output would be
2711 * an unachievable MAC.
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002714 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002715 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002716 }
mohammad16036df908f2018-04-02 08:34:15 -07002717
Paul Elliottdc42ca82023-02-24 18:11:59 +00002718 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002723}
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2726 const uint8_t *mac,
2727 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002733 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002734 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002735 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002739 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002740 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002744 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002745 }
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 status = psa_driver_wrapper_mac_verify_finish(operation,
2748 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002749
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002750exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754}
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2757 psa_algorithm_t alg,
2758 const uint8_t *input,
2759 size_t input_length,
2760 uint8_t *mac,
2761 size_t mac_size,
2762 size_t *mac_length,
2763 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002764{
2765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002766 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2767 psa_key_slot_t *slot;
2768 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002769 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770
Ronald Cron51131b52021-06-17 17:17:20 +02002771 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 key,
2773 &slot,
2774 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2775 alg);
2776 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002779
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002780 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002781 .core = slot->attr
2782 };
2783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2785 &operation_mac_size);
2786 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002791 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002793 }
2794
2795 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 &attributes,
2797 slot->key.data, slot->key.bytes,
2798 alg,
2799 input, input_length,
2800 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002801
2802exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002803 /* In case of success, set the potential excess room in the output buffer
2804 * to an invalid value, to avoid potentially leaking a longer MAC.
2805 * In case of error, set the output length and content to a safe default,
2806 * such that in case the caller misses an error check, the output would be
2807 * an unachievable MAC.
2808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002810 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002811 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002812 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002813
2814 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002819}
2820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002822 psa_algorithm_t alg,
2823 const uint8_t *input,
2824 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 uint8_t *mac,
2826 size_t mac_size,
2827 size_t *mac_length)
2828{
2829 return psa_mac_compute_internal(key, alg,
2830 input, input_length,
2831 mac, mac_size, mac_length, 1);
2832}
2833
2834psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2835 psa_algorithm_t alg,
2836 const uint8_t *input,
2837 size_t input_length,
2838 const uint8_t *mac,
2839 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002840{
2841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002842 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2843 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 status = psa_mac_compute_internal(key, alg,
2846 input, input_length,
2847 actual_mac, sizeof(actual_mac),
2848 &actual_mac_length, 0);
2849 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002850 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002854 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002856 }
Dave Rodgman78701152023-08-29 14:20:18 +01002857 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002858 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002859 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002860 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002861
2862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002866}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002867
Gilles Peskinee59236f2018-01-27 23:32:46 +01002868/****************************************************************/
2869/* Asymmetric cryptography */
2870/****************************************************************/
2871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2873 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002874{
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (input_is_message) {
2876 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2877 return PSA_ERROR_INVALID_ARGUMENT;
2878 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2881 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2882 return PSA_ERROR_INVALID_ARGUMENT;
2883 }
2884 }
2885 } else {
2886 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2887 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002888 }
2889 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002892}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2895 int input_is_message,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 uint8_t *signature,
2900 size_t signature_size,
2901 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2905 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002906 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002907
2908 *signature_length = 0;
2909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 status = psa_sign_verify_check_alg(input_is_message, alg);
2911 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002916 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917 * buffer can in principle be empty since it doesn't actually have
2918 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (signature_size == 0) {
2920 return PSA_ERROR_BUFFER_TOO_SMALL;
2921 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002923 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 key, &slot,
2925 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2926 PSA_KEY_USAGE_SIGN_HASH,
2927 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934 status = PSA_ERROR_INVALID_ARGUMENT;
2935 goto exit;
2936 }
2937
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002938 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002940 };
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002945 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 signature, signature_size, signature_length);
2947 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002948
2949 status = psa_driver_wrapper_sign_hash(
2950 &attributes, slot->key.data, slot->key.bytes,
2951 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953 }
2954
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002955
2956exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002957 psa_wipe_tag_output_buffer(signature, status, signature_size,
2958 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002963}
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2966 int input_is_message,
2967 psa_algorithm_t alg,
2968 const uint8_t *input,
2969 size_t input_length,
2970 const uint8_t *signature,
2971 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002972{
2973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2974 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2975 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 status = psa_sign_verify_check_alg(input_is_message, alg);
2978 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002979 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002981
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002982 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 key, &slot,
2984 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2985 PSA_KEY_USAGE_VERIFY_HASH,
2986 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (status != PSA_SUCCESS) {
2989 return status;
2990 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002991
2992 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002994 };
2995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002997 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002998 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002999 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 signature, signature_length);
3001 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003002 status = psa_driver_wrapper_verify_hash(
3003 &attributes, slot->key.data, slot->key.bytes,
3004 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003006 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003011
3012}
3013
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003014psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015 const psa_key_attributes_t *attributes,
3016 const uint8_t *key_buffer,
3017 size_t key_buffer_size,
3018 psa_algorithm_t alg,
3019 const uint8_t *input,
3020 size_t input_length,
3021 uint8_t *signature,
3022 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024{
3025 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003028 size_t hash_length;
3029 uint8_t hash[PSA_HASH_MAX_SIZE];
3030
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003031 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 PSA_ALG_SIGN_GET_HASH(alg),
3033 input, input_length,
3034 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003037 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003039
3040 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 attributes, key_buffer, key_buffer_size,
3042 alg, hash, hash_length,
3043 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003044 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003047}
3048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3050 psa_algorithm_t alg,
3051 const uint8_t *input,
3052 size_t input_length,
3053 uint8_t *signature,
3054 size_t signature_size,
3055 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003056{
3057 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003058 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003060}
3061
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003062psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003063 const psa_key_attributes_t *attributes,
3064 const uint8_t *key_buffer,
3065 size_t key_buffer_size,
3066 psa_algorithm_t alg,
3067 const uint8_t *input,
3068 size_t input_length,
3069 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071{
3072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003075 size_t hash_length;
3076 uint8_t hash[PSA_HASH_MAX_SIZE];
3077
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003078 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 PSA_ALG_SIGN_GET_HASH(alg),
3080 input, input_length,
3081 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003084 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003086
3087 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 attributes, key_buffer, key_buffer_size,
3089 alg, hash, hash_length,
3090 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003091 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003094}
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3097 psa_algorithm_t alg,
3098 const uint8_t *input,
3099 size_t input_length,
3100 const uint8_t *signature,
3101 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003102{
3103 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003104 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003106}
3107
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003108psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003109 const psa_key_attributes_t *attributes,
3110 const uint8_t *key_buffer, size_t key_buffer_size,
3111 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003113{
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3115 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3116 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003117#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3119 return mbedtls_psa_rsa_sign_hash(
3120 attributes,
3121 key_buffer, key_buffer_size,
3122 alg, hash, hash_length,
3123 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 } else {
3127 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3130 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3133 return mbedtls_psa_ecdsa_sign_hash(
3134 attributes,
3135 key_buffer, key_buffer_size,
3136 alg, hash, hash_length,
3137 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3139 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 } else {
3141 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003142 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003143 }
Ronald Cron4501c982021-03-19 20:09:32 +01003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 (void) key_buffer;
3146 (void) key_buffer_size;
3147 (void) hash;
3148 (void) hash_length;
3149 (void) signature;
3150 (void) signature_size;
3151 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003154}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3157 psa_algorithm_t alg,
3158 const uint8_t *hash,
3159 size_t hash_length,
3160 uint8_t *signature,
3161 size_t signature_size,
3162 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003163{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003164 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003165 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003167}
3168
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003169psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003170 const psa_key_attributes_t *attributes,
3171 const uint8_t *key_buffer, size_t key_buffer_size,
3172 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003174{
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3176 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3177 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3180 return mbedtls_psa_rsa_verify_hash(
3181 attributes,
3182 key_buffer, key_buffer_size,
3183 alg, hash, hash_length,
3184 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003185#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3186 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else {
3188 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3191 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003192#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3194 return mbedtls_psa_ecdsa_verify_hash(
3195 attributes,
3196 key_buffer, key_buffer_size,
3197 alg, hash, hash_length,
3198 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003199#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3200 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 } else {
3202 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003203 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003204 }
Ronald Cron4501c982021-03-19 20:09:32 +01003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 (void) key_buffer;
3207 (void) key_buffer_size;
3208 (void) hash;
3209 (void) hash_length;
3210 (void) signature;
3211 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *hash,
3219 size_t hash_length,
3220 const uint8_t *signature,
3221 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003222{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003223 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003224 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003226}
3227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3229 psa_algorithm_t alg,
3230 const uint8_t *input,
3231 size_t input_length,
3232 const uint8_t *salt,
3233 size_t salt_length,
3234 uint8_t *output,
3235 size_t output_size,
3236 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003239 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003240 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003241 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003242
Darryl Green5cc689a2018-07-24 15:34:10 +01003243 (void) input;
3244 (void) input_length;
3245 (void) salt;
3246 (void) output;
3247 (void) output_size;
3248
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003249 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3252 return PSA_ERROR_INVALID_ARGUMENT;
3253 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003254
Ronald Cron5c522922020-11-14 16:35:34 +01003255 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3257 if (status != PSA_SUCCESS) {
3258 return status;
3259 }
3260 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3261 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262 status = PSA_ERROR_INVALID_ARGUMENT;
3263 goto exit;
3264 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003266 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003268 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003269
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003270 status = psa_driver_wrapper_asymmetric_encrypt(
3271 &attributes, slot->key.data, slot->key.bytes,
3272 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003278}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3281 psa_algorithm_t alg,
3282 const uint8_t *input,
3283 size_t input_length,
3284 const uint8_t *salt,
3285 size_t salt_length,
3286 uint8_t *output,
3287 size_t output_size,
3288 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003291 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003292 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003293 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003294
Darryl Green5cc689a2018-07-24 15:34:10 +01003295 (void) input;
3296 (void) input_length;
3297 (void) salt;
3298 (void) output;
3299 (void) output_size;
3300
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003301 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3304 return PSA_ERROR_INVALID_ARGUMENT;
3305 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003306
Ronald Cron5c522922020-11-14 16:35:34 +01003307 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3309 if (status != PSA_SUCCESS) {
3310 return status;
3311 }
3312 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313 status = PSA_ERROR_INVALID_ARGUMENT;
3314 goto exit;
3315 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003317 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003319 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003321 status = psa_driver_wrapper_asymmetric_decrypt(
3322 &attributes, slot->key.data, slot->key.bytes,
3323 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003325
3326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003330}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003331
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332/****************************************************************/
3333/* Asymmetric interruptible cryptography */
3334/****************************************************************/
3335
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003336static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3337
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338void psa_interruptible_set_max_ops(uint32_t max_ops)
3339{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003340 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341}
3342
3343uint32_t psa_interruptible_get_max_ops(void)
3344{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003345 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346}
3347
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348uint32_t psa_sign_hash_get_num_ops(
3349 const psa_sign_hash_interruptible_operation_t *operation)
3350{
Paul Elliott296ede92022-12-15 17:00:30 +00003351 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352}
3353
3354uint32_t psa_verify_hash_get_num_ops(
3355 const psa_verify_hash_interruptible_operation_t *operation)
3356{
Paul Elliott296ede92022-12-15 17:00:30 +00003357 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358}
3359
Paul Elliott59ad9452022-12-18 15:09:02 +00003360static psa_status_t psa_sign_hash_abort_internal(
3361 psa_sign_hash_interruptible_operation_t *operation)
3362{
3363 if (operation->id == 0) {
3364 /* The object has (apparently) been initialized but it is not (yet)
3365 * in use. It's ok to call abort on such an object, and there's
3366 * nothing to do. */
3367 return PSA_SUCCESS;
3368 }
3369
3370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_driver_wrapper_sign_hash_abort(operation);
3373
3374 operation->id = 0;
3375
Paul Elliotte6145dc2023-02-07 12:51:21 +00003376 /* Do not clear either the error_occurred or num_ops elements here as they
3377 * only want to be cleared by the application calling abort, not by abort
3378 * being called at completion of an operation. */
3379
Paul Elliott59ad9452022-12-18 15:09:02 +00003380 return status;
3381}
3382
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383psa_status_t psa_sign_hash_start(
3384 psa_sign_hash_interruptible_operation_t *operation,
3385 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3386 const uint8_t *hash, size_t hash_length)
3387{
3388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3389 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3390 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003391 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392
Paul Elliottc9774412023-02-06 15:14:07 +00003393 /* Check that start has not been previously called, or operation has not
3394 * previously errored. */
3395 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396 return PSA_ERROR_BAD_STATE;
3397 }
3398
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 status = psa_sign_verify_check_alg(0, alg);
3400 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003401 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402 return status;
3403 }
3404
3405 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3406 PSA_KEY_USAGE_SIGN_HASH,
3407 alg);
3408
3409 if (status != PSA_SUCCESS) {
3410 goto exit;
3411 }
3412
3413 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3414 status = PSA_ERROR_INVALID_ARGUMENT;
3415 goto exit;
3416 }
3417
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003418 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419 .core = slot->attr
3420 };
3421
Paul Elliott296ede92022-12-15 17:00:30 +00003422 /* Ensure ops count gets reset, in case of operation re-use. */
3423 operation->num_ops = 0;
3424
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3426 slot->key.data,
3427 slot->key.bytes, alg,
3428 hash, hash_length);
3429exit:
3430
3431 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003432 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003433 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003434 }
3435
3436 unlock_status = psa_unlock_key_slot(slot);
3437
Paul Elliottc9774412023-02-06 15:14:07 +00003438 if (unlock_status != PSA_SUCCESS) {
3439 operation->error_occurred = 1;
3440 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441
Paul Elliottc9774412023-02-06 15:14:07 +00003442 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443}
3444
3445
3446psa_status_t psa_sign_hash_complete(
3447 psa_sign_hash_interruptible_operation_t *operation,
3448 uint8_t *signature, size_t signature_size,
3449 size_t *signature_length)
3450{
3451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3452
3453 *signature_length = 0;
3454
Paul Elliottc9774412023-02-06 15:14:07 +00003455 /* Check that start has been called first, and that operation has not
3456 * previously errored. */
3457 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458 status = PSA_ERROR_BAD_STATE;
3459 goto exit;
3460 }
3461
Paul Elliott096abc42023-02-03 18:33:23 +00003462 /* Immediately reject a zero-length signature buffer. This guarantees that
3463 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464 if (signature_size == 0) {
3465 status = PSA_ERROR_BUFFER_TOO_SMALL;
3466 goto exit;
3467 }
3468
3469 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3470 signature_size,
3471 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliott296ede92022-12-15 17:00:30 +00003473 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003474 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003475
Paul Elliottebe225c2023-02-10 14:32:53 +00003476exit:
3477
Paul Elliott59200a22023-02-21 15:07:40 +00003478 psa_wipe_tag_output_buffer(signature, status, signature_size,
3479 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003480
Paul Elliott53bb3122023-02-10 14:22:22 +00003481 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003482 if (status != PSA_SUCCESS) {
3483 operation->error_occurred = 1;
3484 }
3485
Paul Elliott59ad9452022-12-18 15:09:02 +00003486 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 }
3488
3489 return status;
3490}
3491
3492psa_status_t psa_sign_hash_abort(
3493 psa_sign_hash_interruptible_operation_t *operation)
3494{
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3496
3497 status = psa_sign_hash_abort_internal(operation);
3498
3499 /* We clear the number of ops done here, so that it is not cleared when
3500 * the operation fails or succeeds, only on manual abort. */
3501 operation->num_ops = 0;
3502
Paul Elliottc9774412023-02-06 15:14:07 +00003503 /* Likewise, failure state. */
3504 operation->error_occurred = 0;
3505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 return status;
3507}
3508
3509static psa_status_t psa_verify_hash_abort_internal(
3510 psa_verify_hash_interruptible_operation_t *operation)
3511{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512 if (operation->id == 0) {
3513 /* The object has (apparently) been initialized but it is not (yet)
3514 * in use. It's ok to call abort on such an object, and there's
3515 * nothing to do. */
3516 return PSA_SUCCESS;
3517 }
3518
Paul Elliott59ad9452022-12-18 15:09:02 +00003519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3520
3521 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522
3523 operation->id = 0;
3524
Paul Elliotte6145dc2023-02-07 12:51:21 +00003525 /* Do not clear either the error_occurred or num_ops elements here as they
3526 * only want to be cleared by the application calling abort, not by abort
3527 * being called at completion of an operation. */
3528
Paul Elliott59ad9452022-12-18 15:09:02 +00003529 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003530}
3531
3532psa_status_t psa_verify_hash_start(
3533 psa_verify_hash_interruptible_operation_t *operation,
3534 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3535 const uint8_t *hash, size_t hash_length,
3536 const uint8_t *signature, size_t signature_length)
3537{
3538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3539 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3540 psa_key_slot_t *slot;
3541
Paul Elliottc9774412023-02-06 15:14:07 +00003542 /* Check that start has not been previously called, or operation has not
3543 * previously errored. */
3544 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003545 return PSA_ERROR_BAD_STATE;
3546 }
3547
3548 status = psa_sign_verify_check_alg(0, alg);
3549 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003550 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003551 return status;
3552 }
3553
3554 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3555 PSA_KEY_USAGE_VERIFY_HASH,
3556 alg);
3557
3558 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003559 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560 return status;
3561 }
3562
3563 psa_key_attributes_t attributes = {
3564 .core = slot->attr
3565 };
3566
Paul Elliott296ede92022-12-15 17:00:30 +00003567 /* Ensure ops count gets reset, in case of operation re-use. */
3568 operation->num_ops = 0;
3569
Paul Elliott9fe12f62022-11-30 19:16:02 +00003570 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3571 slot->key.data,
3572 slot->key.bytes,
3573 alg, hash, hash_length,
3574 signature, signature_length);
3575
3576 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003577 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003578 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003579 }
3580
3581 unlock_status = psa_unlock_key_slot(slot);
3582
Paul Elliottc9774412023-02-06 15:14:07 +00003583 if (unlock_status != PSA_SUCCESS) {
3584 operation->error_occurred = 1;
3585 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586
Paul Elliottc9774412023-02-06 15:14:07 +00003587 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003588}
3589
3590psa_status_t psa_verify_hash_complete(
3591 psa_verify_hash_interruptible_operation_t *operation)
3592{
3593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3594
Paul Elliottc9774412023-02-06 15:14:07 +00003595 /* Check that start has been called first, and that operation has not
3596 * previously errored. */
3597 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598 status = PSA_ERROR_BAD_STATE;
3599 goto exit;
3600 }
3601
3602 status = psa_driver_wrapper_verify_hash_complete(operation);
3603
Paul Elliott296ede92022-12-15 17:00:30 +00003604 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003605 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003606 operation);
3607
Paul Elliottebe225c2023-02-10 14:32:53 +00003608exit:
3609
Paul Elliott9fe12f62022-11-30 19:16:02 +00003610 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003611 if (status != PSA_SUCCESS) {
3612 operation->error_occurred = 1;
3613 }
3614
Paul Elliott59ad9452022-12-18 15:09:02 +00003615 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003616 }
3617
3618 return status;
3619}
3620
3621psa_status_t psa_verify_hash_abort(
3622 psa_verify_hash_interruptible_operation_t *operation)
3623{
Paul Elliott59ad9452022-12-18 15:09:02 +00003624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627
Paul Elliott59ad9452022-12-18 15:09:02 +00003628 /* We clear the number of ops done here, so that it is not cleared when
3629 * the operation fails or succeeds, only on manual abort. */
3630 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631
Paul Elliottc9774412023-02-06 15:14:07 +00003632 /* Likewise, failure state. */
3633 operation->error_occurred = 0;
3634
Paul Elliott59ad9452022-12-18 15:09:02 +00003635 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003636}
3637
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638/****************************************************************/
3639/* Asymmetric interruptible cryptography internal */
3640/* implementations */
3641/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003642
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3644{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003645
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
3649
3650 /* Internal implementation uses zero to indicate infinite number max ops,
3651 * therefore avoid this value, and set to minimum possible. */
3652 if (max_ops == 0) {
3653 max_ops = 1;
3654 }
3655
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003657#else
3658 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3660 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3661 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3662}
3663
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003665 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666{
3667#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3668 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3669 defined(MBEDTLS_ECP_RESTARTABLE)
3670
Paul Elliott93d9ca82023-02-15 18:14:21 +00003671 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672#else
3673 (void) operation;
3674 return 0;
3675#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3676 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3677 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3678}
3679
3680uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003681 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682{
3683 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3684 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3685 defined(MBEDTLS_ECP_RESTARTABLE)
3686
Paul Elliott93d9ca82023-02-15 18:14:21 +00003687 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688#else
3689 (void) operation;
3690 return 0;
3691#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3692 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3693 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3694}
3695
3696psa_status_t mbedtls_psa_sign_hash_start(
3697 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3698 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3699 size_t key_buffer_size, psa_algorithm_t alg,
3700 const uint8_t *hash, size_t hash_length)
3701{
3702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003703 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704
Paul Elliott068fe072023-01-16 13:59:15 +00003705 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3706 return PSA_ERROR_NOT_SUPPORTED;
3707 }
3708
3709 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003710 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003711 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003714 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3715 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716
Paul Elliotta1c94092023-02-15 16:38:04 +00003717 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3718
Paul Elliott93d9ca82023-02-15 18:14:21 +00003719 /* Ensure num_ops is zero'ed in case of context re-use. */
3720 operation->num_ops = 0;
3721
Paul Elliott068fe072023-01-16 13:59:15 +00003722 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3723 attributes->core.bits,
3724 key_buffer,
3725 key_buffer_size,
3726 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727
Paul Elliott068fe072023-01-16 13:59:15 +00003728 if (status != PSA_SUCCESS) {
3729 return status;
3730 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott1bc59df2023-02-05 13:41:57 +00003732 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003733 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734
Paul Elliott068fe072023-01-16 13:59:15 +00003735 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003736 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003737 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott0290a762023-02-09 14:30:24 +00003739 /* We only need to store the same length of hash as the private key size
3740 * here, it would be truncated by the internal implementation anyway. */
3741 required_hash_length = (hash_length < operation->coordinate_bytes ?
3742 hash_length : operation->coordinate_bytes);
3743
Paul Elliottba70ad42023-02-15 18:23:53 +00003744 if (required_hash_length > sizeof(operation->hash)) {
3745 /* Shouldn't happen, but better safe than sorry. */
3746 return PSA_ERROR_CORRUPTION_DETECTED;
3747 }
3748
Paul Elliott0290a762023-02-09 14:30:24 +00003749 memcpy(operation->hash, hash, required_hash_length);
3750 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003751
3752 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753
3754#else
Paul Elliott068fe072023-01-16 13:59:15 +00003755 (void) operation;
3756 (void) key_buffer;
3757 (void) key_buffer_size;
3758 (void) alg;
3759 (void) hash;
3760 (void) hash_length;
3761 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003762 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
Paul Elliott068fe072023-01-16 13:59:15 +00003764 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3766 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3767 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768}
3769
3770psa_status_t mbedtls_psa_sign_hash_complete(
3771 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3772 uint8_t *signature, size_t signature_size,
3773 size_t *signature_length)
3774{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3776 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3777 defined(MBEDTLS_ECP_RESTARTABLE)
3778
Paul Elliotta1c94092023-02-15 16:38:04 +00003779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003780 mbedtls_mpi r;
3781 mbedtls_mpi s;
3782
Paul Elliott46845252023-02-03 14:59:11 +00003783 mbedtls_mpi_init(&r);
3784 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003786 /* Ensure max_ops is set to the current value (or default). */
3787 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3788
Paul Elliotta1c94092023-02-15 16:38:04 +00003789 if (signature_size < 2 * operation->coordinate_bytes) {
3790 status = PSA_ERROR_BUFFER_TOO_SMALL;
3791 goto exit;
3792 }
3793
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003795
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3797 status = mbedtls_to_psa_error(
3798 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003799 &r,
3800 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801 &operation->ctx->d,
3802 operation->hash,
3803 operation->hash_length,
3804 operation->md_alg,
3805 mbedtls_psa_get_random,
3806 MBEDTLS_PSA_RANDOM_STATE,
3807 &operation->restart_ctx));
3808#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003809 status = PSA_ERROR_NOT_SUPPORTED;
3810 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3812 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813 status = mbedtls_to_psa_error(
3814 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003815 &r,
3816 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817 &operation->ctx->d,
3818 operation->hash,
3819 operation->hash_length,
3820 mbedtls_psa_get_random,
3821 MBEDTLS_PSA_RANDOM_STATE,
3822 mbedtls_psa_get_random,
3823 MBEDTLS_PSA_RANDOM_STATE,
3824 &operation->restart_ctx));
3825 }
3826
Paul Elliottf8e5b562023-02-19 18:43:45 +00003827 /* Hide the fact that the restart context only holds a delta of number of
3828 * ops done during the last operation, not an absolute value. */
3829 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3830
Paul Elliott724bd252023-02-08 12:35:08 +00003831 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003833 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003835 operation->coordinate_bytes)
3836 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
3838 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003839 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840 }
3841
3842 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003843 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003845 operation->coordinate_bytes,
3846 operation->coordinate_bytes)
3847 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
3849 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003850 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851 }
3852
Paul Elliott1bc59df2023-02-05 13:41:57 +00003853 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott724bd252023-02-08 12:35:08 +00003855 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 }
Paul Elliott724bd252023-02-08 12:35:08 +00003857
3858exit:
3859
3860 mbedtls_mpi_free(&r);
3861 mbedtls_mpi_free(&s);
3862 return status;
3863
Paul Elliott588f8ed2022-12-02 18:10:26 +00003864 #else
3865
3866 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867 (void) signature;
3868 (void) signature_size;
3869 (void) signature_length;
3870
3871 return PSA_ERROR_NOT_SUPPORTED;
3872
3873#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3874 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3875 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3876}
3877
3878psa_status_t mbedtls_psa_sign_hash_abort(
3879 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3880{
3881
3882#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3883 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3884 defined(MBEDTLS_ECP_RESTARTABLE)
3885
3886 if (operation->ctx) {
3887 mbedtls_ecdsa_free(operation->ctx);
3888 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003889 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890 }
3891
3892 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3893
Paul Elliott93d9ca82023-02-15 18:14:21 +00003894 operation->num_ops = 0;
3895
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896 return PSA_SUCCESS;
3897
3898#else
3899
3900 (void) operation;
3901
3902 return PSA_ERROR_NOT_SUPPORTED;
3903
3904#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3905 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3906 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3907}
3908
3909psa_status_t mbedtls_psa_verify_hash_start(
3910 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3911 const psa_key_attributes_t *attributes,
3912 const uint8_t *key_buffer, size_t key_buffer_size,
3913 psa_algorithm_t alg,
3914 const uint8_t *hash, size_t hash_length,
3915 const uint8_t *signature, size_t signature_length)
3916{
3917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003918 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003919 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003920
Paul Elliott068fe072023-01-16 13:59:15 +00003921 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3922 return PSA_ERROR_NOT_SUPPORTED;
3923 }
3924
3925 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003926 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003927 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928
3929#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003930 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3931 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932
Paul Elliotta1c94092023-02-15 16:38:04 +00003933 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3934 mbedtls_mpi_init(&operation->r);
3935 mbedtls_mpi_init(&operation->s);
3936
Paul Elliott93d9ca82023-02-15 18:14:21 +00003937 /* Ensure num_ops is zero'ed in case of context re-use. */
3938 operation->num_ops = 0;
3939
Paul Elliott068fe072023-01-16 13:59:15 +00003940 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3941 attributes->core.bits,
3942 key_buffer,
3943 key_buffer_size,
3944 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945
Paul Elliott068fe072023-01-16 13:59:15 +00003946 if (status != PSA_SUCCESS) {
3947 return status;
3948 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949
Paul Elliottc569fc22023-02-10 13:02:54 +00003950 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliott1bc59df2023-02-05 13:41:57 +00003952 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003953 return PSA_ERROR_INVALID_SIGNATURE;
3954 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003955
Paul Elliott068fe072023-01-16 13:59:15 +00003956 status = mbedtls_to_psa_error(
3957 mbedtls_mpi_read_binary(&operation->r,
3958 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003959 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003960
Paul Elliott068fe072023-01-16 13:59:15 +00003961 if (status != PSA_SUCCESS) {
3962 return status;
3963 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
Paul Elliott068fe072023-01-16 13:59:15 +00003965 status = mbedtls_to_psa_error(
3966 mbedtls_mpi_read_binary(&operation->s,
3967 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003968 coordinate_bytes,
3969 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003970
Paul Elliott068fe072023-01-16 13:59:15 +00003971 if (status != PSA_SUCCESS) {
3972 return status;
3973 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974
Paul Elliott2c9843f2023-02-15 17:32:42 +00003975 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliott2c9843f2023-02-15 17:32:42 +00003977 if (status != PSA_SUCCESS) {
3978 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003979 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980
Paul Elliott0290a762023-02-09 14:30:24 +00003981 /* We only need to store the same length of hash as the private key size
3982 * here, it would be truncated by the internal implementation anyway. */
3983 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3984 coordinate_bytes);
3985
Paul Elliottba70ad42023-02-15 18:23:53 +00003986 if (required_hash_length > sizeof(operation->hash)) {
3987 /* Shouldn't happen, but better safe than sorry. */
3988 return PSA_ERROR_CORRUPTION_DETECTED;
3989 }
3990
Paul Elliott0290a762023-02-09 14:30:24 +00003991 memcpy(operation->hash, hash, required_hash_length);
3992 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003993
3994 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995#else
Paul Elliott068fe072023-01-16 13:59:15 +00003996 (void) operation;
3997 (void) key_buffer;
3998 (void) key_buffer_size;
3999 (void) alg;
4000 (void) hash;
4001 (void) hash_length;
4002 (void) signature;
4003 (void) signature_length;
4004 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004005 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004006 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007
Paul Elliott068fe072023-01-16 13:59:15 +00004008 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4010 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4011 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004012}
4013
4014psa_status_t mbedtls_psa_verify_hash_complete(
4015 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4016{
4017
4018#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4019 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4020 defined(MBEDTLS_ECP_RESTARTABLE)
4021
Paul Elliottf8e5b562023-02-19 18:43:45 +00004022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4023
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004024 /* Ensure max_ops is set to the current value (or default). */
4025 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4026
Paul Elliottf8e5b562023-02-19 18:43:45 +00004027 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4029 operation->hash,
4030 operation->hash_length,
4031 &operation->ctx->Q,
4032 &operation->r,
4033 &operation->s,
4034 &operation->restart_ctx));
4035
Paul Elliottf8e5b562023-02-19 18:43:45 +00004036 /* Hide the fact that the restart context only holds a delta of number of
4037 * ops done during the last operation, not an absolute value. */
4038 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4039
4040 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004041#else
4042 (void) operation;
4043
4044 return PSA_ERROR_NOT_SUPPORTED;
4045
4046#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4047 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4048 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4049}
4050
4051psa_status_t mbedtls_psa_verify_hash_abort(
4052 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4053{
4054
4055#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4056 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4057 defined(MBEDTLS_ECP_RESTARTABLE)
4058
4059 if (operation->ctx) {
4060 mbedtls_ecdsa_free(operation->ctx);
4061 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004062 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004063 }
4064
4065 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4066
Paul Elliott93d9ca82023-02-15 18:14:21 +00004067 operation->num_ops = 0;
4068
Paul Elliott588f8ed2022-12-02 18:10:26 +00004069 mbedtls_mpi_free(&operation->r);
4070 mbedtls_mpi_free(&operation->s);
4071
4072 return PSA_SUCCESS;
4073
4074#else
4075 (void) operation;
4076
4077 return PSA_ERROR_NOT_SUPPORTED;
4078
4079#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4080 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4081 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4082}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004083
mohammad1603503973b2018-03-12 15:59:30 +02004084/****************************************************************/
4085/* Symmetric cryptography */
4086/****************************************************************/
4087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4089 mbedtls_svc_key_id_t key,
4090 psa_algorithm_t alg,
4091 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004092{
4093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4094 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004095 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4097 PSA_KEY_USAGE_ENCRYPT :
4098 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004099 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004100
4101 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004103 status = PSA_ERROR_BAD_STATE;
4104 goto exit;
4105 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004108 status = PSA_ERROR_INVALID_ARGUMENT;
4109 goto exit;
4110 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4113 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004116
Ronald Cron49fafa92021-03-10 08:34:23 +01004117 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004118 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004119 * so we only set it (in the driver wrapper) after resources have been
4120 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004123 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004125 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 }
4127 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004128
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004129 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004131 };
4132
Ronald Cronab99ac22020-10-01 16:38:28 +02004133 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (cipher_operation == MBEDTLS_ENCRYPT) {
4135 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4136 &attributes,
4137 slot->key.data,
4138 slot->key.bytes,
4139 alg);
4140 } else {
4141 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4142 &attributes,
4143 slot->key.data,
4144 slot->key.bytes,
4145 alg);
4146 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004147
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (status != PSA_SUCCESS) {
4150 psa_cipher_abort(operation);
4151 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004156}
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4159 mbedtls_svc_key_id_t key,
4160 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004161{
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004163}
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4166 mbedtls_svc_key_id_t key,
4167 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004168{
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004170}
4171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4173 uint8_t *iv,
4174 size_t iv_size,
4175 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004176{
Ronald Cron6d051732020-10-01 14:10:20 +02004177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004178 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004179 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004182 status = PSA_ERROR_BAD_STATE;
4183 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004184 }
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004187 status = PSA_ERROR_BAD_STATE;
4188 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004189 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004190
Ronald Cron2fb90522021-07-05 12:31:44 +02004191 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004193 status = PSA_ERROR_BUFFER_TOO_SMALL;
4194 goto exit;
4195 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004198 status = PSA_ERROR_GENERIC_ERROR;
4199 goto exit;
4200 }
4201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 status = psa_generate_random(local_iv, default_iv_length);
4203 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 }
Ronald Cron5618a392021-03-26 09:52:26 +01004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 status = psa_driver_wrapper_cipher_set_iv(operation,
4208 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004209
4210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 if (status == PSA_SUCCESS) {
4212 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004213 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004214 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004216 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004218 }
Ronald Cron6d051732020-10-01 14:10:20 +02004219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004221}
4222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4224 const uint8_t *iv,
4225 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004226{
Ronald Cron6d051732020-10-01 14:10:20 +02004227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004230 status = PSA_ERROR_BAD_STATE;
4231 goto exit;
4232 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235 status = PSA_ERROR_BAD_STATE;
4236 goto exit;
4237 }
Ronald Crona0d68172021-03-26 10:15:08 +01004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004240 status = PSA_ERROR_INVALID_ARGUMENT;
4241 goto exit;
4242 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 status = psa_driver_wrapper_cipher_set_iv(operation,
4245 iv,
4246 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004247
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004250 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 } else {
4252 psa_cipher_abort(operation);
4253 }
4254 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004255}
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4258 const uint8_t *input,
4259 size_t input_length,
4260 uint8_t *output,
4261 size_t output_size,
4262 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004263{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004267 status = PSA_ERROR_BAD_STATE;
4268 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004269 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004272 status = PSA_ERROR_BAD_STATE;
4273 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004274 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 status = psa_driver_wrapper_cipher_update(operation,
4277 input,
4278 input_length,
4279 output,
4280 output_size,
4281 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004282
4283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (status != PSA_SUCCESS) {
4285 psa_cipher_abort(operation);
4286 }
Ronald Cron6d051732020-10-01 14:10:20 +02004287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004289}
4290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4292 uint8_t *output,
4293 size_t output_size,
4294 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004295{
David Saadab4ecc272019-02-14 13:48:10 +02004296 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004299 status = PSA_ERROR_BAD_STATE;
4300 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004301 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304 status = PSA_ERROR_BAD_STATE;
4305 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004306 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 status = psa_driver_wrapper_cipher_finish(operation,
4309 output,
4310 output_size,
4311 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004312
4313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (status == PSA_SUCCESS) {
4315 return psa_cipher_abort(operation);
4316 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004317 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004321 }
mohammad1603503973b2018-03-12 15:59:30 +02004322}
4323
Gilles Peskine449bd832023-01-11 14:50:10 +01004324psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004325{
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004327 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004328 * in use. It's ok to call abort on such an object, and there's
4329 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004331 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004334
Ronald Cron49fafa92021-03-10 08:34:23 +01004335 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004336 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004337 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004340}
4341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4343 psa_algorithm_t alg,
4344 const uint8_t *input,
4345 size_t input_length,
4346 uint8_t *output,
4347 size_t output_size,
4348 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004349{
4350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004352 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004353 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4354 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004355 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004358 status = PSA_ERROR_INVALID_ARGUMENT;
4359 goto exit;
4360 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4363 PSA_KEY_USAGE_ENCRYPT,
4364 alg);
4365 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004366 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004368
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004369 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004371 };
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4374 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004375 status = PSA_ERROR_GENERIC_ERROR;
4376 goto exit;
4377 }
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (default_iv_length > 0) {
4380 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004381 status = PSA_ERROR_BUFFER_TOO_SMALL;
4382 goto exit;
4383 }
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_generate_random(local_iv, default_iv_length);
4386 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004387 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389 }
4390
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004391 status = psa_driver_wrapper_cipher_encrypt(
4392 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004393 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004394 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004396
4397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 unlock_status = psa_unlock_key_slot(slot);
4399 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004400 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004401 }
Ronald Cron23919522021-07-08 19:00:07 +02004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (status == PSA_SUCCESS) {
4404 if (default_iv_length > 0) {
4405 memcpy(output, local_iv, default_iv_length);
4406 }
4407 *output_length += default_iv_length;
4408 } else {
4409 *output_length = 0;
4410 }
4411
4412 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004413}
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4416 psa_algorithm_t alg,
4417 const uint8_t *input,
4418 size_t input_length,
4419 uint8_t *output,
4420 size_t output_size,
4421 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004422{
4423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004424 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004425 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004426 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004429 status = PSA_ERROR_INVALID_ARGUMENT;
4430 goto exit;
4431 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4434 PSA_KEY_USAGE_DECRYPT,
4435 alg);
4436 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004437 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004439
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004440 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004442 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4445 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004446 status = PSA_ERROR_INVALID_ARGUMENT;
4447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004449 status = PSA_ERROR_INVALID_ARGUMENT;
4450 goto exit;
4451 }
4452
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004453 status = psa_driver_wrapper_cipher_decrypt(
4454 &attributes, slot->key.data, slot->key.bytes,
4455 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004457
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004458exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 unlock_status = psa_unlock_key_slot(slot);
4460 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004461 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004465 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 }
Ronald Cron23919522021-07-08 19:00:07 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004469}
4470
4471
mohammad16035955c982018-04-26 00:53:03 +03004472/****************************************************************/
4473/* AEAD */
4474/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004475
Paul Elliottbaff51c2021-09-28 17:44:45 +01004476/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004477static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004478{
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004480}
4481
4482/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4484 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004485{
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004489#if defined(PSA_WANT_ALG_GCM)
4490 case PSA_ALG_GCM:
4491 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 * arbitrarily large nonces. Please note that we do not generally
4493 * recommend the usage of nonces of greater length than
4494 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4495 * size, which can then lead to collisions if you encrypt a very
4496 * large number of messages.*/
4497 if (nonce_length != 0) {
4498 return PSA_SUCCESS;
4499 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004500 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004501#endif /* PSA_WANT_ALG_GCM */
4502#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004503 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (nonce_length >= 7 && nonce_length <= 13) {
4505 return PSA_SUCCESS;
4506 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004507 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004508#endif /* PSA_WANT_ALG_CCM */
4509#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 if (nonce_length == 12) {
4512 return PSA_SUCCESS;
4513 } else if (nonce_length == 8) {
4514 return PSA_ERROR_NOT_SUPPORTED;
4515 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004516 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004517#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004518 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004519 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004521 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004524}
4525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004527{
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4529 return PSA_ERROR_INVALID_ARGUMENT;
4530 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004533}
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4536 psa_algorithm_t alg,
4537 const uint8_t *nonce,
4538 size_t nonce_length,
4539 const uint8_t *additional_data,
4540 size_t additional_data_length,
4541 const uint8_t *plaintext,
4542 size_t plaintext_length,
4543 uint8_t *ciphertext,
4544 size_t ciphertext_size,
4545 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004546{
Ronald Cron215633c2021-03-16 17:15:37 +01004547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4548 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004549
mohammad1603f08a5502018-06-03 15:05:47 +03004550 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 status = psa_aead_check_algorithm(alg);
4553 if (status != PSA_SUCCESS) {
4554 return status;
4555 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004556
Ronald Cron9a986162021-03-26 12:40:07 +01004557 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4559 if (status != PSA_SUCCESS) {
4560 return status;
4561 }
mohammad16035955c982018-04-26 00:53:03 +03004562
Ronald Cron215633c2021-03-16 17:15:37 +01004563 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004565 };
mohammad16035955c982018-04-26 00:53:03 +03004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_nonce_length(alg, nonce_length);
4568 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004571
Ronald Cronde822812021-03-17 16:08:20 +01004572 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004573 &attributes, slot->key.data, slot->key.bytes,
4574 alg,
4575 nonce, nonce_length,
4576 additional_data, additional_data_length,
4577 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4581 memset(ciphertext, 0, ciphertext_size);
4582 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004583
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004588}
4589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4591 psa_algorithm_t alg,
4592 const uint8_t *nonce,
4593 size_t nonce_length,
4594 const uint8_t *additional_data,
4595 size_t additional_data_length,
4596 const uint8_t *ciphertext,
4597 size_t ciphertext_length,
4598 uint8_t *plaintext,
4599 size_t plaintext_size,
4600 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004601{
Ronald Cron215633c2021-03-16 17:15:37 +01004602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4603 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004604
Gilles Peskineee652a32018-06-01 19:23:52 +02004605 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 status = psa_aead_check_algorithm(alg);
4608 if (status != PSA_SUCCESS) {
4609 return status;
4610 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004611
Ronald Cron9a986162021-03-26 12:40:07 +01004612 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4614 if (status != PSA_SUCCESS) {
4615 return status;
4616 }
mohammad16035955c982018-04-26 00:53:03 +03004617
Ronald Cron215633c2021-03-16 17:15:37 +01004618 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004620 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 status = psa_aead_check_nonce_length(alg, nonce_length);
4623 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004626
Ronald Cronde822812021-03-17 16:08:20 +01004627 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004628 &attributes, slot->key.data, slot->key.bytes,
4629 alg,
4630 nonce, nonce_length,
4631 additional_data, additional_data_length,
4632 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (status != PSA_SUCCESS && plaintext_size != 0) {
4636 memset(plaintext, 0, plaintext_size);
4637 }
mohammad160360a64d02018-06-03 17:20:42 +03004638
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004639exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 return status;
mohammad16035955c982018-04-26 00:53:03 +03004643}
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4646{
4647 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004650#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004652 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4654 return PSA_ERROR_INVALID_ARGUMENT;
4655 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004657#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658
Przemek Stekiel86679c72022-10-06 17:06:56 +02004659#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4663 return PSA_ERROR_INVALID_ARGUMENT;
4664 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004665 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004666#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667
Przemek Stekiel86679c72022-10-06 17:06:56 +02004668#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (tag_len != 16) {
4672 return PSA_ERROR_INVALID_ARGUMENT;
4673 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004675#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676
4677 default:
4678 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004680 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004682}
4683
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4686 int is_encrypt,
4687 mbedtls_svc_key_id_t key,
4688 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004689{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4691 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4692 psa_key_slot_t *slot = NULL;
4693 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004694 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 status = psa_aead_check_algorithm(alg);
4697 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004698 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004702 status = PSA_ERROR_BAD_STATE;
4703 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004704 }
4705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->nonce_set || operation->lengths_set ||
4707 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004708 status = PSA_ERROR_BAD_STATE;
4709 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004710 }
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4719 alg);
4720 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004724 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004725 .core = slot->attr
4726 };
4727
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if (is_encrypt) {
4733 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4734 &attributes,
4735 slot->key.data,
4736 slot->key.bytes,
4737 alg);
4738 } else {
4739 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4740 &attributes,
4741 slot->key.data,
4742 slot->key.bytes,
4743 alg);
4744 }
4745 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
4751exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004757 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 } else {
4759 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004760 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004763}
4764
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4767 mbedtls_svc_key_id_t key,
4768 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004769{
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
4772
4773/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4775 mbedtls_svc_key_id_t key,
4776 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777{
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779}
4780
4781/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004782psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4783 uint8_t *nonce,
4784 size_t nonce_size,
4785 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786{
4787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004788 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004789 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004791 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004794 status = PSA_ERROR_BAD_STATE;
4795 goto exit;
4796 }
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004799 status = PSA_ERROR_BAD_STATE;
4800 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004801 }
4802
Paul Elliotte193ea82021-10-01 13:00:16 +01004803 /* For CCM, this size may not be correct according to the PSA
4804 * specification. The PSA Crypto 1.0.1 specification states:
4805 *
4806 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4807 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4808 *
4809 * However this restriction that L has to be the smallest integer is not
4810 * applied in practice, and it is not implementable here since the
4811 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4813 operation->alg);
4814 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815 status = PSA_ERROR_BUFFER_TOO_SMALL;
4816 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817 }
4818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 status = psa_generate_random(local_nonce, required_nonce_size);
4820 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
Paul Elliott39dc6b82021-05-11 19:16:09 +01004826exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (status == PSA_SUCCESS) {
4828 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 } else {
4831 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004832 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835}
4836
4837/* Set the nonce for a multipart authenticated encryption or decryption
4838 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004839psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4840 const uint8_t *nonce,
4841 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004842{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004846 status = PSA_ERROR_BAD_STATE;
4847 goto exit;
4848 }
4849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851 status = PSA_ERROR_BAD_STATE;
4852 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853 }
4854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4856 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004857 status = PSA_ERROR_INVALID_ARGUMENT;
4858 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004859 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4862 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004863
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 } else {
4868 psa_aead_abort(operation);
4869 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872}
4873
4874/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004875psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4876 size_t ad_length,
4877 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004882 status = PSA_ERROR_BAD_STATE;
4883 goto exit;
4884 }
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (operation->lengths_set || operation->ad_started ||
4887 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004888 status = PSA_ERROR_BAD_STATE;
4889 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004890 }
4891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004893#if defined(PSA_WANT_ALG_GCM)
4894 case PSA_ALG_GCM:
4895 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 * bits. Without the guard this code will generate warnings on 32bit
4897 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004898#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (((uint64_t) ad_length) >> 61 != 0 ||
4900 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004901 status = PSA_ERROR_INVALID_ARGUMENT;
4902 goto exit;
4903 }
Paul Elliott325d3742021-09-27 17:56:28 +01004904#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004905 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004906#endif /* PSA_WANT_ALG_GCM */
4907#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 status = PSA_ERROR_INVALID_ARGUMENT;
4911 goto exit;
4912 }
4913 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004914#endif /* PSA_WANT_ALG_CCM */
4915#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004916 case PSA_ALG_CHACHA20_POLY1305:
4917 /* No length restrictions for ChaChaPoly. */
4918 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004919#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004920 default:
4921 break;
4922 }
Paul Elliott325d3742021-09-27 17:56:28 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4925 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004926
Paul Elliott39dc6b82021-05-11 19:16:09 +01004927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004929 operation->ad_remaining = ad_length;
4930 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004931 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 } else {
4933 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004934 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004938
4939/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004940psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4941 const uint8_t *input,
4942 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004947 status = PSA_ERROR_BAD_STATE;
4948 goto exit;
4949 }
4950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004954 }
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (operation->lengths_set) {
4957 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004958 status = PSA_ERROR_INVALID_ARGUMENT;
4959 goto exit;
4960 }
4961
4962 operation->ad_remaining -= input_length;
4963 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004964#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004966 status = PSA_ERROR_BAD_STATE;
4967 goto exit;
4968 }
4969#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 status = psa_driver_wrapper_aead_update_ad(operation, input,
4972 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004973
Paul Elliott39dc6b82021-05-11 19:16:09 +01004974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004976 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 } else {
4978 psa_aead_abort(operation);
4979 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982}
4983
4984/* Encrypt or decrypt a message fragment in an active multipart AEAD
4985 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004986psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4987 const uint8_t *input,
4988 size_t input_length,
4989 uint8_t *output,
4990 size_t output_size,
4991 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994
4995 *output_length = 0;
4996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004998 status = PSA_ERROR_BAD_STATE;
4999 goto exit;
5000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003 status = PSA_ERROR_BAD_STATE;
5004 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005 }
5006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005008 /* Additional data length was supplied, but not all the additional
5009 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005011 status = PSA_ERROR_INVALID_ARGUMENT;
5012 goto exit;
5013 }
5014
5015 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005017 status = PSA_ERROR_INVALID_ARGUMENT;
5018 goto exit;
5019 }
5020
5021 operation->body_remaining -= input_length;
5022 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005023#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005025 status = PSA_ERROR_BAD_STATE;
5026 goto exit;
5027 }
5028#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5031 output, output_size,
5032 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005033
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 } else {
5038 psa_aead_abort(operation);
5039 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005042}
5043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005045{
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (operation->id == 0 || !operation->nonce_set) {
5047 return PSA_ERROR_BAD_STATE;
5048 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5051 operation->body_remaining != 0)) {
5052 return PSA_ERROR_INVALID_ARGUMENT;
5053 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005056}
5057
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005059psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5060 uint8_t *ciphertext,
5061 size_t ciphertext_size,
5062 size_t *ciphertext_length,
5063 uint8_t *tag,
5064 size_t tag_size,
5065 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5068
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005070 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 status = psa_aead_final_checks(operation);
5073 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005078 status = PSA_ERROR_BAD_STATE;
5079 goto exit;
5080 }
5081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5083 ciphertext_size,
5084 ciphertext_length,
5085 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005086
Paul Elliott39dc6b82021-05-11 19:16:09 +01005087exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005088
5089
Paul Elliottf47b0952021-05-21 18:02:33 +01005090 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005091 * the zero tag size, make sure the tag is set to something implausible.
5092 * Even if the operation succeeds, make sure we clear the rest of the
5093 * buffer to prevent potential leakage of anything previously placed in
5094 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005095 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005100}
5101
5102/* Finish authenticating and decrypting a message in a multipart AEAD
5103 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005104psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5105 uint8_t *plaintext,
5106 size_t plaintext_size,
5107 size_t *plaintext_length,
5108 const uint8_t *tag,
5109 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005110{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5112
Paul Elliott302ff6b2021-04-20 18:10:30 +01005113 *plaintext_length = 0;
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 status = psa_aead_final_checks(operation);
5116 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005121 status = PSA_ERROR_BAD_STATE;
5122 goto exit;
5123 }
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5126 plaintext_size,
5127 plaintext_length,
5128 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005129
5130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005134}
5135
5136/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005138{
5139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005142 /* The object has (apparently) been initialized but it is not (yet)
5143 * in use. It's ok to call abort on such an object, and there's
5144 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005146 }
5147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005153}
5154
Gilles Peskinee59236f2018-01-27 23:32:46 +01005155/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156/* Generators */
5157/****************************************************************/
5158
Przemek Stekiel69c46792022-06-10 12:59:51 +02005159#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305163 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005164#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005165#endif /* At least one builtin KDF */
5166
Przemek Stekiel69c46792022-06-10 12:59:51 +02005167#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005168 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5169 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5170static psa_status_t psa_key_derivation_start_hmac(
5171 psa_mac_operation_t *operation,
5172 psa_algorithm_t hash_alg,
5173 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005175{
5176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5179 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5180 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005181
Steven Cooreman72f736a2021-05-07 14:14:37 +02005182 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 status = psa_driver_wrapper_mac_sign_setup(operation,
5186 &attributes,
5187 hmac_key, hmac_key_length,
5188 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 psa_reset_key_attributes(&attributes);
5191 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005192}
5193#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005194
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195#define HKDF_STATE_INIT 0 /* no input yet */
5196#define HKDF_STATE_STARTED 1 /* got salt */
5197#define HKDF_STATE_KEYED 2 /* got key */
5198#define HKDF_STATE_OUTPUT 3 /* output started */
5199
Gilles Peskinecbe66502019-05-16 16:59:18 +02005200static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005202{
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5204 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5205 } else {
5206 return operation->alg;
5207 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208}
5209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211{
5212 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5214 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005215 /* The object has (apparently) been initialized but it is not
5216 * in use. It's ok to call abort on such an object, and there's
5217 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005219#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5221 mbedtls_free(operation->ctx.hkdf.info);
5222 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5223 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005224#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005225#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5226 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5228 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5229 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5230 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005231 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005233 }
5234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005236 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005238 }
5239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005241 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005243 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005244#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005246 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005248 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005249#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005250 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005251
5252 /* We leave the fields Ai and output_block to be erased safely by the
5253 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005255#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5256 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005257#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5259 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5260 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5261 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005262#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305263#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305264 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305265 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005266 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305268 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305269
5270 status = PSA_SUCCESS;
5271 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305272#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005273 {
5274 status = PSA_ERROR_BAD_STATE;
5275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 mbedtls_platform_zeroize(operation, sizeof(*operation));
5277 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278}
5279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282{
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005286 }
5287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005288 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290}
5291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5293 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294{
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 if (operation->alg == 0) {
5296 return PSA_ERROR_BAD_STATE;
5297 }
5298 if (capacity > operation->capacity) {
5299 return PSA_ERROR_INVALID_ARGUMENT;
5300 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005303}
5304
Przemek Stekiel69c46792022-06-10 12:59:51 +02005305#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005306/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5308 psa_algorithm_t kdf_alg,
5309 uint8_t *output,
5310 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005311{
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5313 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005314 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005315 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#else
5319 const uint8_t last_block = 0xff;
5320#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 if (hkdf->state < HKDF_STATE_KEYED ||
5323 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005324#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005326#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 )) {
5328 return PSA_ERROR_BAD_STATE;
5329 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005330 hkdf->state = HKDF_STATE_OUTPUT;
5331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005333 /* Copy what remains of the current block */
5334 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005336 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 }
5338 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339 output += n;
5340 output_length -= n;
5341 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005343 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005345 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005346 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005348 * object was corrupted or if this function is called directly
5349 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if (hkdf->block_number == last_block) {
5351 return PSA_ERROR_BAD_STATE;
5352 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005353
5354 /* We need a new block */
5355 ++hkdf->block_number;
5356 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5359 hash_alg,
5360 hkdf->prk,
5361 hash_length);
5362 if (status != PSA_SUCCESS) {
5363 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005364 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005365
5366 if (hkdf->block_number != 1) {
5367 status = psa_mac_update(&hkdf->hmac,
5368 hkdf->output_block,
5369 hash_length);
5370 if (status != PSA_SUCCESS) {
5371 return status;
5372 }
5373 }
5374 status = psa_mac_update(&hkdf->hmac,
5375 hkdf->info,
5376 hkdf->info_length);
5377 if (status != PSA_SUCCESS) {
5378 return status;
5379 }
5380 status = psa_mac_update(&hkdf->hmac,
5381 &hkdf->block_number, 1);
5382 if (status != PSA_SUCCESS) {
5383 return status;
5384 }
5385 status = psa_mac_sign_finish(&hkdf->hmac,
5386 hkdf->output_block,
5387 sizeof(hkdf->output_block),
5388 &hmac_output_length);
5389 if (status != PSA_SUCCESS) {
5390 return status;
5391 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005392 }
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005395}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005396#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005397
John Durkop07cc04a2020-11-16 22:08:34 -08005398#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5399 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005400static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5401 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005403{
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5405 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005406 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5407 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005408 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005409
Janos Follath7742fee2019-06-17 12:58:10 +01005410 /* We can't be wanting more output after block 0xff, otherwise
5411 * the capacity check in psa_key_derivation_output_bytes() would have
5412 * prevented this call. It could happen only if the operation
5413 * object was corrupted or if this function is called directly
5414 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (tls12_prf->block_number == 0xff) {
5416 return PSA_ERROR_CORRUPTION_DETECTED;
5417 }
Janos Follath7742fee2019-06-17 12:58:10 +01005418
5419 /* We need a new block */
5420 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005421 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005422
5423 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5424 *
5425 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5426 *
5427 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5428 * HMAC_hash(secret, A(2) + seed) +
5429 * HMAC_hash(secret, A(3) + seed) + ...
5430 *
5431 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005433 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005435 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005436 * is currently extracted as `output_block` and where i is
5437 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005438 */
5439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 status = psa_key_derivation_start_hmac(&hmac,
5441 hash_alg,
5442 tls12_prf->secret,
5443 tls12_prf->secret_length);
5444 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005447
5448 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5451 * the variable seed and in this instance means it in the context of the
5452 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 status = psa_mac_update(&hmac,
5454 tls12_prf->label,
5455 tls12_prf->label_length);
5456 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 }
5459 status = psa_mac_update(&hmac,
5460 tls12_prf->seed,
5461 tls12_prf->seed_length);
5462 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 }
5465 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5468 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 }
5472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_mac_sign_finish(&hmac,
5474 tls12_prf->Ai, hash_length,
5475 &hmac_output_length);
5476 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005477 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 }
5479 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005480 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005482
5483 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_key_derivation_start_hmac(&hmac,
5485 hash_alg,
5486 tls12_prf->secret,
5487 tls12_prf->secret_length);
5488 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
5491 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5492 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005493 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 }
5495 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5496 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005497 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 }
5499 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5500 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005501 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 }
5503 status = psa_mac_sign_finish(&hmac,
5504 tls12_prf->output_block, hash_length,
5505 &hmac_output_length);
5506 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005509
Janos Follath7742fee2019-06-17 12:58:10 +01005510
5511cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 cleanup_status = psa_mac_abort(&hmac);
5513 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005514 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005518}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005519
Janos Follath844eb0e2019-06-19 12:10:49 +01005520static psa_status_t psa_key_derivation_tls12_prf_read(
5521 psa_tls12_prf_key_derivation_t *tls12_prf,
5522 psa_algorithm_t alg,
5523 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005525{
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5527 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005528 psa_status_t status;
5529 uint8_t offset, length;
5530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005532 case PSA_TLS12_PRF_STATE_LABEL_SET:
5533 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5534 break;
5535 case PSA_TLS12_PRF_STATE_OUTPUT:
5536 break;
5537 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005539 }
5540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005542 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 if (tls12_prf->left_in_block == 0) {
5544 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5545 alg);
5546 if (status != PSA_SUCCESS) {
5547 return status;
5548 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005549
5550 continue;
5551 }
5552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005554 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005556 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005558
5559 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005561 output += length;
5562 output_length -= length;
5563 tls12_prf->left_in_block -= length;
5564 }
5565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005567}
John Durkop07cc04a2020-11-16 22:08:34 -08005568#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5569 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005570
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5572static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5573 psa_tls12_ecjpake_to_pms_t *ecjpake,
5574 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576{
5577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005578 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (output_length != 32) {
5581 return PSA_ERROR_INVALID_ARGUMENT;
5582 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5585 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5586 &output_size);
5587 if (status != PSA_SUCCESS) {
5588 return status;
5589 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 if (output_size != output_length) {
5592 return PSA_ERROR_GENERIC_ERROR;
5593 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596}
5597#endif
5598
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305599#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305600static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5601 psa_pbkdf2_key_derivation_t *pbkdf2,
5602 psa_algorithm_t prf_alg,
5603 uint8_t prf_output_length,
5604 psa_key_attributes_t *attributes)
5605{
5606 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305607 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305608 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305609 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305610 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 mac_operation.is_sign = 1;
5615 mac_operation.mac_size = prf_output_length;
5616 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5619 attributes,
5620 pbkdf2->password,
5621 pbkdf2->password_length,
5622 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305623 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305624 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305625 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305626 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5627 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305628 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305629 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305630 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305632 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305633 }
5634 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5635 &mac_output_length);
5636 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305637 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305638 }
5639
5640 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305641 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305642 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305643 }
5644
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305645 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305646
5647 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305648 /* We are passing prf_output_length as mac_size because the driver
5649 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305650 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305651 status = psa_driver_wrapper_mac_compute(attributes,
5652 pbkdf2->password,
5653 pbkdf2->password_length,
5654 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305655 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 &mac_output_length);
5657 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305658 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305659 }
5660
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305661 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305662 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305663
5664cleanup:
5665 /* Zeroise buffers to clear sensitive data from memory. */
5666 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5667 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305668}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305669
5670static psa_status_t psa_key_derivation_pbkdf2_read(
5671 psa_pbkdf2_key_derivation_t *pbkdf2,
5672 psa_algorithm_t kdf_alg,
5673 uint8_t *output,
5674 size_t output_length)
5675{
5676 psa_status_t status;
5677 psa_algorithm_t prf_alg;
5678 uint8_t prf_output_length;
5679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5680 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5681 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5682
5683 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5684 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5685 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5686 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305687 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5688 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305689 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305690 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305691 } else {
5692 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305693 }
5694
5695 switch (pbkdf2->state) {
5696 case PSA_PBKDF2_STATE_PASSWORD_SET:
5697 /* Initially we need a new block so bytes_used is equal to block size*/
5698 pbkdf2->bytes_used = prf_output_length;
5699 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5700 break;
5701 case PSA_PBKDF2_STATE_OUTPUT:
5702 break;
5703 default:
5704 return PSA_ERROR_BAD_STATE;
5705 }
5706
5707 while (output_length != 0) {
5708 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5709 if (n > output_length) {
5710 n = (uint8_t) output_length;
5711 }
5712 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5713 output += n;
5714 output_length -= n;
5715 pbkdf2->bytes_used += n;
5716
5717 if (output_length == 0) {
5718 break;
5719 }
5720
5721 /* We need a new block */
5722 pbkdf2->bytes_used = 0;
5723 pbkdf2->block_number++;
5724
5725 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5726 prf_output_length,
5727 &attributes);
5728 if (status != PSA_SUCCESS) {
5729 return status;
5730 }
5731 }
5732
5733 return PSA_SUCCESS;
5734}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305735#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305736
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005737psa_status_t psa_key_derivation_output_bytes(
5738 psa_key_derivation_operation_t *operation,
5739 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005741{
5742 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005748 }
5749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005752 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005754 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005755 goto exit;
5756 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005759 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005760 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5761 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005763 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005765 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005766 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767
Przemek Stekiel69c46792022-06-10 12:59:51 +02005768#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5770 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5771 output, output_length);
5772 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005773#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005774#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5775 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5777 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5778 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5779 kdf_alg, output,
5780 output_length);
5781 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005782#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5783 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005784#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005786 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5788 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005789#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305790#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305791 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305792 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5793 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305794 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305795#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005796
Gilles Peskineeab56e42018-07-12 17:12:33 +02005797 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005798 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005800 }
5801
5802exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005804 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 * This allows us to differentiate between exhausted operations and
5806 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5807 * operations. */
5808 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005810 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005814}
5815
David Brown7807bf72021-02-09 16:28:23 -07005816#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005817static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005818{
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 if (data_size >= 8) {
5820 mbedtls_des_key_set_parity(data);
5821 }
5822 if (data_size >= 16) {
5823 mbedtls_des_key_set_parity(data + 8);
5824 }
5825 if (data_size >= 24) {
5826 mbedtls_des_key_set_parity(data + 16);
5827 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005828}
David Brown7807bf72021-02-09 16:28:23 -07005829#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005830
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 * ECC keys on a Weierstrass elliptic curve require the generation
5833 * of a private key which is an integer
5834 * in the range [1, N - 1], where N is the boundary of the private key domain:
5835 * N is the prime p for Diffie-Hellman, or the order of the
5836 * curve’s base point for ECC.
5837 *
5838 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5839 * This function generates the private key using the following process:
5840 *
5841 * 1. Draw a byte string of length ceiling(m/8) bytes.
5842 * 2. If m is not a multiple of 8, set the most significant
5843 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5844 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5845 * 4. If k > N - 2, discard the result and return to step 1.
5846 * 5. Output k + 1 as the private key.
5847 *
5848 * This method allows compliance to NIST standards, specifically the methods titled
5849 * Key-Pair Generation by Testing Candidates in the following publications:
5850 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5851 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5852 * Diffie-Hellman keys.
5853 *
5854 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5855 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5856 *
5857 * Note: Function allocates memory for *data buffer, so given *data should be
5858 * always NULL.
5859 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005860#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5861#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005862static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005863 psa_key_slot_t *slot,
5864 size_t bits,
5865 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005866 uint8_t **data
5867 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005868{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005869 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870 mbedtls_mpi k;
5871 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005874 size_t m;
5875 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 mbedtls_mpi_init(&k);
5878 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005879
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005880 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005886 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005887 goto cleanup;
5888 }
5889
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005894
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005895 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005897 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005899 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005900
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005901 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005903
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005904 /* Note: This function is always called with *data == NULL and it
5905 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 *data = mbedtls_calloc(1, m_bytes);
5907 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005908 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005909 goto cleanup;
5910 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005913 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005915 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005917
5918 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005920 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005921 * (8 * ceiling(m/8) - m) bits of the first byte in
5922 * the string to zero.
5923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005925 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005926 }
5927
5928 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 * big-endian byte string.
5930 */
5931 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005932
5933 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 * Result of comparison is returned. When it indicates error
5935 * then this function is called again.
5936 */
5937 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005938 }
5939
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005940 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5942 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005943cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (ret != 0) {
5945 status = mbedtls_to_psa_error(ret);
5946 }
5947 if (status != PSA_SUCCESS) {
5948 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005949 *data = NULL;
5950 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 mbedtls_mpi_free(&k);
5952 mbedtls_mpi_free(&diff_N_2);
5953 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005954}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005955
5956/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5957 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5958 *
5959 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5960 * draw a 32-byte string and process it as specified in
5961 * Elliptic Curves for Security [RFC7748] §5.
5962 *
5963 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5964 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5965 *
5966 * Note: Function allocates memory for *data buffer, so given *data should be
5967 * always NULL.
5968 */
5969
5970static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5971 size_t bits,
5972 psa_key_derivation_operation_t *operation,
5973 uint8_t **data
5974 )
5975{
5976 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005977 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005980 case 255:
5981 output_length = 32;
5982 break;
5983 case 448:
5984 output_length = 56;
5985 break;
5986 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005988 break;
5989 }
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (*data == NULL) {
5994 return PSA_ERROR_INSUFFICIENT_MEMORY;
5995 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006000 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006004 case 255:
6005 (*data)[0] &= 248;
6006 (*data)[31] &= 127;
6007 (*data)[31] |= 64;
6008 break;
6009 case 448:
6010 (*data)[0] &= 252;
6011 (*data)[55] |= 128;
6012 break;
6013 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006015 break;
6016 }
6017
6018 return status;
6019}
Valerio Setti86587ab2023-06-27 13:09:30 +02006020#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6021static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6022 psa_key_slot_t *slot, size_t bits,
6023 psa_key_derivation_operation_t *operation, uint8_t **data)
6024{
6025 (void) slot;
6026 (void) bits;
6027 (void) operation;
6028 (void) data;
6029 return PSA_ERROR_NOT_SUPPORTED;
6030}
6031
6032static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6033 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6034{
6035 (void) bits;
6036 (void) operation;
6037 (void) data;
6038 return PSA_ERROR_NOT_SUPPORTED;
6039}
6040#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6041#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006042
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006043static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006044 psa_key_slot_t *slot,
6045 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006047{
6048 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306050 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006052 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6055 return PSA_ERROR_INVALID_ARGUMENT;
6056 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006057
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006058#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006059 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6061 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6062 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006063 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6065 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 }
6068 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006069 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6071 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006074 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006075 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006076#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006077 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (key_type_is_raw_bytes(slot->attr.type)) {
6079 if (bits % 8 != 0) {
6080 return PSA_ERROR_INVALID_ARGUMENT;
6081 }
6082 data = mbedtls_calloc(1, bytes);
6083 if (data == NULL) {
6084 return PSA_ERROR_INSUFFICIENT_MEMORY;
6085 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 status = psa_key_derivation_output_bytes(operation, data, bytes);
6088 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006089 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 }
David Brown12ca5032021-02-11 11:02:00 -07006091#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6093 psa_des_set_key_parity(data, bytes);
6094 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006095#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 } else {
6097 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006098 }
Ronald Crondd04d422020-11-28 15:14:42 +01006099
Ronald Cronbf33c932020-11-28 18:06:53 +01006100 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006101 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006103 };
6104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6106 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6107 &storage_size);
6108 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306109 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 }
Archanad8a83dc2021-06-14 10:04:16 +05306111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 status = psa_allocate_buffer_to_slot(slot, storage_size);
6113 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 }
Archanad8a83dc2021-06-14 10:04:16 +05306116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 status = psa_driver_wrapper_import_key(&attributes,
6118 data, bytes,
6119 slot->key.data,
6120 slot->key.bytes,
6121 &slot->key.bytes, &bits);
6122 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006123 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006125
6126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 mbedtls_free(data);
6128 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006129}
6130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6132 psa_key_derivation_operation_t *operation,
6133 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006134{
6135 psa_status_t status;
6136 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006137 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006138
Ronald Cron81709fc2020-11-14 12:10:32 +01006139 *key = MBEDTLS_SVC_KEY_ID_INIT;
6140
Gilles Peskine0f84d622019-09-12 19:03:13 +02006141 /* Reject any attempt to create a zero-length key so that we don't
6142 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (psa_get_key_bits(attributes) == 0) {
6144 return PSA_ERROR_INVALID_ARGUMENT;
6145 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 if (operation->alg == PSA_ALG_NONE) {
6148 return PSA_ERROR_BAD_STATE;
6149 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (!operation->can_output_key) {
6152 return PSA_ERROR_NOT_PERMITTED;
6153 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6156 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006159 /* Deriving a key in a secure element is not implemented yet. */
6160 status = PSA_ERROR_NOT_SUPPORTED;
6161 }
6162#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (status == PSA_SUCCESS) {
6164 status = psa_generate_derived_key_internal(slot,
6165 attributes->core.bits,
6166 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006167 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 if (status == PSA_SUCCESS) {
6169 status = psa_finish_key_creation(slot, driver, key);
6170 }
6171 if (status != PSA_SUCCESS) {
6172 psa_fail_key_creation(slot, driver);
6173 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006176}
6177
Gilles Peskineeab56e42018-07-12 17:12:33 +02006178
6179
6180/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006181/* Key derivation */
6182/****************************************************************/
6183
Steven Cooreman932ffb72021-02-15 12:14:32 +01006184#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006185static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006186{
6187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6189 return 1;
6190 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006191#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6194 return 1;
6195 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006196#endif
6197#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6199 return 1;
6200 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006201#endif
6202#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6204 return 1;
6205 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006206#endif
6207#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6209 return 1;
6210 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006211#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006212#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6214 return 1;
6215 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006216#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306217#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6218 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6219 return 1;
6220 }
6221#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306222#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6223 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6224 return 1;
6225 }
6226#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006228}
6229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006231{
6232 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 psa_status_t status = psa_hash_setup(&operation, alg);
6234 psa_hash_abort(&operation);
6235 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006236}
6237
Gilles Peskine969c5d62019-01-16 15:53:06 +01006238static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006239 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006241{
Janos Follath5fe19732019-06-20 15:09:30 +01006242 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6243 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006245
Gilles Peskine969c5d62019-01-16 15:53:06 +01006246 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 if (!is_kdf_alg_supported(kdf_alg)) {
6248 return PSA_ERROR_NOT_SUPPORTED;
6249 }
John Durkop07cc04a2020-11-16 22:08:34 -08006250
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006251 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306252 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6254 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306255 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6256 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6257 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306258 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306259 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 if (hash_size == 0) {
6261 return PSA_ERROR_NOT_SUPPORTED;
6262 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006263
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006264 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6265 * we might fail later, which is somewhat unfriendly and potentially
6266 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 psa_status_t status = psa_hash_try_support(hash_alg);
6268 if (status != PSA_SUCCESS) {
6269 return status;
6270 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006271 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6274 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6275 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6276 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006277 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006278#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006279 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6281 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006282 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006284#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6285 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 operation->capacity = 255 * hash_size;
6287 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006288}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006291{
6292#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 if (alg == PSA_ALG_ECDH) {
6294 return PSA_SUCCESS;
6295 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006296#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006297#if defined(PSA_WANT_ALG_FFDH)
6298 if (alg == PSA_ALG_FFDH) {
6299 return PSA_SUCCESS;
6300 }
6301#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006302 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006304}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006305
6306static int psa_key_derivation_allows_free_form_secret_input(
6307 psa_algorithm_t kdf_alg)
6308{
6309#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6310 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6311 return 0;
6312 }
6313#endif
6314 (void) kdf_alg;
6315 return 1;
6316}
John Durkop07cc04a2020-11-16 22:08:34 -08006317#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6320 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006321{
6322 psa_status_t status;
6323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (operation->alg != 0) {
6325 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006326 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6329 return PSA_ERROR_INVALID_ARGUMENT;
6330 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6331#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6332 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6333 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6334 status = psa_key_agreement_try_support(ka_alg);
6335 if (status != PSA_SUCCESS) {
6336 return status;
6337 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006338 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6339 return PSA_ERROR_INVALID_ARGUMENT;
6340 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6342#else
6343 return PSA_ERROR_NOT_SUPPORTED;
6344#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6345 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6346#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6347 status = psa_key_derivation_setup_kdf(operation, alg);
6348#else
6349 return PSA_ERROR_NOT_SUPPORTED;
6350#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6351 } else {
6352 return PSA_ERROR_INVALID_ARGUMENT;
6353 }
6354
6355 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006356 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 }
6358 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006359}
6360
Przemek Stekiel69c46792022-06-10 12:59:51 +02006361#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006362static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6363 psa_algorithm_t kdf_alg,
6364 psa_key_derivation_step_t step,
6365 const uint8_t *data,
6366 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006367{
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006369 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006371 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006372#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6374 return PSA_ERROR_INVALID_ARGUMENT;
6375 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006376#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (hkdf->state != HKDF_STATE_INIT) {
6378 return PSA_ERROR_BAD_STATE;
6379 } else {
6380 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6381 hash_alg,
6382 data, data_length);
6383 if (status != PSA_SUCCESS) {
6384 return status;
6385 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006386 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006388 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006389 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006390#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006392 /* We shouldn't be in different state as HKDF_EXPAND only allows
6393 * two inputs: SECRET (this case) and INFO which does not modify
6394 * the state. It could happen only if the hkdf
6395 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (hkdf->state != HKDF_STATE_INIT) {
6397 return PSA_ERROR_BAD_STATE;
6398 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006399
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006400 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6402 return PSA_ERROR_INVALID_ARGUMENT;
6403 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 memcpy(hkdf->prk, data, data_length);
6406 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006407#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006408 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006409 /* HKDF: If no salt was provided, use an empty salt.
6410 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006412#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6414 return PSA_ERROR_BAD_STATE;
6415 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006416#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6418 hash_alg,
6419 NULL, 0);
6420 if (status != PSA_SUCCESS) {
6421 return status;
6422 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006423 hkdf->state = HKDF_STATE_STARTED;
6424 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 if (hkdf->state != HKDF_STATE_STARTED) {
6426 return PSA_ERROR_BAD_STATE;
6427 }
6428 status = psa_mac_update(&hkdf->hmac,
6429 data, data_length);
6430 if (status != PSA_SUCCESS) {
6431 return status;
6432 }
6433 status = psa_mac_sign_finish(&hkdf->hmac,
6434 hkdf->prk,
6435 sizeof(hkdf->prk),
6436 &data_length);
6437 if (status != PSA_SUCCESS) {
6438 return status;
6439 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006440 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006441
Gilles Peskine2b522db2019-04-12 15:11:49 +02006442 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006443 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006444#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006446 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006448 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006450#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006451 {
6452 /* Block 0 is empty, and the next block will be
6453 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006458 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006459#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6461 return PSA_ERROR_INVALID_ARGUMENT;
6462 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006463#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006464#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6466 hkdf->state == HKDF_STATE_INIT) {
6467 return PSA_ERROR_BAD_STATE;
6468 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006469#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 if (hkdf->state == HKDF_STATE_OUTPUT) {
6471 return PSA_ERROR_BAD_STATE;
6472 }
6473 if (hkdf->info_set) {
6474 return PSA_ERROR_BAD_STATE;
6475 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006476 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 if (data_length != 0) {
6478 hkdf->info = mbedtls_calloc(1, data_length);
6479 if (hkdf->info == NULL) {
6480 return PSA_ERROR_INSUFFICIENT_MEMORY;
6481 }
6482 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006483 }
6484 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006486 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488 }
6489}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006490#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006491
John Durkop07cc04a2020-11-16 22:08:34 -08006492#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6493 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006494static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6495 const uint8_t *data,
6496 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006497{
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6499 return PSA_ERROR_BAD_STATE;
6500 }
Janos Follathf08e2652019-06-13 09:05:41 +01006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 if (data_length != 0) {
6503 prf->seed = mbedtls_calloc(1, data_length);
6504 if (prf->seed == NULL) {
6505 return PSA_ERROR_INSUFFICIENT_MEMORY;
6506 }
Janos Follathf08e2652019-06-13 09:05:41 +01006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006509 prf->seed_length = data_length;
6510 }
Janos Follathf08e2652019-06-13 09:05:41 +01006511
Gilles Peskine43f958b2020-12-13 14:55:14 +01006512 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006515}
6516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6518 const uint8_t *data,
6519 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006520{
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6522 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6523 return PSA_ERROR_BAD_STATE;
6524 }
Janos Follath81550542019-06-13 14:26:34 +01006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 if (data_length != 0) {
6527 prf->secret = mbedtls_calloc(1, data_length);
6528 if (prf->secret == NULL) {
6529 return PSA_ERROR_INSUFFICIENT_MEMORY;
6530 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006533 prf->secret_length = data_length;
6534 }
Janos Follath81550542019-06-13 14:26:34 +01006535
Gilles Peskine43f958b2020-12-13 14:55:14 +01006536 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006539}
6540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6542 const uint8_t *data,
6543 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006544{
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6546 return PSA_ERROR_BAD_STATE;
6547 }
Janos Follath63028dd2019-06-13 09:15:47 +01006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 if (data_length != 0) {
6550 prf->label = mbedtls_calloc(1, data_length);
6551 if (prf->label == NULL) {
6552 return PSA_ERROR_INSUFFICIENT_MEMORY;
6553 }
Janos Follath63028dd2019-06-13 09:15:47 +01006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006556 prf->label_length = data_length;
6557 }
Janos Follath63028dd2019-06-13 09:15:47 +01006558
Gilles Peskine43f958b2020-12-13 14:55:14 +01006559 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006562}
6563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6565 psa_key_derivation_step_t step,
6566 const uint8_t *data,
6567 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006568{
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006570 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006572 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006574 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006576 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006578 }
6579}
John Durkop07cc04a2020-11-16 22:08:34 -08006580#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6581 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6582
6583#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6584static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6585 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006586 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006588{
6589 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6591 4 + data_length + prf->other_secret_length :
6592 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6595 return PSA_ERROR_INVALID_ARGUMENT;
6596 }
John Durkop07cc04a2020-11-16 22:08:34 -08006597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 uint8_t *pms = mbedtls_calloc(1, pms_len);
6599 if (pms == NULL) {
6600 return PSA_ERROR_INSUFFICIENT_MEMORY;
6601 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006602 uint8_t *cur = pms;
6603
6604 /* pure-PSK:
6605 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006606 *
6607 * The premaster secret is formed as follows: if the PSK is N octets
6608 * long, concatenate a uint16 with the value N, N zero octets, a second
6609 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006610 *
6611 * mixed-PSK:
6612 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6613 * follows: concatenate a uint16 with the length of the other secret,
6614 * the other secret itself, uint16 with the length of PSK, and the
6615 * PSK itself.
6616 * For details please check:
6617 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6618 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6619 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006620 */
6621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6623 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6624 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6625 if (prf->other_secret_length != 0) {
6626 memcpy(cur, prf->other_secret, prf->other_secret_length);
6627 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006628 cur += prf->other_secret_length;
6629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 } else {
6631 *cur++ = MBEDTLS_BYTE_1(data_length);
6632 *cur++ = MBEDTLS_BYTE_0(data_length);
6633 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006634 cur += data_length;
6635 }
6636
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 *cur++ = MBEDTLS_BYTE_1(data_length);
6638 *cur++ = MBEDTLS_BYTE_0(data_length);
6639 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006640 cur += data_length;
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006643
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006644 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006646}
Janos Follath6660f0e2019-06-17 08:44:03 +01006647
Przemek Stekiele47201b2022-04-19 13:53:28 +02006648static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6649 psa_tls12_prf_key_derivation_t *prf,
6650 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006652{
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6654 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006656
6657 if (data_length != 0) {
6658 prf->other_secret = mbedtls_calloc(1, data_length);
6659 if (prf->other_secret == NULL) {
6660 return PSA_ERROR_INSUFFICIENT_MEMORY;
6661 }
6662
6663 memcpy(prf->other_secret, data, data_length);
6664 prf->other_secret_length = data_length;
6665 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006666 prf->other_secret_length = 0;
6667 }
6668
6669 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006672}
6673
Janos Follath6660f0e2019-06-17 08:44:03 +01006674static psa_status_t psa_tls12_prf_psk_to_ms_input(
6675 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006676 psa_key_derivation_step_t step,
6677 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006679{
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006681 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 return psa_tls12_prf_psk_to_ms_set_key(prf,
6683 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006684 break;
6685 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6687 data,
6688 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006689 break;
6690 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006692 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006693
Przemek Stekiele47201b2022-04-19 13:53:28 +02006694 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006695}
John Durkop07cc04a2020-11-16 22:08:34 -08006696#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006697
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006698#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6699static psa_status_t psa_tls12_ecjpake_to_pms_input(
6700 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006701 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006702 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006704{
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6706 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6707 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006708 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006709
6710 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 if (data[0] != 0x04) {
6712 return PSA_ERROR_INVALID_ARGUMENT;
6713 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006714
6715 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006719}
6720#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306721
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306722#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306723static psa_status_t psa_pbkdf2_set_input_cost(
6724 psa_pbkdf2_key_derivation_t *pbkdf2,
6725 psa_key_derivation_step_t step,
6726 uint64_t data)
6727{
6728 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6729 return PSA_ERROR_INVALID_ARGUMENT;
6730 }
6731
6732 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6733 return PSA_ERROR_BAD_STATE;
6734 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306735
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306736 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306737 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306738 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306739
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306740 if (data == 0) {
6741 return PSA_ERROR_INVALID_ARGUMENT;
6742 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306743
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306744 pbkdf2->input_cost = data;
6745 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6746
6747 return PSA_SUCCESS;
6748}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306749
6750static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6751 const uint8_t *data,
6752 size_t data_length)
6753{
Gilles Peskineead17662023-08-20 22:02:51 +02006754 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6755 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6756 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6757 /* Appending to existing salt. No state change. */
6758 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306759 return PSA_ERROR_BAD_STATE;
6760 }
6761
Gilles Peskineca57d782023-07-20 19:08:06 +02006762 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006763 /* Appending an empty string, nothing to do. */
6764 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306765 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306766
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306767 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6768 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306769 return PSA_ERROR_INSUFFICIENT_MEMORY;
6770 }
6771
Gilles Peskineead17662023-08-20 22:02:51 +02006772 if (pbkdf2->salt_length != 0) {
6773 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6774 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306775 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306776 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306777 mbedtls_free(pbkdf2->salt);
6778 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306779 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306780 return PSA_SUCCESS;
6781}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306782
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306783#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306784static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306785 const uint8_t *input,
6786 size_t input_len,
6787 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306788 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306789{
6790 psa_status_t status = PSA_SUCCESS;
6791 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6792 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306793 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306794 } else {
6795 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306796 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306797 }
6798 return status;
6799}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306800#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306801
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306802#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306803static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6804 size_t input_len,
6805 uint8_t *output,
6806 size_t *output_len)
6807{
6808 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306809 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306811 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6813 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6814 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306815 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306816 * mac_size as the driver function sets mac_output_length = mac_size
6817 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306818 status = psa_driver_wrapper_mac_compute(&attributes,
6819 zeros, sizeof(zeros),
6820 PSA_ALG_CMAC, input, input_len,
6821 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306822 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6823 128U,
6824 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306825 output_len);
6826 } else {
6827 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306828 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306829 }
6830 return status;
6831}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306832#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306833
6834static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306835 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306836 const uint8_t *data,
6837 size_t data_length)
6838{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306839 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306840 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6841 return PSA_ERROR_BAD_STATE;
6842 }
6843
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306844#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306845 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6846 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6847 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6848 pbkdf2->password,
6849 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306850 } else
6851#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6852#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6853 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306854 status = psa_pbkdf2_cmac_set_password(data, data_length,
6855 pbkdf2->password,
6856 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306857 } else
6858#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6859 {
6860 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306861 }
6862
6863 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6864
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306865 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306866}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306867
6868static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306869 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306870 psa_key_derivation_step_t step,
6871 const uint8_t *data,
6872 size_t data_length)
6873{
6874 switch (step) {
6875 case PSA_KEY_DERIVATION_INPUT_SALT:
6876 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6877 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306878 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306879 default:
6880 return PSA_ERROR_INVALID_ARGUMENT;
6881 }
6882}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306883#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306884
Gilles Peskineb8965192019-09-24 16:21:10 +02006885/** Check whether the given key type is acceptable for the given
6886 * input step of a key derivation.
6887 *
6888 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6889 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6890 * Both secret and non-secret inputs can alternatively have the type
6891 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6892 * that the input was passed as a buffer rather than via a key object.
6893 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006894static int psa_key_derivation_check_input_type(
6895 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006897{
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 if (key_type == PSA_KEY_TYPE_DERIVE) {
6901 return PSA_SUCCESS;
6902 }
6903 if (key_type == PSA_KEY_TYPE_NONE) {
6904 return PSA_SUCCESS;
6905 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006906 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006907 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (key_type == PSA_KEY_TYPE_DERIVE) {
6909 return PSA_SUCCESS;
6910 }
6911 if (key_type == PSA_KEY_TYPE_NONE) {
6912 return PSA_SUCCESS;
6913 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006914 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006915 case PSA_KEY_DERIVATION_INPUT_LABEL:
6916 case PSA_KEY_DERIVATION_INPUT_SALT:
6917 case PSA_KEY_DERIVATION_INPUT_INFO:
6918 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6920 return PSA_SUCCESS;
6921 }
6922 if (key_type == PSA_KEY_TYPE_NONE) {
6923 return PSA_SUCCESS;
6924 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006925 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306926 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6927 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6928 return PSA_SUCCESS;
6929 }
6930 if (key_type == PSA_KEY_TYPE_DERIVE) {
6931 return PSA_SUCCESS;
6932 }
6933 if (key_type == PSA_KEY_TYPE_NONE) {
6934 return PSA_SUCCESS;
6935 }
6936 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006937 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939}
6940
Janos Follathb80a94e2019-06-12 15:54:46 +01006941static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006942 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006943 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006944 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006945 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006947{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006948 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 status = psa_key_derivation_check_input_type(step, key_type);
6952 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006953 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955
Przemek Stekiel69c46792022-06-10 12:59:51 +02006956#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6958 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6959 step, data, data_length);
6960 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006961#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006962#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6964 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6965 step, data, data_length);
6966 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006967#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6968#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6970 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6971 step, data, data_length);
6972 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006973#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006974#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006976 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6978 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006979#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306980#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306981 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306982 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6983 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306984 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306985#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006986 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006987 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006988 (void) data;
6989 (void) data_length;
6990 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006992 }
6993
Gilles Peskine224b0d62019-09-23 18:13:17 +02006994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 if (status != PSA_SUCCESS) {
6996 psa_key_derivation_abort(operation);
6997 }
6998 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006999}
7000
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307001static psa_status_t psa_key_derivation_input_integer_internal(
7002 psa_key_derivation_operation_t *operation,
7003 psa_key_derivation_step_t step,
7004 uint64_t value)
7005{
7006 psa_status_t status;
7007 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7008
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307009#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307010 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307011 status = psa_pbkdf2_set_input_cost(
7012 &operation->ctx.pbkdf2, step, value);
7013 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307014#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307015 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307016 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307017 (void) value;
7018 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307019 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307020 }
7021
7022 if (status != PSA_SUCCESS) {
7023 psa_key_derivation_abort(operation);
7024 }
7025 return status;
7026}
7027
Janos Follath51f4a0f2019-06-14 11:35:55 +01007028psa_status_t psa_key_derivation_input_bytes(
7029 psa_key_derivation_operation_t *operation,
7030 psa_key_derivation_step_t step,
7031 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007033{
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 return psa_key_derivation_input_internal(operation, step,
7035 PSA_KEY_TYPE_NONE,
7036 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007037}
7038
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307039psa_status_t psa_key_derivation_input_integer(
7040 psa_key_derivation_operation_t *operation,
7041 psa_key_derivation_step_t step,
7042 uint64_t value)
7043{
7044 return psa_key_derivation_input_integer_internal(operation, step, value);
7045}
7046
Janos Follath51f4a0f2019-06-14 11:35:55 +01007047psa_status_t psa_key_derivation_input_key(
7048 psa_key_derivation_operation_t *operation,
7049 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007051{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007052 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007053 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007054 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007055
Ronald Cron5c522922020-11-14 16:35:34 +01007056 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7058 if (status != PSA_SUCCESS) {
7059 psa_key_derivation_abort(operation);
7060 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007061 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007062
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307063 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7064 * permission to output to a key object. */
7065 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7066 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007067 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 status = psa_key_derivation_input_internal(operation,
7071 step, slot->attr.type,
7072 slot->key.data,
7073 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007078}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007079
7080
7081
7082/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007083/* Key agreement */
7084/****************************************************************/
7085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7087 const uint8_t *key_buffer,
7088 size_t key_buffer_size,
7089 psa_algorithm_t alg,
7090 const uint8_t *peer_key,
7091 size_t peer_key_length,
7092 uint8_t *shared_secret,
7093 size_t shared_secret_size,
7094 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007095{
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007097#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007098 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7100 key_buffer_size, alg,
7101 peer_key, peer_key_length,
7102 shared_secret,
7103 shared_secret_size,
7104 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007105#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007106
7107#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7108 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007109 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007110 peer_key,
7111 peer_key_length,
7112 key_buffer,
7113 key_buffer_size,
7114 shared_secret,
7115 shared_secret_size,
7116 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007117#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7118
Gilles Peskine01d718c2018-09-18 12:01:02 +02007119 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007120 (void) attributes;
7121 (void) key_buffer;
7122 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007123 (void) peer_key;
7124 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007125 (void) shared_secret;
7126 (void) shared_secret_size;
7127 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007129 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007130}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007131
Aditya Deshpande17845b82022-10-13 17:21:01 +01007132/** Internal function for raw key agreement
7133 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007134 * to the driver's implementation if a driver is present.
7135 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007136 * (psa_key_agreement_raw_builtin).
7137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7139 psa_key_slot_t *private_key,
7140 const uint8_t *peer_key,
7141 size_t peer_key_length,
7142 uint8_t *shared_secret,
7143 size_t shared_secret_size,
7144 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007145{
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7147 return PSA_ERROR_NOT_SUPPORTED;
7148 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007149
7150 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007152 };
7153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 return psa_driver_wrapper_key_agreement(&attributes,
7155 private_key->key.data,
7156 private_key->key.bytes, alg,
7157 peer_key, peer_key_length,
7158 shared_secret,
7159 shared_secret_size,
7160 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007161}
7162
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007163/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007164 * to potentially free embedded data structures and wipe confidential data.
7165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007166static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7167 psa_key_derivation_step_t step,
7168 psa_key_slot_t *private_key,
7169 const uint8_t *peer_key,
7170 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007171{
7172 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007173 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007174 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176
7177 /* Step 1: run the secret agreement algorithm to generate the shared
7178 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 status = psa_key_agreement_raw_internal(ka_alg,
7180 private_key,
7181 peer_key, peer_key_length,
7182 shared_secret,
7183 sizeof(shared_secret),
7184 &shared_secret_length);
7185 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007186 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007189 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007190 * the shared secret. A shared secret is permitted wherever a key
7191 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 status = psa_key_derivation_input_internal(operation, step,
7193 PSA_KEY_TYPE_DERIVE,
7194 shared_secret,
7195 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7198 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007199}
7200
Gilles Peskine449bd832023-01-11 14:50:10 +01007201psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7202 psa_key_derivation_step_t step,
7203 mbedtls_svc_key_id_t private_key,
7204 const uint8_t *peer_key,
7205 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007206{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007208 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007209 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007210
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7212 return PSA_ERROR_INVALID_ARGUMENT;
7213 }
Ronald Cron5c522922020-11-14 16:35:34 +01007214 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7216 if (status != PSA_SUCCESS) {
7217 return status;
7218 }
7219 status = psa_key_agreement_internal(operation, step,
7220 slot,
7221 peer_key, peer_key_length);
7222 if (status != PSA_SUCCESS) {
7223 psa_key_derivation_abort(operation);
7224 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007225 /* If a private key has been added as SECRET, we allow the derived
7226 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007228 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007230 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007231
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007235}
7236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7238 mbedtls_svc_key_id_t private_key,
7239 const uint8_t *peer_key,
7240 size_t peer_key_length,
7241 uint8_t *output,
7242 size_t output_size,
7243 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007244{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007246 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007247 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007248 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251 status = PSA_ERROR_INVALID_ARGUMENT;
7252 goto exit;
7253 }
Ronald Cron5c522922020-11-14 16:35:34 +01007254 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7256 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007257 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007259
Gilles Peskine3e561302022-04-14 00:17:15 +02007260 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7261 * for the output size. The PSA specification only guarantees that this
7262 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7263 * but it might be nice to allow smaller buffers if the output fits.
7264 * At the time of writing this comment, with only ECDH implemented,
7265 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7266 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7267 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007268 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7270 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007271 status = PSA_ERROR_BUFFER_TOO_SMALL;
7272 goto exit;
7273 }
7274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 status = psa_key_agreement_raw_internal(alg, slot,
7276 peer_key, peer_key_length,
7277 output, output_size,
7278 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007279
7280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007282 /* If an error happens and is not handled properly, the output
7283 * may be used as a key to protect sensitive data. Arrange for such
7284 * a key to be random, which is likely to result in decryption or
7285 * verification errors. This is better than filling the buffer with
7286 * some constant data such as zeros, which would result in the data
7287 * being protected with a reproducible, easily knowable key.
7288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007290 *output_length = output_size;
7291 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007292
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007294
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007296}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007297
7298
Gilles Peskine30524eb2020-11-13 17:02:26 +01007299
Gilles Peskine86a440b2018-11-12 18:39:40 +01007300/****************************************************************/
7301/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007302/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007303
Gilles Peskine672a7712023-04-28 21:00:28 +02007304#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7305#include "entropy_poll.h"
7306#endif
7307
Gilles Peskine30524eb2020-11-13 17:02:26 +01007308/** Initialize the PSA random generator.
7309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007310static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007311{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007312#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007314#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7315
Gilles Peskine30524eb2020-11-13 17:02:26 +01007316 /* Set default configuration if
7317 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007319 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 }
7321 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007322 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7327 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7328 /* The PSA entropy injection feature depends on using NV seed as an entropy
7329 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 mbedtls_entropy_add_source(&rng->entropy,
7331 mbedtls_nv_seed_poll, NULL,
7332 MBEDTLS_ENTROPY_BLOCK_SIZE,
7333 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007334#endif
7335
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007337#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007338}
7339
7340/** Deinitialize the PSA random generator.
7341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007342static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007343{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007344#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7348 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007349#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007350}
7351
7352/** Seed the PSA random generator.
7353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007354static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007355{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007356#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7357 /* Do nothing: the external RNG seeds itself. */
7358 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007360#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007361 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7363 drbg_seed, sizeof(drbg_seed) - 1);
7364 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007365#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007366}
7367
Gilles Peskine449bd832023-01-11 14:50:10 +01007368psa_status_t psa_generate_random(uint8_t *output,
7369 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007370{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007371 GUARD_MODULE_INITIALIZED;
7372
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007373#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7374
7375 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7377 output, output_size,
7378 &output_length);
7379 if (status != PSA_SUCCESS) {
7380 return status;
7381 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007382 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007383 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 if (output_length != output_size) {
7385 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7386 }
7387 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007388
7389#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7390
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007392 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7394 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7395 output_size);
7396 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7397 output, request_size);
7398 if (ret != 0) {
7399 return mbedtls_to_psa_error(ret);
7400 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007401 output_size -= request_size;
7402 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007403 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007405#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007406}
7407
Gilles Peskine8814fc42020-12-14 15:33:44 +01007408/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007409 *
7410 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7411 * `psa_generate_random(...)`. The state parameter is ignored since the
7412 * PSA API doesn't support passing an explicit state.
7413 *
7414 * In the non-external case, psa_generate_random() calls an
7415 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7416 * and semantics as mbedtls_psa_get_random(). As an optimization,
7417 * instead of doing this back-and-forth between the PSA API and the
7418 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7419 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007421#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7422int mbedtls_psa_get_random(void *p_rng,
7423 unsigned char *output,
7424 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007425{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007426 /* This function takes a pointer to the RNG state because that's what
7427 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7428 * its own state internally and doesn't let the caller access that state.
7429 * So we just ignore the state parameter, and in practice we'll pass
7430 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007431 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 psa_status_t status = psa_generate_random(output, output_size);
7433 if (status == PSA_SUCCESS) {
7434 return 0;
7435 } else {
7436 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7437 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007438}
7439#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7440
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007441#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007442psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7443 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007444{
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (global_data.initialized) {
7446 return PSA_ERROR_NOT_PERMITTED;
7447 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7450 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7451 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7452 return PSA_ERROR_INVALID_ARGUMENT;
7453 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007456}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007457#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007458
Ronald Cron3772afe2021-02-08 16:10:05 +01007459/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007460 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007461 * \param type The key type
7462 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007463 *
7464 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007465 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007466 * \retval #PSA_ERROR_INVALID_ARGUMENT
7467 * The size in bits of the key is not valid.
7468 * \retval #PSA_ERROR_NOT_SUPPORTED
7469 * The type and/or the size in bits of the key or the combination of
7470 * the two is not supported.
7471 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007472static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007474{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (key_type_is_raw_bytes(type)) {
7478 status = psa_validate_unstructured_key_bit_size(type, bits);
7479 if (status != PSA_SUCCESS) {
7480 return status;
7481 }
7482 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007483#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7485 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7486 return PSA_ERROR_NOT_SUPPORTED;
7487 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007488 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007489 return PSA_ERROR_NOT_SUPPORTED;
7490 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007491
Ronald Cron01b2aba2020-10-05 09:42:02 +02007492 /* Accept only byte-aligned keys, for the same reasons as
7493 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 if (bits % 8 != 0) {
7495 return PSA_ERROR_NOT_SUPPORTED;
7496 }
7497 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007498#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007499
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007500#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007502 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 return PSA_SUCCESS;
7504 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007505#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007506
Valerio Settia55f0422023-07-10 15:34:41 +02007507#if defined(PSA_WANT_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)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007509 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007510 return PSA_ERROR_NOT_SUPPORTED;
7511 }
7512 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007513#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007514 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516 }
7517
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007519}
7520
Ronald Cron55ed0592020-10-05 10:30:40 +02007521psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007522 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007524{
7525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007526 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007527
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 if ((attributes->domain_parameters == NULL) &&
7529 (attributes->domain_parameters_size != 0)) {
7530 return PSA_ERROR_INVALID_ARGUMENT;
7531 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007532
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (key_type_is_raw_bytes(type)) {
7534 status = psa_generate_random(key_buffer, key_buffer_size);
7535 if (status != PSA_SUCCESS) {
7536 return status;
7537 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007538
David Brown12ca5032021-02-11 11:02:00 -07007539#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 if (type == PSA_KEY_TYPE_DES) {
7541 psa_des_set_key_parity(key_buffer, key_buffer_size);
7542 }
David Brown8107e312021-02-12 08:08:46 -07007543#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007545
Valerio Setti76df8c12023-07-11 14:11:28 +02007546#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7548 return mbedtls_psa_rsa_generate_key(attributes,
7549 key_buffer,
7550 key_buffer_size,
7551 key_buffer_length);
7552 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007553#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007554
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007555#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7557 return mbedtls_psa_ecp_generate_key(attributes,
7558 key_buffer,
7559 key_buffer_size,
7560 key_buffer_length);
7561 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007562#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007563
Valerio Settia55f0422023-07-10 15:34:41 +02007564#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007565 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7566 return mbedtls_psa_ffdh_generate_key(attributes,
7567 key_buffer,
7568 key_buffer_size,
7569 key_buffer_length);
7570 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007571#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007572 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 (void) key_buffer_length;
7574 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007575 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007578}
Darryl Green0c6575a2018-11-07 16:05:30 +00007579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7581 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007582{
7583 psa_status_t status;
7584 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007585 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007586 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007587
Ronald Cron81709fc2020-11-14 12:10:32 +01007588 *key = MBEDTLS_SVC_KEY_ID_INIT;
7589
Gilles Peskine0f84d622019-09-12 19:03:13 +02007590 /* Reject any attempt to create a zero-length key so that we don't
7591 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 if (psa_get_key_bits(attributes) == 0) {
7593 return PSA_ERROR_INVALID_ARGUMENT;
7594 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007595
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007596 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7598 return PSA_ERROR_INVALID_ARGUMENT;
7599 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007600
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7602 &slot, &driver);
7603 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 }
Gilles Peskine11792082019-08-06 18:36:36 +02007606
Ronald Cron977c2472020-10-13 08:32:21 +02007607 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307608 * storage ( thus not in the case of generating a key in a secure element
7609 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7610 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 if (slot->key.data == NULL) {
7612 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7613 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007614 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 attributes->core.type, attributes->core.bits);
7616 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007617 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007619
7620 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 attributes->core.type,
7622 attributes->core.bits);
7623 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007624 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 attributes, &key_buffer_size);
7626 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 }
Ronald Cron977c2472020-10-13 08:32:21 +02007629 }
Ronald Cron977c2472020-10-13 08:32:21 +02007630
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7632 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 }
Ronald Cron977c2472020-10-13 08:32:21 +02007635 }
7636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 status = psa_driver_wrapper_generate_key(attributes,
7638 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 if (status != PSA_SUCCESS) {
7641 psa_remove_key_data_from_memory(slot);
7642 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007643
Gilles Peskine11792082019-08-06 18:36:36 +02007644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 if (status == PSA_SUCCESS) {
7646 status = psa_finish_key_creation(slot, driver, key);
7647 }
7648 if (status != PSA_SUCCESS) {
7649 psa_fail_key_creation(slot, driver);
7650 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007653}
7654
Gilles Peskinee59236f2018-01-27 23:32:46 +01007655/****************************************************************/
7656/* Module setup */
7657/****************************************************************/
7658
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007659#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007660psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 void (* entropy_init)(mbedtls_entropy_context *ctx),
7662 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007663{
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7665 return PSA_ERROR_BAD_STATE;
7666 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007667 global_data.rng.entropy_init = entropy_init;
7668 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007670}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007671#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007672
Gilles Peskine449bd832023-01-11 14:50:10 +01007673void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007674{
Gilles Peskine449bd832023-01-11 14:50:10 +01007675 psa_wipe_all_key_slots();
7676 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7677 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007678 }
7679 /* Wipe all remaining data, including configuration.
7680 * In particular, this sets all state indicator to the value
7681 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007683
7684 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007686}
7687
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007688#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7689/** Recover a transaction that was interrupted by a power failure.
7690 *
7691 * This function is called during initialization, before psa_crypto_init()
7692 * returns. If this function returns a failure status, the initialization
7693 * fails.
7694 */
7695static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007697{
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7700 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 /* TODO - fall through to the failure case until this
7702 * is implemented.
7703 * https://github.com/ARMmbed/mbed-crypto/issues/218
7704 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007705 default:
7706 /* We found an unsupported transaction in the storage.
7707 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007708 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007709 }
7710}
7711#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7712
Gilles Peskine449bd832023-01-11 14:50:10 +01007713psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007714{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007715 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007716
Gilles Peskinec6b69072018-11-20 21:42:52 +01007717 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 if (global_data.initialized != 0) {
7719 return PSA_SUCCESS;
7720 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007721
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007722 /* Init drivers */
7723 status = psa_driver_wrapper_init();
7724 if (status != PSA_SUCCESS) {
7725 goto exit;
7726 }
7727 global_data.drivers_initialized = 1;
7728
Gilles Peskine30524eb2020-11-13 17:02:26 +01007729 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007731 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 status = mbedtls_psa_random_seed(&global_data.rng);
7733 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007734 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007736 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007737
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 status = psa_initialize_key_slots();
7739 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007742
Gilles Peskine4b734222019-07-24 15:56:31 +02007743#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 status = psa_crypto_load_transaction();
7745 if (status == PSA_SUCCESS) {
7746 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7747 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 }
7750 status = psa_crypto_stop_transaction();
7751 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007752 /* There's no transaction to complete. It's all good. */
7753 status = PSA_SUCCESS;
7754 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007755#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007756
Gilles Peskinec6b69072018-11-20 21:42:52 +01007757 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007758 global_data.initialized = 1;
7759
7760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 if (status != PSA_SUCCESS) {
7762 mbedtls_psa_crypto_free();
7763 }
7764 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007765}
7766
Yanray Wangffc3c482023-07-11 12:01:04 +08007767#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007768psa_status_t psa_crypto_driver_pake_get_password_len(
7769 const psa_crypto_driver_pake_inputs_t *inputs,
7770 size_t *password_len)
7771{
7772 if (inputs->password_len == 0) {
7773 return PSA_ERROR_BAD_STATE;
7774 }
7775
7776 *password_len = inputs->password_len;
7777
7778 return PSA_SUCCESS;
7779}
7780
7781psa_status_t psa_crypto_driver_pake_get_password(
7782 const psa_crypto_driver_pake_inputs_t *inputs,
7783 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7784{
7785 if (inputs->password_len == 0) {
7786 return PSA_ERROR_BAD_STATE;
7787 }
7788
7789 if (buffer_size < inputs->password_len) {
7790 return PSA_ERROR_BUFFER_TOO_SMALL;
7791 }
7792
7793 memcpy(buffer, inputs->password, inputs->password_len);
7794 *buffer_length = inputs->password_len;
7795
7796 return PSA_SUCCESS;
7797}
7798
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007799psa_status_t psa_crypto_driver_pake_get_user_len(
7800 const psa_crypto_driver_pake_inputs_t *inputs,
7801 size_t *user_len)
7802{
7803 if (inputs->user_len == 0) {
7804 return PSA_ERROR_BAD_STATE;
7805 }
7806
7807 *user_len = inputs->user_len;
7808
7809 return PSA_SUCCESS;
7810}
7811
7812psa_status_t psa_crypto_driver_pake_get_user(
7813 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007814 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007815{
7816 if (inputs->user_len == 0) {
7817 return PSA_ERROR_BAD_STATE;
7818 }
7819
Przemek Stekielc0e62502023-03-14 11:49:36 +01007820 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007821 return PSA_ERROR_BUFFER_TOO_SMALL;
7822 }
7823
Przemek Stekielc0e62502023-03-14 11:49:36 +01007824 memcpy(user_id, inputs->user, inputs->user_len);
7825 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007826
7827 return PSA_SUCCESS;
7828}
7829
7830psa_status_t psa_crypto_driver_pake_get_peer_len(
7831 const psa_crypto_driver_pake_inputs_t *inputs,
7832 size_t *peer_len)
7833{
7834 if (inputs->peer_len == 0) {
7835 return PSA_ERROR_BAD_STATE;
7836 }
7837
7838 *peer_len = inputs->peer_len;
7839
7840 return PSA_SUCCESS;
7841}
7842
7843psa_status_t psa_crypto_driver_pake_get_peer(
7844 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007845 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007846{
7847 if (inputs->peer_len == 0) {
7848 return PSA_ERROR_BAD_STATE;
7849 }
7850
Przemek Stekielc0e62502023-03-14 11:49:36 +01007851 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007852 return PSA_ERROR_BUFFER_TOO_SMALL;
7853 }
7854
Przemek Stekielc0e62502023-03-14 11:49:36 +01007855 memcpy(peer_id, inputs->peer, inputs->peer_len);
7856 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007857
7858 return PSA_SUCCESS;
7859}
7860
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007861psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7862 const psa_crypto_driver_pake_inputs_t *inputs,
7863 psa_pake_cipher_suite_t *cipher_suite)
7864{
7865 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7866 return PSA_ERROR_BAD_STATE;
7867 }
7868
7869 *cipher_suite = inputs->cipher_suite;
7870
7871 return PSA_SUCCESS;
7872}
7873
Neil Armstronga7d08c32022-06-01 18:21:20 +02007874psa_status_t psa_pake_setup(
7875 psa_pake_operation_t *operation,
7876 const psa_pake_cipher_suite_t *cipher_suite)
7877{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007878 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007879
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007880 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881 status = PSA_ERROR_BAD_STATE;
7882 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007883 }
7884
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007885 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007886 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007887 status = PSA_ERROR_INVALID_ARGUMENT;
7888 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007889 }
7890
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7892
Przemek Stekiele12ed362022-12-21 12:54:46 +01007893 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007894 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7895 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007896 operation->data.inputs.cipher_suite = *cipher_suite;
7897
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007898#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007900 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007901 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007902
David Horstmann16f01512023-06-14 17:21:07 +01007903 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007904 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007905 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007906#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 {
7908 status = PSA_ERROR_NOT_SUPPORTED;
7909 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007910 }
7911
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007912 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7913
Przemek Stekiel51eac532022-12-07 11:04:51 +01007914 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007915exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007916 psa_pake_abort(operation);
7917 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007918}
7919
7920psa_status_t psa_pake_set_password_key(
7921 psa_pake_operation_t *operation,
7922 mbedtls_svc_key_id_t password)
7923{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007924 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007925 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7926 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007927 psa_key_attributes_t attributes;
7928 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007929
Przemek Stekiel51eac532022-12-07 11:04:51 +01007930 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007931 status = PSA_ERROR_BAD_STATE;
7932 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007933 }
7934
Przemek Stekiel6c764412022-11-22 14:05:12 +01007935 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7936 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007937 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007938 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007939 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 }
7941
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007942 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007943 .core = slot->attr
7944 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007945
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007946 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007947
Przemek Stekiel51eac532022-12-07 11:04:51 +01007948 if (type != PSA_KEY_TYPE_PASSWORD &&
7949 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7950 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007951 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007952 }
7953
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7955 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007956 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007957 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007958 }
7959
7960 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7961 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007962 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007963exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007964 if (status != PSA_SUCCESS) {
7965 psa_pake_abort(operation);
7966 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007968 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007969}
7970
7971psa_status_t psa_pake_set_user(
7972 psa_pake_operation_t *operation,
7973 const uint8_t *user_id,
7974 size_t user_id_len)
7975{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007977
7978 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007979 status = PSA_ERROR_BAD_STATE;
7980 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007981 }
7982
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007984 status = PSA_ERROR_INVALID_ARGUMENT;
7985 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007986 }
7987
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007988 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007989 status = PSA_ERROR_BAD_STATE;
7990 goto exit;
7991 }
7992
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007993 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7994 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007995 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7996 goto exit;
7997 }
7998
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007999 memcpy(operation->data.inputs.user, user_id, user_id_len);
8000 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008001
8002 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008003exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008004 psa_pake_abort(operation);
8005 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008006}
8007
8008psa_status_t psa_pake_set_peer(
8009 psa_pake_operation_t *operation,
8010 const uint8_t *peer_id,
8011 size_t peer_id_len)
8012{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008014
8015 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008016 status = PSA_ERROR_BAD_STATE;
8017 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008018 }
8019
Przemek Stekiel51eac532022-12-07 11:04:51 +01008020 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008021 status = PSA_ERROR_INVALID_ARGUMENT;
8022 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008023 }
8024
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008025 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008026 status = PSA_ERROR_BAD_STATE;
8027 goto exit;
8028 }
8029
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008030 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8031 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008032 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8033 goto exit;
8034 }
8035
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008036 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8037 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008038
8039 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008040exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008041 psa_pake_abort(operation);
8042 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008043}
8044
8045psa_status_t psa_pake_set_role(
8046 psa_pake_operation_t *operation,
8047 psa_pake_role_t role)
8048{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008050
Przemek Stekiel51eac532022-12-07 11:04:51 +01008051 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008052 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008053 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008054 }
8055
Przemek Stekiel09104b82023-03-06 14:11:51 +01008056 switch (operation->alg) {
8057#if defined(PSA_WANT_ALG_JPAKE)
8058 case PSA_ALG_JPAKE:
8059 if (role == PSA_PAKE_ROLE_NONE) {
8060 return PSA_SUCCESS;
8061 }
8062 status = PSA_ERROR_INVALID_ARGUMENT;
8063 break;
8064#endif
8065 default:
8066 (void) role;
8067 status = PSA_ERROR_NOT_SUPPORTED;
8068 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008069 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008070exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008071 psa_pake_abort(operation);
8072 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008073}
8074
David Horstmanne7f21e62023-05-12 18:17:21 +01008075/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008076#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008077static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008078 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008079{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008080 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008081 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008082 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008083
David Horstmann024e5c52023-06-14 15:48:21 +01008084 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008085 is_x1 = (stage->outputs < 1);
8086 } else {
8087 is_x1 = (stage->inputs < 1);
8088 }
8089
David Horstmann74a3d8c2023-06-14 18:28:19 +01008090 key_share_step = is_x1 ?
8091 PSA_JPAKE_X1_STEP_KEY_SHARE :
8092 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008093 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008094 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8095 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8096 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8097 } else {
8098 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008099 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008100 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008101}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008102#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008103
Przemek Stekiel2797d372022-12-22 11:19:22 +01008104static psa_status_t psa_pake_complete_inputs(
8105 psa_pake_operation_t *operation)
8106{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008108 /* Create copy of the inputs on stack as inputs share memory
8109 with the driver context which will be setup by the driver. */
8110 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008111
Przemek Stekielfde11282023-03-13 16:06:09 +01008112 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113 return PSA_ERROR_BAD_STATE;
8114 }
8115
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008116 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008117 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8118 return PSA_ERROR_BAD_STATE;
8119 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008120 }
8121
Przemek Stekiel18620a32023-01-17 16:34:52 +01008122 /* Clear driver context */
8123 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8124
8125 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008126
8127 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008128 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008129
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008130 /* User and peer are translated to role. */
8131 mbedtls_free(inputs.user);
8132 mbedtls_free(inputs.peer);
8133
Przemek Stekiel2797d372022-12-22 11:19:22 +01008134 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008135#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008137 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008138 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008139#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008140 {
8141 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008143 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144 return status;
8145}
8146
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008147#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008148static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008149 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008150 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008151 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008152{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8154 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8155 step != PSA_PAKE_STEP_ZK_PROOF) {
8156 return PSA_ERROR_INVALID_ARGUMENT;
8157 }
8158
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008159 psa_jpake_computation_stage_t *computation_stage =
8160 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161
David Horstmann5da95602023-06-08 15:37:12 +01008162 if (computation_stage->round != PSA_JPAKE_FIRST &&
8163 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008164 return PSA_ERROR_BAD_STATE;
8165 }
8166
David Horstmanne7f21e62023-05-12 18:17:21 +01008167 /* Check that the step we are given is the one we were expecting */
8168 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008169 return PSA_ERROR_BAD_STATE;
8170 }
8171
David Horstmanne7f21e62023-05-12 18:17:21 +01008172 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8173 computation_stage->inputs == 0 &&
8174 computation_stage->outputs == 0) {
8175 /* Start of the round, so function decides whether we are inputting
8176 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008177 computation_stage->io_mode = io_mode;
8178 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008179 /* Middle of the round so the mode we are in must match the function
8180 * called by the user */
8181 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008182 }
8183
8184 return PSA_SUCCESS;
8185}
8186
David Horstmanne7f21e62023-05-12 18:17:21 +01008187static psa_status_t psa_jpake_epilogue(
8188 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008189 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008190{
David Horstmanne7f21e62023-05-12 18:17:21 +01008191 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008192 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008193
David Horstmanne7f21e62023-05-12 18:17:21 +01008194 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8195 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008196 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008198 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008199 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 }
8201 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008202 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008203 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008204 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008205 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008206 }
8207 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008208 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8209 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008210 /* End of a round, move to the next round */
8211 stage->inputs = 0;
8212 stage->outputs = 0;
8213 stage->round++;
8214 }
8215 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008216 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008217 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 return PSA_SUCCESS;
8220}
David Horstmanne7f21e62023-05-12 18:17:21 +01008221
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008222#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008223
Neil Armstronga7d08c32022-06-01 18:21:20 +02008224psa_status_t psa_pake_output(
8225 psa_pake_operation_t *operation,
8226 psa_pake_step_t step,
8227 uint8_t *output,
8228 size_t output_size,
8229 size_t *output_length)
8230{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008232 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008233 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008234
8235 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008236 status = psa_pake_complete_inputs(operation);
8237 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008238 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008239 }
8240 }
8241
8242 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008243 status = PSA_ERROR_BAD_STATE;
8244 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008245 }
8246
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008247 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008248 status = PSA_ERROR_INVALID_ARGUMENT;
8249 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008250 }
8251
Przemek Stekiele12ed362022-12-21 12:54:46 +01008252 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008253#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008255 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008257 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008259 driver_step = convert_jpake_computation_stage_to_driver_step(
8260 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008261 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008262#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008264 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008265 status = PSA_ERROR_NOT_SUPPORTED;
8266 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 }
8268
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008269 status = psa_driver_wrapper_pake_output(operation, driver_step,
8270 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271
8272 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008273 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 }
8275
8276 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008277#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008278 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008279 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008281 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 }
8283 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008284#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008285 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008286 status = PSA_ERROR_NOT_SUPPORTED;
8287 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008288 }
8289
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008290 return PSA_SUCCESS;
8291exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008292 psa_pake_abort(operation);
8293 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008294}
8295
8296psa_status_t psa_pake_input(
8297 psa_pake_operation_t *operation,
8298 psa_pake_step_t step,
8299 const uint8_t *input,
8300 size_t input_length)
8301{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008303 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008304 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8305 operation->primitive,
8306 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008307
8308 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008309 status = psa_pake_complete_inputs(operation);
8310 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008311 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008312 }
8313 }
8314
8315 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008316 status = PSA_ERROR_BAD_STATE;
8317 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008318 }
8319
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008320 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008321 status = PSA_ERROR_INVALID_ARGUMENT;
8322 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008323 }
8324
Przemek Stekiele12ed362022-12-21 12:54:46 +01008325 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008326#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008328 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008330 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008332 driver_step = convert_jpake_computation_stage_to_driver_step(
8333 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008334 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008335#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008337 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008338 status = PSA_ERROR_NOT_SUPPORTED;
8339 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008340 }
8341
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008342 status = psa_driver_wrapper_pake_input(operation, driver_step,
8343 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008344
8345 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008346 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008347 }
8348
8349 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008350#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008351 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008352 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008354 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008355 }
8356 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008357#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008358 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008359 status = PSA_ERROR_NOT_SUPPORTED;
8360 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008361 }
8362
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008363 return PSA_SUCCESS;
8364exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008365 psa_pake_abort(operation);
8366 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008367}
8368
8369psa_status_t psa_pake_get_implicit_key(
8370 psa_pake_operation_t *operation,
8371 psa_key_derivation_operation_t *output)
8372{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008374 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008375 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008376 size_t shared_key_len = 0;
8377
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008378 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008379 status = PSA_ERROR_BAD_STATE;
8380 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008381 }
8382
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008383#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008384 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008385 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008386 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008387 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008388 status = PSA_ERROR_BAD_STATE;
8389 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008390 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008391 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008392#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008393 {
8394 status = PSA_ERROR_NOT_SUPPORTED;
8395 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008396 }
8397
Przemek Stekiel0c781802022-11-29 14:53:13 +01008398 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8399 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008400 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008401 &shared_key_len);
8402
8403 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008404 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008405 }
8406
8407 status = psa_key_derivation_input_bytes(output,
8408 PSA_KEY_DERIVATION_INPUT_SECRET,
8409 shared_key,
8410 shared_key_len);
8411
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008412 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008413exit:
8414 abort_status = psa_pake_abort(operation);
8415 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008416}
8417
8418psa_status_t psa_pake_abort(
8419 psa_pake_operation_t *operation)
8420{
Przemek Stekield69dca92023-01-31 19:59:20 +01008421 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008422
Przemek Stekield69dca92023-01-31 19:59:20 +01008423 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008424 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008425 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008427 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8428 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008429 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008430 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008431 }
8432 if (operation->data.inputs.user != NULL) {
8433 mbedtls_free(operation->data.inputs.user);
8434 }
8435 if (operation->data.inputs.peer != NULL) {
8436 mbedtls_free(operation->data.inputs.peer);
8437 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008438 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008439 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008440
Przemek Stekield69dca92023-01-31 19:59:20 +01008441 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008442}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008443#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008444
Gilles Peskinee59236f2018-01-27 23:32:46 +01008445#endif /* MBEDTLS_PSA_CRYPTO_C */