blob: b51b3b93986c532f29e613f0b54fa9af9bb177c5 [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. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003473 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003474
Paul Elliottebe225c2023-02-10 14:32:53 +00003475exit:
3476
Paul Elliott59200a22023-02-21 15:07:40 +00003477 psa_wipe_tag_output_buffer(signature, status, signature_size,
3478 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003479
Paul Elliott53bb3122023-02-10 14:22:22 +00003480 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003481 if (status != PSA_SUCCESS) {
3482 operation->error_occurred = 1;
3483 }
3484
Paul Elliott59ad9452022-12-18 15:09:02 +00003485 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003486 }
3487
3488 return status;
3489}
3490
3491psa_status_t psa_sign_hash_abort(
3492 psa_sign_hash_interruptible_operation_t *operation)
3493{
Paul Elliott59ad9452022-12-18 15:09:02 +00003494 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3495
3496 status = psa_sign_hash_abort_internal(operation);
3497
3498 /* We clear the number of ops done here, so that it is not cleared when
3499 * the operation fails or succeeds, only on manual abort. */
3500 operation->num_ops = 0;
3501
Paul Elliottc9774412023-02-06 15:14:07 +00003502 /* Likewise, failure state. */
3503 operation->error_occurred = 0;
3504
Paul Elliott59ad9452022-12-18 15:09:02 +00003505 return status;
3506}
3507
3508static psa_status_t psa_verify_hash_abort_internal(
3509 psa_verify_hash_interruptible_operation_t *operation)
3510{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003511 if (operation->id == 0) {
3512 /* The object has (apparently) been initialized but it is not (yet)
3513 * in use. It's ok to call abort on such an object, and there's
3514 * nothing to do. */
3515 return PSA_SUCCESS;
3516 }
3517
Paul Elliott59ad9452022-12-18 15:09:02 +00003518 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3519
3520 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003521
3522 operation->id = 0;
3523
Paul Elliotte6145dc2023-02-07 12:51:21 +00003524 /* Do not clear either the error_occurred or num_ops elements here as they
3525 * only want to be cleared by the application calling abort, not by abort
3526 * being called at completion of an operation. */
3527
Paul Elliott59ad9452022-12-18 15:09:02 +00003528 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003529}
3530
3531psa_status_t psa_verify_hash_start(
3532 psa_verify_hash_interruptible_operation_t *operation,
3533 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3534 const uint8_t *hash, size_t hash_length,
3535 const uint8_t *signature, size_t signature_length)
3536{
3537 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3538 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3539 psa_key_slot_t *slot;
3540
Paul Elliottc9774412023-02-06 15:14:07 +00003541 /* Check that start has not been previously called, or operation has not
3542 * previously errored. */
3543 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003544 return PSA_ERROR_BAD_STATE;
3545 }
3546
3547 status = psa_sign_verify_check_alg(0, alg);
3548 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003549 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003550 return status;
3551 }
3552
3553 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3554 PSA_KEY_USAGE_VERIFY_HASH,
3555 alg);
3556
3557 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003558 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003559 return status;
3560 }
3561
3562 psa_key_attributes_t attributes = {
3563 .core = slot->attr
3564 };
3565
Paul Elliott296ede92022-12-15 17:00:30 +00003566 /* Ensure ops count gets reset, in case of operation re-use. */
3567 operation->num_ops = 0;
3568
Paul Elliott9fe12f62022-11-30 19:16:02 +00003569 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3570 slot->key.data,
3571 slot->key.bytes,
3572 alg, hash, hash_length,
3573 signature, signature_length);
3574
3575 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003576 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003577 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003578 }
3579
3580 unlock_status = psa_unlock_key_slot(slot);
3581
Paul Elliottc9774412023-02-06 15:14:07 +00003582 if (unlock_status != PSA_SUCCESS) {
3583 operation->error_occurred = 1;
3584 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003585
Paul Elliottc9774412023-02-06 15:14:07 +00003586 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003587}
3588
3589psa_status_t psa_verify_hash_complete(
3590 psa_verify_hash_interruptible_operation_t *operation)
3591{
3592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3593
Paul Elliottc9774412023-02-06 15:14:07 +00003594 /* Check that start has been called first, and that operation has not
3595 * previously errored. */
3596 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003597 status = PSA_ERROR_BAD_STATE;
3598 goto exit;
3599 }
3600
3601 status = psa_driver_wrapper_verify_hash_complete(operation);
3602
Paul Elliott296ede92022-12-15 17:00:30 +00003603 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003604 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003605 operation);
3606
Paul Elliottebe225c2023-02-10 14:32:53 +00003607exit:
3608
Paul Elliott9fe12f62022-11-30 19:16:02 +00003609 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003610 if (status != PSA_SUCCESS) {
3611 operation->error_occurred = 1;
3612 }
3613
Paul Elliott59ad9452022-12-18 15:09:02 +00003614 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003615 }
3616
3617 return status;
3618}
3619
3620psa_status_t psa_verify_hash_abort(
3621 psa_verify_hash_interruptible_operation_t *operation)
3622{
Paul Elliott59ad9452022-12-18 15:09:02 +00003623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003624
Paul Elliott59ad9452022-12-18 15:09:02 +00003625 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003626
Paul Elliott59ad9452022-12-18 15:09:02 +00003627 /* We clear the number of ops done here, so that it is not cleared when
3628 * the operation fails or succeeds, only on manual abort. */
3629 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003630
Paul Elliottc9774412023-02-06 15:14:07 +00003631 /* Likewise, failure state. */
3632 operation->error_occurred = 0;
3633
Paul Elliott59ad9452022-12-18 15:09:02 +00003634 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003635}
3636
Paul Elliott588f8ed2022-12-02 18:10:26 +00003637/****************************************************************/
3638/* Asymmetric interruptible cryptography internal */
3639/* implementations */
3640/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003641
Paul Elliott588f8ed2022-12-02 18:10:26 +00003642void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3643{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003644
Paul Elliott588f8ed2022-12-02 18:10:26 +00003645#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3646 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3647 defined(MBEDTLS_ECP_RESTARTABLE)
3648
3649 /* Internal implementation uses zero to indicate infinite number max ops,
3650 * therefore avoid this value, and set to minimum possible. */
3651 if (max_ops == 0) {
3652 max_ops = 1;
3653 }
3654
Paul Elliott588f8ed2022-12-02 18:10:26 +00003655 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003656#else
3657 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003658#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3659 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3660 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3661}
3662
Paul Elliott588f8ed2022-12-02 18:10:26 +00003663uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003664 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665{
3666#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3667 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3668 defined(MBEDTLS_ECP_RESTARTABLE)
3669
Paul Elliott93d9ca82023-02-15 18:14:21 +00003670 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671#else
3672 (void) operation;
3673 return 0;
3674#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3675 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3676 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3677}
3678
3679uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003680 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003681{
3682 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3683 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3684 defined(MBEDTLS_ECP_RESTARTABLE)
3685
Paul Elliott93d9ca82023-02-15 18:14:21 +00003686 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003687#else
3688 (void) operation;
3689 return 0;
3690#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3691 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3692 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3693}
3694
3695psa_status_t mbedtls_psa_sign_hash_start(
3696 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3697 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3698 size_t key_buffer_size, psa_algorithm_t alg,
3699 const uint8_t *hash, size_t hash_length)
3700{
3701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003702 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003703
Paul Elliott068fe072023-01-16 13:59:15 +00003704 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3705 return PSA_ERROR_NOT_SUPPORTED;
3706 }
3707
3708 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003709 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003710 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003711
3712#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003713 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3714 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003715
Paul Elliotta1c94092023-02-15 16:38:04 +00003716 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3717
Paul Elliott93d9ca82023-02-15 18:14:21 +00003718 /* Ensure num_ops is zero'ed in case of context re-use. */
3719 operation->num_ops = 0;
3720
Paul Elliott068fe072023-01-16 13:59:15 +00003721 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3722 attributes->core.bits,
3723 key_buffer,
3724 key_buffer_size,
3725 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003726
Paul Elliott068fe072023-01-16 13:59:15 +00003727 if (status != PSA_SUCCESS) {
3728 return status;
3729 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003730
Paul Elliott1bc59df2023-02-05 13:41:57 +00003731 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003732 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733
Paul Elliott068fe072023-01-16 13:59:15 +00003734 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003735 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003736 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737
Paul Elliott0290a762023-02-09 14:30:24 +00003738 /* We only need to store the same length of hash as the private key size
3739 * here, it would be truncated by the internal implementation anyway. */
3740 required_hash_length = (hash_length < operation->coordinate_bytes ?
3741 hash_length : operation->coordinate_bytes);
3742
Paul Elliottba70ad42023-02-15 18:23:53 +00003743 if (required_hash_length > sizeof(operation->hash)) {
3744 /* Shouldn't happen, but better safe than sorry. */
3745 return PSA_ERROR_CORRUPTION_DETECTED;
3746 }
3747
Paul Elliott0290a762023-02-09 14:30:24 +00003748 memcpy(operation->hash, hash, required_hash_length);
3749 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003750
3751 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003752
3753#else
Paul Elliott068fe072023-01-16 13:59:15 +00003754 (void) operation;
3755 (void) key_buffer;
3756 (void) key_buffer_size;
3757 (void) alg;
3758 (void) hash;
3759 (void) hash_length;
3760 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003761 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003762
Paul Elliott068fe072023-01-16 13:59:15 +00003763 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003764#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3765 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3766 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003767}
3768
3769psa_status_t mbedtls_psa_sign_hash_complete(
3770 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3771 uint8_t *signature, size_t signature_size,
3772 size_t *signature_length)
3773{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3775 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3776 defined(MBEDTLS_ECP_RESTARTABLE)
3777
Paul Elliotta1c94092023-02-15 16:38:04 +00003778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003779 mbedtls_mpi r;
3780 mbedtls_mpi s;
3781
Paul Elliott46845252023-02-03 14:59:11 +00003782 mbedtls_mpi_init(&r);
3783 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003784
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003785 /* Ensure max_ops is set to the current value (or default). */
3786 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3787
Paul Elliotta1c94092023-02-15 16:38:04 +00003788 if (signature_size < 2 * operation->coordinate_bytes) {
3789 status = PSA_ERROR_BUFFER_TOO_SMALL;
3790 goto exit;
3791 }
3792
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003794
Paul Elliott588f8ed2022-12-02 18:10:26 +00003795#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3796 status = mbedtls_to_psa_error(
3797 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003798 &r,
3799 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800 &operation->ctx->d,
3801 operation->hash,
3802 operation->hash_length,
3803 operation->md_alg,
3804 mbedtls_psa_get_random,
3805 MBEDTLS_PSA_RANDOM_STATE,
3806 &operation->restart_ctx));
3807#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003808 status = PSA_ERROR_NOT_SUPPORTED;
3809 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003810#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3811 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003812 status = mbedtls_to_psa_error(
3813 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003814 &r,
3815 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003816 &operation->ctx->d,
3817 operation->hash,
3818 operation->hash_length,
3819 mbedtls_psa_get_random,
3820 MBEDTLS_PSA_RANDOM_STATE,
3821 mbedtls_psa_get_random,
3822 MBEDTLS_PSA_RANDOM_STATE,
3823 &operation->restart_ctx));
3824 }
3825
Paul Elliottf8e5b562023-02-19 18:43:45 +00003826 /* Hide the fact that the restart context only holds a delta of number of
3827 * ops done during the last operation, not an absolute value. */
3828 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3829
Paul Elliott724bd252023-02-08 12:35:08 +00003830 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003831 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003832 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003834 operation->coordinate_bytes)
3835 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003836
3837 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003838 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003839 }
3840
3841 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003842 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003844 operation->coordinate_bytes,
3845 operation->coordinate_bytes)
3846 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003847
3848 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003849 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850 }
3851
Paul Elliott1bc59df2023-02-05 13:41:57 +00003852 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003853
Paul Elliott724bd252023-02-08 12:35:08 +00003854 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003855 }
Paul Elliott724bd252023-02-08 12:35:08 +00003856
3857exit:
3858
3859 mbedtls_mpi_free(&r);
3860 mbedtls_mpi_free(&s);
3861 return status;
3862
Paul Elliott588f8ed2022-12-02 18:10:26 +00003863 #else
3864
3865 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003866 (void) signature;
3867 (void) signature_size;
3868 (void) signature_length;
3869
3870 return PSA_ERROR_NOT_SUPPORTED;
3871
3872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3873 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3874 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3875}
3876
3877psa_status_t mbedtls_psa_sign_hash_abort(
3878 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3879{
3880
3881#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3882 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3883 defined(MBEDTLS_ECP_RESTARTABLE)
3884
3885 if (operation->ctx) {
3886 mbedtls_ecdsa_free(operation->ctx);
3887 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003888 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003889 }
3890
3891 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3892
Paul Elliott93d9ca82023-02-15 18:14:21 +00003893 operation->num_ops = 0;
3894
Paul Elliott588f8ed2022-12-02 18:10:26 +00003895 return PSA_SUCCESS;
3896
3897#else
3898
3899 (void) operation;
3900
3901 return PSA_ERROR_NOT_SUPPORTED;
3902
3903#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3904 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3905 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3906}
3907
3908psa_status_t mbedtls_psa_verify_hash_start(
3909 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3910 const psa_key_attributes_t *attributes,
3911 const uint8_t *key_buffer, size_t key_buffer_size,
3912 psa_algorithm_t alg,
3913 const uint8_t *hash, size_t hash_length,
3914 const uint8_t *signature, size_t signature_length)
3915{
3916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003917 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003918 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003919
Paul Elliott068fe072023-01-16 13:59:15 +00003920 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3921 return PSA_ERROR_NOT_SUPPORTED;
3922 }
3923
3924 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003925 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003926 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003927
3928#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003929 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3930 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003931
Paul Elliotta1c94092023-02-15 16:38:04 +00003932 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3933 mbedtls_mpi_init(&operation->r);
3934 mbedtls_mpi_init(&operation->s);
3935
Paul Elliott93d9ca82023-02-15 18:14:21 +00003936 /* Ensure num_ops is zero'ed in case of context re-use. */
3937 operation->num_ops = 0;
3938
Paul Elliott068fe072023-01-16 13:59:15 +00003939 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3940 attributes->core.bits,
3941 key_buffer,
3942 key_buffer_size,
3943 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944
Paul Elliott068fe072023-01-16 13:59:15 +00003945 if (status != PSA_SUCCESS) {
3946 return status;
3947 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003948
Paul Elliottc569fc22023-02-10 13:02:54 +00003949 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003950
Paul Elliott1bc59df2023-02-05 13:41:57 +00003951 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003952 return PSA_ERROR_INVALID_SIGNATURE;
3953 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003954
Paul Elliott068fe072023-01-16 13:59:15 +00003955 status = mbedtls_to_psa_error(
3956 mbedtls_mpi_read_binary(&operation->r,
3957 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003958 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003959
Paul Elliott068fe072023-01-16 13:59:15 +00003960 if (status != PSA_SUCCESS) {
3961 return status;
3962 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003963
Paul Elliott068fe072023-01-16 13:59:15 +00003964 status = mbedtls_to_psa_error(
3965 mbedtls_mpi_read_binary(&operation->s,
3966 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003967 coordinate_bytes,
3968 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003969
Paul Elliott068fe072023-01-16 13:59:15 +00003970 if (status != PSA_SUCCESS) {
3971 return status;
3972 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003973
Paul Elliott2c9843f2023-02-15 17:32:42 +00003974 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003975
Paul Elliott2c9843f2023-02-15 17:32:42 +00003976 if (status != PSA_SUCCESS) {
3977 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003978 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003979
Paul Elliott0290a762023-02-09 14:30:24 +00003980 /* We only need to store the same length of hash as the private key size
3981 * here, it would be truncated by the internal implementation anyway. */
3982 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3983 coordinate_bytes);
3984
Paul Elliottba70ad42023-02-15 18:23:53 +00003985 if (required_hash_length > sizeof(operation->hash)) {
3986 /* Shouldn't happen, but better safe than sorry. */
3987 return PSA_ERROR_CORRUPTION_DETECTED;
3988 }
3989
Paul Elliott0290a762023-02-09 14:30:24 +00003990 memcpy(operation->hash, hash, required_hash_length);
3991 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003992
3993 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003994#else
Paul Elliott068fe072023-01-16 13:59:15 +00003995 (void) operation;
3996 (void) key_buffer;
3997 (void) key_buffer_size;
3998 (void) alg;
3999 (void) hash;
4000 (void) hash_length;
4001 (void) signature;
4002 (void) signature_length;
4003 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004004 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004005 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004006
Paul Elliott068fe072023-01-16 13:59:15 +00004007 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004008#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4009 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4010 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004011}
4012
4013psa_status_t mbedtls_psa_verify_hash_complete(
4014 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4015{
4016
4017#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4018 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4019 defined(MBEDTLS_ECP_RESTARTABLE)
4020
Paul Elliottf8e5b562023-02-19 18:43:45 +00004021 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4022
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004023 /* Ensure max_ops is set to the current value (or default). */
4024 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4025
Paul Elliottf8e5b562023-02-19 18:43:45 +00004026 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004027 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4028 operation->hash,
4029 operation->hash_length,
4030 &operation->ctx->Q,
4031 &operation->r,
4032 &operation->s,
4033 &operation->restart_ctx));
4034
Paul Elliottf8e5b562023-02-19 18:43:45 +00004035 /* Hide the fact that the restart context only holds a delta of number of
4036 * ops done during the last operation, not an absolute value. */
4037 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4038
4039 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004040#else
4041 (void) operation;
4042
4043 return PSA_ERROR_NOT_SUPPORTED;
4044
4045#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4046 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4047 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4048}
4049
4050psa_status_t mbedtls_psa_verify_hash_abort(
4051 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4052{
4053
4054#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4055 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4056 defined(MBEDTLS_ECP_RESTARTABLE)
4057
4058 if (operation->ctx) {
4059 mbedtls_ecdsa_free(operation->ctx);
4060 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004061 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004062 }
4063
4064 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4065
Paul Elliott93d9ca82023-02-15 18:14:21 +00004066 operation->num_ops = 0;
4067
Paul Elliott588f8ed2022-12-02 18:10:26 +00004068 mbedtls_mpi_free(&operation->r);
4069 mbedtls_mpi_free(&operation->s);
4070
4071 return PSA_SUCCESS;
4072
4073#else
4074 (void) operation;
4075
4076 return PSA_ERROR_NOT_SUPPORTED;
4077
4078#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4079 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4080 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4081}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004082
mohammad1603503973b2018-03-12 15:59:30 +02004083/****************************************************************/
4084/* Symmetric cryptography */
4085/****************************************************************/
4086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4088 mbedtls_svc_key_id_t key,
4089 psa_algorithm_t alg,
4090 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004091{
4092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4093 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004094 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4096 PSA_KEY_USAGE_ENCRYPT :
4097 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004098 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004099
4100 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004102 status = PSA_ERROR_BAD_STATE;
4103 goto exit;
4104 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004105
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004107 status = PSA_ERROR_INVALID_ARGUMENT;
4108 goto exit;
4109 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4112 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004115
Ronald Cron49fafa92021-03-10 08:34:23 +01004116 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004117 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004118 * so we only set it (in the driver wrapper) after resources have been
4119 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004122 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004124 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 }
4126 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004127
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004128 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004130 };
4131
Ronald Cronab99ac22020-10-01 16:38:28 +02004132 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 if (cipher_operation == MBEDTLS_ENCRYPT) {
4134 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4135 &attributes,
4136 slot->key.data,
4137 slot->key.bytes,
4138 alg);
4139 } else {
4140 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4141 &attributes,
4142 slot->key.data,
4143 slot->key.bytes,
4144 alg);
4145 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004146
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (status != PSA_SUCCESS) {
4149 psa_cipher_abort(operation);
4150 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004155}
4156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4158 mbedtls_svc_key_id_t key,
4159 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004160{
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004162}
4163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4165 mbedtls_svc_key_id_t key,
4166 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004167{
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004169}
4170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4172 uint8_t *iv,
4173 size_t iv_size,
4174 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004175{
Ronald Cron6d051732020-10-01 14:10:20 +02004176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004177 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004178 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004181 status = PSA_ERROR_BAD_STATE;
4182 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004183 }
4184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004186 status = PSA_ERROR_BAD_STATE;
4187 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004188 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004189
Ronald Cron2fb90522021-07-05 12:31:44 +02004190 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004192 status = PSA_ERROR_BUFFER_TOO_SMALL;
4193 goto exit;
4194 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004197 status = PSA_ERROR_GENERIC_ERROR;
4198 goto exit;
4199 }
4200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 status = psa_generate_random(local_iv, default_iv_length);
4202 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 }
Ronald Cron5618a392021-03-26 09:52:26 +01004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 status = psa_driver_wrapper_cipher_set_iv(operation,
4207 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004208
4209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 if (status == PSA_SUCCESS) {
4211 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004212 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004213 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004215 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004217 }
Ronald Cron6d051732020-10-01 14:10:20 +02004218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004220}
4221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4223 const uint8_t *iv,
4224 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004225{
Ronald Cron6d051732020-10-01 14:10:20 +02004226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004229 status = PSA_ERROR_BAD_STATE;
4230 goto exit;
4231 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004234 status = PSA_ERROR_BAD_STATE;
4235 goto exit;
4236 }
Ronald Crona0d68172021-03-26 10:15:08 +01004237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004239 status = PSA_ERROR_INVALID_ARGUMENT;
4240 goto exit;
4241 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 status = psa_driver_wrapper_cipher_set_iv(operation,
4244 iv,
4245 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004246
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 } else {
4251 psa_cipher_abort(operation);
4252 }
4253 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004254}
4255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4257 const uint8_t *input,
4258 size_t input_length,
4259 uint8_t *output,
4260 size_t output_size,
4261 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004262{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004266 status = PSA_ERROR_BAD_STATE;
4267 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004268 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004271 status = PSA_ERROR_BAD_STATE;
4272 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004273 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 status = psa_driver_wrapper_cipher_update(operation,
4276 input,
4277 input_length,
4278 output,
4279 output_size,
4280 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004281
4282exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 if (status != PSA_SUCCESS) {
4284 psa_cipher_abort(operation);
4285 }
Ronald Cron6d051732020-10-01 14:10:20 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004288}
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4291 uint8_t *output,
4292 size_t output_size,
4293 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004294{
David Saadab4ecc272019-02-14 13:48:10 +02004295 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004298 status = PSA_ERROR_BAD_STATE;
4299 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004300 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004303 status = PSA_ERROR_BAD_STATE;
4304 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004305 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_driver_wrapper_cipher_finish(operation,
4308 output,
4309 output_size,
4310 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004311
4312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (status == PSA_SUCCESS) {
4314 return psa_cipher_abort(operation);
4315 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004316 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004320 }
mohammad1603503973b2018-03-12 15:59:30 +02004321}
4322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004324{
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004326 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004327 * in use. It's ok to call abort on such an object, and there's
4328 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004330 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004333
Ronald Cron49fafa92021-03-10 08:34:23 +01004334 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004335 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004336 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004339}
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4342 psa_algorithm_t alg,
4343 const uint8_t *input,
4344 size_t input_length,
4345 uint8_t *output,
4346 size_t output_size,
4347 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004348{
4349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004350 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004351 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004352 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4353 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004354 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004357 status = PSA_ERROR_INVALID_ARGUMENT;
4358 goto exit;
4359 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4362 PSA_KEY_USAGE_ENCRYPT,
4363 alg);
4364 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004367
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004368 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004370 };
4371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4373 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004374 status = PSA_ERROR_GENERIC_ERROR;
4375 goto exit;
4376 }
4377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (default_iv_length > 0) {
4379 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004380 status = PSA_ERROR_BUFFER_TOO_SMALL;
4381 goto exit;
4382 }
4383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 status = psa_generate_random(local_iv, default_iv_length);
4385 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004386 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004388 }
4389
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004390 status = psa_driver_wrapper_cipher_encrypt(
4391 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004392 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004393 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004395
4396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 unlock_status = psa_unlock_key_slot(slot);
4398 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004399 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004400 }
Ronald Cron23919522021-07-08 19:00:07 +02004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (status == PSA_SUCCESS) {
4403 if (default_iv_length > 0) {
4404 memcpy(output, local_iv, default_iv_length);
4405 }
4406 *output_length += default_iv_length;
4407 } else {
4408 *output_length = 0;
4409 }
4410
4411 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004412}
4413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4415 psa_algorithm_t alg,
4416 const uint8_t *input,
4417 size_t input_length,
4418 uint8_t *output,
4419 size_t output_size,
4420 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004421{
4422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004423 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004424 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004425 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004428 status = PSA_ERROR_INVALID_ARGUMENT;
4429 goto exit;
4430 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4433 PSA_KEY_USAGE_DECRYPT,
4434 alg);
4435 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004438
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004439 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004441 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004442
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4444 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004445 status = PSA_ERROR_INVALID_ARGUMENT;
4446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004448 status = PSA_ERROR_INVALID_ARGUMENT;
4449 goto exit;
4450 }
4451
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004452 status = psa_driver_wrapper_cipher_decrypt(
4453 &attributes, slot->key.data, slot->key.bytes,
4454 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004456
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004457exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 unlock_status = psa_unlock_key_slot(slot);
4459 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004460 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004464 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 }
Ronald Cron23919522021-07-08 19:00:07 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004468}
4469
4470
mohammad16035955c982018-04-26 00:53:03 +03004471/****************************************************************/
4472/* AEAD */
4473/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004474
Paul Elliottbaff51c2021-09-28 17:44:45 +01004475/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004476static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004477{
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004479}
4480
4481/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004482static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4483 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004484{
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004488#if defined(PSA_WANT_ALG_GCM)
4489 case PSA_ALG_GCM:
4490 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 * arbitrarily large nonces. Please note that we do not generally
4492 * recommend the usage of nonces of greater length than
4493 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4494 * size, which can then lead to collisions if you encrypt a very
4495 * large number of messages.*/
4496 if (nonce_length != 0) {
4497 return PSA_SUCCESS;
4498 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004499 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004500#endif /* PSA_WANT_ALG_GCM */
4501#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004502 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (nonce_length >= 7 && nonce_length <= 13) {
4504 return PSA_SUCCESS;
4505 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004506 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004507#endif /* PSA_WANT_ALG_CCM */
4508#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (nonce_length == 12) {
4511 return PSA_SUCCESS;
4512 } else if (nonce_length == 8) {
4513 return PSA_ERROR_NOT_SUPPORTED;
4514 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004515 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004516#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004517 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004518 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004520 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004523}
4524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004526{
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4528 return PSA_ERROR_INVALID_ARGUMENT;
4529 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004532}
4533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4535 psa_algorithm_t alg,
4536 const uint8_t *nonce,
4537 size_t nonce_length,
4538 const uint8_t *additional_data,
4539 size_t additional_data_length,
4540 const uint8_t *plaintext,
4541 size_t plaintext_length,
4542 uint8_t *ciphertext,
4543 size_t ciphertext_size,
4544 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004545{
Ronald Cron215633c2021-03-16 17:15:37 +01004546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4547 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004548
mohammad1603f08a5502018-06-03 15:05:47 +03004549 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 status = psa_aead_check_algorithm(alg);
4552 if (status != PSA_SUCCESS) {
4553 return status;
4554 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004555
Ronald Cron9a986162021-03-26 12:40:07 +01004556 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4558 if (status != PSA_SUCCESS) {
4559 return status;
4560 }
mohammad16035955c982018-04-26 00:53:03 +03004561
Ronald Cron215633c2021-03-16 17:15:37 +01004562 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004564 };
mohammad16035955c982018-04-26 00:53:03 +03004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 status = psa_aead_check_nonce_length(alg, nonce_length);
4567 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004570
Ronald Cronde822812021-03-17 16:08:20 +01004571 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004572 &attributes, slot->key.data, slot->key.bytes,
4573 alg,
4574 nonce, nonce_length,
4575 additional_data, additional_data_length,
4576 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4580 memset(ciphertext, 0, ciphertext_size);
4581 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004582
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004587}
4588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4590 psa_algorithm_t alg,
4591 const uint8_t *nonce,
4592 size_t nonce_length,
4593 const uint8_t *additional_data,
4594 size_t additional_data_length,
4595 const uint8_t *ciphertext,
4596 size_t ciphertext_length,
4597 uint8_t *plaintext,
4598 size_t plaintext_size,
4599 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004600{
Ronald Cron215633c2021-03-16 17:15:37 +01004601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4602 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004603
Gilles Peskineee652a32018-06-01 19:23:52 +02004604 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 status = psa_aead_check_algorithm(alg);
4607 if (status != PSA_SUCCESS) {
4608 return status;
4609 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004610
Ronald Cron9a986162021-03-26 12:40:07 +01004611 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4613 if (status != PSA_SUCCESS) {
4614 return status;
4615 }
mohammad16035955c982018-04-26 00:53:03 +03004616
Ronald Cron215633c2021-03-16 17:15:37 +01004617 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004619 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 status = psa_aead_check_nonce_length(alg, nonce_length);
4622 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004625
Ronald Cronde822812021-03-17 16:08:20 +01004626 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004627 &attributes, slot->key.data, slot->key.bytes,
4628 alg,
4629 nonce, nonce_length,
4630 additional_data, additional_data_length,
4631 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (status != PSA_SUCCESS && plaintext_size != 0) {
4635 memset(plaintext, 0, plaintext_size);
4636 }
mohammad160360a64d02018-06-03 17:20:42 +03004637
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return status;
mohammad16035955c982018-04-26 00:53:03 +03004642}
4643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4645{
4646 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004649#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004651 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4653 return PSA_ERROR_INVALID_ARGUMENT;
4654 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004655 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004656#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004657
Przemek Stekiel86679c72022-10-06 17:06:56 +02004658#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004660 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4662 return PSA_ERROR_INVALID_ARGUMENT;
4663 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004665#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004666
Przemek Stekiel86679c72022-10-06 17:06:56 +02004667#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004669 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (tag_len != 16) {
4671 return PSA_ERROR_INVALID_ARGUMENT;
4672 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004673 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004674#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004675
4676 default:
4677 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004679 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004681}
4682
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004683/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004684static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4685 int is_encrypt,
4686 mbedtls_svc_key_id_t key,
4687 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004688{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004689 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4690 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4691 psa_key_slot_t *slot = NULL;
4692 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004693 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 status = psa_aead_check_algorithm(alg);
4696 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004701 status = PSA_ERROR_BAD_STATE;
4702 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004703 }
4704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 if (operation->nonce_set || operation->lengths_set ||
4706 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004707 status = PSA_ERROR_BAD_STATE;
4708 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004709 }
4710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004712 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4718 alg);
4719 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004720 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004722
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004723 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004724 .core = slot->attr
4725 };
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if (is_encrypt) {
4732 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4733 &attributes,
4734 slot->key.data,
4735 slot->key.bytes,
4736 alg);
4737 } else {
4738 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4739 &attributes,
4740 slot->key.data,
4741 slot->key.bytes,
4742 alg);
4743 }
4744 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004745 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004749
4750exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004754 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004756 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 } else {
4758 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004762}
4763
Paul Elliott302ff6b2021-04-20 18:10:30 +01004764/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4766 mbedtls_svc_key_id_t key,
4767 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004768{
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004770}
4771
4772/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004773psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4774 mbedtls_svc_key_id_t key,
4775 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004776{
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004778}
4779
4780/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4782 uint8_t *nonce,
4783 size_t nonce_size,
4784 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004785{
4786 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004787 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004788 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004789
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004790 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004793 status = PSA_ERROR_BAD_STATE;
4794 goto exit;
4795 }
4796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004798 status = PSA_ERROR_BAD_STATE;
4799 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004800 }
4801
Paul Elliotte193ea82021-10-01 13:00:16 +01004802 /* For CCM, this size may not be correct according to the PSA
4803 * specification. The PSA Crypto 1.0.1 specification states:
4804 *
4805 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4806 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4807 *
4808 * However this restriction that L has to be the smallest integer is not
4809 * applied in practice, and it is not implementable here since the
4810 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4812 operation->alg);
4813 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004814 status = PSA_ERROR_BUFFER_TOO_SMALL;
4815 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004816 }
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 status = psa_generate_random(local_nonce, required_nonce_size);
4819 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004820 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004824
Paul Elliott39dc6b82021-05-11 19:16:09 +01004825exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (status == PSA_SUCCESS) {
4827 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004828 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 } else {
4830 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004831 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004834}
4835
4836/* Set the nonce for a multipart authenticated encryption or decryption
4837 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004838psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4839 const uint8_t *nonce,
4840 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004841{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004845 status = PSA_ERROR_BAD_STATE;
4846 goto exit;
4847 }
4848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004850 status = PSA_ERROR_BAD_STATE;
4851 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004852 }
4853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4855 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004856 status = PSA_ERROR_INVALID_ARGUMENT;
4857 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004858 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4861 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004862
Paul Elliott39dc6b82021-05-11 19:16:09 +01004863exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004865 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 } else {
4867 psa_aead_abort(operation);
4868 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004871}
4872
4873/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004874psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4875 size_t ad_length,
4876 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004877{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004878 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4879
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004881 status = PSA_ERROR_BAD_STATE;
4882 goto exit;
4883 }
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (operation->lengths_set || operation->ad_started ||
4886 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004887 status = PSA_ERROR_BAD_STATE;
4888 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004889 }
4890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004892#if defined(PSA_WANT_ALG_GCM)
4893 case PSA_ALG_GCM:
4894 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 * bits. Without the guard this code will generate warnings on 32bit
4896 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004897#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 if (((uint64_t) ad_length) >> 61 != 0 ||
4899 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004900 status = PSA_ERROR_INVALID_ARGUMENT;
4901 goto exit;
4902 }
Paul Elliott325d3742021-09-27 17:56:28 +01004903#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004904 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004905#endif /* PSA_WANT_ALG_GCM */
4906#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004909 status = PSA_ERROR_INVALID_ARGUMENT;
4910 goto exit;
4911 }
4912 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004913#endif /* PSA_WANT_ALG_CCM */
4914#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004915 case PSA_ALG_CHACHA20_POLY1305:
4916 /* No length restrictions for ChaChaPoly. */
4917 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004918#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004919 default:
4920 break;
4921 }
Paul Elliott325d3742021-09-27 17:56:28 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4924 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004925
Paul Elliott39dc6b82021-05-11 19:16:09 +01004926exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004928 operation->ad_remaining = ad_length;
4929 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004930 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 } else {
4932 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004933 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004936}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004937
4938/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004939psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4940 const uint8_t *input,
4941 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4944
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004946 status = PSA_ERROR_BAD_STATE;
4947 goto exit;
4948 }
4949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004951 status = PSA_ERROR_BAD_STATE;
4952 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004953 }
4954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 if (operation->lengths_set) {
4956 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004957 status = PSA_ERROR_INVALID_ARGUMENT;
4958 goto exit;
4959 }
4960
4961 operation->ad_remaining -= input_length;
4962 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004963#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004965 status = PSA_ERROR_BAD_STATE;
4966 goto exit;
4967 }
4968#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004969
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 status = psa_driver_wrapper_aead_update_ad(operation, input,
4971 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004972
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004975 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 } else {
4977 psa_aead_abort(operation);
4978 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004979
Gilles Peskine449bd832023-01-11 14:50:10 +01004980 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004981}
4982
4983/* Encrypt or decrypt a message fragment in an active multipart AEAD
4984 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004985psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4986 const uint8_t *input,
4987 size_t input_length,
4988 uint8_t *output,
4989 size_t output_size,
4990 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004991{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004992 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004993
4994 *output_length = 0;
4995
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004997 status = PSA_ERROR_BAD_STATE;
4998 goto exit;
4999 }
5000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005002 status = PSA_ERROR_BAD_STATE;
5003 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005004 }
5005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005007 /* Additional data length was supplied, but not all the additional
5008 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005010 status = PSA_ERROR_INVALID_ARGUMENT;
5011 goto exit;
5012 }
5013
5014 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005016 status = PSA_ERROR_INVALID_ARGUMENT;
5017 goto exit;
5018 }
5019
5020 operation->body_remaining -= input_length;
5021 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005022#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005024 status = PSA_ERROR_BAD_STATE;
5025 goto exit;
5026 }
5027#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005028
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5030 output, output_size,
5031 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005032
Paul Elliott39dc6b82021-05-11 19:16:09 +01005033exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005035 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 } else {
5037 psa_aead_abort(operation);
5038 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005041}
5042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005044{
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 if (operation->id == 0 || !operation->nonce_set) {
5046 return PSA_ERROR_BAD_STATE;
5047 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5050 operation->body_remaining != 0)) {
5051 return PSA_ERROR_INVALID_ARGUMENT;
5052 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005055}
5056
Paul Elliott302ff6b2021-04-20 18:10:30 +01005057/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005058psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5059 uint8_t *ciphertext,
5060 size_t ciphertext_size,
5061 size_t *ciphertext_length,
5062 uint8_t *tag,
5063 size_t tag_size,
5064 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005065{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5067
Paul Elliott302ff6b2021-04-20 18:10:30 +01005068 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005069 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 status = psa_aead_final_checks(operation);
5072 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005075
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005077 status = PSA_ERROR_BAD_STATE;
5078 goto exit;
5079 }
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5082 ciphertext_size,
5083 ciphertext_length,
5084 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005085
Paul Elliott39dc6b82021-05-11 19:16:09 +01005086exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005087
5088
Paul Elliottf47b0952021-05-21 18:02:33 +01005089 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005090 * the zero tag size, make sure the tag is set to something implausible.
5091 * Even if the operation succeeds, make sure we clear the rest of the
5092 * buffer to prevent potential leakage of anything previously placed in
5093 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005094 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005097
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005099}
5100
5101/* Finish authenticating and decrypting a message in a multipart AEAD
5102 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005103psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5104 uint8_t *plaintext,
5105 size_t plaintext_size,
5106 size_t *plaintext_length,
5107 const uint8_t *tag,
5108 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005109{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5111
Paul Elliott302ff6b2021-04-20 18:10:30 +01005112 *plaintext_length = 0;
5113
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 status = psa_aead_final_checks(operation);
5115 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005118
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005120 status = PSA_ERROR_BAD_STATE;
5121 goto exit;
5122 }
5123
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5125 plaintext_size,
5126 plaintext_length,
5127 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005128
5129exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005133}
5134
5135/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005136psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005137{
5138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005141 /* The object has (apparently) been initialized but it is not (yet)
5142 * in use. It's ok to call abort on such an object, and there's
5143 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005145 }
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005152}
5153
Gilles Peskinee59236f2018-01-27 23:32:46 +01005154/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005155/* Generators */
5156/****************************************************************/
5157
Przemek Stekiel69c46792022-06-10 12:59:51 +02005158#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005159 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305162 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005163#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005164#endif /* At least one builtin KDF */
5165
Przemek Stekiel69c46792022-06-10 12:59:51 +02005166#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005167 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5168 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5169static psa_status_t psa_key_derivation_start_hmac(
5170 psa_mac_operation_t *operation,
5171 psa_algorithm_t hash_alg,
5172 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005174{
5175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5178 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5179 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005180
Steven Cooreman72f736a2021-05-07 14:14:37 +02005181 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 status = psa_driver_wrapper_mac_sign_setup(operation,
5185 &attributes,
5186 hmac_key, hmac_key_length,
5187 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 psa_reset_key_attributes(&attributes);
5190 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005191}
5192#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005193
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194#define HKDF_STATE_INIT 0 /* no input yet */
5195#define HKDF_STATE_STARTED 1 /* got salt */
5196#define HKDF_STATE_KEYED 2 /* got key */
5197#define HKDF_STATE_OUTPUT 3 /* output started */
5198
Gilles Peskinecbe66502019-05-16 16:59:18 +02005199static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005201{
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5203 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5204 } else {
5205 return operation->alg;
5206 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207}
5208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005210{
5211 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5213 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214 /* The object has (apparently) been initialized but it is not
5215 * in use. It's ok to call abort on such an object, and there's
5216 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005218#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5220 mbedtls_free(operation->ctx.hkdf.info);
5221 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5222 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005223#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005224#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5225 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5227 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5228 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5229 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005230 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005232 }
5233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005235 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005237 }
5238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005240 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005242 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005243#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005245 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005247 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005248#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005249 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005250
5251 /* We leave the fields Ai and output_block to be erased safely by the
5252 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005254#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5255 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005256#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5258 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5259 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5260 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005261#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305262#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305263 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305264 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005265 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305266 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305268
5269 status = PSA_SUCCESS;
5270 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305271#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 {
5273 status = PSA_ERROR_BAD_STATE;
5274 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 mbedtls_platform_zeroize(operation, sizeof(*operation));
5276 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277}
5278
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281{
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005285 }
5286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005289}
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5292 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005293{
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (operation->alg == 0) {
5295 return PSA_ERROR_BAD_STATE;
5296 }
5297 if (capacity > operation->capacity) {
5298 return PSA_ERROR_INVALID_ARGUMENT;
5299 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005302}
5303
Przemek Stekiel69c46792022-06-10 12:59:51 +02005304#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005305/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005306static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5307 psa_algorithm_t kdf_alg,
5308 uint8_t *output,
5309 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005310{
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5312 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005313 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005314 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005315#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005317#else
5318 const uint8_t last_block = 0xff;
5319#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 if (hkdf->state < HKDF_STATE_KEYED ||
5322 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005323#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005325#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 )) {
5327 return PSA_ERROR_BAD_STATE;
5328 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 hkdf->state = HKDF_STATE_OUTPUT;
5330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005332 /* Copy what remains of the current block */
5333 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 }
5337 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005338 output += n;
5339 output_length -= n;
5340 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005342 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005344 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005345 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005347 * object was corrupted or if this function is called directly
5348 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 if (hkdf->block_number == last_block) {
5350 return PSA_ERROR_BAD_STATE;
5351 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005352
5353 /* We need a new block */
5354 ++hkdf->block_number;
5355 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5358 hash_alg,
5359 hkdf->prk,
5360 hash_length);
5361 if (status != PSA_SUCCESS) {
5362 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005363 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005364
5365 if (hkdf->block_number != 1) {
5366 status = psa_mac_update(&hkdf->hmac,
5367 hkdf->output_block,
5368 hash_length);
5369 if (status != PSA_SUCCESS) {
5370 return status;
5371 }
5372 }
5373 status = psa_mac_update(&hkdf->hmac,
5374 hkdf->info,
5375 hkdf->info_length);
5376 if (status != PSA_SUCCESS) {
5377 return status;
5378 }
5379 status = psa_mac_update(&hkdf->hmac,
5380 &hkdf->block_number, 1);
5381 if (status != PSA_SUCCESS) {
5382 return status;
5383 }
5384 status = psa_mac_sign_finish(&hkdf->hmac,
5385 hkdf->output_block,
5386 sizeof(hkdf->output_block),
5387 &hmac_output_length);
5388 if (status != PSA_SUCCESS) {
5389 return status;
5390 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005391 }
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005394}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005395#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005396
John Durkop07cc04a2020-11-16 22:08:34 -08005397#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5398 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005399static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5400 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005402{
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5404 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005405 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5406 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005407 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005408
Janos Follath7742fee2019-06-17 12:58:10 +01005409 /* We can't be wanting more output after block 0xff, otherwise
5410 * the capacity check in psa_key_derivation_output_bytes() would have
5411 * prevented this call. It could happen only if the operation
5412 * object was corrupted or if this function is called directly
5413 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (tls12_prf->block_number == 0xff) {
5415 return PSA_ERROR_CORRUPTION_DETECTED;
5416 }
Janos Follath7742fee2019-06-17 12:58:10 +01005417
5418 /* We need a new block */
5419 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005420 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005421
5422 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5423 *
5424 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5425 *
5426 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5427 * HMAC_hash(secret, A(2) + seed) +
5428 * HMAC_hash(secret, A(3) + seed) + ...
5429 *
5430 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005432 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005433 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005434 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005435 * is currently extracted as `output_block` and where i is
5436 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005437 */
5438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 status = psa_key_derivation_start_hmac(&hmac,
5440 hash_alg,
5441 tls12_prf->secret,
5442 tls12_prf->secret_length);
5443 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005446
5447 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005449 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5450 * the variable seed and in this instance means it in the context of the
5451 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 status = psa_mac_update(&hmac,
5453 tls12_prf->label,
5454 tls12_prf->label_length);
5455 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 }
5458 status = psa_mac_update(&hmac,
5459 tls12_prf->seed,
5460 tls12_prf->seed_length);
5461 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005462 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 }
5464 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005465 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5467 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005468 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005470 }
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 status = psa_mac_sign_finish(&hmac,
5473 tls12_prf->Ai, hash_length,
5474 &hmac_output_length);
5475 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005476 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 }
5478 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005479 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005481
5482 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 status = psa_key_derivation_start_hmac(&hmac,
5484 hash_alg,
5485 tls12_prf->secret,
5486 tls12_prf->secret_length);
5487 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005488 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 }
5490 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5491 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005492 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 }
5494 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5495 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005496 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
5498 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5499 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005500 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 }
5502 status = psa_mac_sign_finish(&hmac,
5503 tls12_prf->output_block, hash_length,
5504 &hmac_output_length);
5505 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005506 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005508
Janos Follath7742fee2019-06-17 12:58:10 +01005509
5510cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 cleanup_status = psa_mac_abort(&hmac);
5512 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005513 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005517}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005518
Janos Follath844eb0e2019-06-19 12:10:49 +01005519static psa_status_t psa_key_derivation_tls12_prf_read(
5520 psa_tls12_prf_key_derivation_t *tls12_prf,
5521 psa_algorithm_t alg,
5522 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005524{
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5526 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005527 psa_status_t status;
5528 uint8_t offset, length;
5529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005531 case PSA_TLS12_PRF_STATE_LABEL_SET:
5532 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5533 break;
5534 case PSA_TLS12_PRF_STATE_OUTPUT:
5535 break;
5536 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005538 }
5539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005541 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (tls12_prf->left_in_block == 0) {
5543 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5544 alg);
5545 if (status != PSA_SUCCESS) {
5546 return status;
5547 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005548
5549 continue;
5550 }
5551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005553 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005555 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005557
5558 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005560 output += length;
5561 output_length -= length;
5562 tls12_prf->left_in_block -= length;
5563 }
5564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005566}
John Durkop07cc04a2020-11-16 22:08:34 -08005567#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5568 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005569
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005570#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5571static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5572 psa_tls12_ecjpake_to_pms_t *ecjpake,
5573 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005575{
5576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005577 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 if (output_length != 32) {
5580 return PSA_ERROR_INVALID_ARGUMENT;
5581 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5584 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5585 &output_size);
5586 if (status != PSA_SUCCESS) {
5587 return status;
5588 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 if (output_size != output_length) {
5591 return PSA_ERROR_GENERIC_ERROR;
5592 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005595}
5596#endif
5597
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305598#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305599static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5600 psa_pbkdf2_key_derivation_t *pbkdf2,
5601 psa_algorithm_t prf_alg,
5602 uint8_t prf_output_length,
5603 psa_key_attributes_t *attributes)
5604{
5605 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305606 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305607 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305608 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305609 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305610 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305611 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305612
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305613 mac_operation.is_sign = 1;
5614 mac_operation.mac_size = prf_output_length;
5615 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305616
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305617 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5618 attributes,
5619 pbkdf2->password,
5620 pbkdf2->password_length,
5621 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305622 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305623 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305624 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305625 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5626 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305627 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305628 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305629 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305630 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305631 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305632 }
5633 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5634 &mac_output_length);
5635 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305636 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305637 }
5638
5639 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305640 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305641 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305642 }
5643
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305644 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305645
5646 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305647 /* We are passing prf_output_length as mac_size because the driver
5648 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305649 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305650 status = psa_driver_wrapper_mac_compute(attributes,
5651 pbkdf2->password,
5652 pbkdf2->password_length,
5653 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305654 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305655 &mac_output_length);
5656 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305657 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305658 }
5659
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305660 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305661 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305662
5663cleanup:
5664 /* Zeroise buffers to clear sensitive data from memory. */
5665 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5666 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305667}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305668
5669static psa_status_t psa_key_derivation_pbkdf2_read(
5670 psa_pbkdf2_key_derivation_t *pbkdf2,
5671 psa_algorithm_t kdf_alg,
5672 uint8_t *output,
5673 size_t output_length)
5674{
5675 psa_status_t status;
5676 psa_algorithm_t prf_alg;
5677 uint8_t prf_output_length;
5678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5679 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5680 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5681
5682 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5683 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5684 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5685 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305686 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5687 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305688 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305689 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305690 } else {
5691 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305692 }
5693
5694 switch (pbkdf2->state) {
5695 case PSA_PBKDF2_STATE_PASSWORD_SET:
5696 /* Initially we need a new block so bytes_used is equal to block size*/
5697 pbkdf2->bytes_used = prf_output_length;
5698 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5699 break;
5700 case PSA_PBKDF2_STATE_OUTPUT:
5701 break;
5702 default:
5703 return PSA_ERROR_BAD_STATE;
5704 }
5705
5706 while (output_length != 0) {
5707 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5708 if (n > output_length) {
5709 n = (uint8_t) output_length;
5710 }
5711 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5712 output += n;
5713 output_length -= n;
5714 pbkdf2->bytes_used += n;
5715
5716 if (output_length == 0) {
5717 break;
5718 }
5719
5720 /* We need a new block */
5721 pbkdf2->bytes_used = 0;
5722 pbkdf2->block_number++;
5723
5724 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5725 prf_output_length,
5726 &attributes);
5727 if (status != PSA_SUCCESS) {
5728 return status;
5729 }
5730 }
5731
5732 return PSA_SUCCESS;
5733}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305734#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305735
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005736psa_status_t psa_key_derivation_output_bytes(
5737 psa_key_derivation_operation_t *operation,
5738 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005740{
5741 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005747 }
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005751 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005753 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005754 goto exit;
5755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005758 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005759 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5760 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005761 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005762 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005764 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005765 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766
Przemek Stekiel69c46792022-06-10 12:59:51 +02005767#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5769 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5770 output, output_length);
5771 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005772#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005773#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5774 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5776 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5777 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5778 kdf_alg, output,
5779 output_length);
5780 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005781#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5782 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005783#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005785 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5787 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005788#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305789#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305790 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305791 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5792 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305793 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305794#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005795
Gilles Peskineeab56e42018-07-12 17:12:33 +02005796 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005797 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005799 }
5800
5801exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005803 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005804 * This allows us to differentiate between exhausted operations and
5805 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5806 * operations. */
5807 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005811 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005813}
5814
David Brown7807bf72021-02-09 16:28:23 -07005815#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005816static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005817{
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 if (data_size >= 8) {
5819 mbedtls_des_key_set_parity(data);
5820 }
5821 if (data_size >= 16) {
5822 mbedtls_des_key_set_parity(data + 8);
5823 }
5824 if (data_size >= 24) {
5825 mbedtls_des_key_set_parity(data + 16);
5826 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005827}
David Brown7807bf72021-02-09 16:28:23 -07005828#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005829
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005830/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 * ECC keys on a Weierstrass elliptic curve require the generation
5832 * of a private key which is an integer
5833 * in the range [1, N - 1], where N is the boundary of the private key domain:
5834 * N is the prime p for Diffie-Hellman, or the order of the
5835 * curve’s base point for ECC.
5836 *
5837 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5838 * This function generates the private key using the following process:
5839 *
5840 * 1. Draw a byte string of length ceiling(m/8) bytes.
5841 * 2. If m is not a multiple of 8, set the most significant
5842 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5843 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5844 * 4. If k > N - 2, discard the result and return to step 1.
5845 * 5. Output k + 1 as the private key.
5846 *
5847 * This method allows compliance to NIST standards, specifically the methods titled
5848 * Key-Pair Generation by Testing Candidates in the following publications:
5849 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5850 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5851 * Diffie-Hellman keys.
5852 *
5853 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5854 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5855 *
5856 * Note: Function allocates memory for *data buffer, so given *data should be
5857 * always NULL.
5858 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005859#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5860#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005861static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005862 psa_key_slot_t *slot,
5863 size_t bits,
5864 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005865 uint8_t **data
5866 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005867{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005868 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005869 mbedtls_mpi k;
5870 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005872 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005873 size_t m;
5874 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 mbedtls_mpi_init(&k);
5877 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005878
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005879 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005881 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005885 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005886 goto cleanup;
5887 }
5888
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005889 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005893
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005894 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005895 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005896 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005897
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005898 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005899
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005900 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005902
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005903 /* Note: This function is always called with *data == NULL and it
5904 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 *data = mbedtls_calloc(1, m_bytes);
5906 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005907 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005908 goto cleanup;
5909 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005912 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005914 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005916
5917 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005920 * (8 * ceiling(m/8) - m) bits of the first byte in
5921 * the string to zero.
5922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005924 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005925 }
5926
5927 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 * big-endian byte string.
5929 */
5930 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005931
5932 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 * Result of comparison is returned. When it indicates error
5934 * then this function is called again.
5935 */
5936 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005937 }
5938
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005939 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5941 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 if (ret != 0) {
5944 status = mbedtls_to_psa_error(ret);
5945 }
5946 if (status != PSA_SUCCESS) {
5947 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005948 *data = NULL;
5949 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 mbedtls_mpi_free(&k);
5951 mbedtls_mpi_free(&diff_N_2);
5952 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005953}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005954
5955/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5956 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5957 *
5958 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5959 * draw a 32-byte string and process it as specified in
5960 * Elliptic Curves for Security [RFC7748] §5.
5961 *
5962 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5963 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5964 *
5965 * Note: Function allocates memory for *data buffer, so given *data should be
5966 * always NULL.
5967 */
5968
5969static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5970 size_t bits,
5971 psa_key_derivation_operation_t *operation,
5972 uint8_t **data
5973 )
5974{
5975 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005979 case 255:
5980 output_length = 32;
5981 break;
5982 case 448:
5983 output_length = 56;
5984 break;
5985 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005987 break;
5988 }
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 if (*data == NULL) {
5993 return PSA_ERROR_INSUFFICIENT_MEMORY;
5994 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005999 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006003 case 255:
6004 (*data)[0] &= 248;
6005 (*data)[31] &= 127;
6006 (*data)[31] |= 64;
6007 break;
6008 case 448:
6009 (*data)[0] &= 252;
6010 (*data)[55] |= 128;
6011 break;
6012 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006014 break;
6015 }
6016
6017 return status;
6018}
Valerio Setti86587ab2023-06-27 13:09:30 +02006019#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6020static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6021 psa_key_slot_t *slot, size_t bits,
6022 psa_key_derivation_operation_t *operation, uint8_t **data)
6023{
6024 (void) slot;
6025 (void) bits;
6026 (void) operation;
6027 (void) data;
6028 return PSA_ERROR_NOT_SUPPORTED;
6029}
6030
6031static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6032 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6033{
6034 (void) bits;
6035 (void) operation;
6036 (void) data;
6037 return PSA_ERROR_NOT_SUPPORTED;
6038}
6039#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6040#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006041
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006042static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006043 psa_key_slot_t *slot,
6044 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046{
6047 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306049 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006050 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006051 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6054 return PSA_ERROR_INVALID_ARGUMENT;
6055 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006056
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006057#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006058 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6060 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6061 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006062 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6064 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 }
6067 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006068 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6070 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006073 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006074 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006075#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006076 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (key_type_is_raw_bytes(slot->attr.type)) {
6078 if (bits % 8 != 0) {
6079 return PSA_ERROR_INVALID_ARGUMENT;
6080 }
6081 data = mbedtls_calloc(1, bytes);
6082 if (data == NULL) {
6083 return PSA_ERROR_INSUFFICIENT_MEMORY;
6084 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 status = psa_key_derivation_output_bytes(operation, data, bytes);
6087 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 }
David Brown12ca5032021-02-11 11:02:00 -07006090#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6092 psa_des_set_key_parity(data, bytes);
6093 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006094#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 } else {
6096 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006097 }
Ronald Crondd04d422020-11-28 15:14:42 +01006098
Ronald Cronbf33c932020-11-28 18:06:53 +01006099 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006100 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006102 };
6103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6105 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6106 &storage_size);
6107 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 }
Archanad8a83dc2021-06-14 10:04:16 +05306110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 status = psa_allocate_buffer_to_slot(slot, storage_size);
6112 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306113 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 }
Archanad8a83dc2021-06-14 10:04:16 +05306115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 status = psa_driver_wrapper_import_key(&attributes,
6117 data, bytes,
6118 slot->key.data,
6119 slot->key.bytes,
6120 &slot->key.bytes, &bits);
6121 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006122 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006124
6125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 mbedtls_free(data);
6127 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006128}
6129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6131 psa_key_derivation_operation_t *operation,
6132 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006133{
6134 psa_status_t status;
6135 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006136 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006137
Ronald Cron81709fc2020-11-14 12:10:32 +01006138 *key = MBEDTLS_SVC_KEY_ID_INIT;
6139
Gilles Peskine0f84d622019-09-12 19:03:13 +02006140 /* Reject any attempt to create a zero-length key so that we don't
6141 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (psa_get_key_bits(attributes) == 0) {
6143 return PSA_ERROR_INVALID_ARGUMENT;
6144 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (operation->alg == PSA_ALG_NONE) {
6147 return PSA_ERROR_BAD_STATE;
6148 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (!operation->can_output_key) {
6151 return PSA_ERROR_NOT_PERMITTED;
6152 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6155 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006158 /* Deriving a key in a secure element is not implemented yet. */
6159 status = PSA_ERROR_NOT_SUPPORTED;
6160 }
6161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 if (status == PSA_SUCCESS) {
6163 status = psa_generate_derived_key_internal(slot,
6164 attributes->core.bits,
6165 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 if (status == PSA_SUCCESS) {
6168 status = psa_finish_key_creation(slot, driver, key);
6169 }
6170 if (status != PSA_SUCCESS) {
6171 psa_fail_key_creation(slot, driver);
6172 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006175}
6176
Gilles Peskineeab56e42018-07-12 17:12:33 +02006177
6178
6179/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006180/* Key derivation */
6181/****************************************************************/
6182
Steven Cooreman932ffb72021-02-15 12:14:32 +01006183#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006184static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006185{
6186#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6188 return 1;
6189 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006190#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006191#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6193 return 1;
6194 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006195#endif
6196#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6198 return 1;
6199 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006200#endif
6201#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6203 return 1;
6204 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006205#endif
6206#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6208 return 1;
6209 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006210#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006211#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6213 return 1;
6214 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006215#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306216#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6217 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6218 return 1;
6219 }
6220#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306221#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6222 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6223 return 1;
6224 }
6225#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006227}
6228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006230{
6231 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 psa_status_t status = psa_hash_setup(&operation, alg);
6233 psa_hash_abort(&operation);
6234 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006235}
6236
Gilles Peskine969c5d62019-01-16 15:53:06 +01006237static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006238 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006240{
Janos Follath5fe19732019-06-20 15:09:30 +01006241 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6242 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006244
Gilles Peskine969c5d62019-01-16 15:53:06 +01006245 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 if (!is_kdf_alg_supported(kdf_alg)) {
6247 return PSA_ERROR_NOT_SUPPORTED;
6248 }
John Durkop07cc04a2020-11-16 22:08:34 -08006249
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006250 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306251 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6253 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306254 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6255 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6256 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306257 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306258 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if (hash_size == 0) {
6260 return PSA_ERROR_NOT_SUPPORTED;
6261 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006262
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006263 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6264 * we might fail later, which is somewhat unfriendly and potentially
6265 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_status_t status = psa_hash_try_support(hash_alg);
6267 if (status != PSA_SUCCESS) {
6268 return status;
6269 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006270 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6273 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6274 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6275 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006276 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006277#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006278 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6280 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006281 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006283#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6284 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 operation->capacity = 255 * hash_size;
6286 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006287}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006290{
6291#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 if (alg == PSA_ALG_ECDH) {
6293 return PSA_SUCCESS;
6294 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006295#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006296#if defined(PSA_WANT_ALG_FFDH)
6297 if (alg == PSA_ALG_FFDH) {
6298 return PSA_SUCCESS;
6299 }
6300#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006301 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006303}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006304
6305static int psa_key_derivation_allows_free_form_secret_input(
6306 psa_algorithm_t kdf_alg)
6307{
6308#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6309 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6310 return 0;
6311 }
6312#endif
6313 (void) kdf_alg;
6314 return 1;
6315}
John Durkop07cc04a2020-11-16 22:08:34 -08006316#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6319 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006320{
6321 psa_status_t status;
6322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 if (operation->alg != 0) {
6324 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006325 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6328 return PSA_ERROR_INVALID_ARGUMENT;
6329 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6330#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6331 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6332 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6333 status = psa_key_agreement_try_support(ka_alg);
6334 if (status != PSA_SUCCESS) {
6335 return status;
6336 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006337 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6338 return PSA_ERROR_INVALID_ARGUMENT;
6339 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6341#else
6342 return PSA_ERROR_NOT_SUPPORTED;
6343#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6344 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6345#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6346 status = psa_key_derivation_setup_kdf(operation, alg);
6347#else
6348 return PSA_ERROR_NOT_SUPPORTED;
6349#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6350 } else {
6351 return PSA_ERROR_INVALID_ARGUMENT;
6352 }
6353
6354 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006355 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 }
6357 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006358}
6359
Przemek Stekiel69c46792022-06-10 12:59:51 +02006360#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006361static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6362 psa_algorithm_t kdf_alg,
6363 psa_key_derivation_step_t step,
6364 const uint8_t *data,
6365 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006366{
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006368 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006370 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006371#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6373 return PSA_ERROR_INVALID_ARGUMENT;
6374 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006375#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 if (hkdf->state != HKDF_STATE_INIT) {
6377 return PSA_ERROR_BAD_STATE;
6378 } else {
6379 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6380 hash_alg,
6381 data, data_length);
6382 if (status != PSA_SUCCESS) {
6383 return status;
6384 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006385 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006387 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006388 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006389#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006391 /* We shouldn't be in different state as HKDF_EXPAND only allows
6392 * two inputs: SECRET (this case) and INFO which does not modify
6393 * the state. It could happen only if the hkdf
6394 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (hkdf->state != HKDF_STATE_INIT) {
6396 return PSA_ERROR_BAD_STATE;
6397 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006398
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006399 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6401 return PSA_ERROR_INVALID_ARGUMENT;
6402 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 memcpy(hkdf->prk, data, data_length);
6405 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006406#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006407 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006408 /* HKDF: If no salt was provided, use an empty salt.
6409 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006411#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6413 return PSA_ERROR_BAD_STATE;
6414 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006415#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6417 hash_alg,
6418 NULL, 0);
6419 if (status != PSA_SUCCESS) {
6420 return status;
6421 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006422 hkdf->state = HKDF_STATE_STARTED;
6423 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 if (hkdf->state != HKDF_STATE_STARTED) {
6425 return PSA_ERROR_BAD_STATE;
6426 }
6427 status = psa_mac_update(&hkdf->hmac,
6428 data, data_length);
6429 if (status != PSA_SUCCESS) {
6430 return status;
6431 }
6432 status = psa_mac_sign_finish(&hkdf->hmac,
6433 hkdf->prk,
6434 sizeof(hkdf->prk),
6435 &data_length);
6436 if (status != PSA_SUCCESS) {
6437 return status;
6438 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006439 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006440
Gilles Peskine2b522db2019-04-12 15:11:49 +02006441 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006442 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006443#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006445 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006447 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006450 {
6451 /* Block 0 is empty, and the next block will be
6452 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006454 }
6455
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006457 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006458#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6460 return PSA_ERROR_INVALID_ARGUMENT;
6461 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006462#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006463#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6465 hkdf->state == HKDF_STATE_INIT) {
6466 return PSA_ERROR_BAD_STATE;
6467 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006468#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 if (hkdf->state == HKDF_STATE_OUTPUT) {
6470 return PSA_ERROR_BAD_STATE;
6471 }
6472 if (hkdf->info_set) {
6473 return PSA_ERROR_BAD_STATE;
6474 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006475 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 if (data_length != 0) {
6477 hkdf->info = mbedtls_calloc(1, data_length);
6478 if (hkdf->info == NULL) {
6479 return PSA_ERROR_INSUFFICIENT_MEMORY;
6480 }
6481 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006482 }
6483 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006485 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006487 }
6488}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006489#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006490
John Durkop07cc04a2020-11-16 22:08:34 -08006491#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6492 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006493static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6494 const uint8_t *data,
6495 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006496{
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6498 return PSA_ERROR_BAD_STATE;
6499 }
Janos Follathf08e2652019-06-13 09:05:41 +01006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 if (data_length != 0) {
6502 prf->seed = mbedtls_calloc(1, data_length);
6503 if (prf->seed == NULL) {
6504 return PSA_ERROR_INSUFFICIENT_MEMORY;
6505 }
Janos Follathf08e2652019-06-13 09:05:41 +01006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006508 prf->seed_length = data_length;
6509 }
Janos Follathf08e2652019-06-13 09:05:41 +01006510
Gilles Peskine43f958b2020-12-13 14:55:14 +01006511 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006514}
6515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6517 const uint8_t *data,
6518 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006519{
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6521 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6522 return PSA_ERROR_BAD_STATE;
6523 }
Janos Follath81550542019-06-13 14:26:34 +01006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 if (data_length != 0) {
6526 prf->secret = mbedtls_calloc(1, data_length);
6527 if (prf->secret == NULL) {
6528 return PSA_ERROR_INSUFFICIENT_MEMORY;
6529 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006532 prf->secret_length = data_length;
6533 }
Janos Follath81550542019-06-13 14:26:34 +01006534
Gilles Peskine43f958b2020-12-13 14:55:14 +01006535 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006536
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006538}
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6541 const uint8_t *data,
6542 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006543{
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6545 return PSA_ERROR_BAD_STATE;
6546 }
Janos Follath63028dd2019-06-13 09:15:47 +01006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 if (data_length != 0) {
6549 prf->label = mbedtls_calloc(1, data_length);
6550 if (prf->label == NULL) {
6551 return PSA_ERROR_INSUFFICIENT_MEMORY;
6552 }
Janos Follath63028dd2019-06-13 09:15:47 +01006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006555 prf->label_length = data_length;
6556 }
Janos Follath63028dd2019-06-13 09:15:47 +01006557
Gilles Peskine43f958b2020-12-13 14:55:14 +01006558 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006559
Gilles Peskine449bd832023-01-11 14:50:10 +01006560 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006561}
6562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6564 psa_key_derivation_step_t step,
6565 const uint8_t *data,
6566 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006567{
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006569 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006571 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006573 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006575 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006577 }
6578}
John Durkop07cc04a2020-11-16 22:08:34 -08006579#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6580 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6581
6582#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6583static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6584 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006585 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006587{
6588 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6590 4 + data_length + prf->other_secret_length :
6591 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6594 return PSA_ERROR_INVALID_ARGUMENT;
6595 }
John Durkop07cc04a2020-11-16 22:08:34 -08006596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 uint8_t *pms = mbedtls_calloc(1, pms_len);
6598 if (pms == NULL) {
6599 return PSA_ERROR_INSUFFICIENT_MEMORY;
6600 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006601 uint8_t *cur = pms;
6602
6603 /* pure-PSK:
6604 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006605 *
6606 * The premaster secret is formed as follows: if the PSK is N octets
6607 * long, concatenate a uint16 with the value N, N zero octets, a second
6608 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006609 *
6610 * mixed-PSK:
6611 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6612 * follows: concatenate a uint16 with the length of the other secret,
6613 * the other secret itself, uint16 with the length of PSK, and the
6614 * PSK itself.
6615 * For details please check:
6616 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6617 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6618 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006619 */
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6622 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6623 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6624 if (prf->other_secret_length != 0) {
6625 memcpy(cur, prf->other_secret, prf->other_secret_length);
6626 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006627 cur += prf->other_secret_length;
6628 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 } else {
6630 *cur++ = MBEDTLS_BYTE_1(data_length);
6631 *cur++ = MBEDTLS_BYTE_0(data_length);
6632 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006633 cur += data_length;
6634 }
6635
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 *cur++ = MBEDTLS_BYTE_1(data_length);
6637 *cur++ = MBEDTLS_BYTE_0(data_length);
6638 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006639 cur += data_length;
6640
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006642
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006643 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006645}
Janos Follath6660f0e2019-06-17 08:44:03 +01006646
Przemek Stekiele47201b2022-04-19 13:53:28 +02006647static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6648 psa_tls12_prf_key_derivation_t *prf,
6649 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006651{
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6653 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006654 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006655
6656 if (data_length != 0) {
6657 prf->other_secret = mbedtls_calloc(1, data_length);
6658 if (prf->other_secret == NULL) {
6659 return PSA_ERROR_INSUFFICIENT_MEMORY;
6660 }
6661
6662 memcpy(prf->other_secret, data, data_length);
6663 prf->other_secret_length = data_length;
6664 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006665 prf->other_secret_length = 0;
6666 }
6667
6668 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6669
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006671}
6672
Janos Follath6660f0e2019-06-17 08:44:03 +01006673static psa_status_t psa_tls12_prf_psk_to_ms_input(
6674 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006675 psa_key_derivation_step_t step,
6676 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006678{
Gilles Peskine449bd832023-01-11 14:50:10 +01006679 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006680 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 return psa_tls12_prf_psk_to_ms_set_key(prf,
6682 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006683 break;
6684 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6686 data,
6687 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006688 break;
6689 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006691 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006692
Przemek Stekiele47201b2022-04-19 13:53:28 +02006693 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006694}
John Durkop07cc04a2020-11-16 22:08:34 -08006695#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006696
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006697#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6698static psa_status_t psa_tls12_ecjpake_to_pms_input(
6699 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006700 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006701 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703{
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6705 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6706 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006707 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006708
6709 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 if (data[0] != 0x04) {
6711 return PSA_ERROR_INVALID_ARGUMENT;
6712 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006713
6714 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006718}
6719#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306720
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306721#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306722static psa_status_t psa_pbkdf2_set_input_cost(
6723 psa_pbkdf2_key_derivation_t *pbkdf2,
6724 psa_key_derivation_step_t step,
6725 uint64_t data)
6726{
6727 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6728 return PSA_ERROR_INVALID_ARGUMENT;
6729 }
6730
6731 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6732 return PSA_ERROR_BAD_STATE;
6733 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306734
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306735 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306736 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306737 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306738
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306739 if (data == 0) {
6740 return PSA_ERROR_INVALID_ARGUMENT;
6741 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306742
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306743 pbkdf2->input_cost = data;
6744 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6745
6746 return PSA_SUCCESS;
6747}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306748
6749static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6750 const uint8_t *data,
6751 size_t data_length)
6752{
Gilles Peskineead17662023-08-20 22:02:51 +02006753 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6754 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6755 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6756 /* Appending to existing salt. No state change. */
6757 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306758 return PSA_ERROR_BAD_STATE;
6759 }
6760
Gilles Peskineca57d782023-07-20 19:08:06 +02006761 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006762 /* Appending an empty string, nothing to do. */
6763 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306764 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306765
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306766 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6767 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306768 return PSA_ERROR_INSUFFICIENT_MEMORY;
6769 }
6770
Gilles Peskineead17662023-08-20 22:02:51 +02006771 if (pbkdf2->salt_length != 0) {
6772 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6773 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306774 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306775 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306776 mbedtls_free(pbkdf2->salt);
6777 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306778 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306779 return PSA_SUCCESS;
6780}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306781
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306782#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306784 const uint8_t *input,
6785 size_t input_len,
6786 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306787 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306788{
6789 psa_status_t status = PSA_SUCCESS;
6790 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6791 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306792 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306793 } else {
6794 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306795 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306796 }
6797 return status;
6798}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306799#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306800
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306801#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306802static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6803 size_t input_len,
6804 uint8_t *output,
6805 size_t *output_len)
6806{
6807 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306808 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306810 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306811 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6812 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306814 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306815 * mac_size as the driver function sets mac_output_length = mac_size
6816 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306817 status = psa_driver_wrapper_mac_compute(&attributes,
6818 zeros, sizeof(zeros),
6819 PSA_ALG_CMAC, input, input_len,
6820 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306821 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6822 128U,
6823 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306824 output_len);
6825 } else {
6826 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306827 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306828 }
6829 return status;
6830}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306831#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306832
6833static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306834 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306835 const uint8_t *data,
6836 size_t data_length)
6837{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306838 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306839 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6840 return PSA_ERROR_BAD_STATE;
6841 }
6842
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306843#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306844 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6845 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6846 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6847 pbkdf2->password,
6848 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306849 } else
6850#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6851#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6852 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306853 status = psa_pbkdf2_cmac_set_password(data, data_length,
6854 pbkdf2->password,
6855 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306856 } else
6857#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6858 {
6859 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306860 }
6861
6862 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6863
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306864 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306865}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306866
6867static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306868 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306869 psa_key_derivation_step_t step,
6870 const uint8_t *data,
6871 size_t data_length)
6872{
6873 switch (step) {
6874 case PSA_KEY_DERIVATION_INPUT_SALT:
6875 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6876 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306877 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306878 default:
6879 return PSA_ERROR_INVALID_ARGUMENT;
6880 }
6881}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306882#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306883
Gilles Peskineb8965192019-09-24 16:21:10 +02006884/** Check whether the given key type is acceptable for the given
6885 * input step of a key derivation.
6886 *
6887 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6888 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6889 * Both secret and non-secret inputs can alternatively have the type
6890 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6891 * that the input was passed as a buffer rather than via a key object.
6892 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006893static int psa_key_derivation_check_input_type(
6894 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006896{
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006898 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 if (key_type == PSA_KEY_TYPE_DERIVE) {
6900 return PSA_SUCCESS;
6901 }
6902 if (key_type == PSA_KEY_TYPE_NONE) {
6903 return PSA_SUCCESS;
6904 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006905 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006906 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (key_type == PSA_KEY_TYPE_DERIVE) {
6908 return PSA_SUCCESS;
6909 }
6910 if (key_type == PSA_KEY_TYPE_NONE) {
6911 return PSA_SUCCESS;
6912 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006913 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006914 case PSA_KEY_DERIVATION_INPUT_LABEL:
6915 case PSA_KEY_DERIVATION_INPUT_SALT:
6916 case PSA_KEY_DERIVATION_INPUT_INFO:
6917 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6919 return PSA_SUCCESS;
6920 }
6921 if (key_type == PSA_KEY_TYPE_NONE) {
6922 return PSA_SUCCESS;
6923 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006924 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306925 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6926 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6927 return PSA_SUCCESS;
6928 }
6929 if (key_type == PSA_KEY_TYPE_DERIVE) {
6930 return PSA_SUCCESS;
6931 }
6932 if (key_type == PSA_KEY_TYPE_NONE) {
6933 return PSA_SUCCESS;
6934 }
6935 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006936 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006938}
6939
Janos Follathb80a94e2019-06-12 15:54:46 +01006940static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006941 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006942 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006943 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006944 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006946{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006947 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 status = psa_key_derivation_check_input_type(step, key_type);
6951 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006954
Przemek Stekiel69c46792022-06-10 12:59:51 +02006955#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6957 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6958 step, data, data_length);
6959 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006960#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006961#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6963 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6964 step, data, data_length);
6965 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006966#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6967#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6969 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6970 step, data, data_length);
6971 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006972#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006973#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006975 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6977 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006978#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306979#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306980 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306981 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6982 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306983 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306984#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006985 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006986 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006987 (void) data;
6988 (void) data_length;
6989 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006991 }
6992
Gilles Peskine224b0d62019-09-23 18:13:17 +02006993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 if (status != PSA_SUCCESS) {
6995 psa_key_derivation_abort(operation);
6996 }
6997 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006998}
6999
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307000static psa_status_t psa_key_derivation_input_integer_internal(
7001 psa_key_derivation_operation_t *operation,
7002 psa_key_derivation_step_t step,
7003 uint64_t value)
7004{
7005 psa_status_t status;
7006 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7007
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307008#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307009 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307010 status = psa_pbkdf2_set_input_cost(
7011 &operation->ctx.pbkdf2, step, value);
7012 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307013#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307014 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307015 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307016 (void) value;
7017 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307018 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307019 }
7020
7021 if (status != PSA_SUCCESS) {
7022 psa_key_derivation_abort(operation);
7023 }
7024 return status;
7025}
7026
Janos Follath51f4a0f2019-06-14 11:35:55 +01007027psa_status_t psa_key_derivation_input_bytes(
7028 psa_key_derivation_operation_t *operation,
7029 psa_key_derivation_step_t step,
7030 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007032{
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 return psa_key_derivation_input_internal(operation, step,
7034 PSA_KEY_TYPE_NONE,
7035 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007036}
7037
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307038psa_status_t psa_key_derivation_input_integer(
7039 psa_key_derivation_operation_t *operation,
7040 psa_key_derivation_step_t step,
7041 uint64_t value)
7042{
7043 return psa_key_derivation_input_integer_internal(operation, step, value);
7044}
7045
Janos Follath51f4a0f2019-06-14 11:35:55 +01007046psa_status_t psa_key_derivation_input_key(
7047 psa_key_derivation_operation_t *operation,
7048 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007049 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007053 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007054
Ronald Cron5c522922020-11-14 16:35:34 +01007055 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7057 if (status != PSA_SUCCESS) {
7058 psa_key_derivation_abort(operation);
7059 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007060 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007061
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307062 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7063 * permission to output to a key object. */
7064 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7065 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007066 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 status = psa_key_derivation_input_internal(operation,
7070 step, slot->attr.type,
7071 slot->key.data,
7072 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007077}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007078
7079
7080
7081/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007082/* Key agreement */
7083/****************************************************************/
7084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7086 const uint8_t *key_buffer,
7087 size_t key_buffer_size,
7088 psa_algorithm_t alg,
7089 const uint8_t *peer_key,
7090 size_t peer_key_length,
7091 uint8_t *shared_secret,
7092 size_t shared_secret_size,
7093 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007094{
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007096#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007097 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7099 key_buffer_size, alg,
7100 peer_key, peer_key_length,
7101 shared_secret,
7102 shared_secret_size,
7103 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007104#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007105
7106#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7107 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007108 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007109 peer_key,
7110 peer_key_length,
7111 key_buffer,
7112 key_buffer_size,
7113 shared_secret,
7114 shared_secret_size,
7115 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007116#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7117
Gilles Peskine01d718c2018-09-18 12:01:02 +02007118 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007119 (void) attributes;
7120 (void) key_buffer;
7121 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007122 (void) peer_key;
7123 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007124 (void) shared_secret;
7125 (void) shared_secret_size;
7126 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007128 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007129}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007130
Aditya Deshpande17845b82022-10-13 17:21:01 +01007131/** Internal function for raw key agreement
7132 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007133 * to the driver's implementation if a driver is present.
7134 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007135 * (psa_key_agreement_raw_builtin).
7136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7138 psa_key_slot_t *private_key,
7139 const uint8_t *peer_key,
7140 size_t peer_key_length,
7141 uint8_t *shared_secret,
7142 size_t shared_secret_size,
7143 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007144{
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7146 return PSA_ERROR_NOT_SUPPORTED;
7147 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007148
7149 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007151 };
7152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 return psa_driver_wrapper_key_agreement(&attributes,
7154 private_key->key.data,
7155 private_key->key.bytes, alg,
7156 peer_key, peer_key_length,
7157 shared_secret,
7158 shared_secret_size,
7159 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007160}
7161
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007162/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007163 * to potentially free embedded data structures and wipe confidential data.
7164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007165static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7166 psa_key_derivation_step_t step,
7167 psa_key_slot_t *private_key,
7168 const uint8_t *peer_key,
7169 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007170{
7171 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007172 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007173 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007175
7176 /* Step 1: run the secret agreement algorithm to generate the shared
7177 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 status = psa_key_agreement_raw_internal(ka_alg,
7179 private_key,
7180 peer_key, peer_key_length,
7181 shared_secret,
7182 sizeof(shared_secret),
7183 &shared_secret_length);
7184 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007187
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007188 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007189 * the shared secret. A shared secret is permitted wherever a key
7190 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 status = psa_key_derivation_input_internal(operation, step,
7192 PSA_KEY_TYPE_DERIVE,
7193 shared_secret,
7194 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007195exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7197 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007198}
7199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7201 psa_key_derivation_step_t step,
7202 mbedtls_svc_key_id_t private_key,
7203 const uint8_t *peer_key,
7204 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007205{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007207 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007208 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007209
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7211 return PSA_ERROR_INVALID_ARGUMENT;
7212 }
Ronald Cron5c522922020-11-14 16:35:34 +01007213 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7215 if (status != PSA_SUCCESS) {
7216 return status;
7217 }
7218 status = psa_key_agreement_internal(operation, step,
7219 slot,
7220 peer_key, peer_key_length);
7221 if (status != PSA_SUCCESS) {
7222 psa_key_derivation_abort(operation);
7223 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007224 /* If a private key has been added as SECRET, we allow the derived
7225 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007227 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007229 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007234}
7235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7237 mbedtls_svc_key_id_t private_key,
7238 const uint8_t *peer_key,
7239 size_t peer_key_length,
7240 uint8_t *output,
7241 size_t output_size,
7242 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007243{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007245 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007246 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007247 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007250 status = PSA_ERROR_INVALID_ARGUMENT;
7251 goto exit;
7252 }
Ronald Cron5c522922020-11-14 16:35:34 +01007253 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7255 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007258
Gilles Peskine3e561302022-04-14 00:17:15 +02007259 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7260 * for the output size. The PSA specification only guarantees that this
7261 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7262 * but it might be nice to allow smaller buffers if the output fits.
7263 * At the time of writing this comment, with only ECDH implemented,
7264 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7265 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7266 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007267 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7269 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007270 status = PSA_ERROR_BUFFER_TOO_SMALL;
7271 goto exit;
7272 }
7273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 status = psa_key_agreement_raw_internal(alg, slot,
7275 peer_key, peer_key_length,
7276 output, output_size,
7277 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007278
7279exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007281 /* If an error happens and is not handled properly, the output
7282 * may be used as a key to protect sensitive data. Arrange for such
7283 * a key to be random, which is likely to result in decryption or
7284 * verification errors. This is better than filling the buffer with
7285 * some constant data such as zeros, which would result in the data
7286 * being protected with a reproducible, easily knowable key.
7287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007289 *output_length = output_size;
7290 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007291
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007293
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007295}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007296
7297
Gilles Peskine30524eb2020-11-13 17:02:26 +01007298
Gilles Peskine86a440b2018-11-12 18:39:40 +01007299/****************************************************************/
7300/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007301/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007302
Gilles Peskine672a7712023-04-28 21:00:28 +02007303#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7304#include "entropy_poll.h"
7305#endif
7306
Gilles Peskine30524eb2020-11-13 17:02:26 +01007307/** Initialize the PSA random generator.
7308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007309static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007310{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007311#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007313#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7314
Gilles Peskine30524eb2020-11-13 17:02:26 +01007315 /* Set default configuration if
7316 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007318 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 }
7320 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007321 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007323
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007325#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7326 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7327 /* The PSA entropy injection feature depends on using NV seed as an entropy
7328 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 mbedtls_entropy_add_source(&rng->entropy,
7330 mbedtls_nv_seed_poll, NULL,
7331 MBEDTLS_ENTROPY_BLOCK_SIZE,
7332 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007333#endif
7334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007336#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007337}
7338
7339/** Deinitialize the PSA random generator.
7340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007341static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007342{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007343#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007345#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7347 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007348#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007349}
7350
7351/** Seed the PSA random generator.
7352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007353static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007354{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007355#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7356 /* Do nothing: the external RNG seeds itself. */
7357 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007359#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007360 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007361 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7362 drbg_seed, sizeof(drbg_seed) - 1);
7363 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007364#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007365}
7366
Gilles Peskine449bd832023-01-11 14:50:10 +01007367psa_status_t psa_generate_random(uint8_t *output,
7368 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007369{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007370 GUARD_MODULE_INITIALIZED;
7371
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007372#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7373
7374 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7376 output, output_size,
7377 &output_length);
7378 if (status != PSA_SUCCESS) {
7379 return status;
7380 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007381 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007382 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 if (output_length != output_size) {
7384 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7385 }
7386 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007387
7388#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7389
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007391 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7393 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7394 output_size);
7395 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7396 output, request_size);
7397 if (ret != 0) {
7398 return mbedtls_to_psa_error(ret);
7399 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007400 output_size -= request_size;
7401 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007404#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007405}
7406
Gilles Peskine8814fc42020-12-14 15:33:44 +01007407/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007408 *
7409 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7410 * `psa_generate_random(...)`. The state parameter is ignored since the
7411 * PSA API doesn't support passing an explicit state.
7412 *
7413 * In the non-external case, psa_generate_random() calls an
7414 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7415 * and semantics as mbedtls_psa_get_random(). As an optimization,
7416 * instead of doing this back-and-forth between the PSA API and the
7417 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7418 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007420#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7421int mbedtls_psa_get_random(void *p_rng,
7422 unsigned char *output,
7423 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007424{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007425 /* This function takes a pointer to the RNG state because that's what
7426 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7427 * its own state internally and doesn't let the caller access that state.
7428 * So we just ignore the state parameter, and in practice we'll pass
7429 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007430 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 psa_status_t status = psa_generate_random(output, output_size);
7432 if (status == PSA_SUCCESS) {
7433 return 0;
7434 } else {
7435 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7436 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007437}
7438#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7439
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007440#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007441psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7442 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007443{
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 if (global_data.initialized) {
7445 return PSA_ERROR_NOT_PERMITTED;
7446 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7449 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7450 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7451 return PSA_ERROR_INVALID_ARGUMENT;
7452 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007455}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007456#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007457
Ronald Cron3772afe2021-02-08 16:10:05 +01007458/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007459 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007460 * \param type The key type
7461 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462 *
7463 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007464 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465 * \retval #PSA_ERROR_INVALID_ARGUMENT
7466 * The size in bits of the key is not valid.
7467 * \retval #PSA_ERROR_NOT_SUPPORTED
7468 * The type and/or the size in bits of the key or the combination of
7469 * the two is not supported.
7470 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007471static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007473{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 if (key_type_is_raw_bytes(type)) {
7477 status = psa_validate_unstructured_key_bit_size(type, bits);
7478 if (status != PSA_SUCCESS) {
7479 return status;
7480 }
7481 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007482#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7484 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7485 return PSA_ERROR_NOT_SUPPORTED;
7486 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007487 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007488 return PSA_ERROR_NOT_SUPPORTED;
7489 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007490
Ronald Cron01b2aba2020-10-05 09:42:02 +02007491 /* Accept only byte-aligned keys, for the same reasons as
7492 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (bits % 8 != 0) {
7494 return PSA_ERROR_NOT_SUPPORTED;
7495 }
7496 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007497#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007498
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007499#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007501 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 return PSA_SUCCESS;
7503 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007504#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007505
Valerio Settia55f0422023-07-10 15:34:41 +02007506#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007507 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007508 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007509 return PSA_ERROR_NOT_SUPPORTED;
7510 }
7511 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007512#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007513 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007515 }
7516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007518}
7519
Ronald Cron55ed0592020-10-05 10:30:40 +02007520psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007521 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007523{
7524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007525 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if ((attributes->domain_parameters == NULL) &&
7528 (attributes->domain_parameters_size != 0)) {
7529 return PSA_ERROR_INVALID_ARGUMENT;
7530 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (key_type_is_raw_bytes(type)) {
7533 status = psa_generate_random(key_buffer, key_buffer_size);
7534 if (status != PSA_SUCCESS) {
7535 return status;
7536 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007537
David Brown12ca5032021-02-11 11:02:00 -07007538#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 if (type == PSA_KEY_TYPE_DES) {
7540 psa_des_set_key_parity(key_buffer, key_buffer_size);
7541 }
David Brown8107e312021-02-12 08:08:46 -07007542#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007544
Valerio Setti76df8c12023-07-11 14:11:28 +02007545#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7547 return mbedtls_psa_rsa_generate_key(attributes,
7548 key_buffer,
7549 key_buffer_size,
7550 key_buffer_length);
7551 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007552#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007553
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007554#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7556 return mbedtls_psa_ecp_generate_key(attributes,
7557 key_buffer,
7558 key_buffer_size,
7559 key_buffer_length);
7560 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007561#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007562
Valerio Settia55f0422023-07-10 15:34:41 +02007563#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007564 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7565 return mbedtls_psa_ffdh_generate_key(attributes,
7566 key_buffer,
7567 key_buffer_size,
7568 key_buffer_length);
7569 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007570#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007571 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 (void) key_buffer_length;
7573 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007574 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007577}
Darryl Green0c6575a2018-11-07 16:05:30 +00007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7580 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007581{
7582 psa_status_t status;
7583 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007584 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007585 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007586
Ronald Cron81709fc2020-11-14 12:10:32 +01007587 *key = MBEDTLS_SVC_KEY_ID_INIT;
7588
Gilles Peskine0f84d622019-09-12 19:03:13 +02007589 /* Reject any attempt to create a zero-length key so that we don't
7590 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (psa_get_key_bits(attributes) == 0) {
7592 return PSA_ERROR_INVALID_ARGUMENT;
7593 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007594
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007595 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7597 return PSA_ERROR_INVALID_ARGUMENT;
7598 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007599
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7601 &slot, &driver);
7602 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 }
Gilles Peskine11792082019-08-06 18:36:36 +02007605
Ronald Cron977c2472020-10-13 08:32:21 +02007606 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307607 * storage ( thus not in the case of generating a key in a secure element
7608 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7609 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (slot->key.data == NULL) {
7611 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7612 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007613 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 attributes->core.type, attributes->core.bits);
7615 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007618
7619 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 attributes->core.type,
7621 attributes->core.bits);
7622 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007623 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 attributes, &key_buffer_size);
7625 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 }
Ronald Cron977c2472020-10-13 08:32:21 +02007628 }
Ronald Cron977c2472020-10-13 08:32:21 +02007629
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7631 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 }
Ronald Cron977c2472020-10-13 08:32:21 +02007634 }
7635
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 status = psa_driver_wrapper_generate_key(attributes,
7637 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 if (status != PSA_SUCCESS) {
7640 psa_remove_key_data_from_memory(slot);
7641 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007642
Gilles Peskine11792082019-08-06 18:36:36 +02007643exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 if (status == PSA_SUCCESS) {
7645 status = psa_finish_key_creation(slot, driver, key);
7646 }
7647 if (status != PSA_SUCCESS) {
7648 psa_fail_key_creation(slot, driver);
7649 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007652}
7653
Gilles Peskinee59236f2018-01-27 23:32:46 +01007654/****************************************************************/
7655/* Module setup */
7656/****************************************************************/
7657
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007658#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007659psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 void (* entropy_init)(mbedtls_entropy_context *ctx),
7661 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007662{
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7664 return PSA_ERROR_BAD_STATE;
7665 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007666 global_data.rng.entropy_init = entropy_init;
7667 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007669}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007670#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007671
Gilles Peskine449bd832023-01-11 14:50:10 +01007672void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007673{
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 psa_wipe_all_key_slots();
7675 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7676 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007677 }
7678 /* Wipe all remaining data, including configuration.
7679 * In particular, this sets all state indicator to the value
7680 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007682
7683 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007685}
7686
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007687#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7688/** Recover a transaction that was interrupted by a power failure.
7689 *
7690 * This function is called during initialization, before psa_crypto_init()
7691 * returns. If this function returns a failure status, the initialization
7692 * fails.
7693 */
7694static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007696{
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007698 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7699 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 /* TODO - fall through to the failure case until this
7701 * is implemented.
7702 * https://github.com/ARMmbed/mbed-crypto/issues/218
7703 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007704 default:
7705 /* We found an unsupported transaction in the storage.
7706 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007708 }
7709}
7710#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007713{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007714 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007715
Gilles Peskinec6b69072018-11-20 21:42:52 +01007716 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 if (global_data.initialized != 0) {
7718 return PSA_SUCCESS;
7719 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007720
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007721 /* Init drivers */
7722 status = psa_driver_wrapper_init();
7723 if (status != PSA_SUCCESS) {
7724 goto exit;
7725 }
7726 global_data.drivers_initialized = 1;
7727
Gilles Peskine30524eb2020-11-13 17:02:26 +01007728 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007730 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 status = mbedtls_psa_random_seed(&global_data.rng);
7732 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007735 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 status = psa_initialize_key_slots();
7738 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007739 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007741
Gilles Peskine4b734222019-07-24 15:56:31 +02007742#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 status = psa_crypto_load_transaction();
7744 if (status == PSA_SUCCESS) {
7745 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7746 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007747 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 }
7749 status = psa_crypto_stop_transaction();
7750 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007751 /* There's no transaction to complete. It's all good. */
7752 status = PSA_SUCCESS;
7753 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007754#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007755
Gilles Peskinec6b69072018-11-20 21:42:52 +01007756 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007757 global_data.initialized = 1;
7758
7759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 if (status != PSA_SUCCESS) {
7761 mbedtls_psa_crypto_free();
7762 }
7763 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007764}
7765
Yanray Wangffc3c482023-07-11 12:01:04 +08007766#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007767psa_status_t psa_crypto_driver_pake_get_password_len(
7768 const psa_crypto_driver_pake_inputs_t *inputs,
7769 size_t *password_len)
7770{
7771 if (inputs->password_len == 0) {
7772 return PSA_ERROR_BAD_STATE;
7773 }
7774
7775 *password_len = inputs->password_len;
7776
7777 return PSA_SUCCESS;
7778}
7779
7780psa_status_t psa_crypto_driver_pake_get_password(
7781 const psa_crypto_driver_pake_inputs_t *inputs,
7782 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7783{
7784 if (inputs->password_len == 0) {
7785 return PSA_ERROR_BAD_STATE;
7786 }
7787
7788 if (buffer_size < inputs->password_len) {
7789 return PSA_ERROR_BUFFER_TOO_SMALL;
7790 }
7791
7792 memcpy(buffer, inputs->password, inputs->password_len);
7793 *buffer_length = inputs->password_len;
7794
7795 return PSA_SUCCESS;
7796}
7797
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007798psa_status_t psa_crypto_driver_pake_get_user_len(
7799 const psa_crypto_driver_pake_inputs_t *inputs,
7800 size_t *user_len)
7801{
7802 if (inputs->user_len == 0) {
7803 return PSA_ERROR_BAD_STATE;
7804 }
7805
7806 *user_len = inputs->user_len;
7807
7808 return PSA_SUCCESS;
7809}
7810
7811psa_status_t psa_crypto_driver_pake_get_user(
7812 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007813 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007814{
7815 if (inputs->user_len == 0) {
7816 return PSA_ERROR_BAD_STATE;
7817 }
7818
Przemek Stekielc0e62502023-03-14 11:49:36 +01007819 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007820 return PSA_ERROR_BUFFER_TOO_SMALL;
7821 }
7822
Przemek Stekielc0e62502023-03-14 11:49:36 +01007823 memcpy(user_id, inputs->user, inputs->user_len);
7824 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007825
7826 return PSA_SUCCESS;
7827}
7828
7829psa_status_t psa_crypto_driver_pake_get_peer_len(
7830 const psa_crypto_driver_pake_inputs_t *inputs,
7831 size_t *peer_len)
7832{
7833 if (inputs->peer_len == 0) {
7834 return PSA_ERROR_BAD_STATE;
7835 }
7836
7837 *peer_len = inputs->peer_len;
7838
7839 return PSA_SUCCESS;
7840}
7841
7842psa_status_t psa_crypto_driver_pake_get_peer(
7843 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007844 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007845{
7846 if (inputs->peer_len == 0) {
7847 return PSA_ERROR_BAD_STATE;
7848 }
7849
Przemek Stekielc0e62502023-03-14 11:49:36 +01007850 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007851 return PSA_ERROR_BUFFER_TOO_SMALL;
7852 }
7853
Przemek Stekielc0e62502023-03-14 11:49:36 +01007854 memcpy(peer_id, inputs->peer, inputs->peer_len);
7855 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007856
7857 return PSA_SUCCESS;
7858}
7859
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007860psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7861 const psa_crypto_driver_pake_inputs_t *inputs,
7862 psa_pake_cipher_suite_t *cipher_suite)
7863{
7864 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7865 return PSA_ERROR_BAD_STATE;
7866 }
7867
7868 *cipher_suite = inputs->cipher_suite;
7869
7870 return PSA_SUCCESS;
7871}
7872
Neil Armstronga7d08c32022-06-01 18:21:20 +02007873psa_status_t psa_pake_setup(
7874 psa_pake_operation_t *operation,
7875 const psa_pake_cipher_suite_t *cipher_suite)
7876{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007878
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007879 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007880 status = PSA_ERROR_BAD_STATE;
7881 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007882 }
7883
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007884 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007885 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007886 status = PSA_ERROR_INVALID_ARGUMENT;
7887 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007888 }
7889
Przemek Stekiel51eac532022-12-07 11:04:51 +01007890 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7891
Przemek Stekiele12ed362022-12-21 12:54:46 +01007892 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007893 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7894 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 operation->data.inputs.cipher_suite = *cipher_suite;
7896
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007897#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007898 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007899 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007900 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007901
David Horstmann16f01512023-06-14 17:21:07 +01007902 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007903 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007904 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007905#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007906 {
7907 status = PSA_ERROR_NOT_SUPPORTED;
7908 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007909 }
7910
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007911 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7912
Przemek Stekiel51eac532022-12-07 11:04:51 +01007913 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007914exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007915 psa_pake_abort(operation);
7916 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007917}
7918
7919psa_status_t psa_pake_set_password_key(
7920 psa_pake_operation_t *operation,
7921 mbedtls_svc_key_id_t password)
7922{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007924 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7925 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007926 psa_key_attributes_t attributes;
7927 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007928
Przemek Stekiel51eac532022-12-07 11:04:51 +01007929 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007930 status = PSA_ERROR_BAD_STATE;
7931 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007932 }
7933
Przemek Stekiel6c764412022-11-22 14:05:12 +01007934 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7935 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007936 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007937 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007938 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007939 }
7940
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007941 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007942 .core = slot->attr
7943 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007944
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007945 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007946
Przemek Stekiel51eac532022-12-07 11:04:51 +01007947 if (type != PSA_KEY_TYPE_PASSWORD &&
7948 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7949 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007950 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007951 }
7952
Przemek Stekiel51eac532022-12-07 11:04:51 +01007953 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7954 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007955 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007956 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007957 }
7958
7959 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7960 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007961 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007963 if (status != PSA_SUCCESS) {
7964 psa_pake_abort(operation);
7965 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007966 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007967 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007968}
7969
7970psa_status_t psa_pake_set_user(
7971 psa_pake_operation_t *operation,
7972 const uint8_t *user_id,
7973 size_t user_id_len)
7974{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007975 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007976
7977 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007978 status = PSA_ERROR_BAD_STATE;
7979 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007980 }
7981
Przemek Stekiel51eac532022-12-07 11:04:51 +01007982 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007983 status = PSA_ERROR_INVALID_ARGUMENT;
7984 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007985 }
7986
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007987 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007988 status = PSA_ERROR_BAD_STATE;
7989 goto exit;
7990 }
7991
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007992 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7993 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007994 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7995 goto exit;
7996 }
7997
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007998 memcpy(operation->data.inputs.user, user_id, user_id_len);
7999 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008000
8001 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008002exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008003 psa_pake_abort(operation);
8004 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008005}
8006
8007psa_status_t psa_pake_set_peer(
8008 psa_pake_operation_t *operation,
8009 const uint8_t *peer_id,
8010 size_t peer_id_len)
8011{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008012 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008013
8014 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008015 status = PSA_ERROR_BAD_STATE;
8016 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008017 }
8018
Przemek Stekiel51eac532022-12-07 11:04:51 +01008019 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008020 status = PSA_ERROR_INVALID_ARGUMENT;
8021 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008022 }
8023
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008024 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008025 status = PSA_ERROR_BAD_STATE;
8026 goto exit;
8027 }
8028
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008029 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8030 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008031 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8032 goto exit;
8033 }
8034
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008035 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8036 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008037
8038 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008039exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008040 psa_pake_abort(operation);
8041 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008042}
8043
8044psa_status_t psa_pake_set_role(
8045 psa_pake_operation_t *operation,
8046 psa_pake_role_t role)
8047{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008049
Przemek Stekiel51eac532022-12-07 11:04:51 +01008050 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008051 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008052 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008053 }
8054
Przemek Stekiel09104b82023-03-06 14:11:51 +01008055 switch (operation->alg) {
8056#if defined(PSA_WANT_ALG_JPAKE)
8057 case PSA_ALG_JPAKE:
8058 if (role == PSA_PAKE_ROLE_NONE) {
8059 return PSA_SUCCESS;
8060 }
8061 status = PSA_ERROR_INVALID_ARGUMENT;
8062 break;
8063#endif
8064 default:
8065 (void) role;
8066 status = PSA_ERROR_NOT_SUPPORTED;
8067 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008068 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008069exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008070 psa_pake_abort(operation);
8071 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008072}
8073
David Horstmanne7f21e62023-05-12 18:17:21 +01008074/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008075#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008076static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008077 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008078{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008079 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008080 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008081 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008082
David Horstmann024e5c52023-06-14 15:48:21 +01008083 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 is_x1 = (stage->outputs < 1);
8085 } else {
8086 is_x1 = (stage->inputs < 1);
8087 }
8088
David Horstmann74a3d8c2023-06-14 18:28:19 +01008089 key_share_step = is_x1 ?
8090 PSA_JPAKE_X1_STEP_KEY_SHARE :
8091 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008092 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008093 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8094 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8095 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8096 } else {
8097 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008098 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008099 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008100}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008101#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008102
Przemek Stekiel2797d372022-12-22 11:19:22 +01008103static psa_status_t psa_pake_complete_inputs(
8104 psa_pake_operation_t *operation)
8105{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008107 /* Create copy of the inputs on stack as inputs share memory
8108 with the driver context which will be setup by the driver. */
8109 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008110
Przemek Stekielfde11282023-03-13 16:06:09 +01008111 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008112 return PSA_ERROR_BAD_STATE;
8113 }
8114
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008115 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008116 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8117 return PSA_ERROR_BAD_STATE;
8118 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008119 }
8120
Przemek Stekiel18620a32023-01-17 16:34:52 +01008121 /* Clear driver context */
8122 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8123
8124 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008125
8126 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008127 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008128
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008129 /* User and peer are translated to role. */
8130 mbedtls_free(inputs.user);
8131 mbedtls_free(inputs.peer);
8132
Przemek Stekiel2797d372022-12-22 11:19:22 +01008133 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008134#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008136 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008137 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008138#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008139 {
8140 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008141 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008143 return status;
8144}
8145
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008146#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008147static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008149 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008150 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008152 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8153 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8154 step != PSA_PAKE_STEP_ZK_PROOF) {
8155 return PSA_ERROR_INVALID_ARGUMENT;
8156 }
8157
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008158 psa_jpake_computation_stage_t *computation_stage =
8159 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008160
David Horstmann5da95602023-06-08 15:37:12 +01008161 if (computation_stage->round != PSA_JPAKE_FIRST &&
8162 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008163 return PSA_ERROR_BAD_STATE;
8164 }
8165
David Horstmanne7f21e62023-05-12 18:17:21 +01008166 /* Check that the step we are given is the one we were expecting */
8167 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008168 return PSA_ERROR_BAD_STATE;
8169 }
8170
David Horstmanne7f21e62023-05-12 18:17:21 +01008171 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8172 computation_stage->inputs == 0 &&
8173 computation_stage->outputs == 0) {
8174 /* Start of the round, so function decides whether we are inputting
8175 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008176 computation_stage->io_mode = io_mode;
8177 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008178 /* Middle of the round so the mode we are in must match the function
8179 * called by the user */
8180 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008181 }
8182
8183 return PSA_SUCCESS;
8184}
8185
David Horstmanne7f21e62023-05-12 18:17:21 +01008186static psa_status_t psa_jpake_epilogue(
8187 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008188 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008189{
David Horstmanne7f21e62023-05-12 18:17:21 +01008190 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008191 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008192
David Horstmanne7f21e62023-05-12 18:17:21 +01008193 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8194 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008195 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008196 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008197 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008198 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008199 }
8200 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008201 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008202 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008203 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008204 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008205 }
8206 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008207 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8208 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 /* End of a round, move to the next round */
8210 stage->inputs = 0;
8211 stage->outputs = 0;
8212 stage->round++;
8213 }
8214 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008215 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008216 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008218 return PSA_SUCCESS;
8219}
David Horstmanne7f21e62023-05-12 18:17:21 +01008220
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008221#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008222
Neil Armstronga7d08c32022-06-01 18:21:20 +02008223psa_status_t psa_pake_output(
8224 psa_pake_operation_t *operation,
8225 psa_pake_step_t step,
8226 uint8_t *output,
8227 size_t output_size,
8228 size_t *output_length)
8229{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008231 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008232 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008233
8234 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008235 status = psa_pake_complete_inputs(operation);
8236 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008237 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008238 }
8239 }
8240
8241 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008242 status = PSA_ERROR_BAD_STATE;
8243 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008244 }
8245
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008246 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008247 status = PSA_ERROR_INVALID_ARGUMENT;
8248 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008249 }
8250
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008252#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008253 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008254 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008255 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008256 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008257 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008258 driver_step = convert_jpake_computation_stage_to_driver_step(
8259 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008261#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008262 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008263 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 status = PSA_ERROR_NOT_SUPPORTED;
8265 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008266 }
8267
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008268 status = psa_driver_wrapper_pake_output(operation, driver_step,
8269 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008270
8271 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008272 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 }
8274
8275 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008276#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008278 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008279 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008280 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008281 }
8282 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008283#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008285 status = PSA_ERROR_NOT_SUPPORTED;
8286 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008287 }
8288
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008289 return PSA_SUCCESS;
8290exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008291 psa_pake_abort(operation);
8292 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008293}
8294
8295psa_status_t psa_pake_input(
8296 psa_pake_operation_t *operation,
8297 psa_pake_step_t step,
8298 const uint8_t *input,
8299 size_t input_length)
8300{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008302 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008303 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8304 operation->primitive,
8305 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008306
8307 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008308 status = psa_pake_complete_inputs(operation);
8309 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008310 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008311 }
8312 }
8313
8314 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008315 status = PSA_ERROR_BAD_STATE;
8316 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008317 }
8318
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008319 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008320 status = PSA_ERROR_INVALID_ARGUMENT;
8321 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008322 }
8323
Przemek Stekiele12ed362022-12-21 12:54:46 +01008324 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008325#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008326 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008327 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008328 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008329 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008331 driver_step = convert_jpake_computation_stage_to_driver_step(
8332 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008334#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008336 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008337 status = PSA_ERROR_NOT_SUPPORTED;
8338 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008339 }
8340
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008341 status = psa_driver_wrapper_pake_input(operation, driver_step,
8342 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008343
8344 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008345 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 }
8347
8348 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008349#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008351 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008352 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008353 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008354 }
8355 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008356#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008358 status = PSA_ERROR_NOT_SUPPORTED;
8359 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008360 }
8361
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008362 return PSA_SUCCESS;
8363exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008364 psa_pake_abort(operation);
8365 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008366}
8367
8368psa_status_t psa_pake_get_implicit_key(
8369 psa_pake_operation_t *operation,
8370 psa_key_derivation_operation_t *output)
8371{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008373 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008374 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008375 size_t shared_key_len = 0;
8376
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008377 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008378 status = PSA_ERROR_BAD_STATE;
8379 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008380 }
8381
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008382#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008383 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008384 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008385 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008386 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008387 status = PSA_ERROR_BAD_STATE;
8388 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008389 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008390 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008391#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008392 {
8393 status = PSA_ERROR_NOT_SUPPORTED;
8394 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008395 }
8396
Przemek Stekiel0c781802022-11-29 14:53:13 +01008397 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8398 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008399 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008400 &shared_key_len);
8401
8402 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008403 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008404 }
8405
8406 status = psa_key_derivation_input_bytes(output,
8407 PSA_KEY_DERIVATION_INPUT_SECRET,
8408 shared_key,
8409 shared_key_len);
8410
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008411 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008412exit:
8413 abort_status = psa_pake_abort(operation);
8414 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008415}
8416
8417psa_status_t psa_pake_abort(
8418 psa_pake_operation_t *operation)
8419{
Przemek Stekield69dca92023-01-31 19:59:20 +01008420 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008421
Przemek Stekield69dca92023-01-31 19:59:20 +01008422 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008423 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008424 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008425
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008426 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8427 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008428 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008429 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008430 }
8431 if (operation->data.inputs.user != NULL) {
8432 mbedtls_free(operation->data.inputs.user);
8433 }
8434 if (operation->data.inputs.peer != NULL) {
8435 mbedtls_free(operation->data.inputs.peer);
8436 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008437 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008438 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008439
Przemek Stekield69dca92023-01-31 19:59:20 +01008440 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008441}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008442#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008443
David Horstmannfde97392023-10-30 20:29:43 +00008444
David Horstmann1b7279a2023-11-15 15:18:30 +00008445/** Copy from an input buffer to a local copy.
8446 *
8447 * \param[in] input Pointer to input buffer.
8448 * \param[in] input_len Length of the input buffer.
8449 * \param[out] input_copy Pointer to a local copy in which to store the input data.
8450 * \param[out] input_copy_len Length of the local copy buffer.
8451 * \return #PSA_SUCCESS, if the buffer was successfully
8452 * copied.
8453 * \return #PSA_ERROR_CORRUPTION_DETECTED, if the local
8454 * copy is too small to hold contents of the
8455 * input buffer.
8456 */
8457MBEDTLS_STATIC_TESTABLE
David Horstmannfde97392023-10-30 20:29:43 +00008458psa_status_t psa_crypto_copy_input(const uint8_t *input, size_t input_len,
8459 uint8_t *input_copy, size_t input_copy_len)
8460{
8461 if (input_len > input_copy_len) {
David Horstmann49a72762023-11-03 19:51:40 +00008462 return PSA_ERROR_CORRUPTION_DETECTED;
David Horstmannfde97392023-10-30 20:29:43 +00008463 }
8464
David Horstmann777e7412023-11-15 17:33:47 +00008465 if (input_len > 0) {
8466 memcpy(input_copy, input, input_len);
8467 }
David Horstmannfde97392023-10-30 20:29:43 +00008468
8469 return PSA_SUCCESS;
8470}
8471
David Horstmann1b7279a2023-11-15 15:18:30 +00008472/** Copy from a local output buffer into a user-supplied one.
8473 *
8474 * \param[in] output_copy Pointer to a local buffer containing the output.
8475 * \param[in] output_copy_len Length of the local buffer.
8476 * \param[out] output Pointer to user-supplied output buffer.
8477 * \param[out] output_len Length of the user-supplied output buffer.
8478 * \return #PSA_SUCCESS, if the buffer was successfully
8479 * copied.
David Horstmann671f5f52023-11-20 12:39:38 +00008480 * \return #PSA_ERROR_BUFFER_TOO_SMALL, if the
David Horstmann1b7279a2023-11-15 15:18:30 +00008481 * user-supplied output buffer is too small to
8482 * hold the contents of the local buffer.
8483 */
8484MBEDTLS_STATIC_TESTABLE
David Horstmann8978f5c2023-10-30 20:37:12 +00008485psa_status_t psa_crypto_copy_output(const uint8_t *output_copy, size_t output_copy_len,
8486 uint8_t *output, size_t output_len)
8487{
8488 if (output_len < output_copy_len) {
David Horstmann671f5f52023-11-20 12:39:38 +00008489 return PSA_ERROR_BUFFER_TOO_SMALL;
David Horstmann8978f5c2023-10-30 20:37:12 +00008490 }
David Horstmann777e7412023-11-15 17:33:47 +00008491
8492 if (output_copy_len > 0) {
8493 memcpy(output, output_copy, output_copy_len);
8494 }
8495
David Horstmann8978f5c2023-10-30 20:37:12 +00008496 return PSA_SUCCESS;
8497}
8498
David Horstmannf1734052023-11-20 17:15:32 +00008499psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
8500 psa_crypto_local_input_t *local_input)
David Horstmann4ac78852023-11-07 16:36:48 +00008501{
8502 psa_status_t status;
8503
David Horstmannf1734052023-11-20 17:15:32 +00008504 local_input->buffer = NULL;
8505 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008506
David Horstmannc5cc1c32023-11-15 18:11:26 +00008507 if (input_len == 0) {
David Horstmann4ac78852023-11-07 16:36:48 +00008508 return PSA_SUCCESS;
8509 }
8510
David Horstmannf1734052023-11-20 17:15:32 +00008511 local_input->buffer = mbedtls_calloc(input_len, 1);
8512 if (local_input->buffer == NULL) {
David Horstmann4ac78852023-11-07 16:36:48 +00008513 /* Since we dealt with the zero-length case above, we know that
8514 * a NULL return value means a failure of allocation. */
8515 return PSA_ERROR_INSUFFICIENT_MEMORY;
8516 }
David Horstmannf1734052023-11-20 17:15:32 +00008517 /* From now on, we must free local_input->buffer on error. */
David Horstmann4ac78852023-11-07 16:36:48 +00008518
David Horstmannf1734052023-11-20 17:15:32 +00008519 local_input->length = input_len;
David Horstmann4ac78852023-11-07 16:36:48 +00008520
8521 status = psa_crypto_copy_input(input, input_len,
David Horstmannf1734052023-11-20 17:15:32 +00008522 local_input->buffer, local_input->length);
David Horstmann4ac78852023-11-07 16:36:48 +00008523 if (status != PSA_SUCCESS) {
8524 goto error;
8525 }
8526
8527 return PSA_SUCCESS;
8528
8529error:
David Horstmannf1734052023-11-20 17:15:32 +00008530 mbedtls_free(local_input->buffer);
8531 local_input->buffer = NULL;
8532 local_input->length = 0;
David Horstmann4ac78852023-11-07 16:36:48 +00008533 return status;
8534}
8535
David Horstmannf1734052023-11-20 17:15:32 +00008536void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
David Horstmanne6042ff2023-11-07 18:00:41 +00008537{
David Horstmannf1734052023-11-20 17:15:32 +00008538 mbedtls_free(local_input->buffer);
8539 local_input->buffer = NULL;
8540 local_input->length = 0;
David Horstmanne6042ff2023-11-07 18:00:41 +00008541}
8542
David Horstmannba3c7d62023-11-08 15:19:43 +00008543psa_status_t psa_crypto_output_copy_alloc(uint8_t *output, size_t output_len,
8544 psa_crypto_output_copy_t *output_copy)
8545{
8546 output_copy->original = NULL;
8547 output_copy->buffer = NULL;
David Horstmann2f307b42023-11-20 12:54:09 +00008548 output_copy->length = 0;
David Horstmannba3c7d62023-11-08 15:19:43 +00008549
David Horstmannc5cc1c32023-11-15 18:11:26 +00008550 if (output_len == 0) {
David Horstmannba3c7d62023-11-08 15:19:43 +00008551 return PSA_SUCCESS;
8552 }
8553 output_copy->buffer = mbedtls_calloc(output_len, 1);
8554 if (output_copy->buffer == NULL) {
8555 /* Since we dealt with the zero-length case above, we know that
8556 * a NULL return value means a failure of allocation. */
8557 return PSA_ERROR_INSUFFICIENT_MEMORY;
8558 }
David Horstmann2f307b42023-11-20 12:54:09 +00008559 output_copy->length = output_len;
David Horstmannba3c7d62023-11-08 15:19:43 +00008560 output_copy->original = output;
8561
8562 return PSA_SUCCESS;
8563}
8564
David Horstmann9467ea32023-11-08 17:44:18 +00008565psa_status_t psa_crypto_output_copy_free(psa_crypto_output_copy_t *output_copy)
8566{
David Horstmannc335a4e2023-11-15 15:57:32 +00008567 psa_status_t status;
8568
David Horstmann9467ea32023-11-08 17:44:18 +00008569 if (output_copy->buffer == NULL) {
David Horstmann2f307b42023-11-20 12:54:09 +00008570 output_copy->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008571 return PSA_SUCCESS;
8572 }
8573 if (output_copy->original == NULL) {
8574 /* We have an internal copy but nothing to copy back to. */
8575 return PSA_ERROR_CORRUPTION_DETECTED;
8576 }
8577
David Horstmann2f307b42023-11-20 12:54:09 +00008578 status = psa_crypto_copy_output(output_copy->buffer, output_copy->length,
8579 output_copy->original, output_copy->length);
David Horstmannc335a4e2023-11-15 15:57:32 +00008580 if (status != PSA_SUCCESS) {
8581 return status;
8582 }
David Horstmann9467ea32023-11-08 17:44:18 +00008583
8584 mbedtls_free(output_copy->buffer);
8585 output_copy->buffer = NULL;
David Horstmann2f307b42023-11-20 12:54:09 +00008586 output_copy->length = 0;
David Horstmann9467ea32023-11-08 17:44:18 +00008587
8588 return PSA_SUCCESS;
8589}
8590
Gilles Peskinee59236f2018-01-27 23:32:46 +01008591#endif /* MBEDTLS_PSA_CRYPTO_C */