blob: 8e339ea79fac9c82f13584dae496af09337e1044 [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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100161 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
162 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100163#endif
164
165#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200166 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
167 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
168 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
169 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
170 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200172 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100176#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200177
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100178#if defined(MBEDTLS_CAMELLIA_C)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100180 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100184#if defined(MBEDTLS_CCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100189#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100190
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100191#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200192 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100194#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200195
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100196#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200199 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100201#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200202
Dave Rodgman509b5672023-08-16 19:26:23 +0100203#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100218#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
221 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100222 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
223 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100231#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100232
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100233#if defined(MBEDTLS_DES_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100234 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100236#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
Gilles Peskinee59236f2018-01-27 23:32:46 +0100238 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
239 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
240 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100242
Dave Rodgman4f47f3d2023-08-31 12:10:00 +0100243#if defined(MBEDTLS_GCM_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200246 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100250#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100251
Gilles Peskine82e57d12020-11-13 21:31:17 +0100252#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100254 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
255 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100258 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
259 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100261 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100263#endif
264
Dave Rodgmandea266f2023-08-31 11:52:43 +0100265#if defined(MBEDTLS_MD_LIGHT)
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100272#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100273 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100275#endif
276#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100277
Dave Rodgman509b5672023-08-16 19:26:23 +0100278#if defined(MBEDTLS_BIGNUM_C)
279#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100282#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100287 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100289 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100291 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100293 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100295 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100297#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100298
Dave Rodgman509b5672023-08-16 19:26:23 +0100299#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
303 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100305#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
306 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100309#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
311 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100313 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
316 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 case MBEDTLS_ERR_PK_INVALID_ALG:
321 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
322 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200326 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100328#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100329
Gilles Peskineff2d2002019-05-06 15:26:23 +0200330 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200332 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200334
Dave Rodgman509b5672023-08-16 19:26:23 +0100335#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100336 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100338 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
345 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100349 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100353#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200354
Dave Rodgman1dab4452023-09-01 09:59:51 +0100355#if defined(MBEDTLS_ECP_LIGHT)
itayzafrir5c753392018-05-08 11:18:38 +0300356 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
364 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100368 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100370
Dave Rodgman509b5672023-08-16 19:26:23 +0100371#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000372 case MBEDTLS_ERR_ECP_IN_PROGRESS:
373 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100374#endif
375#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000376
Janos Follatha13b9052019-11-22 12:48:59 +0000377 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300379
Gilles Peskinee59236f2018-01-27 23:32:46 +0100380 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100382 }
383}
384
Paul Elliottdc42ca82023-02-24 18:11:59 +0000385/**
386 * \brief For output buffers which contain "tags"
387 * (outputs that may be checked for validity like
388 * hashes, MACs and signatures), fill the unused
389 * part of the output buffer (the whole buffer on
390 * error, the trailing part on success) with
391 * something that isn't a valid tag (barring an
392 * attack on the tag and deliberately-crafted
393 * input), in case the caller doesn't check the
394 * return status properly.
395 *
396 * \param output_buffer Pointer to buffer to wipe. May not be NULL
397 * unless \p output_buffer_size is zero.
398 * \param status Status of function called to generate
399 * output_buffer originally
400 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
401 * could be NULL.
402 * \param output_buffer_length Length of data written to output_buffer, must be
403 * less than \p output_buffer_size
404 */
405static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000406 size_t output_buffer_size, size_t output_buffer_length)
407{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000408 size_t offset = 0;
409
410 if (output_buffer_size == 0) {
411 /* If output_buffer_size is 0 then we have nothing to do. We must not
412 call memset because output_buffer may be NULL in this case */
413 return;
414 }
415
416 if (status == PSA_SUCCESS) {
417 offset = output_buffer_length;
418 }
419
420 memset(output_buffer + offset, '!', output_buffer_size - offset);
421}
422
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200423
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200424
425
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100426/****************************************************************/
427/* Key management */
428/****************************************************************/
429
Valerio Settia9aab1a2023-06-19 13:39:54 +0200430#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200431psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
432 size_t *bits)
433{
434 switch (grpid) {
Valerio Settic437fae2023-09-08 14:58:03 +0200435#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200436 case MBEDTLS_ECP_DP_SECP192R1:
437 *bits = 192;
438 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100439#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200440#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200441 case MBEDTLS_ECP_DP_SECP224R1:
442 *bits = 224;
443 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100444#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200445#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200446 case MBEDTLS_ECP_DP_SECP256R1:
447 *bits = 256;
448 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100449#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200450#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200451 case MBEDTLS_ECP_DP_SECP384R1:
452 *bits = 384;
453 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100454#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200455#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200456 case MBEDTLS_ECP_DP_SECP521R1:
457 *bits = 521;
458 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100459#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200460#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200461 case MBEDTLS_ECP_DP_BP256R1:
462 *bits = 256;
463 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100464#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200465#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200466 case MBEDTLS_ECP_DP_BP384R1:
467 *bits = 384;
468 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100469#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200470#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200471 case MBEDTLS_ECP_DP_BP512R1:
472 *bits = 512;
473 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100474#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200475#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200476 case MBEDTLS_ECP_DP_CURVE25519:
477 *bits = 255;
478 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100479#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200480#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200481 case MBEDTLS_ECP_DP_SECP192K1:
482 *bits = 192;
483 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100484#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200485#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200486 case MBEDTLS_ECP_DP_SECP224K1:
487 *bits = 224;
488 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100489#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200490#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200491 case MBEDTLS_ECP_DP_SECP256K1:
492 *bits = 256;
493 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100494#endif
Valerio Settic437fae2023-09-08 14:58:03 +0200495#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200496 case MBEDTLS_ECP_DP_CURVE448:
497 *bits = 448;
498 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100499#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200500 default:
501 *bits = 0;
502 return 0;
503 }
504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
507 size_t bits,
508 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200509{
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100511 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100513#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
525#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100526 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100528#endif
529#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100530 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100532 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (bits_is_sloppy) {
534 return MBEDTLS_ECP_DP_SECP521R1;
535 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100536 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100537#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100538 }
539 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100540
Paul Elliott8ff510a2020-06-02 17:19:28 +0100541 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100543#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100544 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100546#endif
547#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100548 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100550#endif
551#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100552 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100554#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100555 }
556 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100557
Paul Elliott8ff510a2020-06-02 17:19:28 +0100558 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100560#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100561 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100563 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (bits_is_sloppy) {
565 return MBEDTLS_ECP_DP_CURVE25519;
566 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100567 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100568#endif
569#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100570 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100572#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100573 }
574 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100575
Paul Elliott8ff510a2020-06-02 17:19:28 +0100576 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100578#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100579 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100581#endif
582#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100583 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100585#endif
586#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100587 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100589#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100590 }
591 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200592 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100593
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100594 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200596}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200597#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
600 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200601{
602 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200604 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200606 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200607 case PSA_KEY_TYPE_PASSWORD:
608 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200609 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100610#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200611 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 if (bits != 128 && bits != 192 && bits != 256) {
613 return PSA_ERROR_INVALID_ARGUMENT;
614 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200615 break;
616#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200617#if defined(PSA_WANT_KEY_TYPE_ARIA)
618 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (bits != 128 && bits != 192 && bits != 256) {
620 return PSA_ERROR_INVALID_ARGUMENT;
621 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200622 break;
623#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100624#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200625 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (bits != 128 && bits != 192 && bits != 256) {
627 return PSA_ERROR_INVALID_ARGUMENT;
628 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200629 break;
630#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100631#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200632 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (bits != 64 && bits != 128 && bits != 192) {
634 return PSA_ERROR_INVALID_ARGUMENT;
635 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200636 break;
637#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100638#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200639 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (bits != 256) {
641 return PSA_ERROR_INVALID_ARGUMENT;
642 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200643 break;
644#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200645 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200647 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (bits % 8 != 0) {
649 return PSA_ERROR_INVALID_ARGUMENT;
650 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200653}
654
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100655/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100656 *
Steven Cooremancd640932021-03-08 12:00:27 +0100657 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
658 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100659 *
660 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100661 * \param[in] key_type The key type of the key to be used with the
662 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100663 *
664 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100665 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100666 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100667 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100668 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100669MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100670 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100672{
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (PSA_ALG_IS_HMAC(algorithm)) {
674 if (key_type == PSA_KEY_TYPE_HMAC) {
675 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100676 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100677 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
680 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
681 * key. */
682 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
683 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
684 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
685 * the block length (larger than 1) for block ciphers. */
686 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
687 return PSA_SUCCESS;
688 }
689 }
690 }
691
692 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100693}
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
696 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200697{
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (slot->key.data != NULL) {
699 return PSA_ERROR_ALREADY_EXISTS;
700 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 slot->key.data = mbedtls_calloc(1, buffer_length);
703 if (slot->key.data == NULL) {
704 return PSA_ERROR_INSUFFICIENT_MEMORY;
705 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200706
Ronald Cronea0f8a62020-11-25 17:52:23 +0100707 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200709}
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
712 const uint8_t *data,
713 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200714{
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 psa_status_t status = psa_allocate_buffer_to_slot(slot,
716 data_length);
717 if (status != PSA_SUCCESS) {
718 return status;
719 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 memcpy(slot->key.data, data, data_length);
722 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200723}
724
Ronald Crona0fe59f2020-11-29 11:14:53 +0100725psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100726 const psa_key_attributes_t *attributes,
727 const uint8_t *data, size_t data_length,
728 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100731 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
732 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200734 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (data_length == 0) {
736 return PSA_ERROR_NOT_SUPPORTED;
737 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 if (key_type_is_raw_bytes(type)) {
740 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
743 *bits);
744 if (status != PSA_SUCCESS) {
745 return status;
746 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200747
Ronald Crondd04d422020-11-28 15:14:42 +0100748 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100750 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return PSA_SUCCESS;
754 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200755#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200756 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100757 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200758 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100759 return PSA_ERROR_INVALID_ARGUMENT;
760 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200761 return mbedtls_psa_ffdh_import_key(attributes,
762 data, data_length,
763 key_buffer, key_buffer_size,
764 key_buffer_length,
765 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100766 }
Valerio Settia55f0422023-07-10 15:34:41 +0200767#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200768 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200769#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
771 if (PSA_KEY_TYPE_IS_ECC(type)) {
772 return mbedtls_psa_ecp_import_key(attributes,
773 data, data_length,
774 key_buffer, key_buffer_size,
775 key_buffer_length,
776 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100777 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200778#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800779 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200780#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
781 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
783 if (PSA_KEY_TYPE_IS_RSA(type)) {
784 return mbedtls_psa_rsa_import_key(attributes,
785 data, data_length,
786 key_buffer, key_buffer_size,
787 key_buffer_length,
788 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200789 }
Valerio Settife478902023-07-25 12:27:19 +0200790#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
791 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800792 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100793 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100796}
797
Gilles Peskinef603c712019-01-19 13:40:11 +0100798/** Calculate the intersection of two algorithm usage policies.
799 *
800 * Return 0 (which allows no operation) on incompatibility.
801 */
802static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100803 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100804 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100806{
Gilles Peskine549ea862019-05-22 11:45:59 +0200807 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (alg1 == alg2) {
809 return alg1;
810 }
Steven Cooreman03488022021-02-18 12:07:20 +0100811 /* If the policies are from the same hash-and-sign family, check
812 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
814 PSA_ALG_IS_SIGN_HASH(alg2) &&
815 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
816 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
817 return alg2;
818 }
819 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
820 return alg1;
821 }
Steven Cooreman03488022021-02-18 12:07:20 +0100822 }
823 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100824 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100825 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
827 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
828 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
829 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
830 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100831 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100832
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100833 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
835 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
836 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
837 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100838 }
839 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
841 (alg1_len <= alg2_len)) {
842 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100843 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
845 (alg2_len <= alg1_len)) {
846 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100847 }
848 }
849 /* If the policies are from the same MAC family, check whether one
850 * of them is a minimum-MAC-length policy. Calculate the most
851 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
853 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
854 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100855 /* Validate the combination of key type and algorithm. Since the base
856 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
858 return 0;
859 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100860
Steven Cooreman31a876d2021-03-03 20:47:40 +0100861 /* Get the (exact or at-least) output lengths for both sides of the
862 * requested intersection. None of the currently supported algorithms
863 * have an output length dependent on the actual key size, so setting it
864 * to a bogus value of 0 is currently OK.
865 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100866 * Note that for at-least-this-length wildcard algorithms, the output
867 * length is set to the shortest allowed length, which allows us to
868 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
870 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100871 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100872
Steven Cooremana1d83222021-02-25 10:20:29 +0100873 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
875 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
876 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100877 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100878
879 /* If only one is an at-least-this-length policy, the intersection would
880 * be the other (fixed-length) policy as long as said fixed length is
881 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
883 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100884 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
886 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100887 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100888
Steven Cooremancd640932021-03-08 12:00:27 +0100889 /* If none of them are wildcards, check whether they define the same tag
890 * length. This is still possible here when one is default-length and
891 * the other specific-length. Ensure to always return the
892 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (alg1_len == alg2_len) {
894 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
895 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100896 }
897 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100899}
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901static int psa_key_algorithm_permits(psa_key_type_t key_type,
902 psa_algorithm_t policy_alg,
903 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200904{
Gilles Peskine549ea862019-05-22 11:45:59 +0200905 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (requested_alg == policy_alg) {
907 return 1;
908 }
Steven Cooreman03488022021-02-18 12:07:20 +0100909 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
910 * and requested_alg is the same hash-and-sign family with any hash,
911 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
913 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
914 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
915 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100916 }
917 /* If policy_alg is a wildcard AEAD algorithm of the same base as
918 * the requested algorithm, check the requested tag length to be
919 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (PSA_ALG_IS_AEAD(policy_alg) &&
921 PSA_ALG_IS_AEAD(requested_alg) &&
922 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
923 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
924 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
925 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
926 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100927 }
Steven Cooremancd640932021-03-08 12:00:27 +0100928 /* If policy_alg is a MAC algorithm of the same base as the requested
929 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (PSA_ALG_IS_MAC(policy_alg) &&
931 PSA_ALG_IS_MAC(requested_alg) &&
932 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
933 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100934 /* Validate the combination of key type and algorithm. Since the policy
935 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
937 return 0;
938 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100939
Steven Cooreman31a876d2021-03-03 20:47:40 +0100940 /* Get both the requested output length for the algorithm which is to be
941 * verified, and the default output length for the base algorithm.
942 * Note that none of the currently supported algorithms have an output
943 * length dependent on actual key size, so setting it to a bogus value
944 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100945 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100947 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 key_type, 0,
949 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100950
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100951 /* If the policy is default-length, only allow an algorithm with
952 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
954 return requested_output_length == default_output_length;
955 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100956
957 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100958 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
960 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
961 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100962 }
963
Steven Cooremancd640932021-03-08 12:00:27 +0100964 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
965 * check for the requested MAC length to be equal to or longer than the
966 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
968 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
969 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100970 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200971 }
Steven Cooremance48e852020-10-05 16:02:45 +0200972 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200973 * a key derivation with that key agreement should also be allowed. This
974 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
976 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
977 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
978 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200979 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100980 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200982}
983
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984/** Test whether a policy permits an algorithm.
985 *
986 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100987 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100988 * \note This function requires providing the key type for which the policy is
989 * being validated, since some algorithm policy definitions (e.g. MAC)
990 * have different properties depending on what kind of cipher it is
991 * combined with.
992 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100993 * \retval PSA_SUCCESS When \p alg is a specific algorithm
994 * allowed by the \p policy.
995 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
996 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
997 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100998 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1000 psa_key_type_t key_type,
1001 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001002{
Steven Cooremand788fab2021-02-25 11:29:17 +01001003 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (alg == 0) {
1005 return PSA_ERROR_INVALID_ARGUMENT;
1006 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001007
Steven Cooreman7e39f052021-02-23 14:18:32 +01001008 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (PSA_ALG_IS_WILDCARD(alg)) {
1010 return PSA_ERROR_INVALID_ARGUMENT;
1011 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1014 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1015 return PSA_SUCCESS;
1016 } else {
1017 return PSA_ERROR_NOT_PERMITTED;
1018 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001019}
1020
Gilles Peskinef603c712019-01-19 13:40:11 +01001021/** Restrict a key policy based on a constraint.
1022 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001023 * \note This function requires providing the key type for which the policy is
1024 * being restricted, since some algorithm policy definitions (e.g. MAC)
1025 * have different properties depending on what kind of cipher it is
1026 * combined with.
1027 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001028 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001029 * \param[in,out] policy The policy to restrict.
1030 * \param[in] constraint The policy constraint to apply.
1031 *
1032 * \retval #PSA_SUCCESS
1033 * \c *policy contains the intersection of the original value of
1034 * \c *policy and \c *constraint.
1035 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001036 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001037 * \c *policy is unchanged.
1038 */
1039static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001040 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001041 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001043{
1044 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1046 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001047 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1049 constraint->alg2);
1050 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1051 return PSA_ERROR_INVALID_ARGUMENT;
1052 }
1053 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1054 return PSA_ERROR_INVALID_ARGUMENT;
1055 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001056 policy->usage &= constraint->usage;
1057 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001058 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001060}
1061
Przemek Stekiel061f6942022-11-30 10:51:35 +01001062/** Get the description of a key given its identifier and policy constraints
1063 * and lock it.
1064 *
1065 * The key must have allow all the usage flags set in \p usage. If \p alg is
1066 * nonzero, the key must allow operations with this algorithm. If \p alg is
1067 * zero, the algorithm is not checked.
1068 *
1069 * In case of a persistent key, the function loads the description of the key
1070 * into a key slot if not already done.
1071 *
1072 * On success, the returned key slot is locked. It is the responsibility of
1073 * the caller to unlock the key slot when it does not access it anymore.
1074 */
1075static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001076 mbedtls_svc_key_id_t key,
1077 psa_key_slot_t **p_slot,
1078 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001080{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001081 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001082 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 status = psa_get_and_lock_key_slot(key, p_slot);
1085 if (status != PSA_SUCCESS) {
1086 return status;
1087 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001088 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001089
1090 /* Enforce that usage policy for the key slot contains all the flags
1091 * required by the usage parameter. There is one exception: public
1092 * keys can always be exported, so we treat public key objects as
1093 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001095 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001099 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001101 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001102
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001103 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (alg != 0) {
1105 status = psa_key_policy_permits(&slot->attr.policy,
1106 slot->attr.type,
1107 alg);
1108 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001109 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001111 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001114
1115error:
1116 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001120}
Darryl Green940d72c2018-07-13 13:18:51 +01001121
Ronald Cron5c522922020-11-14 16:35:34 +01001122/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001123 *
1124 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001125 * available, as opposed to a key in a secure element and/or to be used
1126 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001127 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001128 * This is a temporary function that may be used instead of
1129 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1130 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001131 *
Ronald Cron5c522922020-11-14 16:35:34 +01001132 * On success, the returned key slot is locked. It is the responsibility of the
1133 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001134 */
Ronald Cron5c522922020-11-14 16:35:34 +01001135static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1136 mbedtls_svc_key_id_t key,
1137 psa_key_slot_t **p_slot,
1138 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001140{
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1142 usage, alg);
1143 if (status != PSA_SUCCESS) {
1144 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001145 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1148 psa_unlock_key_slot(*p_slot);
1149 *p_slot = NULL;
1150 return PSA_ERROR_NOT_SUPPORTED;
1151 }
1152
1153 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001154}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001157{
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001159 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001161
Ronald Cronea0f8a62020-11-25 17:52:23 +01001162 slot->key.data = NULL;
1163 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001166}
1167
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001168/** Completely wipe a slot in memory, including its policy.
1169 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001171{
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 /*
1175 * As the return error code may not be handled in case of multiple errors,
1176 * do our best to report an unexpected lock counter. Assert with
1177 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1178 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1179 * function is called as part of the execution of a test suite, the
1180 * execution of the test suite is stopped in error if the assertion fails.
1181 */
1182 if (slot->lock_count != 1) {
1183 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001184 status = PSA_ERROR_CORRUPTION_DETECTED;
1185 }
1186
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001187 /* Multipart operations may still be using the key. This is safe
1188 * because all multipart operation objects are independent from
1189 * the key slot: if they need to access the key after the setup
1190 * phase, they have a copy of the key. Note that this means that
1191 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001192 /* At this point, key material and other type-specific content has
1193 * been wiped. Clear remaining metadata. We can call memset and not
1194 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 memset(slot, 0, sizeof(*slot));
1196 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001197}
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001201 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001202 psa_status_t status; /* status of the last operation */
1203 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205 psa_se_drv_table_entry_t *driver;
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (mbedtls_svc_key_id_is_null(key)) {
1209 return PSA_SUCCESS;
1210 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001211
Ronald Cronf2911112020-10-29 17:51:10 +01001212 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001213 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001214 * key, this will load the key description from persistent memory if not
1215 * done yet. We cannot avoid this loading as without it we don't know if
1216 * the key is operated by an SE or not and this information is needed by
1217 * the current implementation.
1218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 status = psa_get_and_lock_key_slot(key, &slot);
1220 if (status != PSA_SUCCESS) {
1221 return status;
1222 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001223
Ronald Cronf2911112020-10-29 17:51:10 +01001224 /*
1225 * If the key slot containing the key description is under access by the
1226 * library (apart from the present access), the key cannot be destroyed
1227 * yet. For the time being, just return in error. Eventually (to be
1228 * implemented), the key should be destroyed when all accesses have
1229 * stopped.
1230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (slot->lock_count > 1) {
1232 psa_unlock_key_slot(slot);
1233 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001234 }
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001237 /* Refuse the destruction of a read-only key (which may or may not work
1238 * if we attempt it, depending on whether the key is merely read-only
1239 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001240 * Just do the best we can, which is to wipe the copy in memory
1241 * (done in this function's cleanup code). */
1242 overall_status = PSA_ERROR_NOT_PERMITTED;
1243 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001244 }
1245
Gilles Peskine354f7672019-07-12 23:46:38 +02001246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1248 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001249 /* For a key in a secure element, we need to do three things:
1250 * remove the key file in internal storage, destroy the
1251 * key inside the secure element, and update the driver's
1252 * persistent data. Start a transaction that will encompass these
1253 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001255 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001257 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 status = psa_crypto_save_transaction();
1259 if (status != PSA_SUCCESS) {
1260 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001261 /* We should still try to destroy the key in the secure
1262 * element and the key metadata in storage. This is especially
1263 * important if the error is that the storage is full.
1264 * But how to do it exactly without risking an inconsistent
1265 * state after a reset?
1266 * https://github.com/ARMmbed/mbed-crypto/issues/215
1267 */
1268 overall_status = status;
1269 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001270 }
1271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 status = psa_destroy_se_key(driver,
1273 psa_key_slot_get_slot_number(slot));
1274 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001275 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001277 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001278#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1279
Darryl Greend49a4992018-06-18 17:27:26 +01001280#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1282 status = psa_destroy_persistent_key(slot->attr.id);
1283 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001286
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001287 /* TODO: other slots may have a copy of the same key. We should
1288 * invalidate them.
1289 * https://github.com/ARMmbed/mbed-crypto/issues/214
1290 */
Darryl Greend49a4992018-06-18 17:27:26 +01001291 }
1292#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001293
Gilles Peskinefc762652019-07-22 19:30:34 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (driver != NULL) {
1296 status = psa_save_se_persistent_data(driver);
1297 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001298 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 }
1300 status = psa_crypto_stop_transaction();
1301 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001302 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001304 }
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001309 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001311 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 }
1313 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314}
1315
Valerio Settib2bcedb2023-07-10 11:24:00 +02001316#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001317 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001318static psa_status_t psa_get_rsa_public_exponent(
1319 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001321{
1322 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001324 uint8_t *buffer = NULL;
1325 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1329 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001330 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 }
1332 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001333 /* It's the default value, which is reported as an empty string,
1334 * so there's nothing to do. */
1335 goto exit;
1336 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 buflen = mbedtls_mpi_size(&mpi);
1339 buffer = mbedtls_calloc(1, buflen);
1340 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001341 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1342 goto exit;
1343 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1345 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001346 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001348 attributes->domain_parameters = buffer;
1349 attributes->domain_parameters_size = buflen;
1350
1351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_mpi_free(&mpi);
1353 if (ret != 0) {
1354 mbedtls_free(buffer);
1355 }
1356 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001357}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001358#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001359 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001360
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001361/** Retrieve all the publicly-accessible attributes of a key.
1362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1364 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001365{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001367 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001368 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1373 if (status != PSA_SUCCESS) {
1374 return status;
1375 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001376
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001377 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1379 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001380
1381#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1383 psa_set_key_slot_number(attributes,
1384 psa_key_slot_get_slot_number(slot));
1385 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001386#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001389#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1390 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001391 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001392 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001393 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001394 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001395 * is not yet implemented.
1396 * https://github.com/ARMmbed/mbed-crypto/issues/216
1397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001399 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001400
Ronald Cron90857082020-11-25 14:58:33 +01001401 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 slot->attr.type,
1403 slot->key.data,
1404 slot->key.bytes,
1405 &rsa);
1406 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001407 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 status = psa_get_rsa_public_exponent(rsa,
1411 attributes);
1412 mbedtls_rsa_free(rsa);
1413 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001414 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001415 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001416#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1417 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001418 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001419 default:
1420 /* Nothing else to do. */
1421 break;
1422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (status != PSA_SUCCESS) {
1425 psa_reset_key_attributes(attributes);
1426 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431}
1432
Gilles Peskinec8000c02019-08-02 20:15:51 +02001433#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1434psa_status_t psa_get_key_slot_number(
1435 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001437{
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001439 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return PSA_SUCCESS;
1441 } else {
1442 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001443 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001444}
1445#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1448 size_t key_buffer_size,
1449 uint8_t *data,
1450 size_t data_size,
1451 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001452{
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (key_buffer_size > data_size) {
1454 return PSA_ERROR_BUFFER_TOO_SMALL;
1455 }
1456 memcpy(data, key_buffer, key_buffer_size);
1457 memset(data + key_buffer_size, 0,
1458 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001459 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001461}
1462
Ronald Cron7285cda2020-11-26 14:46:37 +01001463psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 const psa_key_attributes_t *attributes,
1465 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467{
Ronald Crond18b5f82020-11-25 16:46:05 +01001468 psa_key_type_t type = attributes->core.type;
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (key_type_is_raw_bytes(type) ||
1471 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001472 PSA_KEY_TYPE_IS_ECC(type) ||
1473 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 return psa_export_key_buffer_internal(
1475 key_buffer, key_buffer_size,
1476 data, data_size, data_length);
1477 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001478 /* This shouldn't happen in the reference implementation, but
1479 it is valid for a special-purpose implementation to omit
1480 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001482 }
1483}
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1486 uint8_t *data,
1487 size_t data_size,
1488 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001489{
1490 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1491 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1492 psa_key_slot_t *slot;
1493
Ronald Crone907e552021-01-18 13:22:38 +01001494 /* Reject a zero-length output buffer now, since this can never be a
1495 * valid key representation. This way we know that data must be a valid
1496 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (data_size == 0) {
1498 return PSA_ERROR_BUFFER_TOO_SMALL;
1499 }
Ronald Crone907e552021-01-18 13:22:38 +01001500
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501 /* Set the key to empty now, so that even when there are errors, we always
1502 * set data_length to a value between 0 and data_size. On error, setting
1503 * the key to empty is a good choice because an empty key representation is
1504 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001505 *data_length = 0;
1506
Ronald Cron9486f9d2020-11-26 13:36:23 +01001507 /* Export requires the EXPORT flag. There is an exception for public keys,
1508 * which don't require any flag, but
1509 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1512 PSA_KEY_USAGE_EXPORT, 0);
1513 if (status != PSA_SUCCESS) {
1514 return status;
1515 }
mohammad160306e79202018-03-28 13:17:44 +03001516
Ronald Crond18b5f82020-11-25 16:46:05 +01001517 psa_key_attributes_t attributes = {
1518 .core = slot->attr
1519 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 status = psa_driver_wrapper_export_key(&attributes,
1521 slot->key.data, slot->key.bytes,
1522 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001527}
1528
Ronald Cron7285cda2020-11-26 14:46:37 +01001529psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001530 const psa_key_attributes_t *attributes,
1531 const uint8_t *key_buffer,
1532 size_t key_buffer_size,
1533 uint8_t *data,
1534 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001536{
Ronald Crond18b5f82020-11-25 16:46:05 +01001537 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001538
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001539 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001540 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1541 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001542 /* Exporting public -> public */
1543 return psa_export_key_buffer_internal(
1544 key_buffer, key_buffer_size,
1545 data, data_size, data_length);
1546 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001547#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001548 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1549 return mbedtls_psa_rsa_export_public_key(attributes,
1550 key_buffer,
1551 key_buffer_size,
1552 data,
1553 data_size,
1554 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001555#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001556 /* We don't know how to convert a private RSA key to public. */
1557 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001558#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001559 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001560 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001561#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001562 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1563 return mbedtls_psa_ecp_export_public_key(attributes,
1564 key_buffer,
1565 key_buffer_size,
1566 data,
1567 data_size,
1568 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001569#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001570 /* We don't know how to convert a private ECC key to public */
1571 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001572#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001573 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001574 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001575#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001576 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001577 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001578 key_buffer,
1579 key_buffer_size,
1580 data, data_size,
1581 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001582#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001583 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001584#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001585 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001587 (void) key_buffer;
1588 (void) key_buffer_size;
1589 (void) data;
1590 (void) data_size;
1591 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001593 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594}
Ronald Crond18b5f82020-11-25 16:46:05 +01001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1597 uint8_t *data,
1598 size_t data_size,
1599 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001600{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001602 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001603 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001604 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001605
Ronald Crone907e552021-01-18 13:22:38 +01001606 /* Reject a zero-length output buffer now, since this can never be a
1607 * valid key representation. This way we know that data must be a valid
1608 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (data_size == 0) {
1610 return PSA_ERROR_BUFFER_TOO_SMALL;
1611 }
Ronald Crone907e552021-01-18 13:22:38 +01001612
Darryl Greendd8fb772018-11-07 16:00:44 +00001613 /* Set the key to empty now, so that even when there are errors, we always
1614 * set data_length to a value between 0 and data_size. On error, setting
1615 * the key to empty is a good choice because an empty key representation is
1616 * unlikely to be accepted anywhere. */
1617 *data_length = 0;
1618
1619 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1621 if (status != PSA_SUCCESS) {
1622 return status;
1623 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1626 status = PSA_ERROR_INVALID_ARGUMENT;
1627 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001628 }
1629
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001630 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001631 .core = slot->attr
1632 };
Ronald Cron67227982020-11-26 15:16:05 +01001633 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001634 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001636
1637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001641}
1642
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001643MBEDTLS_STATIC_ASSERT(
1644 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1645 "One or more key attribute flag is listed as both external-only and dual-use")
1646MBEDTLS_STATIC_ASSERT(
1647 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1648 "One or more key attribute flag is listed as both internal-only and dual-use")
1649MBEDTLS_STATIC_ASSERT(
1650 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1651 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001652
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001653/** Validate that a key policy is internally well-formed.
1654 *
1655 * This function only rejects invalid policies. It does not validate the
1656 * consistency of the policy with respect to other attributes of the key
1657 * such as the key type.
1658 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001660{
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1662 PSA_KEY_USAGE_COPY |
1663 PSA_KEY_USAGE_ENCRYPT |
1664 PSA_KEY_USAGE_DECRYPT |
1665 PSA_KEY_USAGE_SIGN_MESSAGE |
1666 PSA_KEY_USAGE_VERIFY_MESSAGE |
1667 PSA_KEY_USAGE_SIGN_HASH |
1668 PSA_KEY_USAGE_VERIFY_HASH |
1669 PSA_KEY_USAGE_VERIFY_DERIVATION |
1670 PSA_KEY_USAGE_DERIVE)) != 0) {
1671 return PSA_ERROR_INVALID_ARGUMENT;
1672 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001675}
1676
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001677/** Validate the internal consistency of key attributes.
1678 *
1679 * This function only rejects invalid attribute values. If does not
1680 * validate the consistency of the attributes with any key data that may
1681 * be involved in the creation of the key.
1682 *
1683 * Call this function early in the key creation process.
1684 *
1685 * \param[in] attributes Key attributes for the new key.
1686 * \param[out] p_drv On any return, the driver for the key, if any.
1687 * NULL for a transparent key.
1688 *
1689 */
1690static psa_status_t psa_validate_key_attributes(
1691 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001693{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001694 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1696 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 status = psa_validate_key_location(lifetime, p_drv);
1699 if (status != PSA_SUCCESS) {
1700 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001701 }
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 status = psa_validate_key_persistence(lifetime);
1704 if (status != PSA_SUCCESS) {
1705 return status;
1706 }
1707
1708 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1709 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1710 return PSA_ERROR_INVALID_ARGUMENT;
1711 }
1712 } else {
1713 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1714 return PSA_ERROR_INVALID_ARGUMENT;
1715 }
1716 }
1717
1718 status = psa_validate_key_policy(&attributes->core.policy);
1719 if (status != PSA_SUCCESS) {
1720 return status;
1721 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001722
1723 /* Refuse to create overly large keys.
1724 * Note that this doesn't trigger on import if the attributes don't
1725 * explicitly specify a size (so psa_get_key_bits returns 0), so
1726 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1728 return PSA_ERROR_NOT_SUPPORTED;
1729 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730
Gilles Peskine91e8c332019-08-02 19:19:39 +02001731 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1733 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1734 return PSA_ERROR_INVALID_ARGUMENT;
1735 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001738}
1739
Gilles Peskine4747d192019-04-17 15:05:45 +02001740/** Prepare a key slot to receive key material.
1741 *
1742 * This function allocates a key slot and sets its metadata.
1743 *
1744 * If this function fails, call psa_fail_key_creation().
1745 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 * This function is intended to be used as follows:
1747 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001748 * it with the specified attributes, and in case of a volatile key assign it
1749 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001750 * -# Populate the slot with the key material.
1751 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1752 * In case of failure at any step, stop the sequence and call
1753 * psa_fail_key_creation().
1754 *
Ronald Cron5c522922020-11-14 16:35:34 +01001755 * On success, the key slot is locked. It is the responsibility of the caller
1756 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001757 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001758 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[out] p_slot On success, a pointer to the prepared slot.
1761 * \param[out] p_drv On any return, the driver for the key, if any.
1762 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001763 *
1764 * \retval #PSA_SUCCESS
1765 * The key slot is ready to receive key material.
1766 * \return If this function fails, the key slot is an invalid state.
1767 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 */
1769static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001770 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001772 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001774{
1775 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001776 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 psa_key_slot_t *slot;
1778
Gilles Peskinedf179142019-07-15 22:02:14 +02001779 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001780 *p_drv = NULL;
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 status = psa_validate_key_attributes(attributes, p_drv);
1783 if (status != PSA_SUCCESS) {
1784 return status;
1785 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1788 if (status != PSA_SUCCESS) {
1789 return status;
1790 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001791 slot = *p_slot;
1792
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001793 /* We're storing the declared bit-size of the key. It's up to each
1794 * creation mechanism to verify that this information is correct.
1795 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001796 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001797 * is optional (import, copy). In case of a volatile key, assign it the
1798 * volatile key identifier associated to the slot returned to contain its
1799 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001800
1801 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001803#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1804 slot->attr.id = volatile_key_id;
1805#else
1806 slot->attr.id.key_id = volatile_key_id;
1807#endif
1808 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001809
Gilles Peskine91e8c332019-08-02 19:19:39 +02001810 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001811 * external-only flags, query `attributes`. Thanks to the check
1812 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001813 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001814 * may have set. */
1815 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001816
Gilles Peskinecbaff462019-07-12 23:46:04 +02001817#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001818 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001819 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001820 * create the key file in internal storage, create the
1821 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 * persistent data. This is done by starting a transaction that will
1823 * encompass these three actions.
1824 * For registering a volatile key, we just need to find an appropriate
1825 * slot number inside the SE. Since the key is designated volatile, creating
1826 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001827 /* The first thing to do is to find a slot number for the new key.
1828 * We save the slot number in persistent storage as part of the
1829 * transaction data. It will be needed to recover if the power
1830 * fails during the key creation process, to clean up on the secure
1831 * element side after restarting. Obtaining a slot number from the
1832 * secure element driver updates its persistent state, but we do not yet
1833 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001834 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001836 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1838 &slot_number);
1839 if (status != PSA_SUCCESS) {
1840 return status;
1841 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1844 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001846 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001847 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 status = psa_crypto_save_transaction();
1849 if (status != PSA_SUCCESS) {
1850 (void) psa_crypto_stop_transaction();
1851 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001852 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001853 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001854
1855 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001857 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001860 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001862 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001863#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001866}
Gilles Peskine4747d192019-04-17 15:05:45 +02001867
1868/** Finalize the creation of a key once its key material has been set.
1869 *
1870 * This entails writing the key to persistent storage.
1871 *
1872 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001873 * See the documentation of psa_start_key_creation() for the intended use
1874 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 *
Ronald Cron5c522922020-11-14 16:35:34 +01001876 * If the finalization succeeds, the function unlocks the key slot (it was
1877 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1878 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001879 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 * \param[in,out] slot Pointer to the slot with key material.
1881 * \param[in] driver The secure element driver for the key,
1882 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001883 * \param[out] key On success, identifier of the key. Note that the
1884 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 *
1886 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001887 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1889 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1890 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1891 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1892 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1893 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001894 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001895 * \return If this function fails, the key slot is an invalid state.
1896 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001898static psa_status_t psa_finish_key_creation(
1899 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001900 psa_se_drv_table_entry_t *driver,
1901 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001902{
1903 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001904 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001905 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001906
1907#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001909#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001911 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001912 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001914
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001915 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1916 sizeof(data.slot_number),
1917 "Slot number size does not match psa_se_key_data_storage_t");
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1920 status = psa_save_persistent_key(&slot->attr,
1921 (uint8_t *) &data,
1922 sizeof(data));
1923 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001924#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1925 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001926 /* Key material is saved in export representation in the slot, so
1927 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 status = psa_save_persistent_key(&slot->attr,
1929 slot->key.data,
1930 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001931 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001932 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001933#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1934
Gilles Peskinecbaff462019-07-12 23:46:04 +02001935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001936 /* Finish the transaction for a key creation. This does not
1937 * happen when registering an existing key. Detect this case
1938 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001939 * creation of a persistent key in a secure element requires a transaction,
1940 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (driver != NULL &&
1942 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1943 status = psa_save_se_persistent_data(driver);
1944 if (status != PSA_SUCCESS) {
1945 psa_destroy_persistent_key(slot->attr.id);
1946 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001949 }
1950#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001953 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_unlock_key_slot(slot);
1955 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001956 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001958 }
Ronald Cron50972942020-11-14 11:28:25 +01001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001961}
1962
1963/** Abort the creation of a key.
1964 *
1965 * You may call this function after calling psa_start_key_creation(),
1966 * or after psa_finish_key_creation() fails. In other circumstances, this
1967 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001968 * See the documentation of psa_start_key_creation() for the intended use
1969 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001971 * \param[in,out] slot Pointer to the slot with key material.
1972 * \param[in] driver The secure element driver for the key,
1973 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001974 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975static void psa_fail_key_creation(psa_key_slot_t *slot,
1976 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001977{
Gilles Peskine011e4282019-06-26 18:34:38 +02001978 (void) driver;
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001981 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001983
1984#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001985 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001986 * element, and the failure happened later (when saving metadata
1987 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001988 * element.
1989 * https://github.com/ARMmbed/mbed-crypto/issues/217
1990 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001991
Gilles Peskined7729582019-08-05 15:55:54 +02001992 /* Abort the ongoing transaction if any (there may not be one if
1993 * the creation process failed before starting one, or if the
1994 * key creation is a registration of a key in a secure element).
1995 * Earlier functions must already have done what it takes to undo any
1996 * partial creation. All that's left is to update the transaction data
1997 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001999#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002002}
2003
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002004/** Validate optional attributes during key creation.
2005 *
2006 * Some key attributes are optional during key creation. If they are
2007 * specified in the attributes structure, check that they are consistent
2008 * with the data in the slot.
2009 *
2010 * This function should be called near the end of key creation, after
2011 * the slot in memory is fully populated but before saving persistent data.
2012 */
2013static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002014 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016{
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (attributes->core.type != 0) {
2018 if (attributes->core.type != slot->attr.type) {
2019 return PSA_ERROR_INVALID_ARGUMENT;
2020 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021 }
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002024#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2025 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2027 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002028 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002029 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002031
Ronald Cron90857082020-11-25 14:58:33 +01002032 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 slot->attr.type,
2034 slot->key.data,
2035 slot->key.bytes,
2036 &rsa);
2037 if (status != PSA_SUCCESS) {
2038 return status;
2039 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 mbedtls_mpi_init(&actual);
2042 mbedtls_mpi_init(&required);
2043 ret = mbedtls_rsa_export(rsa,
2044 NULL, NULL, NULL, NULL, &actual);
2045 mbedtls_rsa_free(rsa);
2046 mbedtls_free(rsa);
2047 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002048 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 }
2050 ret = mbedtls_mpi_read_binary(&required,
2051 attributes->domain_parameters,
2052 attributes->domain_parameters_size);
2053 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002054 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 }
2056 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
2059rsa_exit:
2060 mbedtls_mpi_free(&actual);
2061 mbedtls_mpi_free(&required);
2062 if (ret != 0) {
2063 return mbedtls_to_psa_error(ret);
2064 }
2065 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002066#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2067 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002068 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002069 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002071 }
2072 }
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (attributes->core.bits != 0) {
2075 if (attributes->core.bits != slot->attr.bits) {
2076 return PSA_ERROR_INVALID_ARGUMENT;
2077 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002078 }
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002081}
2082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2084 const uint8_t *data,
2085 size_t data_length,
2086 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002087{
2088 psa_status_t status;
2089 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002090 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002091 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302092 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002093
Ronald Cron81709fc2020-11-14 12:10:32 +01002094 *key = MBEDTLS_SVC_KEY_ID_INIT;
2095
Gilles Peskine0f84d622019-09-12 19:03:13 +02002096 /* Reject zero-length symmetric keys (including raw data key objects).
2097 * This also rejects any key which might be encoded as an empty string,
2098 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (data_length == 0) {
2100 return PSA_ERROR_INVALID_ARGUMENT;
2101 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002102
Archana449608b2021-09-08 15:36:05 +05302103 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (data_length > SIZE_MAX / 8) {
2105 return PSA_ERROR_NOT_SUPPORTED;
2106 }
Archana6ed4bda2021-08-04 10:47:15 +05302107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2109 &slot, &driver);
2110 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002113
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002114 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302115 * storage ( thus not in the case of importing a key in a secure element
2116 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2117 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (slot->key.data == NULL) {
2119 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302120 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 attributes, data, data_length, &storage_size);
2122 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 }
Archanad8a83dc2021-06-14 10:04:16 +05302125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_allocate_buffer_to_slot(slot, storage_size);
2127 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002130 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002131
2132 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_driver_wrapper_import_key(attributes,
2134 data, data_length,
2135 slot->key.data,
2136 slot->key.bytes,
2137 &slot->key.bytes, &bits);
2138 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002139 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002143 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002145 status = PSA_ERROR_INVALID_ARGUMENT;
2146 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002147 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002148
Archana6ed4bda2021-08-04 10:47:15 +05302149 /* Enforce a size limit, and in particular ensure that the bit
2150 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302152 status = PSA_ERROR_NOT_SUPPORTED;
2153 goto exit;
2154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 status = psa_validate_optional_attributes(slot, attributes);
2156 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002161exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (status != PSA_SUCCESS) {
2163 psa_fail_key_creation(slot, driver);
2164 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002167}
2168
Gilles Peskined7729582019-08-05 15:55:54 +02002169#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2170psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002172{
2173 psa_status_t status;
2174 psa_key_slot_t *slot = NULL;
2175 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002177
2178 /* Leaving attributes unspecified is not currently supported.
2179 * It could make sense to query the key type and size from the
2180 * secure element, but not all secure elements support this
2181 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2183 return PSA_ERROR_NOT_SUPPORTED;
2184 }
2185 if (psa_get_key_bits(attributes) == 0) {
2186 return PSA_ERROR_NOT_SUPPORTED;
2187 }
Gilles Peskined7729582019-08-05 15:55:54 +02002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2190 &slot, &driver);
2191 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 }
Gilles Peskined7729582019-08-05 15:55:54 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002196
2197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (status != PSA_SUCCESS) {
2199 psa_fail_key_creation(slot, driver);
2200 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002201
Gilles Peskined7729582019-08-05 15:55:54 +02002202 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 psa_close_key(key);
2204 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002205}
2206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2209 const psa_key_attributes_t *specified_attributes,
2210 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002211{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002213 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002214 psa_key_slot_t *source_slot = NULL;
2215 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002217 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302218 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002219
Ronald Cron81709fc2020-11-14 12:10:32 +01002220 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2221
Archana8a180362021-07-05 02:18:48 +05302222 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2224 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 status = psa_validate_optional_attributes(source_slot,
2229 specified_attributes);
2230 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002231 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002233
Archana9d17bf42021-09-10 06:22:44 +05302234 /* The target key type and number of bits have been validated by
2235 * psa_validate_optional_attributes() to be either equal to zero or
2236 * equal to the ones of the source key. So it is safe to inherit
2237 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302238 * */
Archana9d17bf42021-09-10 06:22:44 +05302239 actual_attributes.core.bits = source_slot->attr.bits;
2240 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302241
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 status = psa_restrict_key_policy(source_slot->attr.type,
2244 &actual_attributes.core.policy,
2245 &source_slot->attr.policy);
2246 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2251 &target_slot, &driver);
2252 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 }
2255 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2256 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302257 /*
Archana9d17bf42021-09-10 06:22:44 +05302258 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302259 * the source key would need to be exported as plaintext and re-imported
2260 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302261 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302262 * appropriate API invocations from the application, if needed.
2263 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002264 status = PSA_ERROR_NOT_SUPPORTED;
2265 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002266 }
Archana8a180362021-07-05 02:18:48 +05302267 /*
2268 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302269 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302270 * - For opaque keys this translates to an invocation of the drivers'
2271 * copy_key entry point through the dispatch layer.
2272 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2274 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2275 &storage_size);
2276 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 }
Archana374fe5b2021-09-08 15:50:28 +05302279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2281 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302282 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 }
Archana374fe5b2021-09-08 15:50:28 +05302284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 status = psa_driver_wrapper_copy_key(&actual_attributes,
2286 source_slot->key.data,
2287 source_slot->key.bytes,
2288 target_slot->key.data,
2289 target_slot->key.bytes,
2290 &target_slot->key.bytes);
2291 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302292 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 }
2294 } else {
2295 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302296 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 source_slot->key.bytes);
2298 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302299 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 }
Archana8a180362021-07-05 02:18:48 +05302301 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002303exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (status != PSA_SUCCESS) {
2305 psa_fail_key_creation(target_slot, driver);
2306 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002311}
2312
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002313
2314
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002315/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002316/* Message digests */
2317/****************************************************************/
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002321 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (operation->id == 0) {
2323 return PSA_SUCCESS;
2324 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002327 operation->id = 0;
2328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330}
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2333 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002335 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002336
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002339 status = PSA_ERROR_BAD_STATE;
2340 goto exit;
2341 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002344 status = PSA_ERROR_INVALID_ARGUMENT;
2345 goto exit;
2346 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002347
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002348 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2349 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353
2354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (status != PSA_SUCCESS) {
2356 psa_hash_abort(operation);
2357 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002358
2359 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360}
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2363 const uint8_t *input,
2364 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002366 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002369 status = PSA_ERROR_BAD_STATE;
2370 goto exit;
2371 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002372
Steven Cooremanc8288352021-03-04 14:02:19 +01002373 /* Don't require hash implementations to behave correctly on a
2374 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (input_length == 0) {
2376 return PSA_SUCCESS;
2377 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002380
2381exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (status != PSA_SUCCESS) {
2383 psa_hash_abort(operation);
2384 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387}
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2390 uint8_t *hash,
2391 size_t hash_size,
2392 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002394 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (operation->id == 0) {
2396 return PSA_ERROR_BAD_STATE;
2397 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398
Steven Cooreman1e582352021-02-18 17:24:37 +01002399 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 operation, hash, hash_size, hash_length);
2401 psa_hash_abort(operation);
2402 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403}
2404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2406 const uint8_t *hash,
2407 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408{
Ronald Cron69a63422021-10-18 09:47:58 +02002409 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002411 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 operation,
2413 actual_hash, sizeof(actual_hash),
2414 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002417 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002421 status = PSA_ERROR_INVALID_SIGNATURE;
2422 goto exit;
2423 }
2424
Dave Rodgman78701152023-08-29 14:20:18 +01002425 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002426 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002428
2429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2431 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002432 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436}
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438psa_status_t psa_hash_compute(psa_algorithm_t alg,
2439 const uint8_t *input, size_t input_length,
2440 uint8_t *hash, size_t hash_size,
2441 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002442{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002443 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if (!PSA_ALG_IS_HASH(alg)) {
2445 return PSA_ERROR_INVALID_ARGUMENT;
2446 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2449 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002450}
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452psa_status_t psa_hash_compare(psa_algorithm_t alg,
2453 const uint8_t *input, size_t input_length,
2454 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002455{
Ronald Cron69a63422021-10-18 09:47:58 +02002456 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002457 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (!PSA_ALG_IS_HASH(alg)) {
2460 return PSA_ERROR_INVALID_ARGUMENT;
2461 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002462
Steven Cooreman1e582352021-02-18 17:24:37 +01002463 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 alg, input, input_length,
2465 actual_hash, sizeof(actual_hash),
2466 &actual_hash_length);
2467 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002468 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 }
2470 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002471 status = PSA_ERROR_INVALID_SIGNATURE;
2472 goto exit;
2473 }
Dave Rodgman78701152023-08-29 14:20:18 +01002474 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002475 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002477
2478exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2480 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002481}
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2484 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002485{
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (source_operation->id == 0 ||
2487 target_operation->id != 0) {
2488 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002489 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2492 target_operation);
2493 if (status != PSA_SUCCESS) {
2494 psa_hash_abort(target_operation);
2495 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002498}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499
2500
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002501/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502/* MAC */
2503/****************************************************************/
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002506{
Steven Cooremane6804192021-03-19 18:28:56 +01002507 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (operation->id == 0) {
2509 return PSA_SUCCESS;
2510 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002513 operation->mac_size = 0;
2514 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002515 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002518}
2519
Ronald Cron2dff3b22021-06-17 16:33:22 +02002520static psa_status_t psa_mac_finalize_alg_and_key_validation(
2521 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002522 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 psa_key_type_t key_type = psa_get_key_type(attributes);
2527 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (!PSA_ALG_IS_MAC(alg)) {
2530 return PSA_ERROR_INVALID_ARGUMENT;
2531 }
Steven Cooremane6804192021-03-19 18:28:56 +01002532
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002533 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 status = psa_mac_key_can_do(alg, key_type);
2535 if (status != PSA_SUCCESS) {
2536 return status;
2537 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002538
Steven Cooremand1a68f12021-05-10 11:13:41 +02002539 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002543 /* A very short MAC is too short for security since it can be
2544 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2545 * so we make this our minimum, even though 32 bits is still
2546 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002548 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2551 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002552 /* It's impossible to "truncate" to a larger length than the full length
2553 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002555 }
2556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002558 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2559 * that is disabled in the compile-time configuration. The result can
2560 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2561 * configuration into account. In this case, force a return of
2562 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2563 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2564 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2565 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2566 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002568 }
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002571}
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2574 mbedtls_svc_key_id_t key,
2575 psa_algorithm_t alg,
2576 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002577{
2578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2579 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002580 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002581 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582
2583 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002585 status = PSA_ERROR_BAD_STATE;
2586 goto exit;
2587 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588
2589 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 key,
2591 &slot,
2592 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2593 alg);
2594 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002598 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599 .core = slot->attr
2600 };
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2603 &operation->mac_size);
2604 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607
2608 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002609 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (is_sign) {
2611 status = psa_driver_wrapper_mac_sign_setup(operation,
2612 &attributes,
2613 slot->key.data,
2614 slot->key.bytes,
2615 alg);
2616 } else {
2617 status = psa_driver_wrapper_mac_verify_setup(operation,
2618 &attributes,
2619 slot->key.data,
2620 slot->key.bytes,
2621 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002623
Gilles Peskinefbfac682018-07-08 20:51:54 +02002624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (status != PSA_SUCCESS) {
2626 psa_mac_abort(operation);
2627 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632}
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2635 mbedtls_svc_key_id_t key,
2636 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002637{
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002639}
2640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2642 mbedtls_svc_key_id_t key,
2643 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002644{
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002646}
2647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2649 const uint8_t *input,
2650 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651{
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (operation->id == 0) {
2653 return PSA_ERROR_BAD_STATE;
2654 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002656 /* Don't require hash implementations to behave correctly on a
2657 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (input_length == 0) {
2659 return PSA_SUCCESS;
2660 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2663 input, input_length);
2664 if (status != PSA_SUCCESS) {
2665 psa_mac_abort(operation);
2666 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669}
2670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2672 uint8_t *mac,
2673 size_t mac_size,
2674 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002675{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2677 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002680 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002681 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002682 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002685 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002686 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002687 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002688
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002689 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2690 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002692 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002693 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002694 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002697 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002698 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002699 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 status = psa_driver_wrapper_mac_sign_finish(operation,
2702 mac, operation->mac_size,
2703 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002704
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002705exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002706 /* In case of success, set the potential excess room in the output buffer
2707 * to an invalid value, to avoid potentially leaking a longer MAC.
2708 * In case of error, set the output length and content to a safe default,
2709 * such that in case the caller misses an error check, the output would be
2710 * an unachievable MAC.
2711 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002713 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002714 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002715 }
mohammad16036df908f2018-04-02 08:34:15 -07002716
Paul Elliottdc42ca82023-02-24 18:11:59 +00002717 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002722}
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2725 const uint8_t *mac,
2726 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2729 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002732 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002733 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002734 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002737 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002738 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002739 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002742 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002743 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002744 }
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 status = psa_driver_wrapper_mac_verify_finish(operation,
2747 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002748
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002749exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002753}
2754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2756 psa_algorithm_t alg,
2757 const uint8_t *input,
2758 size_t input_length,
2759 uint8_t *mac,
2760 size_t mac_size,
2761 size_t *mac_length,
2762 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002763{
2764 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002765 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2766 psa_key_slot_t *slot;
2767 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002768 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769
Ronald Cron51131b52021-06-17 17:17:20 +02002770 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 key,
2772 &slot,
2773 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2774 alg);
2775 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002776 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002778
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002779 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002780 .core = slot->attr
2781 };
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2784 &operation_mac_size);
2785 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002786 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002790 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002791 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002792 }
2793
2794 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 &attributes,
2796 slot->key.data, slot->key.bytes,
2797 alg,
2798 input, input_length,
2799 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002800
2801exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002802 /* In case of success, set the potential excess room in the output buffer
2803 * to an invalid value, to avoid potentially leaking a longer MAC.
2804 * In case of error, set the output length and content to a safe default,
2805 * such that in case the caller misses an error check, the output would be
2806 * an unachievable MAC.
2807 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002809 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002810 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002811 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002812
2813 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002818}
2819
Gilles Peskine449bd832023-01-11 14:50:10 +01002820psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 uint8_t *mac,
2825 size_t mac_size,
2826 size_t *mac_length)
2827{
2828 return psa_mac_compute_internal(key, alg,
2829 input, input_length,
2830 mac, mac_size, mac_length, 1);
2831}
2832
2833psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2834 psa_algorithm_t alg,
2835 const uint8_t *input,
2836 size_t input_length,
2837 const uint8_t *mac,
2838 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002839{
2840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002841 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2842 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 status = psa_mac_compute_internal(key, alg,
2845 input, input_length,
2846 actual_mac, sizeof(actual_mac),
2847 &actual_mac_length, 0);
2848 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002849 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002853 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002854 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002855 }
Dave Rodgman78701152023-08-29 14:20:18 +01002856 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002857 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002858 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002859 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002860
2861exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002865}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002866
Gilles Peskinee59236f2018-01-27 23:32:46 +01002867/****************************************************************/
2868/* Asymmetric cryptography */
2869/****************************************************************/
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2872 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002873{
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (input_is_message) {
2875 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2876 return PSA_ERROR_INVALID_ARGUMENT;
2877 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2880 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2881 return PSA_ERROR_INVALID_ARGUMENT;
2882 }
2883 }
2884 } else {
2885 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2886 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002887 }
2888 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002891}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2894 int input_is_message,
2895 psa_algorithm_t alg,
2896 const uint8_t *input,
2897 size_t input_length,
2898 uint8_t *signature,
2899 size_t signature_size,
2900 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002901{
2902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2903 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002905 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002906
2907 *signature_length = 0;
2908
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 status = psa_sign_verify_check_alg(input_is_message, alg);
2910 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002911 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002913
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002914 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002915 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002916 * buffer can in principle be empty since it doesn't actually have
2917 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (signature_size == 0) {
2919 return PSA_ERROR_BUFFER_TOO_SMALL;
2920 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002921
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002922 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 key, &slot,
2924 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2925 PSA_KEY_USAGE_SIGN_HASH,
2926 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002929 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002933 status = PSA_ERROR_INVALID_ARGUMENT;
2934 goto exit;
2935 }
2936
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002937 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002939 };
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002942 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002943 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002944 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 signature, signature_size, signature_length);
2946 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002947
2948 status = psa_driver_wrapper_sign_hash(
2949 &attributes, slot->key.data, slot->key.bytes,
2950 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002952 }
2953
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002954
2955exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002956 psa_wipe_tag_output_buffer(signature, status, signature_size,
2957 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002962}
2963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2965 int input_is_message,
2966 psa_algorithm_t alg,
2967 const uint8_t *input,
2968 size_t input_length,
2969 const uint8_t *signature,
2970 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002971{
2972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2973 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2974 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 status = psa_sign_verify_check_alg(input_is_message, alg);
2977 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002978 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002980
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002981 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 key, &slot,
2983 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2984 PSA_KEY_USAGE_VERIFY_HASH,
2985 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (status != PSA_SUCCESS) {
2988 return status;
2989 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002990
2991 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002993 };
2994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002996 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002997 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002998 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 signature, signature_length);
3000 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003001 status = psa_driver_wrapper_verify_hash(
3002 &attributes, slot->key.data, slot->key.bytes,
3003 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003005 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003010
3011}
3012
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003013psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003014 const psa_key_attributes_t *attributes,
3015 const uint8_t *key_buffer,
3016 size_t key_buffer_size,
3017 psa_algorithm_t alg,
3018 const uint8_t *input,
3019 size_t input_length,
3020 uint8_t *signature,
3021 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003023{
3024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003027 size_t hash_length;
3028 uint8_t hash[PSA_HASH_MAX_SIZE];
3029
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003030 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 PSA_ALG_SIGN_GET_HASH(alg),
3032 input, input_length,
3033 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003036 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003038
3039 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 attributes, key_buffer, key_buffer_size,
3041 alg, hash, hash_length,
3042 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003043 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003046}
3047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3049 psa_algorithm_t alg,
3050 const uint8_t *input,
3051 size_t input_length,
3052 uint8_t *signature,
3053 size_t signature_size,
3054 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003055{
3056 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003057 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003059}
3060
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003061psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003062 const psa_key_attributes_t *attributes,
3063 const uint8_t *key_buffer,
3064 size_t key_buffer_size,
3065 psa_algorithm_t alg,
3066 const uint8_t *input,
3067 size_t input_length,
3068 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003070{
3071 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003074 size_t hash_length;
3075 uint8_t hash[PSA_HASH_MAX_SIZE];
3076
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003077 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 PSA_ALG_SIGN_GET_HASH(alg),
3079 input, input_length,
3080 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003083 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003085
3086 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 attributes, key_buffer, key_buffer_size,
3088 alg, hash, hash_length,
3089 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003090 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003093}
3094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3096 psa_algorithm_t alg,
3097 const uint8_t *input,
3098 size_t input_length,
3099 const uint8_t *signature,
3100 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101{
3102 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003103 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003105}
3106
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003107psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003108 const psa_key_attributes_t *attributes,
3109 const uint8_t *key_buffer, size_t key_buffer_size,
3110 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003112{
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3114 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3115 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3118 return mbedtls_psa_rsa_sign_hash(
3119 attributes,
3120 key_buffer, key_buffer_size,
3121 alg, hash, hash_length,
3122 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3124 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 } else {
3126 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3129 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003130#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3132 return mbedtls_psa_ecdsa_sign_hash(
3133 attributes,
3134 key_buffer, key_buffer_size,
3135 alg, hash, hash_length,
3136 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003137#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3138 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 } else {
3140 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003141 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003142 }
Ronald Cron4501c982021-03-19 20:09:32 +01003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 (void) key_buffer;
3145 (void) key_buffer_size;
3146 (void) hash;
3147 (void) hash_length;
3148 (void) signature;
3149 (void) signature_size;
3150 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003153}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3156 psa_algorithm_t alg,
3157 const uint8_t *hash,
3158 size_t hash_length,
3159 uint8_t *signature,
3160 size_t signature_size,
3161 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003162{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003163 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003164 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003166}
3167
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003168psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003169 const psa_key_attributes_t *attributes,
3170 const uint8_t *key_buffer, size_t key_buffer_size,
3171 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003173{
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3175 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3176 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003177#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3179 return mbedtls_psa_rsa_verify_hash(
3180 attributes,
3181 key_buffer, key_buffer_size,
3182 alg, hash, hash_length,
3183 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003184#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3185 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 } else {
3187 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3190 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003191#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3193 return mbedtls_psa_ecdsa_verify_hash(
3194 attributes,
3195 key_buffer, key_buffer_size,
3196 alg, hash, hash_length,
3197 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003198#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3199 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 } else {
3201 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003202 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003203 }
Ronald Cron4501c982021-03-19 20:09:32 +01003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 (void) key_buffer;
3206 (void) key_buffer_size;
3207 (void) hash;
3208 (void) hash_length;
3209 (void) signature;
3210 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003213}
3214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3216 psa_algorithm_t alg,
3217 const uint8_t *hash,
3218 size_t hash_length,
3219 const uint8_t *signature,
3220 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003221{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003222 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003223 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003225}
3226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3228 psa_algorithm_t alg,
3229 const uint8_t *input,
3230 size_t input_length,
3231 const uint8_t *salt,
3232 size_t salt_length,
3233 uint8_t *output,
3234 size_t output_size,
3235 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003238 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003239 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003240 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003241
Darryl Green5cc689a2018-07-24 15:34:10 +01003242 (void) input;
3243 (void) input_length;
3244 (void) salt;
3245 (void) output;
3246 (void) output_size;
3247
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003248 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3251 return PSA_ERROR_INVALID_ARGUMENT;
3252 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003253
Ronald Cron5c522922020-11-14 16:35:34 +01003254 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3256 if (status != PSA_SUCCESS) {
3257 return status;
3258 }
3259 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3260 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003261 status = PSA_ERROR_INVALID_ARGUMENT;
3262 goto exit;
3263 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003264
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003265 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003267 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003268
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003269 status = psa_driver_wrapper_asymmetric_encrypt(
3270 &attributes, slot->key.data, slot->key.bytes,
3271 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003273exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003275
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003277}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3280 psa_algorithm_t alg,
3281 const uint8_t *input,
3282 size_t input_length,
3283 const uint8_t *salt,
3284 size_t salt_length,
3285 uint8_t *output,
3286 size_t output_size,
3287 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003289 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003290 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003291 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003292 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003293
Darryl Green5cc689a2018-07-24 15:34:10 +01003294 (void) input;
3295 (void) input_length;
3296 (void) salt;
3297 (void) output;
3298 (void) output_size;
3299
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003300 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3303 return PSA_ERROR_INVALID_ARGUMENT;
3304 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003305
Ronald Cron5c522922020-11-14 16:35:34 +01003306 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3308 if (status != PSA_SUCCESS) {
3309 return status;
3310 }
3311 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003312 status = PSA_ERROR_INVALID_ARGUMENT;
3313 goto exit;
3314 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003316 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003318 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003319
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003320 status = psa_driver_wrapper_asymmetric_decrypt(
3321 &attributes, slot->key.data, slot->key.bytes,
3322 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003324
3325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003329}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003330
Paul Elliott9fe12f62022-11-30 19:16:02 +00003331/****************************************************************/
3332/* Asymmetric interruptible cryptography */
3333/****************************************************************/
3334
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003335static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3336
Paul Elliott9fe12f62022-11-30 19:16:02 +00003337void psa_interruptible_set_max_ops(uint32_t max_ops)
3338{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003339 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003340}
3341
3342uint32_t psa_interruptible_get_max_ops(void)
3343{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003344 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003345}
3346
Paul Elliott9fe12f62022-11-30 19:16:02 +00003347uint32_t psa_sign_hash_get_num_ops(
3348 const psa_sign_hash_interruptible_operation_t *operation)
3349{
Paul Elliott296ede92022-12-15 17:00:30 +00003350 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003351}
3352
3353uint32_t psa_verify_hash_get_num_ops(
3354 const psa_verify_hash_interruptible_operation_t *operation)
3355{
Paul Elliott296ede92022-12-15 17:00:30 +00003356 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003357}
3358
Paul Elliott59ad9452022-12-18 15:09:02 +00003359static psa_status_t psa_sign_hash_abort_internal(
3360 psa_sign_hash_interruptible_operation_t *operation)
3361{
3362 if (operation->id == 0) {
3363 /* The object has (apparently) been initialized but it is not (yet)
3364 * in use. It's ok to call abort on such an object, and there's
3365 * nothing to do. */
3366 return PSA_SUCCESS;
3367 }
3368
3369 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3370
3371 status = psa_driver_wrapper_sign_hash_abort(operation);
3372
3373 operation->id = 0;
3374
Paul Elliotte6145dc2023-02-07 12:51:21 +00003375 /* Do not clear either the error_occurred or num_ops elements here as they
3376 * only want to be cleared by the application calling abort, not by abort
3377 * being called at completion of an operation. */
3378
Paul Elliott59ad9452022-12-18 15:09:02 +00003379 return status;
3380}
3381
Paul Elliott9fe12f62022-11-30 19:16:02 +00003382psa_status_t psa_sign_hash_start(
3383 psa_sign_hash_interruptible_operation_t *operation,
3384 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3385 const uint8_t *hash, size_t hash_length)
3386{
3387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3388 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3389 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003390 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003391
Paul Elliottc9774412023-02-06 15:14:07 +00003392 /* Check that start has not been previously called, or operation has not
3393 * previously errored. */
3394 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395 return PSA_ERROR_BAD_STATE;
3396 }
3397
Paul Elliott9fe12f62022-11-30 19:16:02 +00003398 status = psa_sign_verify_check_alg(0, alg);
3399 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003400 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401 return status;
3402 }
3403
3404 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3405 PSA_KEY_USAGE_SIGN_HASH,
3406 alg);
3407
3408 if (status != PSA_SUCCESS) {
3409 goto exit;
3410 }
3411
3412 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3413 status = PSA_ERROR_INVALID_ARGUMENT;
3414 goto exit;
3415 }
3416
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003417 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003418 .core = slot->attr
3419 };
3420
Paul Elliott296ede92022-12-15 17:00:30 +00003421 /* Ensure ops count gets reset, in case of operation re-use. */
3422 operation->num_ops = 0;
3423
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3425 slot->key.data,
3426 slot->key.bytes, alg,
3427 hash, hash_length);
3428exit:
3429
3430 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003431 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003432 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003433 }
3434
3435 unlock_status = psa_unlock_key_slot(slot);
3436
Paul Elliottc9774412023-02-06 15:14:07 +00003437 if (unlock_status != PSA_SUCCESS) {
3438 operation->error_occurred = 1;
3439 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003440
Paul Elliottc9774412023-02-06 15:14:07 +00003441 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003442}
3443
3444
3445psa_status_t psa_sign_hash_complete(
3446 psa_sign_hash_interruptible_operation_t *operation,
3447 uint8_t *signature, size_t signature_size,
3448 size_t *signature_length)
3449{
3450 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3451
3452 *signature_length = 0;
3453
Paul Elliottc9774412023-02-06 15:14:07 +00003454 /* Check that start has been called first, and that operation has not
3455 * previously errored. */
3456 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003457 status = PSA_ERROR_BAD_STATE;
3458 goto exit;
3459 }
3460
Paul Elliott096abc42023-02-03 18:33:23 +00003461 /* Immediately reject a zero-length signature buffer. This guarantees that
3462 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003463 if (signature_size == 0) {
3464 status = PSA_ERROR_BUFFER_TOO_SMALL;
3465 goto exit;
3466 }
3467
3468 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3469 signature_size,
3470 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003471
Paul Elliott296ede92022-12-15 17:00:30 +00003472 /* Update ops count with work done. */
Dave Rodgman321c7e92023-09-28 18:07:06 +01003473 if (status == PSA_SUCCESS) {
3474 status = psa_driver_wrapper_sign_hash_get_num_ops(operation, &operation->num_ops);
3475 }
Paul Elliott296ede92022-12-15 17:00:30 +00003476
Paul Elliottebe225c2023-02-10 14:32:53 +00003477exit:
3478
Paul Elliott59200a22023-02-21 15:07:40 +00003479 psa_wipe_tag_output_buffer(signature, status, signature_size,
3480 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003481
Paul Elliott53bb3122023-02-10 14:22:22 +00003482 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003483 if (status != PSA_SUCCESS) {
3484 operation->error_occurred = 1;
3485 }
3486
Paul Elliott59ad9452022-12-18 15:09:02 +00003487 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003488 }
3489
3490 return status;
3491}
3492
3493psa_status_t psa_sign_hash_abort(
3494 psa_sign_hash_interruptible_operation_t *operation)
3495{
Paul Elliott59ad9452022-12-18 15:09:02 +00003496 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3497
3498 status = psa_sign_hash_abort_internal(operation);
3499
3500 /* We clear the number of ops done here, so that it is not cleared when
3501 * the operation fails or succeeds, only on manual abort. */
3502 operation->num_ops = 0;
3503
Paul Elliottc9774412023-02-06 15:14:07 +00003504 /* Likewise, failure state. */
3505 operation->error_occurred = 0;
3506
Paul Elliott59ad9452022-12-18 15:09:02 +00003507 return status;
3508}
3509
3510static psa_status_t psa_verify_hash_abort_internal(
3511 psa_verify_hash_interruptible_operation_t *operation)
3512{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003513 if (operation->id == 0) {
3514 /* The object has (apparently) been initialized but it is not (yet)
3515 * in use. It's ok to call abort on such an object, and there's
3516 * nothing to do. */
3517 return PSA_SUCCESS;
3518 }
3519
Paul Elliott59ad9452022-12-18 15:09:02 +00003520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3521
3522 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003523
3524 operation->id = 0;
3525
Paul Elliotte6145dc2023-02-07 12:51:21 +00003526 /* Do not clear either the error_occurred or num_ops elements here as they
3527 * only want to be cleared by the application calling abort, not by abort
3528 * being called at completion of an operation. */
3529
Paul Elliott59ad9452022-12-18 15:09:02 +00003530 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003531}
3532
3533psa_status_t psa_verify_hash_start(
3534 psa_verify_hash_interruptible_operation_t *operation,
3535 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3536 const uint8_t *hash, size_t hash_length,
3537 const uint8_t *signature, size_t signature_length)
3538{
3539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3540 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3541 psa_key_slot_t *slot;
3542
Paul Elliottc9774412023-02-06 15:14:07 +00003543 /* Check that start has not been previously called, or operation has not
3544 * previously errored. */
3545 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003546 return PSA_ERROR_BAD_STATE;
3547 }
3548
3549 status = psa_sign_verify_check_alg(0, alg);
3550 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003551 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003552 return status;
3553 }
3554
3555 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3556 PSA_KEY_USAGE_VERIFY_HASH,
3557 alg);
3558
3559 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003560 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003561 return status;
3562 }
3563
3564 psa_key_attributes_t attributes = {
3565 .core = slot->attr
3566 };
3567
Paul Elliott296ede92022-12-15 17:00:30 +00003568 /* Ensure ops count gets reset, in case of operation re-use. */
3569 operation->num_ops = 0;
3570
Paul Elliott9fe12f62022-11-30 19:16:02 +00003571 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3572 slot->key.data,
3573 slot->key.bytes,
3574 alg, hash, hash_length,
3575 signature, signature_length);
3576
3577 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003578 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003579 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003580 }
3581
3582 unlock_status = psa_unlock_key_slot(slot);
3583
Paul Elliottc9774412023-02-06 15:14:07 +00003584 if (unlock_status != PSA_SUCCESS) {
3585 operation->error_occurred = 1;
3586 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587
Paul Elliottc9774412023-02-06 15:14:07 +00003588 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003589}
3590
3591psa_status_t psa_verify_hash_complete(
3592 psa_verify_hash_interruptible_operation_t *operation)
3593{
3594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3595
Paul Elliottc9774412023-02-06 15:14:07 +00003596 /* Check that start has been called first, and that operation has not
3597 * previously errored. */
3598 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003599 status = PSA_ERROR_BAD_STATE;
3600 goto exit;
3601 }
3602
3603 status = psa_driver_wrapper_verify_hash_complete(operation);
3604
Paul Elliott296ede92022-12-15 17:00:30 +00003605 /* Update ops count with work done. */
Dave Rodgman321c7e92023-09-28 18:07:06 +01003606 if (status == PSA_SUCCESS) {
3607 status = psa_driver_wrapper_verify_hash_get_num_ops(operation, &operation->num_ops);
3608 }
Paul Elliott296ede92022-12-15 17:00:30 +00003609
Paul Elliottebe225c2023-02-10 14:32:53 +00003610exit:
3611
Paul Elliott9fe12f62022-11-30 19:16:02 +00003612 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003613 if (status != PSA_SUCCESS) {
3614 operation->error_occurred = 1;
3615 }
3616
Paul Elliott59ad9452022-12-18 15:09:02 +00003617 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003618 }
3619
3620 return status;
3621}
3622
3623psa_status_t psa_verify_hash_abort(
3624 psa_verify_hash_interruptible_operation_t *operation)
3625{
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627
Paul Elliott59ad9452022-12-18 15:09:02 +00003628 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003629
Paul Elliott59ad9452022-12-18 15:09:02 +00003630 /* We clear the number of ops done here, so that it is not cleared when
3631 * the operation fails or succeeds, only on manual abort. */
3632 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003633
Paul Elliottc9774412023-02-06 15:14:07 +00003634 /* Likewise, failure state. */
3635 operation->error_occurred = 0;
3636
Paul Elliott59ad9452022-12-18 15:09:02 +00003637 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003638}
3639
Paul Elliott588f8ed2022-12-02 18:10:26 +00003640/****************************************************************/
3641/* Asymmetric interruptible cryptography internal */
3642/* implementations */
3643/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003644
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3646{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003647
Paul Elliott588f8ed2022-12-02 18:10:26 +00003648#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3649 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3650 defined(MBEDTLS_ECP_RESTARTABLE)
3651
3652 /* Internal implementation uses zero to indicate infinite number max ops,
3653 * therefore avoid this value, and set to minimum possible. */
3654 if (max_ops == 0) {
3655 max_ops = 1;
3656 }
3657
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003659#else
3660 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003661#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3662 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3663 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3664}
3665
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003667 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668{
3669#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3670 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3671 defined(MBEDTLS_ECP_RESTARTABLE)
3672
Paul Elliott93d9ca82023-02-15 18:14:21 +00003673 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003674#else
3675 (void) operation;
3676 return 0;
3677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3678 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3679 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3680}
3681
3682uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003683 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003684{
3685 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3686 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3687 defined(MBEDTLS_ECP_RESTARTABLE)
3688
Paul Elliott93d9ca82023-02-15 18:14:21 +00003689 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690#else
3691 (void) operation;
3692 return 0;
3693#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3694 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3695 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3696}
3697
3698psa_status_t mbedtls_psa_sign_hash_start(
3699 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3700 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3701 size_t key_buffer_size, psa_algorithm_t alg,
3702 const uint8_t *hash, size_t hash_length)
3703{
3704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003705 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003706
Paul Elliott068fe072023-01-16 13:59:15 +00003707 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3708 return PSA_ERROR_NOT_SUPPORTED;
3709 }
3710
3711 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003712 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003713 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003714
3715#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003716 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3717 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
Paul Elliotta1c94092023-02-15 16:38:04 +00003719 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3720
Paul Elliott93d9ca82023-02-15 18:14:21 +00003721 /* Ensure num_ops is zero'ed in case of context re-use. */
3722 operation->num_ops = 0;
3723
Paul Elliott068fe072023-01-16 13:59:15 +00003724 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3725 attributes->core.bits,
3726 key_buffer,
3727 key_buffer_size,
3728 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003729
Paul Elliott068fe072023-01-16 13:59:15 +00003730 if (status != PSA_SUCCESS) {
3731 return status;
3732 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733
Paul Elliott1bc59df2023-02-05 13:41:57 +00003734 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003735 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003736
Paul Elliott068fe072023-01-16 13:59:15 +00003737 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003738 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003739 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740
Paul Elliott0290a762023-02-09 14:30:24 +00003741 /* We only need to store the same length of hash as the private key size
3742 * here, it would be truncated by the internal implementation anyway. */
3743 required_hash_length = (hash_length < operation->coordinate_bytes ?
3744 hash_length : operation->coordinate_bytes);
3745
Paul Elliottba70ad42023-02-15 18:23:53 +00003746 if (required_hash_length > sizeof(operation->hash)) {
3747 /* Shouldn't happen, but better safe than sorry. */
3748 return PSA_ERROR_CORRUPTION_DETECTED;
3749 }
3750
Paul Elliott0290a762023-02-09 14:30:24 +00003751 memcpy(operation->hash, hash, required_hash_length);
3752 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003753
3754 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003755
3756#else
Paul Elliott068fe072023-01-16 13:59:15 +00003757 (void) operation;
3758 (void) key_buffer;
3759 (void) key_buffer_size;
3760 (void) alg;
3761 (void) hash;
3762 (void) hash_length;
3763 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003764 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765
Paul Elliott068fe072023-01-16 13:59:15 +00003766 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3768 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3769 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003770}
3771
3772psa_status_t mbedtls_psa_sign_hash_complete(
3773 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3774 uint8_t *signature, size_t signature_size,
3775 size_t *signature_length)
3776{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3778 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3779 defined(MBEDTLS_ECP_RESTARTABLE)
3780
Paul Elliotta1c94092023-02-15 16:38:04 +00003781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003782 mbedtls_mpi r;
3783 mbedtls_mpi s;
3784
Paul Elliott46845252023-02-03 14:59:11 +00003785 mbedtls_mpi_init(&r);
3786 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003787
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003788 /* Ensure max_ops is set to the current value (or default). */
3789 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3790
Paul Elliotta1c94092023-02-15 16:38:04 +00003791 if (signature_size < 2 * operation->coordinate_bytes) {
3792 status = PSA_ERROR_BUFFER_TOO_SMALL;
3793 goto exit;
3794 }
3795
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003797
Paul Elliott588f8ed2022-12-02 18:10:26 +00003798#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3799 status = mbedtls_to_psa_error(
3800 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003801 &r,
3802 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003803 &operation->ctx->d,
3804 operation->hash,
3805 operation->hash_length,
3806 operation->md_alg,
3807 mbedtls_psa_get_random,
3808 MBEDTLS_PSA_RANDOM_STATE,
3809 &operation->restart_ctx));
3810#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003811 status = PSA_ERROR_NOT_SUPPORTED;
3812 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3814 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003815 status = mbedtls_to_psa_error(
3816 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003817 &r,
3818 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819 &operation->ctx->d,
3820 operation->hash,
3821 operation->hash_length,
3822 mbedtls_psa_get_random,
3823 MBEDTLS_PSA_RANDOM_STATE,
3824 mbedtls_psa_get_random,
3825 MBEDTLS_PSA_RANDOM_STATE,
3826 &operation->restart_ctx));
3827 }
3828
Paul Elliottf8e5b562023-02-19 18:43:45 +00003829 /* Hide the fact that the restart context only holds a delta of number of
3830 * ops done during the last operation, not an absolute value. */
3831 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3832
Paul Elliott724bd252023-02-08 12:35:08 +00003833 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003835 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003837 operation->coordinate_bytes)
3838 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839
3840 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003841 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003842 }
3843
3844 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003845 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003847 operation->coordinate_bytes,
3848 operation->coordinate_bytes)
3849 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850
3851 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003852 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853 }
3854
Paul Elliott1bc59df2023-02-05 13:41:57 +00003855 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856
Paul Elliott724bd252023-02-08 12:35:08 +00003857 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003858 }
Paul Elliott724bd252023-02-08 12:35:08 +00003859
3860exit:
3861
3862 mbedtls_mpi_free(&r);
3863 mbedtls_mpi_free(&s);
3864 return status;
3865
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 #else
3867
3868 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869 (void) signature;
3870 (void) signature_size;
3871 (void) signature_length;
3872
3873 return PSA_ERROR_NOT_SUPPORTED;
3874
3875#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3876 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3877 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3878}
3879
3880psa_status_t mbedtls_psa_sign_hash_abort(
3881 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3882{
3883
3884#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3885 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3886 defined(MBEDTLS_ECP_RESTARTABLE)
3887
3888 if (operation->ctx) {
3889 mbedtls_ecdsa_free(operation->ctx);
3890 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003891 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892 }
3893
3894 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3895
Paul Elliott93d9ca82023-02-15 18:14:21 +00003896 operation->num_ops = 0;
3897
Paul Elliott588f8ed2022-12-02 18:10:26 +00003898 return PSA_SUCCESS;
3899
3900#else
3901
3902 (void) operation;
3903
3904 return PSA_ERROR_NOT_SUPPORTED;
3905
3906#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3907 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3908 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3909}
3910
3911psa_status_t mbedtls_psa_verify_hash_start(
3912 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3913 const psa_key_attributes_t *attributes,
3914 const uint8_t *key_buffer, size_t key_buffer_size,
3915 psa_algorithm_t alg,
3916 const uint8_t *hash, size_t hash_length,
3917 const uint8_t *signature, size_t signature_length)
3918{
3919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003920 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003921 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003922
Paul Elliott068fe072023-01-16 13:59:15 +00003923 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3924 return PSA_ERROR_NOT_SUPPORTED;
3925 }
3926
3927 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003928 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003929 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003930
3931#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003932 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3933 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934
Paul Elliotta1c94092023-02-15 16:38:04 +00003935 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3936 mbedtls_mpi_init(&operation->r);
3937 mbedtls_mpi_init(&operation->s);
3938
Paul Elliott93d9ca82023-02-15 18:14:21 +00003939 /* Ensure num_ops is zero'ed in case of context re-use. */
3940 operation->num_ops = 0;
3941
Paul Elliott068fe072023-01-16 13:59:15 +00003942 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3943 attributes->core.bits,
3944 key_buffer,
3945 key_buffer_size,
3946 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003947
Paul Elliott068fe072023-01-16 13:59:15 +00003948 if (status != PSA_SUCCESS) {
3949 return status;
3950 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliottc569fc22023-02-10 13:02:54 +00003952 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003953
Paul Elliott1bc59df2023-02-05 13:41:57 +00003954 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003955 return PSA_ERROR_INVALID_SIGNATURE;
3956 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957
Paul Elliott068fe072023-01-16 13:59:15 +00003958 status = mbedtls_to_psa_error(
3959 mbedtls_mpi_read_binary(&operation->r,
3960 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003961 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003962
Paul Elliott068fe072023-01-16 13:59:15 +00003963 if (status != PSA_SUCCESS) {
3964 return status;
3965 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliott068fe072023-01-16 13:59:15 +00003967 status = mbedtls_to_psa_error(
3968 mbedtls_mpi_read_binary(&operation->s,
3969 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003970 coordinate_bytes,
3971 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003972
Paul Elliott068fe072023-01-16 13:59:15 +00003973 if (status != PSA_SUCCESS) {
3974 return status;
3975 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliott2c9843f2023-02-15 17:32:42 +00003977 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978
Paul Elliott2c9843f2023-02-15 17:32:42 +00003979 if (status != PSA_SUCCESS) {
3980 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003981 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003982
Paul Elliott0290a762023-02-09 14:30:24 +00003983 /* We only need to store the same length of hash as the private key size
3984 * here, it would be truncated by the internal implementation anyway. */
3985 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3986 coordinate_bytes);
3987
Paul Elliottba70ad42023-02-15 18:23:53 +00003988 if (required_hash_length > sizeof(operation->hash)) {
3989 /* Shouldn't happen, but better safe than sorry. */
3990 return PSA_ERROR_CORRUPTION_DETECTED;
3991 }
3992
Paul Elliott0290a762023-02-09 14:30:24 +00003993 memcpy(operation->hash, hash, required_hash_length);
3994 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003995
3996 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003997#else
Paul Elliott068fe072023-01-16 13:59:15 +00003998 (void) operation;
3999 (void) key_buffer;
4000 (void) key_buffer_size;
4001 (void) alg;
4002 (void) hash;
4003 (void) hash_length;
4004 (void) signature;
4005 (void) signature_length;
4006 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004007 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004008 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009
Paul Elliott068fe072023-01-16 13:59:15 +00004010 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004011#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4012 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4013 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004014}
4015
4016psa_status_t mbedtls_psa_verify_hash_complete(
4017 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4018{
4019
4020#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4021 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4022 defined(MBEDTLS_ECP_RESTARTABLE)
4023
Paul Elliottf8e5b562023-02-19 18:43:45 +00004024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4025
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004026 /* Ensure max_ops is set to the current value (or default). */
4027 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4028
Paul Elliottf8e5b562023-02-19 18:43:45 +00004029 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004030 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4031 operation->hash,
4032 operation->hash_length,
4033 &operation->ctx->Q,
4034 &operation->r,
4035 &operation->s,
4036 &operation->restart_ctx));
4037
Paul Elliottf8e5b562023-02-19 18:43:45 +00004038 /* Hide the fact that the restart context only holds a delta of number of
4039 * ops done during the last operation, not an absolute value. */
4040 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4041
4042 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004043#else
4044 (void) operation;
4045
4046 return PSA_ERROR_NOT_SUPPORTED;
4047
4048#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4049 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4050 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4051}
4052
4053psa_status_t mbedtls_psa_verify_hash_abort(
4054 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4055{
4056
4057#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4058 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4059 defined(MBEDTLS_ECP_RESTARTABLE)
4060
4061 if (operation->ctx) {
4062 mbedtls_ecdsa_free(operation->ctx);
4063 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004064 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004065 }
4066
4067 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4068
Paul Elliott93d9ca82023-02-15 18:14:21 +00004069 operation->num_ops = 0;
4070
Paul Elliott588f8ed2022-12-02 18:10:26 +00004071 mbedtls_mpi_free(&operation->r);
4072 mbedtls_mpi_free(&operation->s);
4073
4074 return PSA_SUCCESS;
4075
4076#else
4077 (void) operation;
4078
4079 return PSA_ERROR_NOT_SUPPORTED;
4080
4081#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4082 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4083 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4084}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004085
mohammad1603503973b2018-03-12 15:59:30 +02004086/****************************************************************/
4087/* Symmetric cryptography */
4088/****************************************************************/
4089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4091 mbedtls_svc_key_id_t key,
4092 psa_algorithm_t alg,
4093 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004094{
4095 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4096 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004097 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4099 PSA_KEY_USAGE_ENCRYPT :
4100 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004101 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004102
4103 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004105 status = PSA_ERROR_BAD_STATE;
4106 goto exit;
4107 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004110 status = PSA_ERROR_INVALID_ARGUMENT;
4111 goto exit;
4112 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4115 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004118
Ronald Cron49fafa92021-03-10 08:34:23 +01004119 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004121 * so we only set it (in the driver wrapper) after resources have been
4122 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004123 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004125 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004127 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 }
4129 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004130
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004131 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004133 };
4134
Ronald Cronab99ac22020-10-01 16:38:28 +02004135 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (cipher_operation == MBEDTLS_ENCRYPT) {
4137 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4138 &attributes,
4139 slot->key.data,
4140 slot->key.bytes,
4141 alg);
4142 } else {
4143 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4144 &attributes,
4145 slot->key.data,
4146 slot->key.bytes,
4147 alg);
4148 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004149
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (status != PSA_SUCCESS) {
4152 psa_cipher_abort(operation);
4153 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4161 mbedtls_svc_key_id_t key,
4162 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004163{
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004165}
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4168 mbedtls_svc_key_id_t key,
4169 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004170{
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004172}
4173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4175 uint8_t *iv,
4176 size_t iv_size,
4177 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004178{
Ronald Cron6d051732020-10-01 14:10:20 +02004179 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004180 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004181 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004184 status = PSA_ERROR_BAD_STATE;
4185 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004186 }
4187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004189 status = PSA_ERROR_BAD_STATE;
4190 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004191 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004192
Ronald Cron2fb90522021-07-05 12:31:44 +02004193 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004194 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004195 status = PSA_ERROR_BUFFER_TOO_SMALL;
4196 goto exit;
4197 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004198
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004200 status = PSA_ERROR_GENERIC_ERROR;
4201 goto exit;
4202 }
4203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 status = psa_generate_random(local_iv, default_iv_length);
4205 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 }
Ronald Cron5618a392021-03-26 09:52:26 +01004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_driver_wrapper_cipher_set_iv(operation,
4210 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004211
4212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (status == PSA_SUCCESS) {
4214 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004215 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004216 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004218 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004220 }
Ronald Cron6d051732020-10-01 14:10:20 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004223}
4224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4226 const uint8_t *iv,
4227 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004228{
Ronald Cron6d051732020-10-01 14:10:20 +02004229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004230
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004232 status = PSA_ERROR_BAD_STATE;
4233 goto exit;
4234 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004237 status = PSA_ERROR_BAD_STATE;
4238 goto exit;
4239 }
Ronald Crona0d68172021-03-26 10:15:08 +01004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004242 status = PSA_ERROR_INVALID_ARGUMENT;
4243 goto exit;
4244 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004245
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 status = psa_driver_wrapper_cipher_set_iv(operation,
4247 iv,
4248 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004249
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 } else {
4254 psa_cipher_abort(operation);
4255 }
4256 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004257}
4258
Gilles Peskine449bd832023-01-11 14:50:10 +01004259psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4260 const uint8_t *input,
4261 size_t input_length,
4262 uint8_t *output,
4263 size_t output_size,
4264 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004265{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269 status = PSA_ERROR_BAD_STATE;
4270 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004271 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004274 status = PSA_ERROR_BAD_STATE;
4275 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004276 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 status = psa_driver_wrapper_cipher_update(operation,
4279 input,
4280 input_length,
4281 output,
4282 output_size,
4283 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004284
4285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (status != PSA_SUCCESS) {
4287 psa_cipher_abort(operation);
4288 }
Ronald Cron6d051732020-10-01 14:10:20 +02004289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004291}
4292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4294 uint8_t *output,
4295 size_t output_size,
4296 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004297{
David Saadab4ecc272019-02-14 13:48:10 +02004298 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301 status = PSA_ERROR_BAD_STATE;
4302 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004303 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004306 status = PSA_ERROR_BAD_STATE;
4307 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004308 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_driver_wrapper_cipher_finish(operation,
4311 output,
4312 output_size,
4313 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004314
4315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (status == PSA_SUCCESS) {
4317 return psa_cipher_abort(operation);
4318 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004319 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004323 }
mohammad1603503973b2018-03-12 15:59:30 +02004324}
4325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004327{
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004329 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004330 * in use. It's ok to call abort on such an object, and there's
4331 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004333 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004336
Ronald Cron49fafa92021-03-10 08:34:23 +01004337 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004338 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004339 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004342}
4343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4345 psa_algorithm_t alg,
4346 const uint8_t *input,
4347 size_t input_length,
4348 uint8_t *output,
4349 size_t output_size,
4350 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004351{
4352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004353 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004354 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004355 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4356 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004357 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004360 status = PSA_ERROR_INVALID_ARGUMENT;
4361 goto exit;
4362 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4365 PSA_KEY_USAGE_ENCRYPT,
4366 alg);
4367 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004368 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004370
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004371 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004373 };
4374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4376 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004377 status = PSA_ERROR_GENERIC_ERROR;
4378 goto exit;
4379 }
4380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (default_iv_length > 0) {
4382 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004383 status = PSA_ERROR_BUFFER_TOO_SMALL;
4384 goto exit;
4385 }
4386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_generate_random(local_iv, default_iv_length);
4388 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004391 }
4392
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004393 status = psa_driver_wrapper_cipher_encrypt(
4394 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004395 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004396 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004398
4399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 unlock_status = psa_unlock_key_slot(slot);
4401 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004402 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004403 }
Ronald Cron23919522021-07-08 19:00:07 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (status == PSA_SUCCESS) {
4406 if (default_iv_length > 0) {
4407 memcpy(output, local_iv, default_iv_length);
4408 }
4409 *output_length += default_iv_length;
4410 } else {
4411 *output_length = 0;
4412 }
4413
4414 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004415}
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4418 psa_algorithm_t alg,
4419 const uint8_t *input,
4420 size_t input_length,
4421 uint8_t *output,
4422 size_t output_size,
4423 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004424{
4425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004426 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004427 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004428 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004431 status = PSA_ERROR_INVALID_ARGUMENT;
4432 goto exit;
4433 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4436 PSA_KEY_USAGE_DECRYPT,
4437 alg);
4438 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004441
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004442 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004444 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4447 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004448 status = PSA_ERROR_INVALID_ARGUMENT;
4449 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004451 status = PSA_ERROR_INVALID_ARGUMENT;
4452 goto exit;
4453 }
4454
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004455 status = psa_driver_wrapper_cipher_decrypt(
4456 &attributes, slot->key.data, slot->key.bytes,
4457 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004459
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004460exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 unlock_status = psa_unlock_key_slot(slot);
4462 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004463 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004467 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 }
Ronald Cron23919522021-07-08 19:00:07 +02004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004471}
4472
4473
mohammad16035955c982018-04-26 00:53:03 +03004474/****************************************************************/
4475/* AEAD */
4476/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004477
Paul Elliottbaff51c2021-09-28 17:44:45 +01004478/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004479static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004480{
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004482}
4483
4484/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004485static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4486 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004487{
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004491#if defined(PSA_WANT_ALG_GCM)
4492 case PSA_ALG_GCM:
4493 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 * arbitrarily large nonces. Please note that we do not generally
4495 * recommend the usage of nonces of greater length than
4496 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4497 * size, which can then lead to collisions if you encrypt a very
4498 * large number of messages.*/
4499 if (nonce_length != 0) {
4500 return PSA_SUCCESS;
4501 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004502 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004503#endif /* PSA_WANT_ALG_GCM */
4504#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004505 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (nonce_length >= 7 && nonce_length <= 13) {
4507 return PSA_SUCCESS;
4508 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004510#endif /* PSA_WANT_ALG_CCM */
4511#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004512 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (nonce_length == 12) {
4514 return PSA_SUCCESS;
4515 } else if (nonce_length == 8) {
4516 return PSA_ERROR_NOT_SUPPORTED;
4517 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004518 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004519#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004520 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004521 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004523 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004526}
4527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004529{
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4531 return PSA_ERROR_INVALID_ARGUMENT;
4532 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004535}
4536
Gilles Peskine449bd832023-01-11 14:50:10 +01004537psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4538 psa_algorithm_t alg,
4539 const uint8_t *nonce,
4540 size_t nonce_length,
4541 const uint8_t *additional_data,
4542 size_t additional_data_length,
4543 const uint8_t *plaintext,
4544 size_t plaintext_length,
4545 uint8_t *ciphertext,
4546 size_t ciphertext_size,
4547 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004548{
Ronald Cron215633c2021-03-16 17:15:37 +01004549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4550 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004551
mohammad1603f08a5502018-06-03 15:05:47 +03004552 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004553
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 status = psa_aead_check_algorithm(alg);
4555 if (status != PSA_SUCCESS) {
4556 return status;
4557 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004558
Ronald Cron9a986162021-03-26 12:40:07 +01004559 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4561 if (status != PSA_SUCCESS) {
4562 return status;
4563 }
mohammad16035955c982018-04-26 00:53:03 +03004564
Ronald Cron215633c2021-03-16 17:15:37 +01004565 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004567 };
mohammad16035955c982018-04-26 00:53:03 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 status = psa_aead_check_nonce_length(alg, nonce_length);
4570 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004573
Ronald Cronde822812021-03-17 16:08:20 +01004574 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004575 &attributes, slot->key.data, slot->key.bytes,
4576 alg,
4577 nonce, nonce_length,
4578 additional_data, additional_data_length,
4579 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4583 memset(ciphertext, 0, ciphertext_size);
4584 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004585
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004586exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004590}
4591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4593 psa_algorithm_t alg,
4594 const uint8_t *nonce,
4595 size_t nonce_length,
4596 const uint8_t *additional_data,
4597 size_t additional_data_length,
4598 const uint8_t *ciphertext,
4599 size_t ciphertext_length,
4600 uint8_t *plaintext,
4601 size_t plaintext_size,
4602 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004603{
Ronald Cron215633c2021-03-16 17:15:37 +01004604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4605 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004606
Gilles Peskineee652a32018-06-01 19:23:52 +02004607 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 status = psa_aead_check_algorithm(alg);
4610 if (status != PSA_SUCCESS) {
4611 return status;
4612 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004613
Ronald Cron9a986162021-03-26 12:40:07 +01004614 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4616 if (status != PSA_SUCCESS) {
4617 return status;
4618 }
mohammad16035955c982018-04-26 00:53:03 +03004619
Ronald Cron215633c2021-03-16 17:15:37 +01004620 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004622 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 status = psa_aead_check_nonce_length(alg, nonce_length);
4625 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004628
Ronald Cronde822812021-03-17 16:08:20 +01004629 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004630 &attributes, slot->key.data, slot->key.bytes,
4631 alg,
4632 nonce, nonce_length,
4633 additional_data, additional_data_length,
4634 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if (status != PSA_SUCCESS && plaintext_size != 0) {
4638 memset(plaintext, 0, plaintext_size);
4639 }
mohammad160360a64d02018-06-03 17:20:42 +03004640
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004641exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 return status;
mohammad16035955c982018-04-26 00:53:03 +03004645}
4646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4648{
4649 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004652#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4656 return PSA_ERROR_INVALID_ARGUMENT;
4657 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004659#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004660
Przemek Stekiel86679c72022-10-06 17:06:56 +02004661#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004663 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4665 return PSA_ERROR_INVALID_ARGUMENT;
4666 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004668#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669
Przemek Stekiel86679c72022-10-06 17:06:56 +02004670#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004672 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (tag_len != 16) {
4674 return PSA_ERROR_INVALID_ARGUMENT;
4675 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004677#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004678
4679 default:
4680 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004684}
4685
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004686/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004687static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4688 int is_encrypt,
4689 mbedtls_svc_key_id_t key,
4690 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004691{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004692 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4693 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4694 psa_key_slot_t *slot = NULL;
4695 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004696 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 status = psa_aead_check_algorithm(alg);
4699 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004700 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704 status = PSA_ERROR_BAD_STATE;
4705 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004706 }
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (operation->nonce_set || operation->lengths_set ||
4709 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004710 status = PSA_ERROR_BAD_STATE;
4711 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004712 }
4713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4721 alg);
4722 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004725
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004726 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004727 .core = slot->attr
4728 };
4729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (is_encrypt) {
4735 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4736 &attributes,
4737 slot->key.data,
4738 slot->key.bytes,
4739 alg);
4740 } else {
4741 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4742 &attributes,
4743 slot->key.data,
4744 slot->key.bytes,
4745 alg);
4746 }
4747 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752
4753exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004757 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004759 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 } else {
4761 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004762 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004765}
4766
Paul Elliott302ff6b2021-04-20 18:10:30 +01004767/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4769 mbedtls_svc_key_id_t key,
4770 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771{
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004773}
4774
4775/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004776psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4777 mbedtls_svc_key_id_t key,
4778 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779{
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004781}
4782
4783/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004784psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4785 uint8_t *nonce,
4786 size_t nonce_size,
4787 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004788{
4789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004790 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004791 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004793 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004796 status = PSA_ERROR_BAD_STATE;
4797 goto exit;
4798 }
4799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801 status = PSA_ERROR_BAD_STATE;
4802 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004803 }
4804
Paul Elliotte193ea82021-10-01 13:00:16 +01004805 /* For CCM, this size may not be correct according to the PSA
4806 * specification. The PSA Crypto 1.0.1 specification states:
4807 *
4808 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4809 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4810 *
4811 * However this restriction that L has to be the smallest integer is not
4812 * applied in practice, and it is not implementable here since the
4813 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4815 operation->alg);
4816 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004817 status = PSA_ERROR_BUFFER_TOO_SMALL;
4818 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004819 }
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 status = psa_generate_random(local_nonce, required_nonce_size);
4822 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004823 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827
Paul Elliott39dc6b82021-05-11 19:16:09 +01004828exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (status == PSA_SUCCESS) {
4830 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004831 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 } else {
4833 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004834 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004837}
4838
4839/* Set the nonce for a multipart authenticated encryption or decryption
4840 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004841psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4842 const uint8_t *nonce,
4843 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004844{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004845 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004848 status = PSA_ERROR_BAD_STATE;
4849 goto exit;
4850 }
4851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004853 status = PSA_ERROR_BAD_STATE;
4854 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004855 }
4856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4858 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004859 status = PSA_ERROR_INVALID_ARGUMENT;
4860 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004861 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4864 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004865
Paul Elliott39dc6b82021-05-11 19:16:09 +01004866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004868 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 } else {
4870 psa_aead_abort(operation);
4871 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874}
4875
4876/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4878 size_t ad_length,
4879 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004884 status = PSA_ERROR_BAD_STATE;
4885 goto exit;
4886 }
4887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (operation->lengths_set || operation->ad_started ||
4889 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004890 status = PSA_ERROR_BAD_STATE;
4891 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004892 }
4893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004895#if defined(PSA_WANT_ALG_GCM)
4896 case PSA_ALG_GCM:
4897 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 * bits. Without the guard this code will generate warnings on 32bit
4899 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004900#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 if (((uint64_t) ad_length) >> 61 != 0 ||
4902 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004903 status = PSA_ERROR_INVALID_ARGUMENT;
4904 goto exit;
4905 }
Paul Elliott325d3742021-09-27 17:56:28 +01004906#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004908#endif /* PSA_WANT_ALG_GCM */
4909#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004912 status = PSA_ERROR_INVALID_ARGUMENT;
4913 goto exit;
4914 }
4915 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004916#endif /* PSA_WANT_ALG_CCM */
4917#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004918 case PSA_ALG_CHACHA20_POLY1305:
4919 /* No length restrictions for ChaChaPoly. */
4920 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004921#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004922 default:
4923 break;
4924 }
Paul Elliott325d3742021-09-27 17:56:28 +01004925
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4927 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004928
Paul Elliott39dc6b82021-05-11 19:16:09 +01004929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004931 operation->ad_remaining = ad_length;
4932 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 } else {
4935 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004936 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004939}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004940
4941/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004942psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4943 const uint8_t *input,
4944 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004945{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004949 status = PSA_ERROR_BAD_STATE;
4950 goto exit;
4951 }
4952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004954 status = PSA_ERROR_BAD_STATE;
4955 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004956 }
4957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 if (operation->lengths_set) {
4959 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004960 status = PSA_ERROR_INVALID_ARGUMENT;
4961 goto exit;
4962 }
4963
4964 operation->ad_remaining -= input_length;
4965 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004966#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004968 status = PSA_ERROR_BAD_STATE;
4969 goto exit;
4970 }
4971#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 status = psa_driver_wrapper_aead_update_ad(operation, input,
4974 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004975
Paul Elliott39dc6b82021-05-11 19:16:09 +01004976exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004978 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 } else {
4980 psa_aead_abort(operation);
4981 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004984}
4985
4986/* Encrypt or decrypt a message fragment in an active multipart AEAD
4987 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004988psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4989 const uint8_t *input,
4990 size_t input_length,
4991 uint8_t *output,
4992 size_t output_size,
4993 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004996
4997 *output_length = 0;
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005000 status = PSA_ERROR_BAD_STATE;
5001 goto exit;
5002 }
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005005 status = PSA_ERROR_BAD_STATE;
5006 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005007 }
5008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005010 /* Additional data length was supplied, but not all the additional
5011 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005013 status = PSA_ERROR_INVALID_ARGUMENT;
5014 goto exit;
5015 }
5016
5017 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005019 status = PSA_ERROR_INVALID_ARGUMENT;
5020 goto exit;
5021 }
5022
5023 operation->body_remaining -= input_length;
5024 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005025#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005027 status = PSA_ERROR_BAD_STATE;
5028 goto exit;
5029 }
5030#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5033 output, output_size,
5034 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005035
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005038 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 } else {
5040 psa_aead_abort(operation);
5041 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005044}
5045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005047{
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 if (operation->id == 0 || !operation->nonce_set) {
5049 return PSA_ERROR_BAD_STATE;
5050 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5053 operation->body_remaining != 0)) {
5054 return PSA_ERROR_INVALID_ARGUMENT;
5055 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005058}
5059
Paul Elliott302ff6b2021-04-20 18:10:30 +01005060/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005061psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5062 uint8_t *ciphertext,
5063 size_t ciphertext_size,
5064 size_t *ciphertext_length,
5065 uint8_t *tag,
5066 size_t tag_size,
5067 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5070
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005072 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 status = psa_aead_final_checks(operation);
5075 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005078
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005080 status = PSA_ERROR_BAD_STATE;
5081 goto exit;
5082 }
5083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5085 ciphertext_size,
5086 ciphertext_length,
5087 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005088
Paul Elliott39dc6b82021-05-11 19:16:09 +01005089exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005090
5091
Paul Elliottf47b0952021-05-21 18:02:33 +01005092 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005093 * the zero tag size, make sure the tag is set to something implausible.
5094 * Even if the operation succeeds, make sure we clear the rest of the
5095 * buffer to prevent potential leakage of anything previously placed in
5096 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005097 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005100
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005102}
5103
5104/* Finish authenticating and decrypting a message in a multipart AEAD
5105 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005106psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5107 uint8_t *plaintext,
5108 size_t plaintext_size,
5109 size_t *plaintext_length,
5110 const uint8_t *tag,
5111 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005112{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5114
Paul Elliott302ff6b2021-04-20 18:10:30 +01005115 *plaintext_length = 0;
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 status = psa_aead_final_checks(operation);
5118 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005123 status = PSA_ERROR_BAD_STATE;
5124 goto exit;
5125 }
5126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5128 plaintext_size,
5129 plaintext_length,
5130 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005131
5132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005136}
5137
5138/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005139psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140{
5141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144 /* The object has (apparently) been initialized but it is not (yet)
5145 * in use. It's ok to call abort on such an object, and there's
5146 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005148 }
5149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005155}
5156
Gilles Peskinee59236f2018-01-27 23:32:46 +01005157/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158/* Generators */
5159/****************************************************************/
5160
Przemek Stekiel69c46792022-06-10 12:59:51 +02005161#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305164 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305165 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005166#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005167#endif /* At least one builtin KDF */
5168
Przemek Stekiel69c46792022-06-10 12:59:51 +02005169#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005170 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5171 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5172static psa_status_t psa_key_derivation_start_hmac(
5173 psa_mac_operation_t *operation,
5174 psa_algorithm_t hash_alg,
5175 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005177{
5178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5181 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005183
Steven Cooreman72f736a2021-05-07 14:14:37 +02005184 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 status = psa_driver_wrapper_mac_sign_setup(operation,
5188 &attributes,
5189 hmac_key, hmac_key_length,
5190 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 psa_reset_key_attributes(&attributes);
5193 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005194}
5195#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005196
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005197#define HKDF_STATE_INIT 0 /* no input yet */
5198#define HKDF_STATE_STARTED 1 /* got salt */
5199#define HKDF_STATE_KEYED 2 /* got key */
5200#define HKDF_STATE_OUTPUT 3 /* output started */
5201
Gilles Peskinecbe66502019-05-16 16:59:18 +02005202static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204{
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5206 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5207 } else {
5208 return operation->alg;
5209 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210}
5211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213{
5214 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5216 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217 /* The object has (apparently) been initialized but it is not
5218 * in use. It's ok to call abort on such an object, and there's
5219 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005221#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5223 mbedtls_free(operation->ctx.hkdf.info);
5224 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5225 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005226#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005227#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5228 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5230 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5231 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5232 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005233 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005235 }
5236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005238 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005240 }
5241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005243 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005245 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005248 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005250 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005251#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005252 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005253
5254 /* We leave the fields Ai and output_block to be erased safely by the
5255 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005257#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5258 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005259#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5261 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5262 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5263 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005264#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305265#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305266 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005268 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305269 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305270 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305271
5272 status = PSA_SUCCESS;
5273 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305274#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005275 {
5276 status = PSA_ERROR_BAD_STATE;
5277 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 mbedtls_platform_zeroize(operation, sizeof(*operation));
5279 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280}
5281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284{
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005288 }
5289
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292}
5293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5295 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005296{
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 if (operation->alg == 0) {
5298 return PSA_ERROR_BAD_STATE;
5299 }
5300 if (capacity > operation->capacity) {
5301 return PSA_ERROR_INVALID_ARGUMENT;
5302 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005303 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005305}
5306
Przemek Stekiel69c46792022-06-10 12:59:51 +02005307#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005308/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5310 psa_algorithm_t kdf_alg,
5311 uint8_t *output,
5312 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005313{
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5315 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005316 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005317 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005320#else
5321 const uint8_t last_block = 0xff;
5322#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005323
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (hkdf->state < HKDF_STATE_KEYED ||
5325 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005326#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005328#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 )) {
5330 return PSA_ERROR_BAD_STATE;
5331 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 hkdf->state = HKDF_STATE_OUTPUT;
5333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335 /* Copy what remains of the current block */
5336 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005338 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 }
5340 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341 output += n;
5342 output_length -= n;
5343 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005347 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005348 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005350 * object was corrupted or if this function is called directly
5351 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 if (hkdf->block_number == last_block) {
5353 return PSA_ERROR_BAD_STATE;
5354 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005355
5356 /* We need a new block */
5357 ++hkdf->block_number;
5358 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5361 hash_alg,
5362 hkdf->prk,
5363 hash_length);
5364 if (status != PSA_SUCCESS) {
5365 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005366 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005367
5368 if (hkdf->block_number != 1) {
5369 status = psa_mac_update(&hkdf->hmac,
5370 hkdf->output_block,
5371 hash_length);
5372 if (status != PSA_SUCCESS) {
5373 return status;
5374 }
5375 }
5376 status = psa_mac_update(&hkdf->hmac,
5377 hkdf->info,
5378 hkdf->info_length);
5379 if (status != PSA_SUCCESS) {
5380 return status;
5381 }
5382 status = psa_mac_update(&hkdf->hmac,
5383 &hkdf->block_number, 1);
5384 if (status != PSA_SUCCESS) {
5385 return status;
5386 }
5387 status = psa_mac_sign_finish(&hkdf->hmac,
5388 hkdf->output_block,
5389 sizeof(hkdf->output_block),
5390 &hmac_output_length);
5391 if (status != PSA_SUCCESS) {
5392 return status;
5393 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005397}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005398#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005399
John Durkop07cc04a2020-11-16 22:08:34 -08005400#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5401 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005402static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5403 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005405{
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5407 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005408 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5409 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005410 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005411
Janos Follath7742fee2019-06-17 12:58:10 +01005412 /* We can't be wanting more output after block 0xff, otherwise
5413 * the capacity check in psa_key_derivation_output_bytes() would have
5414 * prevented this call. It could happen only if the operation
5415 * object was corrupted or if this function is called directly
5416 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if (tls12_prf->block_number == 0xff) {
5418 return PSA_ERROR_CORRUPTION_DETECTED;
5419 }
Janos Follath7742fee2019-06-17 12:58:10 +01005420
5421 /* We need a new block */
5422 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005423 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005424
5425 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5426 *
5427 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5428 *
5429 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5430 * HMAC_hash(secret, A(2) + seed) +
5431 * HMAC_hash(secret, A(3) + seed) + ...
5432 *
5433 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005435 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005436 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005437 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005438 * is currently extracted as `output_block` and where i is
5439 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005440 */
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 status = psa_key_derivation_start_hmac(&hmac,
5443 hash_alg,
5444 tls12_prf->secret,
5445 tls12_prf->secret_length);
5446 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005449
5450 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5453 * the variable seed and in this instance means it in the context of the
5454 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_mac_update(&hmac,
5456 tls12_prf->label,
5457 tls12_prf->label_length);
5458 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005459 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 }
5461 status = psa_mac_update(&hmac,
5462 tls12_prf->seed,
5463 tls12_prf->seed_length);
5464 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 }
5467 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5470 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 status = psa_mac_sign_finish(&hmac,
5476 tls12_prf->Ai, hash_length,
5477 &hmac_output_length);
5478 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005479 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 }
5481 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005482 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005484
5485 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 status = psa_key_derivation_start_hmac(&hmac,
5487 hash_alg,
5488 tls12_prf->secret,
5489 tls12_prf->secret_length);
5490 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005491 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 }
5493 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
5497 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5498 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 }
5501 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5502 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 }
5505 status = psa_mac_sign_finish(&hmac,
5506 tls12_prf->output_block, hash_length,
5507 &hmac_output_length);
5508 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005509 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005511
Janos Follath7742fee2019-06-17 12:58:10 +01005512
5513cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 cleanup_status = psa_mac_abort(&hmac);
5515 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005516 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005520}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005521
Janos Follath844eb0e2019-06-19 12:10:49 +01005522static psa_status_t psa_key_derivation_tls12_prf_read(
5523 psa_tls12_prf_key_derivation_t *tls12_prf,
5524 psa_algorithm_t alg,
5525 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005527{
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5529 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005530 psa_status_t status;
5531 uint8_t offset, length;
5532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005534 case PSA_TLS12_PRF_STATE_LABEL_SET:
5535 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5536 break;
5537 case PSA_TLS12_PRF_STATE_OUTPUT:
5538 break;
5539 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005541 }
5542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005544 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 if (tls12_prf->left_in_block == 0) {
5546 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5547 alg);
5548 if (status != PSA_SUCCESS) {
5549 return status;
5550 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005551
5552 continue;
5553 }
5554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005556 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005558 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005560
5561 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005563 output += length;
5564 output_length -= length;
5565 tls12_prf->left_in_block -= length;
5566 }
5567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005569}
John Durkop07cc04a2020-11-16 22:08:34 -08005570#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5571 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005572
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005573#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5574static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5575 psa_tls12_ecjpake_to_pms_t *ecjpake,
5576 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005578{
5579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005580 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 if (output_length != 32) {
5583 return PSA_ERROR_INVALID_ARGUMENT;
5584 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5587 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5588 &output_size);
5589 if (status != PSA_SUCCESS) {
5590 return status;
5591 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if (output_size != output_length) {
5594 return PSA_ERROR_GENERIC_ERROR;
5595 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005598}
5599#endif
5600
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305601#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305602static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5603 psa_pbkdf2_key_derivation_t *pbkdf2,
5604 psa_algorithm_t prf_alg,
5605 uint8_t prf_output_length,
5606 psa_key_attributes_t *attributes)
5607{
5608 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305609 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305610 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305611 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305612 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305615
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305616 mac_operation.is_sign = 1;
5617 mac_operation.mac_size = prf_output_length;
5618 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305619
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5621 attributes,
5622 pbkdf2->password,
5623 pbkdf2->password_length,
5624 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305625 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305626 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305627 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305628 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5629 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305630 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305632 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305633 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305634 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 }
5636 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5637 &mac_output_length);
5638 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305639 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305640 }
5641
5642 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305643 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305644 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305645 }
5646
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305647 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305648
5649 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305650 /* We are passing prf_output_length as mac_size because the driver
5651 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305652 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653 status = psa_driver_wrapper_mac_compute(attributes,
5654 pbkdf2->password,
5655 pbkdf2->password_length,
5656 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305657 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305658 &mac_output_length);
5659 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305660 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305661 }
5662
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305663 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305664 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305665
5666cleanup:
5667 /* Zeroise buffers to clear sensitive data from memory. */
5668 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5669 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305670}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305671
5672static psa_status_t psa_key_derivation_pbkdf2_read(
5673 psa_pbkdf2_key_derivation_t *pbkdf2,
5674 psa_algorithm_t kdf_alg,
5675 uint8_t *output,
5676 size_t output_length)
5677{
5678 psa_status_t status;
5679 psa_algorithm_t prf_alg;
5680 uint8_t prf_output_length;
5681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5682 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5683 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5684
5685 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5686 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5687 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5688 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305689 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5690 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305691 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305692 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305693 } else {
5694 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305695 }
5696
5697 switch (pbkdf2->state) {
5698 case PSA_PBKDF2_STATE_PASSWORD_SET:
5699 /* Initially we need a new block so bytes_used is equal to block size*/
5700 pbkdf2->bytes_used = prf_output_length;
5701 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5702 break;
5703 case PSA_PBKDF2_STATE_OUTPUT:
5704 break;
5705 default:
5706 return PSA_ERROR_BAD_STATE;
5707 }
5708
5709 while (output_length != 0) {
5710 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5711 if (n > output_length) {
5712 n = (uint8_t) output_length;
5713 }
5714 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5715 output += n;
5716 output_length -= n;
5717 pbkdf2->bytes_used += n;
5718
5719 if (output_length == 0) {
5720 break;
5721 }
5722
5723 /* We need a new block */
5724 pbkdf2->bytes_used = 0;
5725 pbkdf2->block_number++;
5726
5727 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5728 prf_output_length,
5729 &attributes);
5730 if (status != PSA_SUCCESS) {
5731 return status;
5732 }
5733 }
5734
5735 return PSA_SUCCESS;
5736}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305737#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305738
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005739psa_status_t psa_key_derivation_output_bytes(
5740 psa_key_derivation_operation_t *operation,
5741 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005743{
5744 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005750 }
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005756 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005757 goto exit;
5758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005761 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005762 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5763 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005765 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005767 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005769
Przemek Stekiel69c46792022-06-10 12:59:51 +02005770#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5772 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5773 output, output_length);
5774 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005775#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005776#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5777 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5779 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5780 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5781 kdf_alg, output,
5782 output_length);
5783 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005784#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5785 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005786#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005788 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5790 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005791#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305792#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305793 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305794 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5795 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305796 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305797#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005798
Gilles Peskineeab56e42018-07-12 17:12:33 +02005799 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005800 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005802 }
5803
5804exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005806 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005807 * This allows us to differentiate between exhausted operations and
5808 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5809 * operations. */
5810 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005812 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005816}
5817
David Brown7807bf72021-02-09 16:28:23 -07005818#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005819static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005820{
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (data_size >= 8) {
5822 mbedtls_des_key_set_parity(data);
5823 }
5824 if (data_size >= 16) {
5825 mbedtls_des_key_set_parity(data + 8);
5826 }
5827 if (data_size >= 24) {
5828 mbedtls_des_key_set_parity(data + 16);
5829 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005830}
David Brown7807bf72021-02-09 16:28:23 -07005831#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005832
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 * ECC keys on a Weierstrass elliptic curve require the generation
5835 * of a private key which is an integer
5836 * in the range [1, N - 1], where N is the boundary of the private key domain:
5837 * N is the prime p for Diffie-Hellman, or the order of the
5838 * curve’s base point for ECC.
5839 *
5840 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5841 * This function generates the private key using the following process:
5842 *
5843 * 1. Draw a byte string of length ceiling(m/8) bytes.
5844 * 2. If m is not a multiple of 8, set the most significant
5845 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5846 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5847 * 4. If k > N - 2, discard the result and return to step 1.
5848 * 5. Output k + 1 as the private key.
5849 *
5850 * This method allows compliance to NIST standards, specifically the methods titled
5851 * Key-Pair Generation by Testing Candidates in the following publications:
5852 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5853 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5854 * Diffie-Hellman keys.
5855 *
5856 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5857 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5858 *
5859 * Note: Function allocates memory for *data buffer, so given *data should be
5860 * always NULL.
5861 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005862#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5863#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005864static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005865 psa_key_slot_t *slot,
5866 size_t bits,
5867 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005868 uint8_t **data
5869 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005870{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005871 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005872 mbedtls_mpi k;
5873 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005875 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005876 size_t m;
5877 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 mbedtls_mpi_init(&k);
5880 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005881
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005888 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005889 goto cleanup;
5890 }
5891
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005892 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005897 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005899 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005900
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005901 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005902
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005903 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005905
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906 /* Note: This function is always called with *data == NULL and it
5907 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 *data = mbedtls_calloc(1, m_bytes);
5909 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005910 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005911 goto cleanup;
5912 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005915 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005917 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919
5920 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005922 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 * (8 * ceiling(m/8) - m) bits of the first byte in
5924 * the string to zero.
5925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005927 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005928 }
5929
5930 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 * big-endian byte string.
5932 */
5933 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005934
5935 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 * Result of comparison is returned. When it indicates error
5937 * then this function is called again.
5938 */
5939 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005940 }
5941
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5944 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005945cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (ret != 0) {
5947 status = mbedtls_to_psa_error(ret);
5948 }
5949 if (status != PSA_SUCCESS) {
5950 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005951 *data = NULL;
5952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 mbedtls_mpi_free(&k);
5954 mbedtls_mpi_free(&diff_N_2);
5955 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005956}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005957
5958/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5959 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5960 *
5961 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5962 * draw a 32-byte string and process it as specified in
5963 * Elliptic Curves for Security [RFC7748] §5.
5964 *
5965 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5966 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5967 *
5968 * Note: Function allocates memory for *data buffer, so given *data should be
5969 * always NULL.
5970 */
5971
5972static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5973 size_t bits,
5974 psa_key_derivation_operation_t *operation,
5975 uint8_t **data
5976 )
5977{
5978 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005982 case 255:
5983 output_length = 32;
5984 break;
5985 case 448:
5986 output_length = 56;
5987 break;
5988 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005990 break;
5991 }
5992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (*data == NULL) {
5996 return PSA_ERROR_INSUFFICIENT_MEMORY;
5997 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006004
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006006 case 255:
6007 (*data)[0] &= 248;
6008 (*data)[31] &= 127;
6009 (*data)[31] |= 64;
6010 break;
6011 case 448:
6012 (*data)[0] &= 252;
6013 (*data)[55] |= 128;
6014 break;
6015 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006017 break;
6018 }
6019
6020 return status;
6021}
Valerio Setti86587ab2023-06-27 13:09:30 +02006022#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6023static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6024 psa_key_slot_t *slot, size_t bits,
6025 psa_key_derivation_operation_t *operation, uint8_t **data)
6026{
6027 (void) slot;
6028 (void) bits;
6029 (void) operation;
6030 (void) data;
6031 return PSA_ERROR_NOT_SUPPORTED;
6032}
6033
6034static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6035 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6036{
6037 (void) bits;
6038 (void) operation;
6039 (void) data;
6040 return PSA_ERROR_NOT_SUPPORTED;
6041}
6042#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6043#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006044
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006045static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006046 psa_key_slot_t *slot,
6047 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006049{
6050 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306052 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006054 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6057 return PSA_ERROR_INVALID_ARGUMENT;
6058 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006059
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006060#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006061 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6063 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6064 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006065 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6067 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 }
6070 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006071 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6073 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006076 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006077 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006078#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006079 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (key_type_is_raw_bytes(slot->attr.type)) {
6081 if (bits % 8 != 0) {
6082 return PSA_ERROR_INVALID_ARGUMENT;
6083 }
6084 data = mbedtls_calloc(1, bytes);
6085 if (data == NULL) {
6086 return PSA_ERROR_INSUFFICIENT_MEMORY;
6087 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 status = psa_key_derivation_output_bytes(operation, data, bytes);
6090 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 }
David Brown12ca5032021-02-11 11:02:00 -07006093#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6095 psa_des_set_key_parity(data, bytes);
6096 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006097#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 } else {
6099 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006100 }
Ronald Crondd04d422020-11-28 15:14:42 +01006101
Ronald Cronbf33c932020-11-28 18:06:53 +01006102 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006103 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006105 };
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6108 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6109 &storage_size);
6110 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
Archanad8a83dc2021-06-14 10:04:16 +05306113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 status = psa_allocate_buffer_to_slot(slot, storage_size);
6115 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 }
Archanad8a83dc2021-06-14 10:04:16 +05306118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 status = psa_driver_wrapper_import_key(&attributes,
6120 data, bytes,
6121 slot->key.data,
6122 slot->key.bytes,
6123 &slot->key.bytes, &bits);
6124 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006125 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006127
6128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 mbedtls_free(data);
6130 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006131}
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6134 psa_key_derivation_operation_t *operation,
6135 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006136{
6137 psa_status_t status;
6138 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006139 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006140
Ronald Cron81709fc2020-11-14 12:10:32 +01006141 *key = MBEDTLS_SVC_KEY_ID_INIT;
6142
Gilles Peskine0f84d622019-09-12 19:03:13 +02006143 /* Reject any attempt to create a zero-length key so that we don't
6144 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (psa_get_key_bits(attributes) == 0) {
6146 return PSA_ERROR_INVALID_ARGUMENT;
6147 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (operation->alg == PSA_ALG_NONE) {
6150 return PSA_ERROR_BAD_STATE;
6151 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (!operation->can_output_key) {
6154 return PSA_ERROR_NOT_PERMITTED;
6155 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6158 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006161 /* Deriving a key in a secure element is not implemented yet. */
6162 status = PSA_ERROR_NOT_SUPPORTED;
6163 }
6164#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (status == PSA_SUCCESS) {
6166 status = psa_generate_derived_key_internal(slot,
6167 attributes->core.bits,
6168 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 if (status == PSA_SUCCESS) {
6171 status = psa_finish_key_creation(slot, driver, key);
6172 }
6173 if (status != PSA_SUCCESS) {
6174 psa_fail_key_creation(slot, driver);
6175 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006178}
6179
Gilles Peskineeab56e42018-07-12 17:12:33 +02006180
6181
6182/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006183/* Key derivation */
6184/****************************************************************/
6185
Steven Cooreman932ffb72021-02-15 12:14:32 +01006186#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006187static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006188{
6189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6191 return 1;
6192 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006193#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006194#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6196 return 1;
6197 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006198#endif
6199#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6201 return 1;
6202 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006203#endif
6204#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6206 return 1;
6207 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006208#endif
6209#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6211 return 1;
6212 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006213#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006214#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6216 return 1;
6217 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006218#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306219#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6220 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6221 return 1;
6222 }
6223#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306224#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6225 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6226 return 1;
6227 }
6228#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006230}
6231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006233{
6234 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 psa_status_t status = psa_hash_setup(&operation, alg);
6236 psa_hash_abort(&operation);
6237 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006238}
6239
Gilles Peskine969c5d62019-01-16 15:53:06 +01006240static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006241 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006243{
Janos Follath5fe19732019-06-20 15:09:30 +01006244 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6245 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006247
Gilles Peskine969c5d62019-01-16 15:53:06 +01006248 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (!is_kdf_alg_supported(kdf_alg)) {
6250 return PSA_ERROR_NOT_SUPPORTED;
6251 }
John Durkop07cc04a2020-11-16 22:08:34 -08006252
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006253 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306254 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6256 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306257 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6258 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6259 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306260 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306261 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 if (hash_size == 0) {
6263 return PSA_ERROR_NOT_SUPPORTED;
6264 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006265
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006266 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6267 * we might fail later, which is somewhat unfriendly and potentially
6268 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_status_t status = psa_hash_try_support(hash_alg);
6270 if (status != PSA_SUCCESS) {
6271 return status;
6272 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006273 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6276 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6277 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6278 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006279 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006280#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006281 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6283 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006284 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006286#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6287 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 operation->capacity = 255 * hash_size;
6289 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006290}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006293{
6294#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 if (alg == PSA_ALG_ECDH) {
6296 return PSA_SUCCESS;
6297 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006298#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006299#if defined(PSA_WANT_ALG_FFDH)
6300 if (alg == PSA_ALG_FFDH) {
6301 return PSA_SUCCESS;
6302 }
6303#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006304 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006306}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006307
6308static int psa_key_derivation_allows_free_form_secret_input(
6309 psa_algorithm_t kdf_alg)
6310{
6311#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6312 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6313 return 0;
6314 }
6315#endif
6316 (void) kdf_alg;
6317 return 1;
6318}
John Durkop07cc04a2020-11-16 22:08:34 -08006319#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6322 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006323{
6324 psa_status_t status;
6325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 if (operation->alg != 0) {
6327 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006328 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6331 return PSA_ERROR_INVALID_ARGUMENT;
6332 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6333#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6334 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6335 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6336 status = psa_key_agreement_try_support(ka_alg);
6337 if (status != PSA_SUCCESS) {
6338 return status;
6339 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006340 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6341 return PSA_ERROR_INVALID_ARGUMENT;
6342 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6344#else
6345 return PSA_ERROR_NOT_SUPPORTED;
6346#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6347 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6348#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6349 status = psa_key_derivation_setup_kdf(operation, alg);
6350#else
6351 return PSA_ERROR_NOT_SUPPORTED;
6352#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6353 } else {
6354 return PSA_ERROR_INVALID_ARGUMENT;
6355 }
6356
6357 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006358 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 }
6360 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006361}
6362
Przemek Stekiel69c46792022-06-10 12:59:51 +02006363#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006364static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6365 psa_algorithm_t kdf_alg,
6366 psa_key_derivation_step_t step,
6367 const uint8_t *data,
6368 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006369{
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006371 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006373 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006374#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6376 return PSA_ERROR_INVALID_ARGUMENT;
6377 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006378#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (hkdf->state != HKDF_STATE_INIT) {
6380 return PSA_ERROR_BAD_STATE;
6381 } else {
6382 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6383 hash_alg,
6384 data, data_length);
6385 if (status != PSA_SUCCESS) {
6386 return status;
6387 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006388 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006390 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006391 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006392#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006394 /* We shouldn't be in different state as HKDF_EXPAND only allows
6395 * two inputs: SECRET (this case) and INFO which does not modify
6396 * the state. It could happen only if the hkdf
6397 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (hkdf->state != HKDF_STATE_INIT) {
6399 return PSA_ERROR_BAD_STATE;
6400 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006401
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006402 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6404 return PSA_ERROR_INVALID_ARGUMENT;
6405 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 memcpy(hkdf->prk, data, data_length);
6408 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006409#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006410 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006411 /* HKDF: If no salt was provided, use an empty salt.
6412 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006414#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6416 return PSA_ERROR_BAD_STATE;
6417 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006418#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6420 hash_alg,
6421 NULL, 0);
6422 if (status != PSA_SUCCESS) {
6423 return status;
6424 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006425 hkdf->state = HKDF_STATE_STARTED;
6426 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 if (hkdf->state != HKDF_STATE_STARTED) {
6428 return PSA_ERROR_BAD_STATE;
6429 }
6430 status = psa_mac_update(&hkdf->hmac,
6431 data, data_length);
6432 if (status != PSA_SUCCESS) {
6433 return status;
6434 }
6435 status = psa_mac_sign_finish(&hkdf->hmac,
6436 hkdf->prk,
6437 sizeof(hkdf->prk),
6438 &data_length);
6439 if (status != PSA_SUCCESS) {
6440 return status;
6441 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006442 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006443
Gilles Peskine2b522db2019-04-12 15:11:49 +02006444 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006445 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006446#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006448 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006450 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006452#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006453 {
6454 /* Block 0 is empty, and the next block will be
6455 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006457 }
6458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006460 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006461#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6463 return PSA_ERROR_INVALID_ARGUMENT;
6464 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006465#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006466#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6468 hkdf->state == HKDF_STATE_INIT) {
6469 return PSA_ERROR_BAD_STATE;
6470 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006471#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 if (hkdf->state == HKDF_STATE_OUTPUT) {
6473 return PSA_ERROR_BAD_STATE;
6474 }
6475 if (hkdf->info_set) {
6476 return PSA_ERROR_BAD_STATE;
6477 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006478 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 if (data_length != 0) {
6480 hkdf->info = mbedtls_calloc(1, data_length);
6481 if (hkdf->info == NULL) {
6482 return PSA_ERROR_INSUFFICIENT_MEMORY;
6483 }
6484 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006485 }
6486 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006490 }
6491}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006492#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006493
John Durkop07cc04a2020-11-16 22:08:34 -08006494#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6495 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006496static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6497 const uint8_t *data,
6498 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006499{
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6501 return PSA_ERROR_BAD_STATE;
6502 }
Janos Follathf08e2652019-06-13 09:05:41 +01006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (data_length != 0) {
6505 prf->seed = mbedtls_calloc(1, data_length);
6506 if (prf->seed == NULL) {
6507 return PSA_ERROR_INSUFFICIENT_MEMORY;
6508 }
Janos Follathf08e2652019-06-13 09:05:41 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006511 prf->seed_length = data_length;
6512 }
Janos Follathf08e2652019-06-13 09:05:41 +01006513
Gilles Peskine43f958b2020-12-13 14:55:14 +01006514 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006517}
6518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6520 const uint8_t *data,
6521 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006522{
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6524 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6525 return PSA_ERROR_BAD_STATE;
6526 }
Janos Follath81550542019-06-13 14:26:34 +01006527
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 if (data_length != 0) {
6529 prf->secret = mbedtls_calloc(1, data_length);
6530 if (prf->secret == NULL) {
6531 return PSA_ERROR_INSUFFICIENT_MEMORY;
6532 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006533
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006535 prf->secret_length = data_length;
6536 }
Janos Follath81550542019-06-13 14:26:34 +01006537
Gilles Peskine43f958b2020-12-13 14:55:14 +01006538 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006541}
6542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6544 const uint8_t *data,
6545 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006546{
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6548 return PSA_ERROR_BAD_STATE;
6549 }
Janos Follath63028dd2019-06-13 09:15:47 +01006550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 if (data_length != 0) {
6552 prf->label = mbedtls_calloc(1, data_length);
6553 if (prf->label == NULL) {
6554 return PSA_ERROR_INSUFFICIENT_MEMORY;
6555 }
Janos Follath63028dd2019-06-13 09:15:47 +01006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006558 prf->label_length = data_length;
6559 }
Janos Follath63028dd2019-06-13 09:15:47 +01006560
Gilles Peskine43f958b2020-12-13 14:55:14 +01006561 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006564}
6565
Gilles Peskine449bd832023-01-11 14:50:10 +01006566static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6567 psa_key_derivation_step_t step,
6568 const uint8_t *data,
6569 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006570{
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006572 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006574 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006576 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006578 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006580 }
6581}
John Durkop07cc04a2020-11-16 22:08:34 -08006582#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6583 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6584
6585#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6586static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6587 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006588 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006590{
6591 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6593 4 + data_length + prf->other_secret_length :
6594 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6597 return PSA_ERROR_INVALID_ARGUMENT;
6598 }
John Durkop07cc04a2020-11-16 22:08:34 -08006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 uint8_t *pms = mbedtls_calloc(1, pms_len);
6601 if (pms == NULL) {
6602 return PSA_ERROR_INSUFFICIENT_MEMORY;
6603 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006604 uint8_t *cur = pms;
6605
6606 /* pure-PSK:
6607 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006608 *
6609 * The premaster secret is formed as follows: if the PSK is N octets
6610 * long, concatenate a uint16 with the value N, N zero octets, a second
6611 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006612 *
6613 * mixed-PSK:
6614 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6615 * follows: concatenate a uint16 with the length of the other secret,
6616 * the other secret itself, uint16 with the length of PSK, and the
6617 * PSK itself.
6618 * For details please check:
6619 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6620 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6621 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006622 */
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6625 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6626 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6627 if (prf->other_secret_length != 0) {
6628 memcpy(cur, prf->other_secret, prf->other_secret_length);
6629 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006630 cur += prf->other_secret_length;
6631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 } else {
6633 *cur++ = MBEDTLS_BYTE_1(data_length);
6634 *cur++ = MBEDTLS_BYTE_0(data_length);
6635 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006636 cur += data_length;
6637 }
6638
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 *cur++ = MBEDTLS_BYTE_1(data_length);
6640 *cur++ = MBEDTLS_BYTE_0(data_length);
6641 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006642 cur += data_length;
6643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006645
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006646 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006648}
Janos Follath6660f0e2019-06-17 08:44:03 +01006649
Przemek Stekiele47201b2022-04-19 13:53:28 +02006650static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6651 psa_tls12_prf_key_derivation_t *prf,
6652 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006654{
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6656 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006658
6659 if (data_length != 0) {
6660 prf->other_secret = mbedtls_calloc(1, data_length);
6661 if (prf->other_secret == NULL) {
6662 return PSA_ERROR_INSUFFICIENT_MEMORY;
6663 }
6664
6665 memcpy(prf->other_secret, data, data_length);
6666 prf->other_secret_length = data_length;
6667 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006668 prf->other_secret_length = 0;
6669 }
6670
6671 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006674}
6675
Janos Follath6660f0e2019-06-17 08:44:03 +01006676static psa_status_t psa_tls12_prf_psk_to_ms_input(
6677 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006678 psa_key_derivation_step_t step,
6679 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006681{
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006683 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 return psa_tls12_prf_psk_to_ms_set_key(prf,
6685 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006686 break;
6687 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6689 data,
6690 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006691 break;
6692 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006694 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006695
Przemek Stekiele47201b2022-04-19 13:53:28 +02006696 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006697}
John Durkop07cc04a2020-11-16 22:08:34 -08006698#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006699
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6701static psa_status_t psa_tls12_ecjpake_to_pms_input(
6702 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006703 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006704 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006706{
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6708 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6709 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006710 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006711
6712 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (data[0] != 0x04) {
6714 return PSA_ERROR_INVALID_ARGUMENT;
6715 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006716
6717 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006721}
6722#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306723
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306724#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306725static psa_status_t psa_pbkdf2_set_input_cost(
6726 psa_pbkdf2_key_derivation_t *pbkdf2,
6727 psa_key_derivation_step_t step,
6728 uint64_t data)
6729{
6730 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6731 return PSA_ERROR_INVALID_ARGUMENT;
6732 }
6733
6734 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6735 return PSA_ERROR_BAD_STATE;
6736 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306737
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306738 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306739 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306740 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306741
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306742 if (data == 0) {
6743 return PSA_ERROR_INVALID_ARGUMENT;
6744 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306745
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306746 pbkdf2->input_cost = data;
6747 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6748
6749 return PSA_SUCCESS;
6750}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306751
6752static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6753 const uint8_t *data,
6754 size_t data_length)
6755{
Gilles Peskineead17662023-08-20 22:02:51 +02006756 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6757 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6758 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6759 /* Appending to existing salt. No state change. */
6760 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306761 return PSA_ERROR_BAD_STATE;
6762 }
6763
Gilles Peskineca57d782023-07-20 19:08:06 +02006764 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006765 /* Appending an empty string, nothing to do. */
6766 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306767 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306768
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306769 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6770 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306771 return PSA_ERROR_INSUFFICIENT_MEMORY;
6772 }
6773
Gilles Peskineead17662023-08-20 22:02:51 +02006774 if (pbkdf2->salt_length != 0) {
6775 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6776 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306777 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306778 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306779 mbedtls_free(pbkdf2->salt);
6780 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306781 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306782 return PSA_SUCCESS;
6783}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306784
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306785#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306787 const uint8_t *input,
6788 size_t input_len,
6789 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306790 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791{
6792 psa_status_t status = PSA_SUCCESS;
6793 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6794 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306795 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306796 } else {
6797 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306798 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306799 }
6800 return status;
6801}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306802#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306803
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306804#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306805static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6806 size_t input_len,
6807 uint8_t *output,
6808 size_t *output_len)
6809{
6810 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306811 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306813 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306814 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6815 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6816 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306817 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306818 * mac_size as the driver function sets mac_output_length = mac_size
6819 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306820 status = psa_driver_wrapper_mac_compute(&attributes,
6821 zeros, sizeof(zeros),
6822 PSA_ALG_CMAC, input, input_len,
6823 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306824 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6825 128U,
6826 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306827 output_len);
6828 } else {
6829 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306830 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306831 }
6832 return status;
6833}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306834#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306835
6836static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306837 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306838 const uint8_t *data,
6839 size_t data_length)
6840{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306841 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306842 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6843 return PSA_ERROR_BAD_STATE;
6844 }
6845
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306846#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306847 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6848 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6849 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6850 pbkdf2->password,
6851 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306852 } else
6853#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6854#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6855 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306856 status = psa_pbkdf2_cmac_set_password(data, data_length,
6857 pbkdf2->password,
6858 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306859 } else
6860#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6861 {
6862 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306863 }
6864
6865 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6866
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306867 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306868}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306869
6870static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306871 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306872 psa_key_derivation_step_t step,
6873 const uint8_t *data,
6874 size_t data_length)
6875{
6876 switch (step) {
6877 case PSA_KEY_DERIVATION_INPUT_SALT:
6878 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6879 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306880 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306881 default:
6882 return PSA_ERROR_INVALID_ARGUMENT;
6883 }
6884}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306885#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306886
Gilles Peskineb8965192019-09-24 16:21:10 +02006887/** Check whether the given key type is acceptable for the given
6888 * input step of a key derivation.
6889 *
6890 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6891 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6892 * Both secret and non-secret inputs can alternatively have the type
6893 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6894 * that the input was passed as a buffer rather than via a key object.
6895 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006896static int psa_key_derivation_check_input_type(
6897 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899{
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006901 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (key_type == PSA_KEY_TYPE_DERIVE) {
6903 return PSA_SUCCESS;
6904 }
6905 if (key_type == PSA_KEY_TYPE_NONE) {
6906 return PSA_SUCCESS;
6907 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006908 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006909 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 if (key_type == PSA_KEY_TYPE_DERIVE) {
6911 return PSA_SUCCESS;
6912 }
6913 if (key_type == PSA_KEY_TYPE_NONE) {
6914 return PSA_SUCCESS;
6915 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006916 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006917 case PSA_KEY_DERIVATION_INPUT_LABEL:
6918 case PSA_KEY_DERIVATION_INPUT_SALT:
6919 case PSA_KEY_DERIVATION_INPUT_INFO:
6920 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6922 return PSA_SUCCESS;
6923 }
6924 if (key_type == PSA_KEY_TYPE_NONE) {
6925 return PSA_SUCCESS;
6926 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006927 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306928 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6929 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6930 return PSA_SUCCESS;
6931 }
6932 if (key_type == PSA_KEY_TYPE_DERIVE) {
6933 return PSA_SUCCESS;
6934 }
6935 if (key_type == PSA_KEY_TYPE_NONE) {
6936 return PSA_SUCCESS;
6937 }
6938 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006939 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006941}
6942
Janos Follathb80a94e2019-06-12 15:54:46 +01006943static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006944 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006945 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006946 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006947 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006949{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006950 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 status = psa_key_derivation_check_input_type(step, key_type);
6954 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006955 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006957
Przemek Stekiel69c46792022-06-10 12:59:51 +02006958#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6960 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6961 step, data, data_length);
6962 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006963#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006964#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6966 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6967 step, data, data_length);
6968 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006969#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6970#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6972 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6973 step, data, data_length);
6974 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006975#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006978 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006979 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6980 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006981#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306982#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306983 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306984 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6985 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306986 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306987#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006988 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006989 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006990 (void) data;
6991 (void) data_length;
6992 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006994 }
6995
Gilles Peskine224b0d62019-09-23 18:13:17 +02006996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (status != PSA_SUCCESS) {
6998 psa_key_derivation_abort(operation);
6999 }
7000 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007001}
7002
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307003static psa_status_t psa_key_derivation_input_integer_internal(
7004 psa_key_derivation_operation_t *operation,
7005 psa_key_derivation_step_t step,
7006 uint64_t value)
7007{
7008 psa_status_t status;
7009 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7010
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307011#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307012 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307013 status = psa_pbkdf2_set_input_cost(
7014 &operation->ctx.pbkdf2, step, value);
7015 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307016#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307017 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307018 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307019 (void) value;
7020 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307021 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307022 }
7023
7024 if (status != PSA_SUCCESS) {
7025 psa_key_derivation_abort(operation);
7026 }
7027 return status;
7028}
7029
Janos Follath51f4a0f2019-06-14 11:35:55 +01007030psa_status_t psa_key_derivation_input_bytes(
7031 psa_key_derivation_operation_t *operation,
7032 psa_key_derivation_step_t step,
7033 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007035{
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 return psa_key_derivation_input_internal(operation, step,
7037 PSA_KEY_TYPE_NONE,
7038 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007039}
7040
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307041psa_status_t psa_key_derivation_input_integer(
7042 psa_key_derivation_operation_t *operation,
7043 psa_key_derivation_step_t step,
7044 uint64_t value)
7045{
7046 return psa_key_derivation_input_integer_internal(operation, step, value);
7047}
7048
Janos Follath51f4a0f2019-06-14 11:35:55 +01007049psa_status_t psa_key_derivation_input_key(
7050 psa_key_derivation_operation_t *operation,
7051 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007053{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007056 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007057
Ronald Cron5c522922020-11-14 16:35:34 +01007058 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7060 if (status != PSA_SUCCESS) {
7061 psa_key_derivation_abort(operation);
7062 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007063 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007064
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307065 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7066 * permission to output to a key object. */
7067 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7068 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007069 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 status = psa_key_derivation_input_internal(operation,
7073 step, slot->attr.type,
7074 slot->key.data,
7075 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007080}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007081
7082
7083
7084/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007085/* Key agreement */
7086/****************************************************************/
7087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7089 const uint8_t *key_buffer,
7090 size_t key_buffer_size,
7091 psa_algorithm_t alg,
7092 const uint8_t *peer_key,
7093 size_t peer_key_length,
7094 uint8_t *shared_secret,
7095 size_t shared_secret_size,
7096 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007097{
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007099#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007100 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7102 key_buffer_size, alg,
7103 peer_key, peer_key_length,
7104 shared_secret,
7105 shared_secret_size,
7106 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007107#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007108
7109#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7110 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007111 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007112 peer_key,
7113 peer_key_length,
7114 key_buffer,
7115 key_buffer_size,
7116 shared_secret,
7117 shared_secret_size,
7118 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007119#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7120
Gilles Peskine01d718c2018-09-18 12:01:02 +02007121 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007122 (void) attributes;
7123 (void) key_buffer;
7124 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007125 (void) peer_key;
7126 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007127 (void) shared_secret;
7128 (void) shared_secret_size;
7129 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007131 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007132}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007133
Aditya Deshpande17845b82022-10-13 17:21:01 +01007134/** Internal function for raw key agreement
7135 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007136 * to the driver's implementation if a driver is present.
7137 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138 * (psa_key_agreement_raw_builtin).
7139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007140static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7141 psa_key_slot_t *private_key,
7142 const uint8_t *peer_key,
7143 size_t peer_key_length,
7144 uint8_t *shared_secret,
7145 size_t shared_secret_size,
7146 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007147{
Gilles Peskine449bd832023-01-11 14:50:10 +01007148 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7149 return PSA_ERROR_NOT_SUPPORTED;
7150 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007151
7152 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007154 };
7155
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 return psa_driver_wrapper_key_agreement(&attributes,
7157 private_key->key.data,
7158 private_key->key.bytes, alg,
7159 peer_key, peer_key_length,
7160 shared_secret,
7161 shared_secret_size,
7162 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007163}
7164
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007165/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007166 * to potentially free embedded data structures and wipe confidential data.
7167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7169 psa_key_derivation_step_t step,
7170 psa_key_slot_t *private_key,
7171 const uint8_t *peer_key,
7172 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173{
7174 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007175 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007176 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007178
7179 /* Step 1: run the secret agreement algorithm to generate the shared
7180 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 status = psa_key_agreement_raw_internal(ka_alg,
7182 private_key,
7183 peer_key, peer_key_length,
7184 shared_secret,
7185 sizeof(shared_secret),
7186 &shared_secret_length);
7187 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007190
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007191 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007192 * the shared secret. A shared secret is permitted wherever a key
7193 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 status = psa_key_derivation_input_internal(operation, step,
7195 PSA_KEY_TYPE_DERIVE,
7196 shared_secret,
7197 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007199 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7200 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007201}
7202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7204 psa_key_derivation_step_t step,
7205 mbedtls_svc_key_id_t private_key,
7206 const uint8_t *peer_key,
7207 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007208{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007210 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007211 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7214 return PSA_ERROR_INVALID_ARGUMENT;
7215 }
Ronald Cron5c522922020-11-14 16:35:34 +01007216 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7218 if (status != PSA_SUCCESS) {
7219 return status;
7220 }
7221 status = psa_key_agreement_internal(operation, step,
7222 slot,
7223 peer_key, peer_key_length);
7224 if (status != PSA_SUCCESS) {
7225 psa_key_derivation_abort(operation);
7226 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007227 /* If a private key has been added as SECRET, we allow the derived
7228 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007230 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007232 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007237}
7238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7240 mbedtls_svc_key_id_t private_key,
7241 const uint8_t *peer_key,
7242 size_t peer_key_length,
7243 uint8_t *output,
7244 size_t output_size,
7245 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007246{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007248 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007249 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007250 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007251
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007253 status = PSA_ERROR_INVALID_ARGUMENT;
7254 goto exit;
7255 }
Ronald Cron5c522922020-11-14 16:35:34 +01007256 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7258 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007259 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007261
Gilles Peskine3e561302022-04-14 00:17:15 +02007262 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7263 * for the output size. The PSA specification only guarantees that this
7264 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7265 * but it might be nice to allow smaller buffers if the output fits.
7266 * At the time of writing this comment, with only ECDH implemented,
7267 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7268 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7269 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007270 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7272 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007273 status = PSA_ERROR_BUFFER_TOO_SMALL;
7274 goto exit;
7275 }
7276
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 status = psa_key_agreement_raw_internal(alg, slot,
7278 peer_key, peer_key_length,
7279 output, output_size,
7280 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007281
7282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007284 /* If an error happens and is not handled properly, the output
7285 * may be used as a key to protect sensitive data. Arrange for such
7286 * a key to be random, which is likely to result in decryption or
7287 * verification errors. This is better than filling the buffer with
7288 * some constant data such as zeros, which would result in the data
7289 * being protected with a reproducible, easily knowable key.
7290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007292 *output_length = output_size;
7293 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007294
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007298}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007299
7300
Gilles Peskine30524eb2020-11-13 17:02:26 +01007301
Gilles Peskine86a440b2018-11-12 18:39:40 +01007302/****************************************************************/
7303/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007304/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007305
Gilles Peskine672a7712023-04-28 21:00:28 +02007306#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7307#include "entropy_poll.h"
7308#endif
7309
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310/** Initialize the PSA random generator.
7311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007312static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007313{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007314#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007316#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7317
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318 /* Set default configuration if
7319 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 }
7323 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007324 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007326
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7329 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7330 /* The PSA entropy injection feature depends on using NV seed as an entropy
7331 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 mbedtls_entropy_add_source(&rng->entropy,
7333 mbedtls_nv_seed_poll, NULL,
7334 MBEDTLS_ENTROPY_BLOCK_SIZE,
7335 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007336#endif
7337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007339#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007340}
7341
7342/** Deinitialize the PSA random generator.
7343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007344static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007345{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007346#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7350 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007352}
7353
7354/** Seed the PSA random generator.
7355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007356static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007357{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007358#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7359 /* Do nothing: the external RNG seeds itself. */
7360 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007362#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007363 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7365 drbg_seed, sizeof(drbg_seed) - 1);
7366 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007367#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007368}
7369
Gilles Peskine449bd832023-01-11 14:50:10 +01007370psa_status_t psa_generate_random(uint8_t *output,
7371 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007372{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007373 GUARD_MODULE_INITIALIZED;
7374
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007375#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7376
7377 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7379 output, output_size,
7380 &output_length);
7381 if (status != PSA_SUCCESS) {
7382 return status;
7383 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007384 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007385 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 if (output_length != output_size) {
7387 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7388 }
7389 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007390
7391#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7392
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007394 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7396 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7397 output_size);
7398 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7399 output, request_size);
7400 if (ret != 0) {
7401 return mbedtls_to_psa_error(ret);
7402 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007403 output_size -= request_size;
7404 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007407#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007408}
7409
Gilles Peskine8814fc42020-12-14 15:33:44 +01007410/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007411 *
7412 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7413 * `psa_generate_random(...)`. The state parameter is ignored since the
7414 * PSA API doesn't support passing an explicit state.
7415 *
7416 * In the non-external case, psa_generate_random() calls an
7417 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7418 * and semantics as mbedtls_psa_get_random(). As an optimization,
7419 * instead of doing this back-and-forth between the PSA API and the
7420 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7421 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007423#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7424int mbedtls_psa_get_random(void *p_rng,
7425 unsigned char *output,
7426 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007427{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007428 /* This function takes a pointer to the RNG state because that's what
7429 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7430 * its own state internally and doesn't let the caller access that state.
7431 * So we just ignore the state parameter, and in practice we'll pass
7432 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007433 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 psa_status_t status = psa_generate_random(output, output_size);
7435 if (status == PSA_SUCCESS) {
7436 return 0;
7437 } else {
7438 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7439 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007440}
7441#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7442
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007443#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007444psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7445 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007446{
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (global_data.initialized) {
7448 return PSA_ERROR_NOT_PERMITTED;
7449 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7452 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7453 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7454 return PSA_ERROR_INVALID_ARGUMENT;
7455 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007458}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007459#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007460
Ronald Cron3772afe2021-02-08 16:10:05 +01007461/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007463 * \param type The key type
7464 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 *
7466 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007467 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007468 * \retval #PSA_ERROR_INVALID_ARGUMENT
7469 * The size in bits of the key is not valid.
7470 * \retval #PSA_ERROR_NOT_SUPPORTED
7471 * The type and/or the size in bits of the key or the combination of
7472 * the two is not supported.
7473 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007474static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007476{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if (key_type_is_raw_bytes(type)) {
7480 status = psa_validate_unstructured_key_bit_size(type, bits);
7481 if (status != PSA_SUCCESS) {
7482 return status;
7483 }
7484 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007485#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7487 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7488 return PSA_ERROR_NOT_SUPPORTED;
7489 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007490 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007491 return PSA_ERROR_NOT_SUPPORTED;
7492 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007493
Ronald Cron01b2aba2020-10-05 09:42:02 +02007494 /* Accept only byte-aligned keys, for the same reasons as
7495 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (bits % 8 != 0) {
7497 return PSA_ERROR_NOT_SUPPORTED;
7498 }
7499 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007500#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007501
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007502#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007504 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return PSA_SUCCESS;
7506 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007507#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007508
Valerio Settia55f0422023-07-10 15:34:41 +02007509#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007510 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007511 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007512 return PSA_ERROR_NOT_SUPPORTED;
7513 }
7514 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007515#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007516 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518 }
7519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007521}
7522
Ronald Cron55ed0592020-10-05 10:30:40 +02007523psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007524 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007526{
7527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007528 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if ((attributes->domain_parameters == NULL) &&
7531 (attributes->domain_parameters_size != 0)) {
7532 return PSA_ERROR_INVALID_ARGUMENT;
7533 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007534
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (key_type_is_raw_bytes(type)) {
7536 status = psa_generate_random(key_buffer, key_buffer_size);
7537 if (status != PSA_SUCCESS) {
7538 return status;
7539 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007540
David Brown12ca5032021-02-11 11:02:00 -07007541#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (type == PSA_KEY_TYPE_DES) {
7543 psa_des_set_key_parity(key_buffer, key_buffer_size);
7544 }
David Brown8107e312021-02-12 08:08:46 -07007545#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007547
Valerio Setti76df8c12023-07-11 14:11:28 +02007548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7550 return mbedtls_psa_rsa_generate_key(attributes,
7551 key_buffer,
7552 key_buffer_size,
7553 key_buffer_length);
7554 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007555#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007556
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7559 return mbedtls_psa_ecp_generate_key(attributes,
7560 key_buffer,
7561 key_buffer_size,
7562 key_buffer_length);
7563 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007564#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007565
Valerio Settia55f0422023-07-10 15:34:41 +02007566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007567 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7568 return mbedtls_psa_ffdh_generate_key(attributes,
7569 key_buffer,
7570 key_buffer_size,
7571 key_buffer_length);
7572 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007574 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 (void) key_buffer_length;
7576 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007577 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007580}
Darryl Green0c6575a2018-11-07 16:05:30 +00007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7583 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007584{
7585 psa_status_t status;
7586 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007587 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007588 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007589
Ronald Cron81709fc2020-11-14 12:10:32 +01007590 *key = MBEDTLS_SVC_KEY_ID_INIT;
7591
Gilles Peskine0f84d622019-09-12 19:03:13 +02007592 /* Reject any attempt to create a zero-length key so that we don't
7593 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 if (psa_get_key_bits(attributes) == 0) {
7595 return PSA_ERROR_INVALID_ARGUMENT;
7596 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007597
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007598 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7600 return PSA_ERROR_INVALID_ARGUMENT;
7601 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7604 &slot, &driver);
7605 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 }
Gilles Peskine11792082019-08-06 18:36:36 +02007608
Ronald Cron977c2472020-10-13 08:32:21 +02007609 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307610 * storage ( thus not in the case of generating a key in a secure element
7611 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7612 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (slot->key.data == NULL) {
7614 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7615 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007616 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 attributes->core.type, attributes->core.bits);
7618 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007621
7622 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 attributes->core.type,
7624 attributes->core.bits);
7625 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007626 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 attributes, &key_buffer_size);
7628 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 }
Ronald Cron977c2472020-10-13 08:32:21 +02007631 }
Ronald Cron977c2472020-10-13 08:32:21 +02007632
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7634 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 }
Ronald Cron977c2472020-10-13 08:32:21 +02007637 }
7638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 status = psa_driver_wrapper_generate_key(attributes,
7640 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 if (status != PSA_SUCCESS) {
7643 psa_remove_key_data_from_memory(slot);
7644 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007645
Gilles Peskine11792082019-08-06 18:36:36 +02007646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 if (status == PSA_SUCCESS) {
7648 status = psa_finish_key_creation(slot, driver, key);
7649 }
7650 if (status != PSA_SUCCESS) {
7651 psa_fail_key_creation(slot, driver);
7652 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007655}
7656
Gilles Peskinee59236f2018-01-27 23:32:46 +01007657/****************************************************************/
7658/* Module setup */
7659/****************************************************************/
7660
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007661#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007662psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 void (* entropy_init)(mbedtls_entropy_context *ctx),
7664 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007665{
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7667 return PSA_ERROR_BAD_STATE;
7668 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007669 global_data.rng.entropy_init = entropy_init;
7670 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007672}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007673#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007674
Gilles Peskine449bd832023-01-11 14:50:10 +01007675void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007676{
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 psa_wipe_all_key_slots();
7678 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7679 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007680 }
7681 /* Wipe all remaining data, including configuration.
7682 * In particular, this sets all state indicator to the value
7683 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007685
7686 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007688}
7689
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7691/** Recover a transaction that was interrupted by a power failure.
7692 *
7693 * This function is called during initialization, before psa_crypto_init()
7694 * returns. If this function returns a failure status, the initialization
7695 * fails.
7696 */
7697static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007699{
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007701 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7702 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 /* TODO - fall through to the failure case until this
7704 * is implemented.
7705 * https://github.com/ARMmbed/mbed-crypto/issues/218
7706 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007707 default:
7708 /* We found an unsupported transaction in the storage.
7709 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007711 }
7712}
7713#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7714
Gilles Peskine449bd832023-01-11 14:50:10 +01007715psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007716{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007717 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007718
Gilles Peskinec6b69072018-11-20 21:42:52 +01007719 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (global_data.initialized != 0) {
7721 return PSA_SUCCESS;
7722 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007723
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007724 /* Init drivers */
7725 status = psa_driver_wrapper_init();
7726 if (status != PSA_SUCCESS) {
7727 goto exit;
7728 }
7729 global_data.drivers_initialized = 1;
7730
Gilles Peskine30524eb2020-11-13 17:02:26 +01007731 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007733 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 status = mbedtls_psa_random_seed(&global_data.rng);
7735 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007736 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007738 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 status = psa_initialize_key_slots();
7741 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007742 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007744
Gilles Peskine4b734222019-07-24 15:56:31 +02007745#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 status = psa_crypto_load_transaction();
7747 if (status == PSA_SUCCESS) {
7748 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7749 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 }
7752 status = psa_crypto_stop_transaction();
7753 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007754 /* There's no transaction to complete. It's all good. */
7755 status = PSA_SUCCESS;
7756 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007757#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007758
Gilles Peskinec6b69072018-11-20 21:42:52 +01007759 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007760 global_data.initialized = 1;
7761
7762exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 if (status != PSA_SUCCESS) {
7764 mbedtls_psa_crypto_free();
7765 }
7766 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007767}
7768
Yanray Wangffc3c482023-07-11 12:01:04 +08007769#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007770psa_status_t psa_crypto_driver_pake_get_password_len(
7771 const psa_crypto_driver_pake_inputs_t *inputs,
7772 size_t *password_len)
7773{
7774 if (inputs->password_len == 0) {
7775 return PSA_ERROR_BAD_STATE;
7776 }
7777
7778 *password_len = inputs->password_len;
7779
7780 return PSA_SUCCESS;
7781}
7782
7783psa_status_t psa_crypto_driver_pake_get_password(
7784 const psa_crypto_driver_pake_inputs_t *inputs,
7785 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7786{
7787 if (inputs->password_len == 0) {
7788 return PSA_ERROR_BAD_STATE;
7789 }
7790
7791 if (buffer_size < inputs->password_len) {
7792 return PSA_ERROR_BUFFER_TOO_SMALL;
7793 }
7794
7795 memcpy(buffer, inputs->password, inputs->password_len);
7796 *buffer_length = inputs->password_len;
7797
7798 return PSA_SUCCESS;
7799}
7800
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007801psa_status_t psa_crypto_driver_pake_get_user_len(
7802 const psa_crypto_driver_pake_inputs_t *inputs,
7803 size_t *user_len)
7804{
7805 if (inputs->user_len == 0) {
7806 return PSA_ERROR_BAD_STATE;
7807 }
7808
7809 *user_len = inputs->user_len;
7810
7811 return PSA_SUCCESS;
7812}
7813
7814psa_status_t psa_crypto_driver_pake_get_user(
7815 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007816 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007817{
7818 if (inputs->user_len == 0) {
7819 return PSA_ERROR_BAD_STATE;
7820 }
7821
Przemek Stekielc0e62502023-03-14 11:49:36 +01007822 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007823 return PSA_ERROR_BUFFER_TOO_SMALL;
7824 }
7825
Przemek Stekielc0e62502023-03-14 11:49:36 +01007826 memcpy(user_id, inputs->user, inputs->user_len);
7827 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007828
7829 return PSA_SUCCESS;
7830}
7831
7832psa_status_t psa_crypto_driver_pake_get_peer_len(
7833 const psa_crypto_driver_pake_inputs_t *inputs,
7834 size_t *peer_len)
7835{
7836 if (inputs->peer_len == 0) {
7837 return PSA_ERROR_BAD_STATE;
7838 }
7839
7840 *peer_len = inputs->peer_len;
7841
7842 return PSA_SUCCESS;
7843}
7844
7845psa_status_t psa_crypto_driver_pake_get_peer(
7846 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007847 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007848{
7849 if (inputs->peer_len == 0) {
7850 return PSA_ERROR_BAD_STATE;
7851 }
7852
Przemek Stekielc0e62502023-03-14 11:49:36 +01007853 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007854 return PSA_ERROR_BUFFER_TOO_SMALL;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 memcpy(peer_id, inputs->peer, inputs->peer_len);
7858 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007859
7860 return PSA_SUCCESS;
7861}
7862
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007863psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7864 const psa_crypto_driver_pake_inputs_t *inputs,
7865 psa_pake_cipher_suite_t *cipher_suite)
7866{
7867 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7868 return PSA_ERROR_BAD_STATE;
7869 }
7870
7871 *cipher_suite = inputs->cipher_suite;
7872
7873 return PSA_SUCCESS;
7874}
7875
Neil Armstronga7d08c32022-06-01 18:21:20 +02007876psa_status_t psa_pake_setup(
7877 psa_pake_operation_t *operation,
7878 const psa_pake_cipher_suite_t *cipher_suite)
7879{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007881
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007882 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007883 status = PSA_ERROR_BAD_STATE;
7884 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 }
7886
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007887 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007889 status = PSA_ERROR_INVALID_ARGUMENT;
7890 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007891 }
7892
Przemek Stekiel51eac532022-12-07 11:04:51 +01007893 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7894
Przemek Stekiele12ed362022-12-21 12:54:46 +01007895 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007896 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7897 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 operation->data.inputs.cipher_suite = *cipher_suite;
7899
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007900#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007901 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007902 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007903 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007904
David Horstmann16f01512023-06-14 17:21:07 +01007905 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007906 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007907 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007908#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007909 {
7910 status = PSA_ERROR_NOT_SUPPORTED;
7911 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007912 }
7913
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007914 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7915
Przemek Stekiel51eac532022-12-07 11:04:51 +01007916 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007917exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007918 psa_pake_abort(operation);
7919 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007920}
7921
7922psa_status_t psa_pake_set_password_key(
7923 psa_pake_operation_t *operation,
7924 mbedtls_svc_key_id_t password)
7925{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7928 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007929 psa_key_attributes_t attributes;
7930 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007931
Przemek Stekiel51eac532022-12-07 11:04:51 +01007932 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007933 status = PSA_ERROR_BAD_STATE;
7934 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007935 }
7936
Przemek Stekiel6c764412022-11-22 14:05:12 +01007937 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7938 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007939 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007940 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007941 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007942 }
7943
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007944 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007945 .core = slot->attr
7946 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007947
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007948 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007949
Przemek Stekiel51eac532022-12-07 11:04:51 +01007950 if (type != PSA_KEY_TYPE_PASSWORD &&
7951 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7952 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007953 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 }
7955
Przemek Stekiel51eac532022-12-07 11:04:51 +01007956 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7957 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007958 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960 }
7961
7962 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7963 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007964 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007965exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007966 if (status != PSA_SUCCESS) {
7967 psa_pake_abort(operation);
7968 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007971}
7972
7973psa_status_t psa_pake_set_user(
7974 psa_pake_operation_t *operation,
7975 const uint8_t *user_id,
7976 size_t user_id_len)
7977{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007979
7980 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007981 status = PSA_ERROR_BAD_STATE;
7982 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007983 }
7984
Przemek Stekiel51eac532022-12-07 11:04:51 +01007985 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986 status = PSA_ERROR_INVALID_ARGUMENT;
7987 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007988 }
7989
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007990 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007991 status = PSA_ERROR_BAD_STATE;
7992 goto exit;
7993 }
7994
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007995 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7996 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007997 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7998 goto exit;
7999 }
8000
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008001 memcpy(operation->data.inputs.user, user_id, user_id_len);
8002 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008003
8004 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008005exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008006 psa_pake_abort(operation);
8007 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008008}
8009
8010psa_status_t psa_pake_set_peer(
8011 psa_pake_operation_t *operation,
8012 const uint8_t *peer_id,
8013 size_t peer_id_len)
8014{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008016
8017 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008018 status = PSA_ERROR_BAD_STATE;
8019 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008020 }
8021
Przemek Stekiel51eac532022-12-07 11:04:51 +01008022 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008023 status = PSA_ERROR_INVALID_ARGUMENT;
8024 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008025 }
8026
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008027 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008028 status = PSA_ERROR_BAD_STATE;
8029 goto exit;
8030 }
8031
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008032 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8033 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008034 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8035 goto exit;
8036 }
8037
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008038 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8039 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008040
8041 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008042exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008043 psa_pake_abort(operation);
8044 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008045}
8046
8047psa_status_t psa_pake_set_role(
8048 psa_pake_operation_t *operation,
8049 psa_pake_role_t role)
8050{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008054 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008056 }
8057
Przemek Stekiel09104b82023-03-06 14:11:51 +01008058 switch (operation->alg) {
8059#if defined(PSA_WANT_ALG_JPAKE)
8060 case PSA_ALG_JPAKE:
8061 if (role == PSA_PAKE_ROLE_NONE) {
8062 return PSA_SUCCESS;
8063 }
8064 status = PSA_ERROR_INVALID_ARGUMENT;
8065 break;
8066#endif
8067 default:
8068 (void) role;
8069 status = PSA_ERROR_NOT_SUPPORTED;
8070 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008071 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008072exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008073 psa_pake_abort(operation);
8074 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008075}
8076
David Horstmanne7f21e62023-05-12 18:17:21 +01008077/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008078#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008079static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008080 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008081{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008083 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008085
David Horstmann024e5c52023-06-14 15:48:21 +01008086 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 is_x1 = (stage->outputs < 1);
8088 } else {
8089 is_x1 = (stage->inputs < 1);
8090 }
8091
David Horstmann74a3d8c2023-06-14 18:28:19 +01008092 key_share_step = is_x1 ?
8093 PSA_JPAKE_X1_STEP_KEY_SHARE :
8094 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008095 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008096 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8097 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8098 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8099 } else {
8100 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008101 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008102 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008103}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008104#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008105
Przemek Stekiel2797d372022-12-22 11:19:22 +01008106static psa_status_t psa_pake_complete_inputs(
8107 psa_pake_operation_t *operation)
8108{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008110 /* Create copy of the inputs on stack as inputs share memory
8111 with the driver context which will be setup by the driver. */
8112 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113
Przemek Stekielfde11282023-03-13 16:06:09 +01008114 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008115 return PSA_ERROR_BAD_STATE;
8116 }
8117
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008118 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008119 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8120 return PSA_ERROR_BAD_STATE;
8121 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008122 }
8123
Przemek Stekiel18620a32023-01-17 16:34:52 +01008124 /* Clear driver context */
8125 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8126
8127 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128
8129 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008130 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008131
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008132 /* User and peer are translated to role. */
8133 mbedtls_free(inputs.user);
8134 mbedtls_free(inputs.peer);
8135
Przemek Stekiel2797d372022-12-22 11:19:22 +01008136 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008137#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008138 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008139 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008140 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008141#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008142 {
8143 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008144 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008145 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008146 return status;
8147}
8148
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008149#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008150static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008152 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008153 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008154{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8156 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8157 step != PSA_PAKE_STEP_ZK_PROOF) {
8158 return PSA_ERROR_INVALID_ARGUMENT;
8159 }
8160
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008161 psa_jpake_computation_stage_t *computation_stage =
8162 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008163
David Horstmann5da95602023-06-08 15:37:12 +01008164 if (computation_stage->round != PSA_JPAKE_FIRST &&
8165 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008166 return PSA_ERROR_BAD_STATE;
8167 }
8168
David Horstmanne7f21e62023-05-12 18:17:21 +01008169 /* Check that the step we are given is the one we were expecting */
8170 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008171 return PSA_ERROR_BAD_STATE;
8172 }
8173
David Horstmanne7f21e62023-05-12 18:17:21 +01008174 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8175 computation_stage->inputs == 0 &&
8176 computation_stage->outputs == 0) {
8177 /* Start of the round, so function decides whether we are inputting
8178 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008179 computation_stage->io_mode = io_mode;
8180 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008181 /* Middle of the round so the mode we are in must match the function
8182 * called by the user */
8183 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008184 }
8185
8186 return PSA_SUCCESS;
8187}
8188
David Horstmanne7f21e62023-05-12 18:17:21 +01008189static psa_status_t psa_jpake_epilogue(
8190 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008191 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192{
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008194 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008195
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8197 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008198 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008200 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008201 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 }
8203 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008204 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008206 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008207 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008208 }
8209 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008210 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8211 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 /* End of a round, move to the next round */
8213 stage->inputs = 0;
8214 stage->outputs = 0;
8215 stage->round++;
8216 }
8217 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008218 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008219 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 return PSA_SUCCESS;
8222}
David Horstmanne7f21e62023-05-12 18:17:21 +01008223
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008224#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225
Neil Armstronga7d08c32022-06-01 18:21:20 +02008226psa_status_t psa_pake_output(
8227 psa_pake_operation_t *operation,
8228 psa_pake_step_t step,
8229 uint8_t *output,
8230 size_t output_size,
8231 size_t *output_length)
8232{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008234 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008235 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008236
8237 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008238 status = psa_pake_complete_inputs(operation);
8239 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008240 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008241 }
8242 }
8243
8244 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008245 status = PSA_ERROR_BAD_STATE;
8246 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008247 }
8248
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008249 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008250 status = PSA_ERROR_INVALID_ARGUMENT;
8251 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008252 }
8253
Przemek Stekiele12ed362022-12-21 12:54:46 +01008254 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008255#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008256 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008257 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008259 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008261 driver_step = convert_jpake_computation_stage_to_driver_step(
8262 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008263 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008264#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008265 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008266 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_NOT_SUPPORTED;
8268 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 }
8270
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008271 status = psa_driver_wrapper_pake_output(operation, driver_step,
8272 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273
8274 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008276 }
8277
8278 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008279#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008281 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008283 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 }
8285 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008286#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008288 status = PSA_ERROR_NOT_SUPPORTED;
8289 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290 }
8291
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008292 return PSA_SUCCESS;
8293exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008294 psa_pake_abort(operation);
8295 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008296}
8297
8298psa_status_t psa_pake_input(
8299 psa_pake_operation_t *operation,
8300 psa_pake_step_t step,
8301 const uint8_t *input,
8302 size_t input_length)
8303{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008305 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008306 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8307 operation->primitive,
8308 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008309
8310 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008311 status = psa_pake_complete_inputs(operation);
8312 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008313 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008314 }
8315 }
8316
8317 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008318 status = PSA_ERROR_BAD_STATE;
8319 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008320 }
8321
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008322 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008323 status = PSA_ERROR_INVALID_ARGUMENT;
8324 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008325 }
8326
Przemek Stekiele12ed362022-12-21 12:54:46 +01008327 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008328#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008329 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008330 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008332 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008334 driver_step = convert_jpake_computation_stage_to_driver_step(
8335 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008337#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008338 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008339 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008340 status = PSA_ERROR_NOT_SUPPORTED;
8341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 }
8343
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008344 status = psa_driver_wrapper_pake_input(operation, driver_step,
8345 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346
8347 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008348 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008349 }
8350
8351 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008352#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008354 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008355 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008356 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 }
8358 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008359#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008361 status = PSA_ERROR_NOT_SUPPORTED;
8362 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008363 }
8364
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 return PSA_SUCCESS;
8366exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008367 psa_pake_abort(operation);
8368 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008369}
8370
8371psa_status_t psa_pake_get_implicit_key(
8372 psa_pake_operation_t *operation,
8373 psa_key_derivation_operation_t *output)
8374{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008375 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008376 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008377 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008378 size_t shared_key_len = 0;
8379
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008380 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008381 status = PSA_ERROR_BAD_STATE;
8382 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008383 }
8384
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008385#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008386 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008388 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008389 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008390 status = PSA_ERROR_BAD_STATE;
8391 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008392 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008393 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008394#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008395 {
8396 status = PSA_ERROR_NOT_SUPPORTED;
8397 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008398 }
8399
Przemek Stekiel0c781802022-11-29 14:53:13 +01008400 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8401 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008402 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008403 &shared_key_len);
8404
8405 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008406 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008407 }
8408
8409 status = psa_key_derivation_input_bytes(output,
8410 PSA_KEY_DERIVATION_INPUT_SECRET,
8411 shared_key,
8412 shared_key_len);
8413
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008414 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008415exit:
8416 abort_status = psa_pake_abort(operation);
8417 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008418}
8419
8420psa_status_t psa_pake_abort(
8421 psa_pake_operation_t *operation)
8422{
Przemek Stekield69dca92023-01-31 19:59:20 +01008423 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008424
Przemek Stekield69dca92023-01-31 19:59:20 +01008425 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008426 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008427 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008428
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008429 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8430 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008431 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008432 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008433 }
8434 if (operation->data.inputs.user != NULL) {
8435 mbedtls_free(operation->data.inputs.user);
8436 }
8437 if (operation->data.inputs.peer != NULL) {
8438 mbedtls_free(operation->data.inputs.peer);
8439 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008440 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008441 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008442
Przemek Stekield69dca92023-01-31 19:59:20 +01008443 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008444}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008445#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008446
Gilles Peskinee59236f2018-01-27 23:32:46 +01008447#endif /* MBEDTLS_PSA_CRYPTO_C */