blob: 0bb9e8e650c528d3c7c8f2d92daa961b670411b2 [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;
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003451 psa_status_t numops_status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003452
3453 *signature_length = 0;
3454
Paul Elliottc9774412023-02-06 15:14:07 +00003455 /* Check that start has been called first, and that operation has not
3456 * previously errored. */
3457 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458 status = PSA_ERROR_BAD_STATE;
3459 goto exit;
3460 }
3461
Paul Elliott096abc42023-02-03 18:33:23 +00003462 /* Immediately reject a zero-length signature buffer. This guarantees that
3463 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464 if (signature_size == 0) {
3465 status = PSA_ERROR_BUFFER_TOO_SMALL;
3466 goto exit;
3467 }
3468
3469 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3470 signature_size,
3471 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliott296ede92022-12-15 17:00:30 +00003473 /* Update ops count with work done. */
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003474 numops_status = psa_driver_wrapper_sign_hash_get_num_ops(operation, &operation->num_ops);
Dave Rodgman321c7e92023-09-28 18:07:06 +01003475 if (status == PSA_SUCCESS) {
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003476 status = numops_status;
Dave Rodgman321c7e92023-09-28 18:07:06 +01003477 }
Paul Elliott296ede92022-12-15 17:00:30 +00003478
Paul Elliottebe225c2023-02-10 14:32:53 +00003479exit:
3480
Paul Elliott59200a22023-02-21 15:07:40 +00003481 psa_wipe_tag_output_buffer(signature, status, signature_size,
3482 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003483
Paul Elliott53bb3122023-02-10 14:22:22 +00003484 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003485 if (status != PSA_SUCCESS) {
3486 operation->error_occurred = 1;
3487 }
3488
Paul Elliott59ad9452022-12-18 15:09:02 +00003489 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003490 }
3491
3492 return status;
3493}
3494
3495psa_status_t psa_sign_hash_abort(
3496 psa_sign_hash_interruptible_operation_t *operation)
3497{
Paul Elliott59ad9452022-12-18 15:09:02 +00003498 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3499
3500 status = psa_sign_hash_abort_internal(operation);
3501
3502 /* We clear the number of ops done here, so that it is not cleared when
3503 * the operation fails or succeeds, only on manual abort. */
3504 operation->num_ops = 0;
3505
Paul Elliottc9774412023-02-06 15:14:07 +00003506 /* Likewise, failure state. */
3507 operation->error_occurred = 0;
3508
Paul Elliott59ad9452022-12-18 15:09:02 +00003509 return status;
3510}
3511
3512static psa_status_t psa_verify_hash_abort_internal(
3513 psa_verify_hash_interruptible_operation_t *operation)
3514{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003515 if (operation->id == 0) {
3516 /* The object has (apparently) been initialized but it is not (yet)
3517 * in use. It's ok to call abort on such an object, and there's
3518 * nothing to do. */
3519 return PSA_SUCCESS;
3520 }
3521
Paul Elliott59ad9452022-12-18 15:09:02 +00003522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3523
3524 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003525
3526 operation->id = 0;
3527
Paul Elliotte6145dc2023-02-07 12:51:21 +00003528 /* Do not clear either the error_occurred or num_ops elements here as they
3529 * only want to be cleared by the application calling abort, not by abort
3530 * being called at completion of an operation. */
3531
Paul Elliott59ad9452022-12-18 15:09:02 +00003532 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003533}
3534
3535psa_status_t psa_verify_hash_start(
3536 psa_verify_hash_interruptible_operation_t *operation,
3537 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3538 const uint8_t *hash, size_t hash_length,
3539 const uint8_t *signature, size_t signature_length)
3540{
3541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3542 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3543 psa_key_slot_t *slot;
3544
Paul Elliottc9774412023-02-06 15:14:07 +00003545 /* Check that start has not been previously called, or operation has not
3546 * previously errored. */
3547 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003548 return PSA_ERROR_BAD_STATE;
3549 }
3550
3551 status = psa_sign_verify_check_alg(0, alg);
3552 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003553 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003554 return status;
3555 }
3556
3557 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3558 PSA_KEY_USAGE_VERIFY_HASH,
3559 alg);
3560
3561 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003562 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003563 return status;
3564 }
3565
3566 psa_key_attributes_t attributes = {
3567 .core = slot->attr
3568 };
3569
Paul Elliott296ede92022-12-15 17:00:30 +00003570 /* Ensure ops count gets reset, in case of operation re-use. */
3571 operation->num_ops = 0;
3572
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3574 slot->key.data,
3575 slot->key.bytes,
3576 alg, hash, hash_length,
3577 signature, signature_length);
3578
3579 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003580 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003581 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003582 }
3583
3584 unlock_status = psa_unlock_key_slot(slot);
3585
Paul Elliottc9774412023-02-06 15:14:07 +00003586 if (unlock_status != PSA_SUCCESS) {
3587 operation->error_occurred = 1;
3588 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003589
Paul Elliottc9774412023-02-06 15:14:07 +00003590 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003591}
3592
3593psa_status_t psa_verify_hash_complete(
3594 psa_verify_hash_interruptible_operation_t *operation)
3595{
3596 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003597 psa_status_t numops_status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598
Paul Elliottc9774412023-02-06 15:14:07 +00003599 /* Check that start has been called first, and that operation has not
3600 * previously errored. */
3601 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003602 status = PSA_ERROR_BAD_STATE;
3603 goto exit;
3604 }
3605
3606 status = psa_driver_wrapper_verify_hash_complete(operation);
3607
Paul Elliott296ede92022-12-15 17:00:30 +00003608 /* Update ops count with work done. */
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003609 numops_status = psa_driver_wrapper_verify_hash_get_num_ops(operation, &operation->num_ops);
Dave Rodgman321c7e92023-09-28 18:07:06 +01003610 if (status == PSA_SUCCESS) {
Dave Rodgmanfe43d122023-09-28 18:46:11 +01003611 status = numops_status;
Dave Rodgman321c7e92023-09-28 18:07:06 +01003612 }
Paul Elliott296ede92022-12-15 17:00:30 +00003613
Paul Elliottebe225c2023-02-10 14:32:53 +00003614exit:
3615
Paul Elliott9fe12f62022-11-30 19:16:02 +00003616 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003617 if (status != PSA_SUCCESS) {
3618 operation->error_occurred = 1;
3619 }
3620
Paul Elliott59ad9452022-12-18 15:09:02 +00003621 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003622 }
3623
3624 return status;
3625}
3626
3627psa_status_t psa_verify_hash_abort(
3628 psa_verify_hash_interruptible_operation_t *operation)
3629{
Paul Elliott59ad9452022-12-18 15:09:02 +00003630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631
Paul Elliott59ad9452022-12-18 15:09:02 +00003632 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003633
Paul Elliott59ad9452022-12-18 15:09:02 +00003634 /* We clear the number of ops done here, so that it is not cleared when
3635 * the operation fails or succeeds, only on manual abort. */
3636 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003637
Paul Elliottc9774412023-02-06 15:14:07 +00003638 /* Likewise, failure state. */
3639 operation->error_occurred = 0;
3640
Paul Elliott59ad9452022-12-18 15:09:02 +00003641 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003642}
3643
Paul Elliott588f8ed2022-12-02 18:10:26 +00003644/****************************************************************/
3645/* Asymmetric interruptible cryptography internal */
3646/* implementations */
3647/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003648
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3650{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003651
Paul Elliott588f8ed2022-12-02 18:10:26 +00003652#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3653 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3654 defined(MBEDTLS_ECP_RESTARTABLE)
3655
3656 /* Internal implementation uses zero to indicate infinite number max ops,
3657 * therefore avoid this value, and set to minimum possible. */
3658 if (max_ops == 0) {
3659 max_ops = 1;
3660 }
3661
Paul Elliott588f8ed2022-12-02 18:10:26 +00003662 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003663#else
3664 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003665#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3666 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3667 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3668}
3669
Paul Elliott588f8ed2022-12-02 18:10:26 +00003670uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003671 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672{
3673#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3674 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3675 defined(MBEDTLS_ECP_RESTARTABLE)
3676
Paul Elliott93d9ca82023-02-15 18:14:21 +00003677 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003678#else
3679 (void) operation;
3680 return 0;
3681#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3682 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3683 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3684}
3685
3686uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003687 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688{
3689 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3690 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3691 defined(MBEDTLS_ECP_RESTARTABLE)
3692
Paul Elliott93d9ca82023-02-15 18:14:21 +00003693 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003694#else
3695 (void) operation;
3696 return 0;
3697#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3698 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3699 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3700}
3701
3702psa_status_t mbedtls_psa_sign_hash_start(
3703 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3704 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3705 size_t key_buffer_size, psa_algorithm_t alg,
3706 const uint8_t *hash, size_t hash_length)
3707{
3708 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003709 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003710
Paul Elliott068fe072023-01-16 13:59:15 +00003711 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3712 return PSA_ERROR_NOT_SUPPORTED;
3713 }
3714
3715 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003716 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003717 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003718
3719#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003720 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3721 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliotta1c94092023-02-15 16:38:04 +00003723 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3724
Paul Elliott93d9ca82023-02-15 18:14:21 +00003725 /* Ensure num_ops is zero'ed in case of context re-use. */
3726 operation->num_ops = 0;
3727
Paul Elliott068fe072023-01-16 13:59:15 +00003728 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3729 attributes->core.bits,
3730 key_buffer,
3731 key_buffer_size,
3732 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733
Paul Elliott068fe072023-01-16 13:59:15 +00003734 if (status != PSA_SUCCESS) {
3735 return status;
3736 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003737
Paul Elliott1bc59df2023-02-05 13:41:57 +00003738 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003739 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003740
Paul Elliott068fe072023-01-16 13:59:15 +00003741 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003742 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003743 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003744
Paul Elliott0290a762023-02-09 14:30:24 +00003745 /* We only need to store the same length of hash as the private key size
3746 * here, it would be truncated by the internal implementation anyway. */
3747 required_hash_length = (hash_length < operation->coordinate_bytes ?
3748 hash_length : operation->coordinate_bytes);
3749
Paul Elliottba70ad42023-02-15 18:23:53 +00003750 if (required_hash_length > sizeof(operation->hash)) {
3751 /* Shouldn't happen, but better safe than sorry. */
3752 return PSA_ERROR_CORRUPTION_DETECTED;
3753 }
3754
Paul Elliott0290a762023-02-09 14:30:24 +00003755 memcpy(operation->hash, hash, required_hash_length);
3756 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003757
3758 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003759
3760#else
Paul Elliott068fe072023-01-16 13:59:15 +00003761 (void) operation;
3762 (void) key_buffer;
3763 (void) key_buffer_size;
3764 (void) alg;
3765 (void) hash;
3766 (void) hash_length;
3767 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003768 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769
Paul Elliott068fe072023-01-16 13:59:15 +00003770 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3772 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3773 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774}
3775
3776psa_status_t mbedtls_psa_sign_hash_complete(
3777 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3778 uint8_t *signature, size_t signature_size,
3779 size_t *signature_length)
3780{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3782 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3783 defined(MBEDTLS_ECP_RESTARTABLE)
3784
Paul Elliotta1c94092023-02-15 16:38:04 +00003785 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003786 mbedtls_mpi r;
3787 mbedtls_mpi s;
3788
Paul Elliott46845252023-02-03 14:59:11 +00003789 mbedtls_mpi_init(&r);
3790 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003792 /* Ensure max_ops is set to the current value (or default). */
3793 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3794
Paul Elliotta1c94092023-02-15 16:38:04 +00003795 if (signature_size < 2 * operation->coordinate_bytes) {
3796 status = PSA_ERROR_BUFFER_TOO_SMALL;
3797 goto exit;
3798 }
3799
Paul Elliott588f8ed2022-12-02 18:10:26 +00003800 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003801
Paul Elliott588f8ed2022-12-02 18:10:26 +00003802#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3803 status = mbedtls_to_psa_error(
3804 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003805 &r,
3806 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003807 &operation->ctx->d,
3808 operation->hash,
3809 operation->hash_length,
3810 operation->md_alg,
3811 mbedtls_psa_get_random,
3812 MBEDTLS_PSA_RANDOM_STATE,
3813 &operation->restart_ctx));
3814#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003815 status = PSA_ERROR_NOT_SUPPORTED;
3816 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3818 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003819 status = mbedtls_to_psa_error(
3820 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003821 &r,
3822 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003823 &operation->ctx->d,
3824 operation->hash,
3825 operation->hash_length,
3826 mbedtls_psa_get_random,
3827 MBEDTLS_PSA_RANDOM_STATE,
3828 mbedtls_psa_get_random,
3829 MBEDTLS_PSA_RANDOM_STATE,
3830 &operation->restart_ctx));
3831 }
3832
Paul Elliottf8e5b562023-02-19 18:43:45 +00003833 /* Hide the fact that the restart context only holds a delta of number of
3834 * ops done during the last operation, not an absolute value. */
3835 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3836
Paul Elliott724bd252023-02-08 12:35:08 +00003837 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003838 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003839 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003841 operation->coordinate_bytes)
3842 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003843
3844 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003845 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003846 }
3847
3848 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003849 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003850 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003851 operation->coordinate_bytes,
3852 operation->coordinate_bytes)
3853 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
3855 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003856 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857 }
3858
Paul Elliott1bc59df2023-02-05 13:41:57 +00003859 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003860
Paul Elliott724bd252023-02-08 12:35:08 +00003861 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003862 }
Paul Elliott724bd252023-02-08 12:35:08 +00003863
3864exit:
3865
3866 mbedtls_mpi_free(&r);
3867 mbedtls_mpi_free(&s);
3868 return status;
3869
Paul Elliott588f8ed2022-12-02 18:10:26 +00003870 #else
3871
3872 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003873 (void) signature;
3874 (void) signature_size;
3875 (void) signature_length;
3876
3877 return PSA_ERROR_NOT_SUPPORTED;
3878
3879#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3880 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3881 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3882}
3883
3884psa_status_t mbedtls_psa_sign_hash_abort(
3885 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3886{
3887
3888#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3889 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3890 defined(MBEDTLS_ECP_RESTARTABLE)
3891
3892 if (operation->ctx) {
3893 mbedtls_ecdsa_free(operation->ctx);
3894 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003895 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896 }
3897
3898 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3899
Paul Elliott93d9ca82023-02-15 18:14:21 +00003900 operation->num_ops = 0;
3901
Paul Elliott588f8ed2022-12-02 18:10:26 +00003902 return PSA_SUCCESS;
3903
3904#else
3905
3906 (void) operation;
3907
3908 return PSA_ERROR_NOT_SUPPORTED;
3909
3910#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3911 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3912 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3913}
3914
3915psa_status_t mbedtls_psa_verify_hash_start(
3916 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3917 const psa_key_attributes_t *attributes,
3918 const uint8_t *key_buffer, size_t key_buffer_size,
3919 psa_algorithm_t alg,
3920 const uint8_t *hash, size_t hash_length,
3921 const uint8_t *signature, size_t signature_length)
3922{
3923 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003924 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003925 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003926
Paul Elliott068fe072023-01-16 13:59:15 +00003927 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3928 return PSA_ERROR_NOT_SUPPORTED;
3929 }
3930
3931 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003932 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003933 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003934
3935#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003936 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3937 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003938
Paul Elliotta1c94092023-02-15 16:38:04 +00003939 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3940 mbedtls_mpi_init(&operation->r);
3941 mbedtls_mpi_init(&operation->s);
3942
Paul Elliott93d9ca82023-02-15 18:14:21 +00003943 /* Ensure num_ops is zero'ed in case of context re-use. */
3944 operation->num_ops = 0;
3945
Paul Elliott068fe072023-01-16 13:59:15 +00003946 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3947 attributes->core.bits,
3948 key_buffer,
3949 key_buffer_size,
3950 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliott068fe072023-01-16 13:59:15 +00003952 if (status != PSA_SUCCESS) {
3953 return status;
3954 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003955
Paul Elliottc569fc22023-02-10 13:02:54 +00003956 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003957
Paul Elliott1bc59df2023-02-05 13:41:57 +00003958 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003959 return PSA_ERROR_INVALID_SIGNATURE;
3960 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003961
Paul Elliott068fe072023-01-16 13:59:15 +00003962 status = mbedtls_to_psa_error(
3963 mbedtls_mpi_read_binary(&operation->r,
3964 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003965 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003966
Paul Elliott068fe072023-01-16 13:59:15 +00003967 if (status != PSA_SUCCESS) {
3968 return status;
3969 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003970
Paul Elliott068fe072023-01-16 13:59:15 +00003971 status = mbedtls_to_psa_error(
3972 mbedtls_mpi_read_binary(&operation->s,
3973 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003974 coordinate_bytes,
3975 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliott068fe072023-01-16 13:59:15 +00003977 if (status != PSA_SUCCESS) {
3978 return status;
3979 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980
Paul Elliott2c9843f2023-02-15 17:32:42 +00003981 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003982
Paul Elliott2c9843f2023-02-15 17:32:42 +00003983 if (status != PSA_SUCCESS) {
3984 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003985 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003986
Paul Elliott0290a762023-02-09 14:30:24 +00003987 /* We only need to store the same length of hash as the private key size
3988 * here, it would be truncated by the internal implementation anyway. */
3989 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3990 coordinate_bytes);
3991
Paul Elliottba70ad42023-02-15 18:23:53 +00003992 if (required_hash_length > sizeof(operation->hash)) {
3993 /* Shouldn't happen, but better safe than sorry. */
3994 return PSA_ERROR_CORRUPTION_DETECTED;
3995 }
3996
Paul Elliott0290a762023-02-09 14:30:24 +00003997 memcpy(operation->hash, hash, required_hash_length);
3998 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003999
4000 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004001#else
Paul Elliott068fe072023-01-16 13:59:15 +00004002 (void) operation;
4003 (void) key_buffer;
4004 (void) key_buffer_size;
4005 (void) alg;
4006 (void) hash;
4007 (void) hash_length;
4008 (void) signature;
4009 (void) signature_length;
4010 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004011 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004012 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004013
Paul Elliott068fe072023-01-16 13:59:15 +00004014 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004015#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4016 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4017 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004018}
4019
4020psa_status_t mbedtls_psa_verify_hash_complete(
4021 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4022{
4023
4024#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4025 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4026 defined(MBEDTLS_ECP_RESTARTABLE)
4027
Paul Elliottf8e5b562023-02-19 18:43:45 +00004028 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4029
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004030 /* Ensure max_ops is set to the current value (or default). */
4031 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4032
Paul Elliottf8e5b562023-02-19 18:43:45 +00004033 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004034 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4035 operation->hash,
4036 operation->hash_length,
4037 &operation->ctx->Q,
4038 &operation->r,
4039 &operation->s,
4040 &operation->restart_ctx));
4041
Paul Elliottf8e5b562023-02-19 18:43:45 +00004042 /* Hide the fact that the restart context only holds a delta of number of
4043 * ops done during the last operation, not an absolute value. */
4044 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4045
4046 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004047#else
4048 (void) operation;
4049
4050 return PSA_ERROR_NOT_SUPPORTED;
4051
4052#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4053 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4054 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4055}
4056
4057psa_status_t mbedtls_psa_verify_hash_abort(
4058 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4059{
4060
4061#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4062 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4063 defined(MBEDTLS_ECP_RESTARTABLE)
4064
4065 if (operation->ctx) {
4066 mbedtls_ecdsa_free(operation->ctx);
4067 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004068 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004069 }
4070
4071 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4072
Paul Elliott93d9ca82023-02-15 18:14:21 +00004073 operation->num_ops = 0;
4074
Paul Elliott588f8ed2022-12-02 18:10:26 +00004075 mbedtls_mpi_free(&operation->r);
4076 mbedtls_mpi_free(&operation->s);
4077
4078 return PSA_SUCCESS;
4079
4080#else
4081 (void) operation;
4082
4083 return PSA_ERROR_NOT_SUPPORTED;
4084
4085#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4086 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4087 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4088}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004089
mohammad1603503973b2018-03-12 15:59:30 +02004090/****************************************************************/
4091/* Symmetric cryptography */
4092/****************************************************************/
4093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4095 mbedtls_svc_key_id_t key,
4096 psa_algorithm_t alg,
4097 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004098{
4099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4100 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004101 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4103 PSA_KEY_USAGE_ENCRYPT :
4104 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004105 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004106
4107 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004109 status = PSA_ERROR_BAD_STATE;
4110 goto exit;
4111 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
4116 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4119 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004122
Ronald Cron49fafa92021-03-10 08:34:23 +01004123 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004124 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004125 * so we only set it (in the driver wrapper) after resources have been
4126 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004127 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004129 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004131 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 }
4133 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004134
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004135 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004137 };
4138
Ronald Cronab99ac22020-10-01 16:38:28 +02004139 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 if (cipher_operation == MBEDTLS_ENCRYPT) {
4141 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4142 &attributes,
4143 slot->key.data,
4144 slot->key.bytes,
4145 alg);
4146 } else {
4147 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4148 &attributes,
4149 slot->key.data,
4150 slot->key.bytes,
4151 alg);
4152 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004153
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 if (status != PSA_SUCCESS) {
4156 psa_cipher_abort(operation);
4157 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004162}
4163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164psa_status_t psa_cipher_encrypt_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_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004169}
4170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4172 mbedtls_svc_key_id_t key,
4173 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004174{
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004176}
4177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4179 uint8_t *iv,
4180 size_t iv_size,
4181 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004182{
Ronald Cron6d051732020-10-01 14:10:20 +02004183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004184 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004185 size_t default_iv_length = 0;
Ronald Cron5618a392021-03-26 09:52:26 +01004186
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004188 status = PSA_ERROR_BAD_STATE;
4189 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004190 }
4191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004193 status = PSA_ERROR_BAD_STATE;
4194 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004195 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004196
Ronald Cron2fb90522021-07-05 12:31:44 +02004197 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004199 status = PSA_ERROR_BUFFER_TOO_SMALL;
4200 goto exit;
4201 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004204 status = PSA_ERROR_GENERIC_ERROR;
4205 goto exit;
4206 }
4207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 status = psa_generate_random(local_iv, default_iv_length);
4209 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 }
Ronald Cron5618a392021-03-26 09:52:26 +01004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 status = psa_driver_wrapper_cipher_set_iv(operation,
4214 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004215
4216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 if (status == PSA_SUCCESS) {
4218 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004219 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004220 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004222 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004224 }
Ronald Cron6d051732020-10-01 14:10:20 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004227}
4228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4230 const uint8_t *iv,
4231 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004232{
Ronald Cron6d051732020-10-01 14:10:20 +02004233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004236 status = PSA_ERROR_BAD_STATE;
4237 goto exit;
4238 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004241 status = PSA_ERROR_BAD_STATE;
4242 goto exit;
4243 }
Ronald Crona0d68172021-03-26 10:15:08 +01004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004246 status = PSA_ERROR_INVALID_ARGUMENT;
4247 goto exit;
4248 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 status = psa_driver_wrapper_cipher_set_iv(operation,
4251 iv,
4252 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004253
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004256 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 } else {
4258 psa_cipher_abort(operation);
4259 }
4260 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004261}
4262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4264 const uint8_t *input,
4265 size_t input_length,
4266 uint8_t *output,
4267 size_t output_size,
4268 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004269{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004273 status = PSA_ERROR_BAD_STATE;
4274 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004275 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004278 status = PSA_ERROR_BAD_STATE;
4279 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004280 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 status = psa_driver_wrapper_cipher_update(operation,
4283 input,
4284 input_length,
4285 output,
4286 output_size,
4287 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004288
4289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (status != PSA_SUCCESS) {
4291 psa_cipher_abort(operation);
4292 }
Ronald Cron6d051732020-10-01 14:10:20 +02004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004295}
4296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4298 uint8_t *output,
4299 size_t output_size,
4300 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004301{
David Saadab4ecc272019-02-14 13:48:10 +02004302 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004305 status = PSA_ERROR_BAD_STATE;
4306 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004307 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004310 status = PSA_ERROR_BAD_STATE;
4311 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004312 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004313
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 status = psa_driver_wrapper_cipher_finish(operation,
4315 output,
4316 output_size,
4317 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004318
4319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (status == PSA_SUCCESS) {
4321 return psa_cipher_abort(operation);
4322 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004323 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004327 }
mohammad1603503973b2018-03-12 15:59:30 +02004328}
4329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004331{
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004333 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004334 * in use. It's ok to call abort on such an object, and there's
4335 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004337 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004340
Ronald Cron49fafa92021-03-10 08:34:23 +01004341 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004342 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004343 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004344
Gilles Peskine449bd832023-01-11 14:50:10 +01004345 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004346}
4347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4349 psa_algorithm_t alg,
4350 const uint8_t *input,
4351 size_t input_length,
4352 uint8_t *output,
4353 size_t output_size,
4354 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004355{
4356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004358 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004359 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4360 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004361 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004364 status = PSA_ERROR_INVALID_ARGUMENT;
4365 goto exit;
4366 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4369 PSA_KEY_USAGE_ENCRYPT,
4370 alg);
4371 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004374
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004375 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004377 };
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4380 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004381 status = PSA_ERROR_GENERIC_ERROR;
4382 goto exit;
4383 }
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (default_iv_length > 0) {
4386 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004387 status = PSA_ERROR_BUFFER_TOO_SMALL;
4388 goto exit;
4389 }
4390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 status = psa_generate_random(local_iv, default_iv_length);
4392 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004393 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004395 }
4396
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004397 status = psa_driver_wrapper_cipher_encrypt(
4398 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004399 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004400 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004402
4403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 unlock_status = psa_unlock_key_slot(slot);
4405 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004406 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004407 }
Ronald Cron23919522021-07-08 19:00:07 +02004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (status == PSA_SUCCESS) {
4410 if (default_iv_length > 0) {
4411 memcpy(output, local_iv, default_iv_length);
4412 }
4413 *output_length += default_iv_length;
4414 } else {
4415 *output_length = 0;
4416 }
4417
4418 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004419}
4420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4422 psa_algorithm_t alg,
4423 const uint8_t *input,
4424 size_t input_length,
4425 uint8_t *output,
4426 size_t output_size,
4427 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004428{
4429 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004430 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004431 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004432 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004435 status = PSA_ERROR_INVALID_ARGUMENT;
4436 goto exit;
4437 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4440 PSA_KEY_USAGE_DECRYPT,
4441 alg);
4442 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004443 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004445
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004446 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004448 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4451 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004452 status = PSA_ERROR_INVALID_ARGUMENT;
4453 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004455 status = PSA_ERROR_INVALID_ARGUMENT;
4456 goto exit;
4457 }
4458
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004459 status = psa_driver_wrapper_cipher_decrypt(
4460 &attributes, slot->key.data, slot->key.bytes,
4461 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004463
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004464exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 unlock_status = psa_unlock_key_slot(slot);
4466 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004467 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004471 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 }
Ronald Cron23919522021-07-08 19:00:07 +02004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004475}
4476
4477
mohammad16035955c982018-04-26 00:53:03 +03004478/****************************************************************/
4479/* AEAD */
4480/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004481
Paul Elliottbaff51c2021-09-28 17:44:45 +01004482/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004484{
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004486}
4487
4488/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004489static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4490 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004491{
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004493
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004495#if defined(PSA_WANT_ALG_GCM)
4496 case PSA_ALG_GCM:
4497 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 * arbitrarily large nonces. Please note that we do not generally
4499 * recommend the usage of nonces of greater length than
4500 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4501 * size, which can then lead to collisions if you encrypt a very
4502 * large number of messages.*/
4503 if (nonce_length != 0) {
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_GCM */
4508#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004509 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (nonce_length >= 7 && nonce_length <= 13) {
4511 return PSA_SUCCESS;
4512 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004513 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004514#endif /* PSA_WANT_ALG_CCM */
4515#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004516 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (nonce_length == 12) {
4518 return PSA_SUCCESS;
4519 } else if (nonce_length == 8) {
4520 return PSA_ERROR_NOT_SUPPORTED;
4521 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004522 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004523#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004524 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004525 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004527 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004530}
4531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004533{
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4535 return PSA_ERROR_INVALID_ARGUMENT;
4536 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004539}
4540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4542 psa_algorithm_t alg,
4543 const uint8_t *nonce,
4544 size_t nonce_length,
4545 const uint8_t *additional_data,
4546 size_t additional_data_length,
4547 const uint8_t *plaintext,
4548 size_t plaintext_length,
4549 uint8_t *ciphertext,
4550 size_t ciphertext_size,
4551 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004552{
Ronald Cron215633c2021-03-16 17:15:37 +01004553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4554 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004555
mohammad1603f08a5502018-06-03 15:05:47 +03004556 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 status = psa_aead_check_algorithm(alg);
4559 if (status != PSA_SUCCESS) {
4560 return status;
4561 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004562
Ronald Cron9a986162021-03-26 12:40:07 +01004563 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4565 if (status != PSA_SUCCESS) {
4566 return status;
4567 }
mohammad16035955c982018-04-26 00:53:03 +03004568
Ronald Cron215633c2021-03-16 17:15:37 +01004569 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004571 };
mohammad16035955c982018-04-26 00:53:03 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 status = psa_aead_check_nonce_length(alg, nonce_length);
4574 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004577
Ronald Cronde822812021-03-17 16:08:20 +01004578 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004579 &attributes, slot->key.data, slot->key.bytes,
4580 alg,
4581 nonce, nonce_length,
4582 additional_data, additional_data_length,
4583 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4587 memset(ciphertext, 0, ciphertext_size);
4588 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004589
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004592
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004594}
4595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4597 psa_algorithm_t alg,
4598 const uint8_t *nonce,
4599 size_t nonce_length,
4600 const uint8_t *additional_data,
4601 size_t additional_data_length,
4602 const uint8_t *ciphertext,
4603 size_t ciphertext_length,
4604 uint8_t *plaintext,
4605 size_t plaintext_size,
4606 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004607{
Ronald Cron215633c2021-03-16 17:15:37 +01004608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4609 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004610
Gilles Peskineee652a32018-06-01 19:23:52 +02004611 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 status = psa_aead_check_algorithm(alg);
4614 if (status != PSA_SUCCESS) {
4615 return status;
4616 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004617
Ronald Cron9a986162021-03-26 12:40:07 +01004618 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4620 if (status != PSA_SUCCESS) {
4621 return status;
4622 }
mohammad16035955c982018-04-26 00:53:03 +03004623
Ronald Cron215633c2021-03-16 17:15:37 +01004624 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004626 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 status = psa_aead_check_nonce_length(alg, nonce_length);
4629 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004632
Ronald Cronde822812021-03-17 16:08:20 +01004633 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004634 &attributes, slot->key.data, slot->key.bytes,
4635 alg,
4636 nonce, nonce_length,
4637 additional_data, additional_data_length,
4638 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 if (status != PSA_SUCCESS && plaintext_size != 0) {
4642 memset(plaintext, 0, plaintext_size);
4643 }
mohammad160360a64d02018-06-03 17:20:42 +03004644
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 return status;
mohammad16035955c982018-04-26 00:53:03 +03004649}
4650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4652{
4653 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004656#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4660 return PSA_ERROR_INVALID_ARGUMENT;
4661 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004662 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004663#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004664
Przemek Stekiel86679c72022-10-06 17:06:56 +02004665#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4669 return PSA_ERROR_INVALID_ARGUMENT;
4670 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004671 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004672#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004673
Przemek Stekiel86679c72022-10-06 17:06:56 +02004674#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if (tag_len != 16) {
4678 return PSA_ERROR_INVALID_ARGUMENT;
4679 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004680 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004681#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004682
4683 default:
4684 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004686 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004688}
4689
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4692 int is_encrypt,
4693 mbedtls_svc_key_id_t key,
4694 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004695{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4698 psa_key_slot_t *slot = NULL;
4699 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004700 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 status = psa_aead_check_algorithm(alg);
4703 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004704 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004708 status = PSA_ERROR_BAD_STATE;
4709 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004710 }
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (operation->nonce_set || operation->lengths_set ||
4713 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004714 status = PSA_ERROR_BAD_STATE;
4715 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004716 }
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004719 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004721 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4725 alg);
4726 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004727 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004729
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004730 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004731 .core = slot->attr
4732 };
4733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004735 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (is_encrypt) {
4739 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4740 &attributes,
4741 slot->key.data,
4742 slot->key.bytes,
4743 alg);
4744 } else {
4745 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4746 &attributes,
4747 slot->key.data,
4748 slot->key.bytes,
4749 alg);
4750 }
4751 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004752 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004756
4757exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004761 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004763 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 } else {
4765 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004766 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004769}
4770
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004772psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4773 mbedtls_svc_key_id_t key,
4774 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004775{
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777}
4778
4779/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4781 mbedtls_svc_key_id_t key,
4782 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004783{
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004785}
4786
4787/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004788psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4789 uint8_t *nonce,
4790 size_t nonce_size,
4791 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792{
4793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004794 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Sergeybef1f632023-03-06 15:25:06 -07004795 size_t required_nonce_size = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004796
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004797 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004800 status = PSA_ERROR_BAD_STATE;
4801 goto exit;
4802 }
4803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004805 status = PSA_ERROR_BAD_STATE;
4806 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004807 }
4808
Paul Elliotte193ea82021-10-01 13:00:16 +01004809 /* For CCM, this size may not be correct according to the PSA
4810 * specification. The PSA Crypto 1.0.1 specification states:
4811 *
4812 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4813 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4814 *
4815 * However this restriction that L has to be the smallest integer is not
4816 * applied in practice, and it is not implementable here since the
4817 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4819 operation->alg);
4820 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 status = PSA_ERROR_BUFFER_TOO_SMALL;
4822 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823 }
4824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 status = psa_generate_random(local_nonce, required_nonce_size);
4826 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004831
Paul Elliott39dc6b82021-05-11 19:16:09 +01004832exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 if (status == PSA_SUCCESS) {
4834 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004835 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 } else {
4837 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004838 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004841}
4842
4843/* Set the nonce for a multipart authenticated encryption or decryption
4844 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004845psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4846 const uint8_t *nonce,
4847 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004848{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004852 status = PSA_ERROR_BAD_STATE;
4853 goto exit;
4854 }
4855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004857 status = PSA_ERROR_BAD_STATE;
4858 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004859 }
4860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4862 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004863 status = PSA_ERROR_INVALID_ARGUMENT;
4864 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004865 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4868 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004869
Paul Elliott39dc6b82021-05-11 19:16:09 +01004870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004872 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 } else {
4874 psa_aead_abort(operation);
4875 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878}
4879
4880/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004881psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4882 size_t ad_length,
4883 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004884{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004888 status = PSA_ERROR_BAD_STATE;
4889 goto exit;
4890 }
4891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (operation->lengths_set || operation->ad_started ||
4893 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004894 status = PSA_ERROR_BAD_STATE;
4895 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004896 }
4897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004899#if defined(PSA_WANT_ALG_GCM)
4900 case PSA_ALG_GCM:
4901 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 * bits. Without the guard this code will generate warnings on 32bit
4903 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004904#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (((uint64_t) ad_length) >> 61 != 0 ||
4906 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004907 status = PSA_ERROR_INVALID_ARGUMENT;
4908 goto exit;
4909 }
Paul Elliott325d3742021-09-27 17:56:28 +01004910#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004911 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004912#endif /* PSA_WANT_ALG_GCM */
4913#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004914 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004916 status = PSA_ERROR_INVALID_ARGUMENT;
4917 goto exit;
4918 }
4919 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004920#endif /* PSA_WANT_ALG_CCM */
4921#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004922 case PSA_ALG_CHACHA20_POLY1305:
4923 /* No length restrictions for ChaChaPoly. */
4924 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004925#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004926 default:
4927 break;
4928 }
Paul Elliott325d3742021-09-27 17:56:28 +01004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4931 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004932
Paul Elliott39dc6b82021-05-11 19:16:09 +01004933exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004935 operation->ad_remaining = ad_length;
4936 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004937 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 } else {
4939 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004940 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004944
4945/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004946psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4947 const uint8_t *input,
4948 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004949{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004950 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004953 status = PSA_ERROR_BAD_STATE;
4954 goto exit;
4955 }
4956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004958 status = PSA_ERROR_BAD_STATE;
4959 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004960 }
4961
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 if (operation->lengths_set) {
4963 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004964 status = PSA_ERROR_INVALID_ARGUMENT;
4965 goto exit;
4966 }
4967
4968 operation->ad_remaining -= input_length;
4969 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004970#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004972 status = PSA_ERROR_BAD_STATE;
4973 goto exit;
4974 }
4975#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 status = psa_driver_wrapper_aead_update_ad(operation, input,
4978 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004979
Paul Elliott39dc6b82021-05-11 19:16:09 +01004980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004982 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 } else {
4984 psa_aead_abort(operation);
4985 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004988}
4989
4990/* Encrypt or decrypt a message fragment in an active multipart AEAD
4991 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004992psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4993 const uint8_t *input,
4994 size_t input_length,
4995 uint8_t *output,
4996 size_t output_size,
4997 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004998{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004999 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005000
5001 *output_length = 0;
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005004 status = PSA_ERROR_BAD_STATE;
5005 goto exit;
5006 }
5007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005009 status = PSA_ERROR_BAD_STATE;
5010 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005011 }
5012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005014 /* Additional data length was supplied, but not all the additional
5015 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005017 status = PSA_ERROR_INVALID_ARGUMENT;
5018 goto exit;
5019 }
5020
5021 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005023 status = PSA_ERROR_INVALID_ARGUMENT;
5024 goto exit;
5025 }
5026
5027 operation->body_remaining -= input_length;
5028 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005029#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005031 status = PSA_ERROR_BAD_STATE;
5032 goto exit;
5033 }
5034#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5037 output, output_size,
5038 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005039
Paul Elliott39dc6b82021-05-11 19:16:09 +01005040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005042 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 } else {
5044 psa_aead_abort(operation);
5045 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005048}
5049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005051{
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 if (operation->id == 0 || !operation->nonce_set) {
5053 return PSA_ERROR_BAD_STATE;
5054 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5057 operation->body_remaining != 0)) {
5058 return PSA_ERROR_INVALID_ARGUMENT;
5059 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005062}
5063
Paul Elliott302ff6b2021-04-20 18:10:30 +01005064/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005065psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5066 uint8_t *ciphertext,
5067 size_t ciphertext_size,
5068 size_t *ciphertext_length,
5069 uint8_t *tag,
5070 size_t tag_size,
5071 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005072{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5074
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005076 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 status = psa_aead_final_checks(operation);
5079 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005080 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005082
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005084 status = PSA_ERROR_BAD_STATE;
5085 goto exit;
5086 }
5087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5089 ciphertext_size,
5090 ciphertext_length,
5091 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005092
Paul Elliott39dc6b82021-05-11 19:16:09 +01005093exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005094
5095
Paul Elliottf47b0952021-05-21 18:02:33 +01005096 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005097 * the zero tag size, make sure the tag is set to something implausible.
5098 * Even if the operation succeeds, make sure we clear the rest of the
5099 * buffer to prevent potential leakage of anything previously placed in
5100 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005101 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005102
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005106}
5107
5108/* Finish authenticating and decrypting a message in a multipart AEAD
5109 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005110psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5111 uint8_t *plaintext,
5112 size_t plaintext_size,
5113 size_t *plaintext_length,
5114 const uint8_t *tag,
5115 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005116{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005117 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5118
Paul Elliott302ff6b2021-04-20 18:10:30 +01005119 *plaintext_length = 0;
5120
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 status = psa_aead_final_checks(operation);
5122 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005127 status = PSA_ERROR_BAD_STATE;
5128 goto exit;
5129 }
5130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5132 plaintext_size,
5133 plaintext_length,
5134 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005135
5136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005140}
5141
5142/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005143psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005144{
5145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005148 /* The object has (apparently) been initialized but it is not (yet)
5149 * in use. It's ok to call abort on such an object, and there's
5150 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005152 }
5153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005159}
5160
Gilles Peskinee59236f2018-01-27 23:32:46 +01005161/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005162/* Generators */
5163/****************************************************************/
5164
Przemek Stekiel69c46792022-06-10 12:59:51 +02005165#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005166 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005167 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305168 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305169 defined(PSA_HAVE_SOFT_PBKDF2)
John Durkop07cc04a2020-11-16 22:08:34 -08005170#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005171#endif /* At least one builtin KDF */
5172
Przemek Stekiel69c46792022-06-10 12:59:51 +02005173#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005174 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5175 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5176static psa_status_t psa_key_derivation_start_hmac(
5177 psa_mac_operation_t *operation,
5178 psa_algorithm_t hash_alg,
5179 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005181{
5182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5185 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5186 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005187
Steven Cooreman72f736a2021-05-07 14:14:37 +02005188 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 status = psa_driver_wrapper_mac_sign_setup(operation,
5192 &attributes,
5193 hmac_key, hmac_key_length,
5194 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 psa_reset_key_attributes(&attributes);
5197 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005198}
5199#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005200
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201#define HKDF_STATE_INIT 0 /* no input yet */
5202#define HKDF_STATE_STARTED 1 /* got salt */
5203#define HKDF_STATE_KEYED 2 /* got key */
5204#define HKDF_STATE_OUTPUT 3 /* output started */
5205
Gilles Peskinecbe66502019-05-16 16:59:18 +02005206static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208{
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5210 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5211 } else {
5212 return operation->alg;
5213 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214}
5215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217{
5218 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5220 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005221 /* The object has (apparently) been initialized but it is not
5222 * in use. It's ok to call abort on such an object, and there's
5223 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005225#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5227 mbedtls_free(operation->ctx.hkdf.info);
5228 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5229 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005230#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005231#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5232 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5234 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5235 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5236 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005237 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005239 }
5240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005242 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005244 }
5245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005247 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005249 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005250#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005252 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005254 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005255#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005256 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005257
5258 /* We leave the fields Ai and output_block to be erased safely by the
5259 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005261#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5262 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005263#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5265 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5266 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5267 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305269#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305270 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305271 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005272 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305273 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305274 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305275
5276 status = PSA_SUCCESS;
5277 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305278#endif /* defined(PSA_HAVE_SOFT_PBKDF2) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279 {
5280 status = PSA_ERROR_BAD_STATE;
5281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 mbedtls_platform_zeroize(operation, sizeof(*operation));
5283 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005284}
5285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005288{
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005292 }
5293
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005296}
5297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5299 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005300{
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 if (operation->alg == 0) {
5302 return PSA_ERROR_BAD_STATE;
5303 }
5304 if (capacity > operation->capacity) {
5305 return PSA_ERROR_INVALID_ARGUMENT;
5306 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005309}
5310
Przemek Stekiel69c46792022-06-10 12:59:51 +02005311#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005312/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5314 psa_algorithm_t kdf_alg,
5315 uint8_t *output,
5316 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005317{
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5319 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005320 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005322#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005324#else
5325 const uint8_t last_block = 0xff;
5326#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 if (hkdf->state < HKDF_STATE_KEYED ||
5329 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005330#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005332#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 )) {
5334 return PSA_ERROR_BAD_STATE;
5335 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005336 hkdf->state = HKDF_STATE_OUTPUT;
5337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339 /* Copy what remains of the current block */
5340 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005342 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 }
5344 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345 output += n;
5346 output_length -= n;
5347 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005349 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005351 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005352 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005354 * object was corrupted or if this function is called directly
5355 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 if (hkdf->block_number == last_block) {
5357 return PSA_ERROR_BAD_STATE;
5358 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005359
5360 /* We need a new block */
5361 ++hkdf->block_number;
5362 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5365 hash_alg,
5366 hkdf->prk,
5367 hash_length);
5368 if (status != PSA_SUCCESS) {
5369 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005370 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005371
5372 if (hkdf->block_number != 1) {
5373 status = psa_mac_update(&hkdf->hmac,
5374 hkdf->output_block,
5375 hash_length);
5376 if (status != PSA_SUCCESS) {
5377 return status;
5378 }
5379 }
5380 status = psa_mac_update(&hkdf->hmac,
5381 hkdf->info,
5382 hkdf->info_length);
5383 if (status != PSA_SUCCESS) {
5384 return status;
5385 }
5386 status = psa_mac_update(&hkdf->hmac,
5387 &hkdf->block_number, 1);
5388 if (status != PSA_SUCCESS) {
5389 return status;
5390 }
5391 status = psa_mac_sign_finish(&hkdf->hmac,
5392 hkdf->output_block,
5393 sizeof(hkdf->output_block),
5394 &hmac_output_length);
5395 if (status != PSA_SUCCESS) {
5396 return status;
5397 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005398 }
5399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005401}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005402#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005403
John Durkop07cc04a2020-11-16 22:08:34 -08005404#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5405 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005406static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5407 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005409{
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5411 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005412 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5413 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005414 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005415
Janos Follath7742fee2019-06-17 12:58:10 +01005416 /* We can't be wanting more output after block 0xff, otherwise
5417 * the capacity check in psa_key_derivation_output_bytes() would have
5418 * prevented this call. It could happen only if the operation
5419 * object was corrupted or if this function is called directly
5420 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (tls12_prf->block_number == 0xff) {
5422 return PSA_ERROR_CORRUPTION_DETECTED;
5423 }
Janos Follath7742fee2019-06-17 12:58:10 +01005424
5425 /* We need a new block */
5426 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005427 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005428
5429 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5430 *
5431 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5432 *
5433 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5434 * HMAC_hash(secret, A(2) + seed) +
5435 * HMAC_hash(secret, A(3) + seed) + ...
5436 *
5437 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005438 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005439 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005440 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005441 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005442 * is currently extracted as `output_block` and where i is
5443 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005444 */
5445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 status = psa_key_derivation_start_hmac(&hmac,
5447 hash_alg,
5448 tls12_prf->secret,
5449 tls12_prf->secret_length);
5450 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005451 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005453
5454 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5457 * the variable seed and in this instance means it in the context of the
5458 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 status = psa_mac_update(&hmac,
5460 tls12_prf->label,
5461 tls12_prf->label_length);
5462 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 }
5465 status = psa_mac_update(&hmac,
5466 tls12_prf->seed,
5467 tls12_prf->seed_length);
5468 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 }
5471 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005472 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5474 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005475 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005477 }
5478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 status = psa_mac_sign_finish(&hmac,
5480 tls12_prf->Ai, hash_length,
5481 &hmac_output_length);
5482 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005483 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 }
5485 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005486 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005488
5489 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 status = psa_key_derivation_start_hmac(&hmac,
5491 hash_alg,
5492 tls12_prf->secret,
5493 tls12_prf->secret_length);
5494 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005495 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 }
5497 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5498 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005499 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 }
5501 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5502 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005503 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 }
5505 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5506 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 }
5509 status = psa_mac_sign_finish(&hmac,
5510 tls12_prf->output_block, hash_length,
5511 &hmac_output_length);
5512 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005513 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005515
Janos Follath7742fee2019-06-17 12:58:10 +01005516
5517cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 cleanup_status = psa_mac_abort(&hmac);
5519 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005520 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005524}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005525
Janos Follath844eb0e2019-06-19 12:10:49 +01005526static psa_status_t psa_key_derivation_tls12_prf_read(
5527 psa_tls12_prf_key_derivation_t *tls12_prf,
5528 psa_algorithm_t alg,
5529 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005531{
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5533 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005534 psa_status_t status;
5535 uint8_t offset, length;
5536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005538 case PSA_TLS12_PRF_STATE_LABEL_SET:
5539 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5540 break;
5541 case PSA_TLS12_PRF_STATE_OUTPUT:
5542 break;
5543 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005545 }
5546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005548 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (tls12_prf->left_in_block == 0) {
5550 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5551 alg);
5552 if (status != PSA_SUCCESS) {
5553 return status;
5554 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005555
5556 continue;
5557 }
5558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005560 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005562 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005564
5565 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005567 output += length;
5568 output_length -= length;
5569 tls12_prf->left_in_block -= length;
5570 }
5571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005573}
John Durkop07cc04a2020-11-16 22:08:34 -08005574#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5575 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005576
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005577#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5578static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5579 psa_tls12_ecjpake_to_pms_t *ecjpake,
5580 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005582{
5583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005584 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (output_length != 32) {
5587 return PSA_ERROR_INVALID_ARGUMENT;
5588 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5591 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5592 &output_size);
5593 if (status != PSA_SUCCESS) {
5594 return status;
5595 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 if (output_size != output_length) {
5598 return PSA_ERROR_GENERIC_ERROR;
5599 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005602}
5603#endif
5604
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305605#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305606static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5607 psa_pbkdf2_key_derivation_t *pbkdf2,
5608 psa_algorithm_t prf_alg,
5609 uint8_t prf_output_length,
5610 psa_key_attributes_t *attributes)
5611{
5612 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305613 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305614 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305615 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305616 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305619
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305620 mac_operation.is_sign = 1;
5621 mac_operation.mac_size = prf_output_length;
5622 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305623
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305624 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5625 attributes,
5626 pbkdf2->password,
5627 pbkdf2->password_length,
5628 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305629 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305630 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305631 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305632 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5633 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305634 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305635 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305636 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305637 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305638 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305639 }
5640 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5641 &mac_output_length);
5642 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305643 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305644 }
5645
5646 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305647 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305648 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305649 }
5650
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305651 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305652
5653 for (i = 1; i < pbkdf2->input_cost; i++) {
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305654 /* We are passing prf_output_length as mac_size because the driver
5655 * function directly sets mac_output_length as mac_size upon success.
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05305656 * See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305657 status = psa_driver_wrapper_mac_compute(attributes,
5658 pbkdf2->password,
5659 pbkdf2->password_length,
5660 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao4536bb62023-06-22 15:42:59 +05305661 U_i, prf_output_length,
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305662 &mac_output_length);
5663 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305664 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305665 }
5666
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305667 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305668 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305669
5670cleanup:
5671 /* Zeroise buffers to clear sensitive data from memory. */
5672 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5673 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305674}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305675
5676static psa_status_t psa_key_derivation_pbkdf2_read(
5677 psa_pbkdf2_key_derivation_t *pbkdf2,
5678 psa_algorithm_t kdf_alg,
5679 uint8_t *output,
5680 size_t output_length)
5681{
5682 psa_status_t status;
5683 psa_algorithm_t prf_alg;
5684 uint8_t prf_output_length;
5685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5686 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5687 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5688
5689 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5690 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5691 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5692 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305693 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
5694 prf_alg = PSA_ALG_CMAC;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05305695 prf_output_length = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderaoa2520a52023-06-22 15:42:19 +05305696 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305697 } else {
5698 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305699 }
5700
5701 switch (pbkdf2->state) {
5702 case PSA_PBKDF2_STATE_PASSWORD_SET:
5703 /* Initially we need a new block so bytes_used is equal to block size*/
5704 pbkdf2->bytes_used = prf_output_length;
5705 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5706 break;
5707 case PSA_PBKDF2_STATE_OUTPUT:
5708 break;
5709 default:
5710 return PSA_ERROR_BAD_STATE;
5711 }
5712
5713 while (output_length != 0) {
5714 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5715 if (n > output_length) {
5716 n = (uint8_t) output_length;
5717 }
5718 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5719 output += n;
5720 output_length -= n;
5721 pbkdf2->bytes_used += n;
5722
5723 if (output_length == 0) {
5724 break;
5725 }
5726
5727 /* We need a new block */
5728 pbkdf2->bytes_used = 0;
5729 pbkdf2->block_number++;
5730
5731 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5732 prf_output_length,
5733 &attributes);
5734 if (status != PSA_SUCCESS) {
5735 return status;
5736 }
5737 }
5738
5739 return PSA_SUCCESS;
5740}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305741#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305742
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005743psa_status_t psa_key_derivation_output_bytes(
5744 psa_key_derivation_operation_t *operation,
5745 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747{
5748 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005752 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005754 }
5755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005757 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005758 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005760 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005761 goto exit;
5762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005765 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005766 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5767 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005769 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005771 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005772 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005773
Przemek Stekiel69c46792022-06-10 12:59:51 +02005774#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5776 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5777 output, output_length);
5778 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005779#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005780#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5781 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5783 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5784 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5785 kdf_alg, output,
5786 output_length);
5787 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005788#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5789 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005790#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005792 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5794 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005795#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305796#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05305797 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305798 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5799 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305800 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05305801#endif /* PSA_HAVE_SOFT_PBKDF2 */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005802
Gilles Peskineeab56e42018-07-12 17:12:33 +02005803 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005804 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005806 }
5807
5808exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005810 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005811 * This allows us to differentiate between exhausted operations and
5812 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5813 * operations. */
5814 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005816 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005820}
5821
David Brown7807bf72021-02-09 16:28:23 -07005822#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005823static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005824{
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 if (data_size >= 8) {
5826 mbedtls_des_key_set_parity(data);
5827 }
5828 if (data_size >= 16) {
5829 mbedtls_des_key_set_parity(data + 8);
5830 }
5831 if (data_size >= 24) {
5832 mbedtls_des_key_set_parity(data + 16);
5833 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005834}
David Brown7807bf72021-02-09 16:28:23 -07005835#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005836
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005837/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 * ECC keys on a Weierstrass elliptic curve require the generation
5839 * of a private key which is an integer
5840 * in the range [1, N - 1], where N is the boundary of the private key domain:
5841 * N is the prime p for Diffie-Hellman, or the order of the
5842 * curve’s base point for ECC.
5843 *
5844 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5845 * This function generates the private key using the following process:
5846 *
5847 * 1. Draw a byte string of length ceiling(m/8) bytes.
5848 * 2. If m is not a multiple of 8, set the most significant
5849 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5850 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5851 * 4. If k > N - 2, discard the result and return to step 1.
5852 * 5. Output k + 1 as the private key.
5853 *
5854 * This method allows compliance to NIST standards, specifically the methods titled
5855 * Key-Pair Generation by Testing Candidates in the following publications:
5856 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5857 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5858 * Diffie-Hellman keys.
5859 *
5860 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5861 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5862 *
5863 * Note: Function allocates memory for *data buffer, so given *data should be
5864 * always NULL.
5865 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005866#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5867#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005868static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005869 psa_key_slot_t *slot,
5870 size_t bits,
5871 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005872 uint8_t **data
5873 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005874{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005875 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005876 mbedtls_mpi k;
5877 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005880 size_t m;
5881 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 mbedtls_mpi_init(&k);
5884 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005885
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005886 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005888 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005892 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005893 goto cleanup;
5894 }
5895
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005896 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005900
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005901 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005902 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005903 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005904
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005905 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005907 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005909
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005910 /* Note: This function is always called with *data == NULL and it
5911 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 *data = mbedtls_calloc(1, m_bytes);
5913 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005914 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005915 goto cleanup;
5916 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005921 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005923
5924 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005926 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005927 * (8 * ceiling(m/8) - m) bits of the first byte in
5928 * the string to zero.
5929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005931 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005932 }
5933
5934 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 * big-endian byte string.
5936 */
5937 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005938
5939 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 * Result of comparison is returned. When it indicates error
5941 * then this function is called again.
5942 */
5943 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005944 }
5945
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005946 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5948 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005949cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 if (ret != 0) {
5951 status = mbedtls_to_psa_error(ret);
5952 }
5953 if (status != PSA_SUCCESS) {
5954 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005955 *data = NULL;
5956 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 mbedtls_mpi_free(&k);
5958 mbedtls_mpi_free(&diff_N_2);
5959 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005960}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005961
5962/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5963 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5964 *
5965 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5966 * draw a 32-byte string and process it as specified in
5967 * Elliptic Curves for Security [RFC7748] §5.
5968 *
5969 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5970 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5971 *
5972 * Note: Function allocates memory for *data buffer, so given *data should be
5973 * always NULL.
5974 */
5975
5976static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5977 size_t bits,
5978 psa_key_derivation_operation_t *operation,
5979 uint8_t **data
5980 )
5981{
5982 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005986 case 255:
5987 output_length = 32;
5988 break;
5989 case 448:
5990 output_length = 56;
5991 break;
5992 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005994 break;
5995 }
5996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (*data == NULL) {
6000 return PSA_ERROR_INSUFFICIENT_MEMORY;
6001 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006004
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006006 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006008
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006010 case 255:
6011 (*data)[0] &= 248;
6012 (*data)[31] &= 127;
6013 (*data)[31] |= 64;
6014 break;
6015 case 448:
6016 (*data)[0] &= 252;
6017 (*data)[55] |= 128;
6018 break;
6019 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006021 break;
6022 }
6023
6024 return status;
6025}
Valerio Setti86587ab2023-06-27 13:09:30 +02006026#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6027static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6028 psa_key_slot_t *slot, size_t bits,
6029 psa_key_derivation_operation_t *operation, uint8_t **data)
6030{
6031 (void) slot;
6032 (void) bits;
6033 (void) operation;
6034 (void) data;
6035 return PSA_ERROR_NOT_SUPPORTED;
6036}
6037
6038static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6039 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6040{
6041 (void) bits;
6042 (void) operation;
6043 (void) data;
6044 return PSA_ERROR_NOT_SUPPORTED;
6045}
6046#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6047#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006048
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006049static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006050 psa_key_slot_t *slot,
6051 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006053{
6054 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306056 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006058 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6061 return PSA_ERROR_INVALID_ARGUMENT;
6062 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006063
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006064#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006065 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6067 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6068 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006069 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6071 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 }
6074 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006075 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6077 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006080 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006081 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006082#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006083 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (key_type_is_raw_bytes(slot->attr.type)) {
6085 if (bits % 8 != 0) {
6086 return PSA_ERROR_INVALID_ARGUMENT;
6087 }
6088 data = mbedtls_calloc(1, bytes);
6089 if (data == NULL) {
6090 return PSA_ERROR_INSUFFICIENT_MEMORY;
6091 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 status = psa_key_derivation_output_bytes(operation, data, bytes);
6094 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 }
David Brown12ca5032021-02-11 11:02:00 -07006097#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6099 psa_des_set_key_parity(data, bytes);
6100 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006101#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 } else {
6103 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006104 }
Ronald Crondd04d422020-11-28 15:14:42 +01006105
Ronald Cronbf33c932020-11-28 18:06:53 +01006106 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006107 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006109 };
6110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6112 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6113 &storage_size);
6114 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 }
Archanad8a83dc2021-06-14 10:04:16 +05306117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 status = psa_allocate_buffer_to_slot(slot, storage_size);
6119 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 }
Archanad8a83dc2021-06-14 10:04:16 +05306122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 status = psa_driver_wrapper_import_key(&attributes,
6124 data, bytes,
6125 slot->key.data,
6126 slot->key.bytes,
6127 &slot->key.bytes, &bits);
6128 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006129 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006131
6132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 mbedtls_free(data);
6134 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006135}
6136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6138 psa_key_derivation_operation_t *operation,
6139 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006140{
6141 psa_status_t status;
6142 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006143 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006144
Ronald Cron81709fc2020-11-14 12:10:32 +01006145 *key = MBEDTLS_SVC_KEY_ID_INIT;
6146
Gilles Peskine0f84d622019-09-12 19:03:13 +02006147 /* Reject any attempt to create a zero-length key so that we don't
6148 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (psa_get_key_bits(attributes) == 0) {
6150 return PSA_ERROR_INVALID_ARGUMENT;
6151 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 if (operation->alg == PSA_ALG_NONE) {
6154 return PSA_ERROR_BAD_STATE;
6155 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 if (!operation->can_output_key) {
6158 return PSA_ERROR_NOT_PERMITTED;
6159 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6162 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006163#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006165 /* Deriving a key in a secure element is not implemented yet. */
6166 status = PSA_ERROR_NOT_SUPPORTED;
6167 }
6168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 if (status == PSA_SUCCESS) {
6170 status = psa_generate_derived_key_internal(slot,
6171 attributes->core.bits,
6172 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 if (status == PSA_SUCCESS) {
6175 status = psa_finish_key_creation(slot, driver, key);
6176 }
6177 if (status != PSA_SUCCESS) {
6178 psa_fail_key_creation(slot, driver);
6179 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006182}
6183
Gilles Peskineeab56e42018-07-12 17:12:33 +02006184
6185
6186/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006187/* Key derivation */
6188/****************************************************************/
6189
Steven Cooreman932ffb72021-02-15 12:14:32 +01006190#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006192{
6193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6195 return 1;
6196 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006197#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006198#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6200 return 1;
6201 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006202#endif
6203#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6205 return 1;
6206 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006207#endif
6208#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6210 return 1;
6211 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006212#endif
6213#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6215 return 1;
6216 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006217#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006218#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6220 return 1;
6221 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006222#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306223#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6224 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6225 return 1;
6226 }
6227#endif
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306228#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6229 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
6230 return 1;
6231 }
6232#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006234}
6235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006237{
6238 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_status_t status = psa_hash_setup(&operation, alg);
6240 psa_hash_abort(&operation);
6241 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006242}
6243
Gilles Peskine969c5d62019-01-16 15:53:06 +01006244static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006247{
Janos Follath5fe19732019-06-20 15:09:30 +01006248 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6249 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006251
Gilles Peskine969c5d62019-01-16 15:53:06 +01006252 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 if (!is_kdf_alg_supported(kdf_alg)) {
6254 return PSA_ERROR_NOT_SUPPORTED;
6255 }
John Durkop07cc04a2020-11-16 22:08:34 -08006256
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006257 /* All currently supported key derivation algorithms (apart from
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306258 * ecjpake to pms and pbkdf2_aes_cmac_128) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6260 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306261 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6262 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
6263 } else if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306264 hash_size = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao2cd64962023-06-22 15:38:57 +05306265 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (hash_size == 0) {
6267 return PSA_ERROR_NOT_SUPPORTED;
6268 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006269
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006270 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6271 * we might fail later, which is somewhat unfriendly and potentially
6272 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 psa_status_t status = psa_hash_try_support(hash_alg);
6274 if (status != PSA_SUCCESS) {
6275 return status;
6276 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006277 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6280 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6281 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006283 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006284#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006285 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6287 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006288 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006290#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6291 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 operation->capacity = 255 * hash_size;
6293 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006294}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006297{
6298#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 if (alg == PSA_ALG_ECDH) {
6300 return PSA_SUCCESS;
6301 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006302#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006303#if defined(PSA_WANT_ALG_FFDH)
6304 if (alg == PSA_ALG_FFDH) {
6305 return PSA_SUCCESS;
6306 }
6307#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006308 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006310}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006311
6312static int psa_key_derivation_allows_free_form_secret_input(
6313 psa_algorithm_t kdf_alg)
6314{
6315#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6316 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6317 return 0;
6318 }
6319#endif
6320 (void) kdf_alg;
6321 return 1;
6322}
John Durkop07cc04a2020-11-16 22:08:34 -08006323#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6326 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006327{
6328 psa_status_t status;
6329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 if (operation->alg != 0) {
6331 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006332 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6335 return PSA_ERROR_INVALID_ARGUMENT;
6336 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6337#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6338 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6339 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6340 status = psa_key_agreement_try_support(ka_alg);
6341 if (status != PSA_SUCCESS) {
6342 return status;
6343 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006344 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6345 return PSA_ERROR_INVALID_ARGUMENT;
6346 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6348#else
6349 return PSA_ERROR_NOT_SUPPORTED;
6350#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6351 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6352#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6353 status = psa_key_derivation_setup_kdf(operation, alg);
6354#else
6355 return PSA_ERROR_NOT_SUPPORTED;
6356#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6357 } else {
6358 return PSA_ERROR_INVALID_ARGUMENT;
6359 }
6360
6361 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006362 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 }
6364 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006365}
6366
Przemek Stekiel69c46792022-06-10 12:59:51 +02006367#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006368static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6369 psa_algorithm_t kdf_alg,
6370 psa_key_derivation_step_t step,
6371 const uint8_t *data,
6372 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006373{
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006375 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006377 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006378#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6380 return PSA_ERROR_INVALID_ARGUMENT;
6381 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006382#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 if (hkdf->state != HKDF_STATE_INIT) {
6384 return PSA_ERROR_BAD_STATE;
6385 } else {
6386 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6387 hash_alg,
6388 data, data_length);
6389 if (status != PSA_SUCCESS) {
6390 return status;
6391 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006392 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006394 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006395 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006398 /* We shouldn't be in different state as HKDF_EXPAND only allows
6399 * two inputs: SECRET (this case) and INFO which does not modify
6400 * the state. It could happen only if the hkdf
6401 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (hkdf->state != HKDF_STATE_INIT) {
6403 return PSA_ERROR_BAD_STATE;
6404 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006405
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006406 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6408 return PSA_ERROR_INVALID_ARGUMENT;
6409 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 memcpy(hkdf->prk, data, data_length);
6412 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006413#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006414 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006415 /* HKDF: If no salt was provided, use an empty salt.
6416 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006418#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6420 return PSA_ERROR_BAD_STATE;
6421 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006422#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6424 hash_alg,
6425 NULL, 0);
6426 if (status != PSA_SUCCESS) {
6427 return status;
6428 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006429 hkdf->state = HKDF_STATE_STARTED;
6430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (hkdf->state != HKDF_STATE_STARTED) {
6432 return PSA_ERROR_BAD_STATE;
6433 }
6434 status = psa_mac_update(&hkdf->hmac,
6435 data, data_length);
6436 if (status != PSA_SUCCESS) {
6437 return status;
6438 }
6439 status = psa_mac_sign_finish(&hkdf->hmac,
6440 hkdf->prk,
6441 sizeof(hkdf->prk),
6442 &data_length);
6443 if (status != PSA_SUCCESS) {
6444 return status;
6445 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006446 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006447
Gilles Peskine2b522db2019-04-12 15:11:49 +02006448 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006449 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006450#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006452 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006454 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006456#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006457 {
6458 /* Block 0 is empty, and the next block will be
6459 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006461 }
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006464 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006465#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6467 return PSA_ERROR_INVALID_ARGUMENT;
6468 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006469#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006470#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6472 hkdf->state == HKDF_STATE_INIT) {
6473 return PSA_ERROR_BAD_STATE;
6474 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006475#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 if (hkdf->state == HKDF_STATE_OUTPUT) {
6477 return PSA_ERROR_BAD_STATE;
6478 }
6479 if (hkdf->info_set) {
6480 return PSA_ERROR_BAD_STATE;
6481 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006482 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (data_length != 0) {
6484 hkdf->info = mbedtls_calloc(1, data_length);
6485 if (hkdf->info == NULL) {
6486 return PSA_ERROR_INSUFFICIENT_MEMORY;
6487 }
6488 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006489 }
6490 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006492 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006494 }
6495}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006496#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006497
John Durkop07cc04a2020-11-16 22:08:34 -08006498#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6499 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006500static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6501 const uint8_t *data,
6502 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006503{
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6505 return PSA_ERROR_BAD_STATE;
6506 }
Janos Follathf08e2652019-06-13 09:05:41 +01006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 if (data_length != 0) {
6509 prf->seed = mbedtls_calloc(1, data_length);
6510 if (prf->seed == NULL) {
6511 return PSA_ERROR_INSUFFICIENT_MEMORY;
6512 }
Janos Follathf08e2652019-06-13 09:05:41 +01006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006515 prf->seed_length = data_length;
6516 }
Janos Follathf08e2652019-06-13 09:05:41 +01006517
Gilles Peskine43f958b2020-12-13 14:55:14 +01006518 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006521}
6522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6524 const uint8_t *data,
6525 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006526{
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6528 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6529 return PSA_ERROR_BAD_STATE;
6530 }
Janos Follath81550542019-06-13 14:26:34 +01006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (data_length != 0) {
6533 prf->secret = mbedtls_calloc(1, data_length);
6534 if (prf->secret == NULL) {
6535 return PSA_ERROR_INSUFFICIENT_MEMORY;
6536 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006539 prf->secret_length = data_length;
6540 }
Janos Follath81550542019-06-13 14:26:34 +01006541
Gilles Peskine43f958b2020-12-13 14:55:14 +01006542 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006545}
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6548 const uint8_t *data,
6549 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006550{
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6552 return PSA_ERROR_BAD_STATE;
6553 }
Janos Follath63028dd2019-06-13 09:15:47 +01006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 if (data_length != 0) {
6556 prf->label = mbedtls_calloc(1, data_length);
6557 if (prf->label == NULL) {
6558 return PSA_ERROR_INSUFFICIENT_MEMORY;
6559 }
Janos Follath63028dd2019-06-13 09:15:47 +01006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006562 prf->label_length = data_length;
6563 }
Janos Follath63028dd2019-06-13 09:15:47 +01006564
Gilles Peskine43f958b2020-12-13 14:55:14 +01006565 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006568}
6569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6571 psa_key_derivation_step_t step,
6572 const uint8_t *data,
6573 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006574{
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006576 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006578 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006580 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006584 }
6585}
John Durkop07cc04a2020-11-16 22:08:34 -08006586#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6587 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6588
6589#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6590static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6591 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006592 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006594{
6595 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6597 4 + data_length + prf->other_secret_length :
6598 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6601 return PSA_ERROR_INVALID_ARGUMENT;
6602 }
John Durkop07cc04a2020-11-16 22:08:34 -08006603
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 uint8_t *pms = mbedtls_calloc(1, pms_len);
6605 if (pms == NULL) {
6606 return PSA_ERROR_INSUFFICIENT_MEMORY;
6607 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006608 uint8_t *cur = pms;
6609
6610 /* pure-PSK:
6611 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006612 *
6613 * The premaster secret is formed as follows: if the PSK is N octets
6614 * long, concatenate a uint16 with the value N, N zero octets, a second
6615 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006616 *
6617 * mixed-PSK:
6618 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6619 * follows: concatenate a uint16 with the length of the other secret,
6620 * the other secret itself, uint16 with the length of PSK, and the
6621 * PSK itself.
6622 * For details please check:
6623 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6624 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6625 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006626 */
6627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6629 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6630 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6631 if (prf->other_secret_length != 0) {
6632 memcpy(cur, prf->other_secret, prf->other_secret_length);
6633 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006634 cur += prf->other_secret_length;
6635 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 } else {
6637 *cur++ = MBEDTLS_BYTE_1(data_length);
6638 *cur++ = MBEDTLS_BYTE_0(data_length);
6639 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006640 cur += data_length;
6641 }
6642
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 *cur++ = MBEDTLS_BYTE_1(data_length);
6644 *cur++ = MBEDTLS_BYTE_0(data_length);
6645 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006646 cur += data_length;
6647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006649
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006650 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006652}
Janos Follath6660f0e2019-06-17 08:44:03 +01006653
Przemek Stekiele47201b2022-04-19 13:53:28 +02006654static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6655 psa_tls12_prf_key_derivation_t *prf,
6656 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006658{
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6660 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006661 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006662
6663 if (data_length != 0) {
6664 prf->other_secret = mbedtls_calloc(1, data_length);
6665 if (prf->other_secret == NULL) {
6666 return PSA_ERROR_INSUFFICIENT_MEMORY;
6667 }
6668
6669 memcpy(prf->other_secret, data, data_length);
6670 prf->other_secret_length = data_length;
6671 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006672 prf->other_secret_length = 0;
6673 }
6674
6675 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678}
6679
Janos Follath6660f0e2019-06-17 08:44:03 +01006680static psa_status_t psa_tls12_prf_psk_to_ms_input(
6681 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006682 psa_key_derivation_step_t step,
6683 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006685{
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006687 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 return psa_tls12_prf_psk_to_ms_set_key(prf,
6689 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006690 break;
6691 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6693 data,
6694 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006695 break;
6696 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006698 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006699
Przemek Stekiele47201b2022-04-19 13:53:28 +02006700 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006701}
John Durkop07cc04a2020-11-16 22:08:34 -08006702#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006703
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006704#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6705static psa_status_t psa_tls12_ecjpake_to_pms_input(
6706 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006707 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006708 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006710{
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6712 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6713 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006714 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006715
6716 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 if (data[0] != 0x04) {
6718 return PSA_ERROR_INVALID_ARGUMENT;
6719 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006720
6721 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006723
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006725}
6726#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306727
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306728#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306729static psa_status_t psa_pbkdf2_set_input_cost(
6730 psa_pbkdf2_key_derivation_t *pbkdf2,
6731 psa_key_derivation_step_t step,
6732 uint64_t data)
6733{
6734 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6735 return PSA_ERROR_INVALID_ARGUMENT;
6736 }
6737
6738 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6739 return PSA_ERROR_BAD_STATE;
6740 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306741
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306742 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306743 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306744 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306745
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306746 if (data == 0) {
6747 return PSA_ERROR_INVALID_ARGUMENT;
6748 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306749
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306750 pbkdf2->input_cost = data;
6751 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6752
6753 return PSA_SUCCESS;
6754}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306755
6756static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6757 const uint8_t *data,
6758 size_t data_length)
6759{
Gilles Peskineead17662023-08-20 22:02:51 +02006760 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6761 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6762 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
6763 /* Appending to existing salt. No state change. */
6764 } else {
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306765 return PSA_ERROR_BAD_STATE;
6766 }
6767
Gilles Peskineca57d782023-07-20 19:08:06 +02006768 if (data_length == 0) {
Gilles Peskineead17662023-08-20 22:02:51 +02006769 /* Appending an empty string, nothing to do. */
6770 } else {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306771 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306772
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306773 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6774 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306775 return PSA_ERROR_INSUFFICIENT_MEMORY;
6776 }
6777
Gilles Peskineead17662023-08-20 22:02:51 +02006778 if (pbkdf2->salt_length != 0) {
6779 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6780 }
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306781 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306782 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306783 mbedtls_free(pbkdf2->salt);
6784 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306785 }
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306786 return PSA_SUCCESS;
6787}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306788
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306789#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306790static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306791 const uint8_t *input,
6792 size_t input_len,
6793 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306794 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306795{
6796 psa_status_t status = PSA_SUCCESS;
6797 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6798 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306799 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306800 } else {
6801 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306802 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306803 }
6804 return status;
6805}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306806#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306807
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306808#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306809static psa_status_t psa_pbkdf2_cmac_set_password(const uint8_t *input,
6810 size_t input_len,
6811 uint8_t *output,
6812 size_t *output_len)
6813{
6814 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306815 if (input_len != PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC)) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306816 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Kusumit Ghoderao3fde8fe2023-06-27 10:41:43 +05306817 uint8_t zeros[16] = { 0 };
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306818 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
6819 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(sizeof(zeros)));
6820 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306821 /* Passing PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC) as
Kusumit Ghoderao5f3345a2023-07-27 21:21:38 +05306822 * mac_size as the driver function sets mac_output_length = mac_size
6823 * on success. See https://github.com/Mbed-TLS/mbedtls/issues/7801 */
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306824 status = psa_driver_wrapper_mac_compute(&attributes,
6825 zeros, sizeof(zeros),
6826 PSA_ALG_CMAC, input, input_len,
6827 output,
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306828 PSA_MAC_LENGTH(PSA_KEY_TYPE_AES,
6829 128U,
6830 PSA_ALG_CMAC),
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306831 output_len);
6832 } else {
6833 memcpy(output, input, input_len);
Kusumit Ghoderaoa12e2d52023-07-27 21:18:30 +05306834 *output_len = PSA_MAC_LENGTH(PSA_KEY_TYPE_AES, 128U, PSA_ALG_CMAC);
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306835 }
6836 return status;
6837}
Kusumit Ghoderao0bca4c52023-07-27 21:20:14 +05306838#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306839
6840static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306841 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306842 const uint8_t *data,
6843 size_t data_length)
6844{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306845 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306846 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6847 return PSA_ERROR_BAD_STATE;
6848 }
6849
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306850#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306851 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6852 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6853 status = psa_pbkdf2_hmac_set_password(hash_alg, 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_HMAC */
6858#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128)
6859 if (kdf_alg == PSA_ALG_PBKDF2_AES_CMAC_PRF_128) {
Kusumit Ghoderao3d5edb82023-06-22 15:41:25 +05306860 status = psa_pbkdf2_cmac_set_password(data, data_length,
6861 pbkdf2->password,
6862 &pbkdf2->password_length);
Kusumit Ghoderaof3e696d2023-07-28 13:30:50 +05306863 } else
6864#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_AES_CMAC_PRF_128 */
6865 {
6866 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306867 }
6868
6869 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6870
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306871 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306872}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306873
6874static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306875 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306876 psa_key_derivation_step_t step,
6877 const uint8_t *data,
6878 size_t data_length)
6879{
6880 switch (step) {
6881 case PSA_KEY_DERIVATION_INPUT_SALT:
6882 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6883 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306884 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306885 default:
6886 return PSA_ERROR_INVALID_ARGUMENT;
6887 }
6888}
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306889#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306890
Gilles Peskineb8965192019-09-24 16:21:10 +02006891/** Check whether the given key type is acceptable for the given
6892 * input step of a key derivation.
6893 *
6894 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6895 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6896 * Both secret and non-secret inputs can alternatively have the type
6897 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6898 * that the input was passed as a buffer rather than via a key object.
6899 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006900static int psa_key_derivation_check_input_type(
6901 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006903{
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006905 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (key_type == PSA_KEY_TYPE_DERIVE) {
6907 return PSA_SUCCESS;
6908 }
6909 if (key_type == PSA_KEY_TYPE_NONE) {
6910 return PSA_SUCCESS;
6911 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006912 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006913 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 if (key_type == PSA_KEY_TYPE_DERIVE) {
6915 return PSA_SUCCESS;
6916 }
6917 if (key_type == PSA_KEY_TYPE_NONE) {
6918 return PSA_SUCCESS;
6919 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006920 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006921 case PSA_KEY_DERIVATION_INPUT_LABEL:
6922 case PSA_KEY_DERIVATION_INPUT_SALT:
6923 case PSA_KEY_DERIVATION_INPUT_INFO:
6924 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6926 return PSA_SUCCESS;
6927 }
6928 if (key_type == PSA_KEY_TYPE_NONE) {
6929 return PSA_SUCCESS;
6930 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006931 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306932 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6933 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6934 return PSA_SUCCESS;
6935 }
6936 if (key_type == PSA_KEY_TYPE_DERIVE) {
6937 return PSA_SUCCESS;
6938 }
6939 if (key_type == PSA_KEY_TYPE_NONE) {
6940 return PSA_SUCCESS;
6941 }
6942 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006943 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006945}
6946
Janos Follathb80a94e2019-06-12 15:54:46 +01006947static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006948 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006949 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006950 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006951 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006953{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006954 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 status = psa_key_derivation_check_input_type(step, key_type);
6958 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006959 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006961
Przemek Stekiel69c46792022-06-10 12:59:51 +02006962#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6964 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6965 step, data, data_length);
6966 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006967#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006968#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6970 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6971 step, data, data_length);
6972 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006973#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6974#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6976 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6977 step, data, data_length);
6978 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006979#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006980#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006982 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006983 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6984 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006985#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306986#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05306987 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306988 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6989 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306990 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05306991#endif /* PSA_HAVE_SOFT_PBKDF2 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006992 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006993 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006994 (void) data;
6995 (void) data_length;
6996 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006998 }
6999
Gilles Peskine224b0d62019-09-23 18:13:17 +02007000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 if (status != PSA_SUCCESS) {
7002 psa_key_derivation_abort(operation);
7003 }
7004 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007005}
7006
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307007static psa_status_t psa_key_derivation_input_integer_internal(
7008 psa_key_derivation_operation_t *operation,
7009 psa_key_derivation_step_t step,
7010 uint64_t value)
7011{
7012 psa_status_t status;
7013 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
7014
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307015#if defined(PSA_HAVE_SOFT_PBKDF2)
Kusumit Ghoderao9ab03c32023-07-27 21:14:05 +05307016 if (PSA_ALG_IS_PBKDF2(kdf_alg)) {
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05307017 status = psa_pbkdf2_set_input_cost(
7018 &operation->ctx.pbkdf2, step, value);
7019 } else
Kusumit Ghoderao2addf352023-07-27 21:09:26 +05307020#endif /* PSA_HAVE_SOFT_PBKDF2 */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307021 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05307022 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307023 (void) value;
7024 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05307025 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307026 }
7027
7028 if (status != PSA_SUCCESS) {
7029 psa_key_derivation_abort(operation);
7030 }
7031 return status;
7032}
7033
Janos Follath51f4a0f2019-06-14 11:35:55 +01007034psa_status_t psa_key_derivation_input_bytes(
7035 psa_key_derivation_operation_t *operation,
7036 psa_key_derivation_step_t step,
7037 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007039{
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 return psa_key_derivation_input_internal(operation, step,
7041 PSA_KEY_TYPE_NONE,
7042 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01007043}
7044
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05307045psa_status_t psa_key_derivation_input_integer(
7046 psa_key_derivation_operation_t *operation,
7047 psa_key_derivation_step_t step,
7048 uint64_t value)
7049{
7050 return psa_key_derivation_input_integer_internal(operation, step, value);
7051}
7052
Janos Follath51f4a0f2019-06-14 11:35:55 +01007053psa_status_t psa_key_derivation_input_key(
7054 psa_key_derivation_operation_t *operation,
7055 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007057{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007059 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007060 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007061
Ronald Cron5c522922020-11-14 16:35:34 +01007062 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7064 if (status != PSA_SUCCESS) {
7065 psa_key_derivation_abort(operation);
7066 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007067 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007068
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307069 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7070 * permission to output to a key object. */
7071 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7072 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007073 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007075
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 status = psa_key_derivation_input_internal(operation,
7077 step, slot->attr.type,
7078 slot->key.data,
7079 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007082
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007084}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007085
7086
7087
7088/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007089/* Key agreement */
7090/****************************************************************/
7091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7093 const uint8_t *key_buffer,
7094 size_t key_buffer_size,
7095 psa_algorithm_t alg,
7096 const uint8_t *peer_key,
7097 size_t peer_key_length,
7098 uint8_t *shared_secret,
7099 size_t shared_secret_size,
7100 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007101{
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007103#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007104 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7106 key_buffer_size, alg,
7107 peer_key, peer_key_length,
7108 shared_secret,
7109 shared_secret_size,
7110 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007111#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007112
7113#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7114 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007115 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007116 peer_key,
7117 peer_key_length,
7118 key_buffer,
7119 key_buffer_size,
7120 shared_secret,
7121 shared_secret_size,
7122 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007123#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7124
Gilles Peskine01d718c2018-09-18 12:01:02 +02007125 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007126 (void) attributes;
7127 (void) key_buffer;
7128 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007129 (void) peer_key;
7130 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007131 (void) shared_secret;
7132 (void) shared_secret_size;
7133 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007135 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007136}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007137
Aditya Deshpande17845b82022-10-13 17:21:01 +01007138/** Internal function for raw key agreement
7139 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007140 * to the driver's implementation if a driver is present.
7141 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007142 * (psa_key_agreement_raw_builtin).
7143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007144static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7145 psa_key_slot_t *private_key,
7146 const uint8_t *peer_key,
7147 size_t peer_key_length,
7148 uint8_t *shared_secret,
7149 size_t shared_secret_size,
7150 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007151{
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7153 return PSA_ERROR_NOT_SUPPORTED;
7154 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007155
7156 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007158 };
7159
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 return psa_driver_wrapper_key_agreement(&attributes,
7161 private_key->key.data,
7162 private_key->key.bytes, alg,
7163 peer_key, peer_key_length,
7164 shared_secret,
7165 shared_secret_size,
7166 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007167}
7168
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007169/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007170 * to potentially free embedded data structures and wipe confidential data.
7171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007172static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7173 psa_key_derivation_step_t step,
7174 psa_key_slot_t *private_key,
7175 const uint8_t *peer_key,
7176 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007177{
7178 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007179 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007180 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007182
7183 /* Step 1: run the secret agreement algorithm to generate the shared
7184 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 status = psa_key_agreement_raw_internal(ka_alg,
7186 private_key,
7187 peer_key, peer_key_length,
7188 shared_secret,
7189 sizeof(shared_secret),
7190 &shared_secret_length);
7191 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007194
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007195 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007196 * the shared secret. A shared secret is permitted wherever a key
7197 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 status = psa_key_derivation_input_internal(operation, step,
7199 PSA_KEY_TYPE_DERIVE,
7200 shared_secret,
7201 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7204 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007205}
7206
Gilles Peskine449bd832023-01-11 14:50:10 +01007207psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7208 psa_key_derivation_step_t step,
7209 mbedtls_svc_key_id_t private_key,
7210 const uint8_t *peer_key,
7211 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007212{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007214 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007215 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007216
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7218 return PSA_ERROR_INVALID_ARGUMENT;
7219 }
Ronald Cron5c522922020-11-14 16:35:34 +01007220 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7222 if (status != PSA_SUCCESS) {
7223 return status;
7224 }
7225 status = psa_key_agreement_internal(operation, step,
7226 slot,
7227 peer_key, peer_key_length);
7228 if (status != PSA_SUCCESS) {
7229 psa_key_derivation_abort(operation);
7230 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007231 /* If a private key has been added as SECRET, we allow the derived
7232 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007234 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007236 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007237
Gilles Peskine449bd832023-01-11 14:50:10 +01007238 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007239
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007241}
7242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7244 mbedtls_svc_key_id_t private_key,
7245 const uint8_t *peer_key,
7246 size_t peer_key_length,
7247 uint8_t *output,
7248 size_t output_size,
7249 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007250{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007252 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007253 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007254 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007255
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007257 status = PSA_ERROR_INVALID_ARGUMENT;
7258 goto exit;
7259 }
Ronald Cron5c522922020-11-14 16:35:34 +01007260 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7262 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007265
Gilles Peskine3e561302022-04-14 00:17:15 +02007266 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7267 * for the output size. The PSA specification only guarantees that this
7268 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7269 * but it might be nice to allow smaller buffers if the output fits.
7270 * At the time of writing this comment, with only ECDH implemented,
7271 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7272 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7273 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007274 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7276 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007277 status = PSA_ERROR_BUFFER_TOO_SMALL;
7278 goto exit;
7279 }
7280
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 status = psa_key_agreement_raw_internal(alg, slot,
7282 peer_key, peer_key_length,
7283 output, output_size,
7284 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007285
7286exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007288 /* If an error happens and is not handled properly, the output
7289 * may be used as a key to protect sensitive data. Arrange for such
7290 * a key to be random, which is likely to result in decryption or
7291 * verification errors. This is better than filling the buffer with
7292 * some constant data such as zeros, which would result in the data
7293 * being protected with a reproducible, easily knowable key.
7294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007296 *output_length = output_size;
7297 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007300
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007302}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007303
7304
Gilles Peskine30524eb2020-11-13 17:02:26 +01007305
Gilles Peskine86a440b2018-11-12 18:39:40 +01007306/****************************************************************/
7307/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007308/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007309
Gilles Peskine672a7712023-04-28 21:00:28 +02007310#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7311#include "entropy_poll.h"
7312#endif
7313
Gilles Peskine30524eb2020-11-13 17:02:26 +01007314/** Initialize the PSA random generator.
7315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007316static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007317{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007318#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007320#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7321
Gilles Peskine30524eb2020-11-13 17:02:26 +01007322 /* Set default configuration if
7323 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007325 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 }
7327 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007328 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007330
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007332#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7333 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7334 /* The PSA entropy injection feature depends on using NV seed as an entropy
7335 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 mbedtls_entropy_add_source(&rng->entropy,
7337 mbedtls_nv_seed_poll, NULL,
7338 MBEDTLS_ENTROPY_BLOCK_SIZE,
7339 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007340#endif
7341
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007343#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007344}
7345
7346/** Deinitialize the PSA random generator.
7347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007348static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007349{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007350#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007352#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007353 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7354 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007355#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007356}
7357
7358/** Seed the PSA random generator.
7359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007360static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007361{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007362#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7363 /* Do nothing: the external RNG seeds itself. */
7364 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007366#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007367 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007368 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7369 drbg_seed, sizeof(drbg_seed) - 1);
7370 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007371#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007372}
7373
Gilles Peskine449bd832023-01-11 14:50:10 +01007374psa_status_t psa_generate_random(uint8_t *output,
7375 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007376{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007377 GUARD_MODULE_INITIALIZED;
7378
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007379#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7380
7381 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7383 output, output_size,
7384 &output_length);
7385 if (status != PSA_SUCCESS) {
7386 return status;
7387 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007388 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007389 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 if (output_length != output_size) {
7391 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7392 }
7393 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007394
7395#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007398 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7400 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7401 output_size);
7402 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7403 output, request_size);
7404 if (ret != 0) {
7405 return mbedtls_to_psa_error(ret);
7406 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007407 output_size -= request_size;
7408 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007411#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007412}
7413
Gilles Peskine8814fc42020-12-14 15:33:44 +01007414/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007415 *
7416 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7417 * `psa_generate_random(...)`. The state parameter is ignored since the
7418 * PSA API doesn't support passing an explicit state.
7419 *
7420 * In the non-external case, psa_generate_random() calls an
7421 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7422 * and semantics as mbedtls_psa_get_random(). As an optimization,
7423 * instead of doing this back-and-forth between the PSA API and the
7424 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7425 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007427#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7428int mbedtls_psa_get_random(void *p_rng,
7429 unsigned char *output,
7430 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007431{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007432 /* This function takes a pointer to the RNG state because that's what
7433 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7434 * its own state internally and doesn't let the caller access that state.
7435 * So we just ignore the state parameter, and in practice we'll pass
7436 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007437 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 psa_status_t status = psa_generate_random(output, output_size);
7439 if (status == PSA_SUCCESS) {
7440 return 0;
7441 } else {
7442 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7443 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007444}
7445#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7446
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007447#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007448psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7449 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007450{
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if (global_data.initialized) {
7452 return PSA_ERROR_NOT_PERMITTED;
7453 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7456 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7457 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7458 return PSA_ERROR_INVALID_ARGUMENT;
7459 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007462}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007463#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007464
Ronald Cron3772afe2021-02-08 16:10:05 +01007465/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007466 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007467 * \param type The key type
7468 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007469 *
7470 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007471 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007472 * \retval #PSA_ERROR_INVALID_ARGUMENT
7473 * The size in bits of the key is not valid.
7474 * \retval #PSA_ERROR_NOT_SUPPORTED
7475 * The type and/or the size in bits of the key or the combination of
7476 * the two is not supported.
7477 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007478static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007480{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 if (key_type_is_raw_bytes(type)) {
7484 status = psa_validate_unstructured_key_bit_size(type, bits);
7485 if (status != PSA_SUCCESS) {
7486 return status;
7487 }
7488 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007489#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7491 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7492 return PSA_ERROR_NOT_SUPPORTED;
7493 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007494 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007495 return PSA_ERROR_NOT_SUPPORTED;
7496 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007497
Ronald Cron01b2aba2020-10-05 09:42:02 +02007498 /* Accept only byte-aligned keys, for the same reasons as
7499 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 if (bits % 8 != 0) {
7501 return PSA_ERROR_NOT_SUPPORTED;
7502 }
7503 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007504#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007505
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007506#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007508 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 return PSA_SUCCESS;
7510 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007511#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007512
Valerio Settia55f0422023-07-10 15:34:41 +02007513#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007514 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007515 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007516 return PSA_ERROR_NOT_SUPPORTED;
7517 }
7518 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007519#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007520 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007522 }
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007525}
7526
Ronald Cron55ed0592020-10-05 10:30:40 +02007527psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007528 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007530{
7531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007532 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007533
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if ((attributes->domain_parameters == NULL) &&
7535 (attributes->domain_parameters_size != 0)) {
7536 return PSA_ERROR_INVALID_ARGUMENT;
7537 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007538
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 if (key_type_is_raw_bytes(type)) {
7540 status = psa_generate_random(key_buffer, key_buffer_size);
7541 if (status != PSA_SUCCESS) {
7542 return status;
7543 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007544
David Brown12ca5032021-02-11 11:02:00 -07007545#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (type == PSA_KEY_TYPE_DES) {
7547 psa_des_set_key_parity(key_buffer, key_buffer_size);
7548 }
David Brown8107e312021-02-12 08:08:46 -07007549#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007551
Valerio Setti76df8c12023-07-11 14:11:28 +02007552#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7554 return mbedtls_psa_rsa_generate_key(attributes,
7555 key_buffer,
7556 key_buffer_size,
7557 key_buffer_length);
7558 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007560
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007561#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7563 return mbedtls_psa_ecp_generate_key(attributes,
7564 key_buffer,
7565 key_buffer_size,
7566 key_buffer_length);
7567 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007568#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007569
Valerio Settia55f0422023-07-10 15:34:41 +02007570#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007571 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7572 return mbedtls_psa_ffdh_generate_key(attributes,
7573 key_buffer,
7574 key_buffer_size,
7575 key_buffer_length);
7576 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007577#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007578 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 (void) key_buffer_length;
7580 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007581 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007584}
Darryl Green0c6575a2018-11-07 16:05:30 +00007585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7587 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007588{
7589 psa_status_t status;
7590 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007591 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007592 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007593
Ronald Cron81709fc2020-11-14 12:10:32 +01007594 *key = MBEDTLS_SVC_KEY_ID_INIT;
7595
Gilles Peskine0f84d622019-09-12 19:03:13 +02007596 /* Reject any attempt to create a zero-length key so that we don't
7597 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 if (psa_get_key_bits(attributes) == 0) {
7599 return PSA_ERROR_INVALID_ARGUMENT;
7600 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007601
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007602 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7604 return PSA_ERROR_INVALID_ARGUMENT;
7605 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7608 &slot, &driver);
7609 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007610 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 }
Gilles Peskine11792082019-08-06 18:36:36 +02007612
Ronald Cron977c2472020-10-13 08:32:21 +02007613 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307614 * storage ( thus not in the case of generating a key in a secure element
7615 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7616 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 if (slot->key.data == NULL) {
7618 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7619 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007620 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 attributes->core.type, attributes->core.bits);
7622 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007625
7626 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 attributes->core.type,
7628 attributes->core.bits);
7629 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007630 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 attributes, &key_buffer_size);
7632 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 }
Ronald Cron977c2472020-10-13 08:32:21 +02007635 }
Ronald Cron977c2472020-10-13 08:32:21 +02007636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7638 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007639 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 }
Ronald Cron977c2472020-10-13 08:32:21 +02007641 }
7642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 status = psa_driver_wrapper_generate_key(attributes,
7644 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007645
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 if (status != PSA_SUCCESS) {
7647 psa_remove_key_data_from_memory(slot);
7648 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007649
Gilles Peskine11792082019-08-06 18:36:36 +02007650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 if (status == PSA_SUCCESS) {
7652 status = psa_finish_key_creation(slot, driver, key);
7653 }
7654 if (status != PSA_SUCCESS) {
7655 psa_fail_key_creation(slot, driver);
7656 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007657
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007659}
7660
Gilles Peskinee59236f2018-01-27 23:32:46 +01007661/****************************************************************/
7662/* Module setup */
7663/****************************************************************/
7664
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007665#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007666psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 void (* entropy_init)(mbedtls_entropy_context *ctx),
7668 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007669{
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7671 return PSA_ERROR_BAD_STATE;
7672 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007673 global_data.rng.entropy_init = entropy_init;
7674 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007675 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007676}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007677#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007678
Gilles Peskine449bd832023-01-11 14:50:10 +01007679void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007680{
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 psa_wipe_all_key_slots();
7682 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7683 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007684 }
7685 /* Wipe all remaining data, including configuration.
7686 * In particular, this sets all state indicator to the value
7687 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007689
7690 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007692}
7693
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007694#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7695/** Recover a transaction that was interrupted by a power failure.
7696 *
7697 * This function is called during initialization, before psa_crypto_init()
7698 * returns. If this function returns a failure status, the initialization
7699 * fails.
7700 */
7701static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007703{
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007705 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7706 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 /* TODO - fall through to the failure case until this
7708 * is implemented.
7709 * https://github.com/ARMmbed/mbed-crypto/issues/218
7710 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007711 default:
7712 /* We found an unsupported transaction in the storage.
7713 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007715 }
7716}
7717#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7718
Gilles Peskine449bd832023-01-11 14:50:10 +01007719psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007720{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007721 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007722
Gilles Peskinec6b69072018-11-20 21:42:52 +01007723 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 if (global_data.initialized != 0) {
7725 return PSA_SUCCESS;
7726 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007727
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007728 /* Init drivers */
7729 status = psa_driver_wrapper_init();
7730 if (status != PSA_SUCCESS) {
7731 goto exit;
7732 }
7733 global_data.drivers_initialized = 1;
7734
Gilles Peskine30524eb2020-11-13 17:02:26 +01007735 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007737 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 status = mbedtls_psa_random_seed(&global_data.rng);
7739 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007740 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007742 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 status = psa_initialize_key_slots();
7745 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007748
Gilles Peskine4b734222019-07-24 15:56:31 +02007749#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 status = psa_crypto_load_transaction();
7751 if (status == PSA_SUCCESS) {
7752 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7753 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007754 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 }
7756 status = psa_crypto_stop_transaction();
7757 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007758 /* There's no transaction to complete. It's all good. */
7759 status = PSA_SUCCESS;
7760 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007761#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007762
Gilles Peskinec6b69072018-11-20 21:42:52 +01007763 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007764 global_data.initialized = 1;
7765
7766exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 if (status != PSA_SUCCESS) {
7768 mbedtls_psa_crypto_free();
7769 }
7770 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007771}
7772
Yanray Wangffc3c482023-07-11 12:01:04 +08007773#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007774psa_status_t psa_crypto_driver_pake_get_password_len(
7775 const psa_crypto_driver_pake_inputs_t *inputs,
7776 size_t *password_len)
7777{
7778 if (inputs->password_len == 0) {
7779 return PSA_ERROR_BAD_STATE;
7780 }
7781
7782 *password_len = inputs->password_len;
7783
7784 return PSA_SUCCESS;
7785}
7786
7787psa_status_t psa_crypto_driver_pake_get_password(
7788 const psa_crypto_driver_pake_inputs_t *inputs,
7789 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7790{
7791 if (inputs->password_len == 0) {
7792 return PSA_ERROR_BAD_STATE;
7793 }
7794
7795 if (buffer_size < inputs->password_len) {
7796 return PSA_ERROR_BUFFER_TOO_SMALL;
7797 }
7798
7799 memcpy(buffer, inputs->password, inputs->password_len);
7800 *buffer_length = inputs->password_len;
7801
7802 return PSA_SUCCESS;
7803}
7804
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007805psa_status_t psa_crypto_driver_pake_get_user_len(
7806 const psa_crypto_driver_pake_inputs_t *inputs,
7807 size_t *user_len)
7808{
7809 if (inputs->user_len == 0) {
7810 return PSA_ERROR_BAD_STATE;
7811 }
7812
7813 *user_len = inputs->user_len;
7814
7815 return PSA_SUCCESS;
7816}
7817
7818psa_status_t psa_crypto_driver_pake_get_user(
7819 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007820 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007821{
7822 if (inputs->user_len == 0) {
7823 return PSA_ERROR_BAD_STATE;
7824 }
7825
Przemek Stekielc0e62502023-03-14 11:49:36 +01007826 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007827 return PSA_ERROR_BUFFER_TOO_SMALL;
7828 }
7829
Przemek Stekielc0e62502023-03-14 11:49:36 +01007830 memcpy(user_id, inputs->user, inputs->user_len);
7831 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007832
7833 return PSA_SUCCESS;
7834}
7835
7836psa_status_t psa_crypto_driver_pake_get_peer_len(
7837 const psa_crypto_driver_pake_inputs_t *inputs,
7838 size_t *peer_len)
7839{
7840 if (inputs->peer_len == 0) {
7841 return PSA_ERROR_BAD_STATE;
7842 }
7843
7844 *peer_len = inputs->peer_len;
7845
7846 return PSA_SUCCESS;
7847}
7848
7849psa_status_t psa_crypto_driver_pake_get_peer(
7850 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007851 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007852{
7853 if (inputs->peer_len == 0) {
7854 return PSA_ERROR_BAD_STATE;
7855 }
7856
Przemek Stekielc0e62502023-03-14 11:49:36 +01007857 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007858 return PSA_ERROR_BUFFER_TOO_SMALL;
7859 }
7860
Przemek Stekielc0e62502023-03-14 11:49:36 +01007861 memcpy(peer_id, inputs->peer, inputs->peer_len);
7862 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007863
7864 return PSA_SUCCESS;
7865}
7866
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007867psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7868 const psa_crypto_driver_pake_inputs_t *inputs,
7869 psa_pake_cipher_suite_t *cipher_suite)
7870{
7871 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7872 return PSA_ERROR_BAD_STATE;
7873 }
7874
7875 *cipher_suite = inputs->cipher_suite;
7876
7877 return PSA_SUCCESS;
7878}
7879
Neil Armstronga7d08c32022-06-01 18:21:20 +02007880psa_status_t psa_pake_setup(
7881 psa_pake_operation_t *operation,
7882 const psa_pake_cipher_suite_t *cipher_suite)
7883{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007885
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007886 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007887 status = PSA_ERROR_BAD_STATE;
7888 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007889 }
7890
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007891 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007892 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007893 status = PSA_ERROR_INVALID_ARGUMENT;
7894 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007895 }
7896
Przemek Stekiel51eac532022-12-07 11:04:51 +01007897 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7898
Przemek Stekiele12ed362022-12-21 12:54:46 +01007899 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007900 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7901 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007902 operation->data.inputs.cipher_suite = *cipher_suite;
7903
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007904#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007905 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007906 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007907 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007908
David Horstmann16f01512023-06-14 17:21:07 +01007909 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007910 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007911 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007912#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007913 {
7914 status = PSA_ERROR_NOT_SUPPORTED;
7915 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007916 }
7917
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007918 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7919
Przemek Stekiel51eac532022-12-07 11:04:51 +01007920 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007921exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007922 psa_pake_abort(operation);
7923 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007924}
7925
7926psa_status_t psa_pake_set_password_key(
7927 psa_pake_operation_t *operation,
7928 mbedtls_svc_key_id_t password)
7929{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007931 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7932 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007933 psa_key_attributes_t attributes;
7934 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007935
Przemek Stekiel51eac532022-12-07 11:04:51 +01007936 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007937 status = PSA_ERROR_BAD_STATE;
7938 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007939 }
7940
Przemek Stekiel6c764412022-11-22 14:05:12 +01007941 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7942 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007943 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007944 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007945 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007946 }
7947
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007948 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007949 .core = slot->attr
7950 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007951
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007952 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007953
Przemek Stekiel51eac532022-12-07 11:04:51 +01007954 if (type != PSA_KEY_TYPE_PASSWORD &&
7955 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7956 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007957 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007958 }
7959
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7961 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007962 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007963 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007964 }
7965
7966 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7967 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007968 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007969exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007970 if (status != PSA_SUCCESS) {
7971 psa_pake_abort(operation);
7972 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007973 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007974 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007975}
7976
7977psa_status_t psa_pake_set_user(
7978 psa_pake_operation_t *operation,
7979 const uint8_t *user_id,
7980 size_t user_id_len)
7981{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007983
7984 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007985 status = PSA_ERROR_BAD_STATE;
7986 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007987 }
7988
Przemek Stekiel51eac532022-12-07 11:04:51 +01007989 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007990 status = PSA_ERROR_INVALID_ARGUMENT;
7991 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007992 }
7993
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007994 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007995 status = PSA_ERROR_BAD_STATE;
7996 goto exit;
7997 }
7998
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007999 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
8000 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008001 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8002 goto exit;
8003 }
8004
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008005 memcpy(operation->data.inputs.user, user_id, user_id_len);
8006 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008007
8008 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008009exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008010 psa_pake_abort(operation);
8011 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008012}
8013
8014psa_status_t psa_pake_set_peer(
8015 psa_pake_operation_t *operation,
8016 const uint8_t *peer_id,
8017 size_t peer_id_len)
8018{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008019 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008020
8021 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008022 status = PSA_ERROR_BAD_STATE;
8023 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008024 }
8025
Przemek Stekiel51eac532022-12-07 11:04:51 +01008026 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008027 status = PSA_ERROR_INVALID_ARGUMENT;
8028 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008029 }
8030
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008031 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008032 status = PSA_ERROR_BAD_STATE;
8033 goto exit;
8034 }
8035
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008036 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
8037 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008038 status = PSA_ERROR_INSUFFICIENT_MEMORY;
8039 goto exit;
8040 }
8041
Przemek Stekielf309d6b2023-03-10 09:54:45 +01008042 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
8043 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008044
8045 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008046exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008047 psa_pake_abort(operation);
8048 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008049}
8050
8051psa_status_t psa_pake_set_role(
8052 psa_pake_operation_t *operation,
8053 psa_pake_role_t role)
8054{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008056
Przemek Stekiel51eac532022-12-07 11:04:51 +01008057 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008058 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008059 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008060 }
8061
Przemek Stekiel09104b82023-03-06 14:11:51 +01008062 switch (operation->alg) {
8063#if defined(PSA_WANT_ALG_JPAKE)
8064 case PSA_ALG_JPAKE:
8065 if (role == PSA_PAKE_ROLE_NONE) {
8066 return PSA_SUCCESS;
8067 }
8068 status = PSA_ERROR_INVALID_ARGUMENT;
8069 break;
8070#endif
8071 default:
8072 (void) role;
8073 status = PSA_ERROR_NOT_SUPPORTED;
8074 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008075 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008076exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008077 psa_pake_abort(operation);
8078 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008079}
8080
David Horstmanne7f21e62023-05-12 18:17:21 +01008081/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008082#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008083static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008084 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008085{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008086 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008087 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008088 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008089
David Horstmann024e5c52023-06-14 15:48:21 +01008090 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008091 is_x1 = (stage->outputs < 1);
8092 } else {
8093 is_x1 = (stage->inputs < 1);
8094 }
8095
David Horstmann74a3d8c2023-06-14 18:28:19 +01008096 key_share_step = is_x1 ?
8097 PSA_JPAKE_X1_STEP_KEY_SHARE :
8098 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008099 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008100 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8101 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8102 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8103 } else {
8104 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008105 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008106 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008107}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008108#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008109
Przemek Stekiel2797d372022-12-22 11:19:22 +01008110static psa_status_t psa_pake_complete_inputs(
8111 psa_pake_operation_t *operation)
8112{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008114 /* Create copy of the inputs on stack as inputs share memory
8115 with the driver context which will be setup by the driver. */
8116 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008117
Przemek Stekielfde11282023-03-13 16:06:09 +01008118 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008119 return PSA_ERROR_BAD_STATE;
8120 }
8121
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008122 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008123 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8124 return PSA_ERROR_BAD_STATE;
8125 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008126 }
8127
Przemek Stekiel18620a32023-01-17 16:34:52 +01008128 /* Clear driver context */
8129 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8130
8131 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008132
8133 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008134 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008135
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008136 /* User and peer are translated to role. */
8137 mbedtls_free(inputs.user);
8138 mbedtls_free(inputs.peer);
8139
Przemek Stekiel2797d372022-12-22 11:19:22 +01008140 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008141#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008142 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008143 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008144 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008145#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008146 {
8147 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008148 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008149 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008150 return status;
8151}
8152
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008153#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008154static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008155 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008156 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008157 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008158{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008159 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8160 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8161 step != PSA_PAKE_STEP_ZK_PROOF) {
8162 return PSA_ERROR_INVALID_ARGUMENT;
8163 }
8164
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008165 psa_jpake_computation_stage_t *computation_stage =
8166 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008167
David Horstmann5da95602023-06-08 15:37:12 +01008168 if (computation_stage->round != PSA_JPAKE_FIRST &&
8169 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008170 return PSA_ERROR_BAD_STATE;
8171 }
8172
David Horstmanne7f21e62023-05-12 18:17:21 +01008173 /* Check that the step we are given is the one we were expecting */
8174 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008175 return PSA_ERROR_BAD_STATE;
8176 }
8177
David Horstmanne7f21e62023-05-12 18:17:21 +01008178 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8179 computation_stage->inputs == 0 &&
8180 computation_stage->outputs == 0) {
8181 /* Start of the round, so function decides whether we are inputting
8182 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008183 computation_stage->io_mode = io_mode;
8184 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008185 /* Middle of the round so the mode we are in must match the function
8186 * called by the user */
8187 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008188 }
8189
8190 return PSA_SUCCESS;
8191}
8192
David Horstmanne7f21e62023-05-12 18:17:21 +01008193static psa_status_t psa_jpake_epilogue(
8194 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008195 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008196{
David Horstmanne7f21e62023-05-12 18:17:21 +01008197 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008198 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008199
David Horstmanne7f21e62023-05-12 18:17:21 +01008200 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8201 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008202 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008203 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008204 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008205 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008206 }
8207 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008208 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008209 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008210 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008211 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008212 }
8213 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008214 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8215 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008216 /* End of a round, move to the next round */
8217 stage->inputs = 0;
8218 stage->outputs = 0;
8219 stage->round++;
8220 }
8221 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008222 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008223 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008225 return PSA_SUCCESS;
8226}
David Horstmanne7f21e62023-05-12 18:17:21 +01008227
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008228#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008229
Neil Armstronga7d08c32022-06-01 18:21:20 +02008230psa_status_t psa_pake_output(
8231 psa_pake_operation_t *operation,
8232 psa_pake_step_t step,
8233 uint8_t *output,
8234 size_t output_size,
8235 size_t *output_length)
8236{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008238 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008239 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008240
8241 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008242 status = psa_pake_complete_inputs(operation);
8243 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008244 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008245 }
8246 }
8247
8248 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 status = PSA_ERROR_BAD_STATE;
8250 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008251 }
8252
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008253 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008254 status = PSA_ERROR_INVALID_ARGUMENT;
8255 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008256 }
8257
Przemek Stekiele12ed362022-12-21 12:54:46 +01008258 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008259#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008260 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008261 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008262 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008263 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008264 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008265 driver_step = convert_jpake_computation_stage_to_driver_step(
8266 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008267 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008268#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008269 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008270 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008271 status = PSA_ERROR_NOT_SUPPORTED;
8272 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 }
8274
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008275 status = psa_driver_wrapper_pake_output(operation, driver_step,
8276 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277
8278 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008279 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 }
8281
8282 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008283#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008284 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008285 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008286 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008287 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008288 }
8289 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008290#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008291 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008292 status = PSA_ERROR_NOT_SUPPORTED;
8293 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008294 }
8295
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008296 return PSA_SUCCESS;
8297exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008298 psa_pake_abort(operation);
8299 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008300}
8301
8302psa_status_t psa_pake_input(
8303 psa_pake_operation_t *operation,
8304 psa_pake_step_t step,
8305 const uint8_t *input,
8306 size_t input_length)
8307{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008309 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008310 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8311 operation->primitive,
8312 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008313
8314 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008315 status = psa_pake_complete_inputs(operation);
8316 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008317 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008318 }
8319 }
8320
8321 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008322 status = PSA_ERROR_BAD_STATE;
8323 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008324 }
8325
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008326 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008327 status = PSA_ERROR_INVALID_ARGUMENT;
8328 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008329 }
8330
Przemek Stekiele12ed362022-12-21 12:54:46 +01008331 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008332#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008333 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008334 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008335 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008336 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008337 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008338 driver_step = convert_jpake_computation_stage_to_driver_step(
8339 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008340 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008341#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008343 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008344 status = PSA_ERROR_NOT_SUPPORTED;
8345 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008346 }
8347
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008348 status = psa_driver_wrapper_pake_input(operation, driver_step,
8349 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008350
8351 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008352 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008353 }
8354
8355 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008356#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008357 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008358 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008359 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008360 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008361 }
8362 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008363#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008364 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008365 status = PSA_ERROR_NOT_SUPPORTED;
8366 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008367 }
8368
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008369 return PSA_SUCCESS;
8370exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008371 psa_pake_abort(operation);
8372 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008373}
8374
8375psa_status_t psa_pake_get_implicit_key(
8376 psa_pake_operation_t *operation,
8377 psa_key_derivation_operation_t *output)
8378{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008380 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008381 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008382 size_t shared_key_len = 0;
8383
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008384 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008385 status = PSA_ERROR_BAD_STATE;
8386 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008387 }
8388
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008389#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008390 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008391 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008392 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008393 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008394 status = PSA_ERROR_BAD_STATE;
8395 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008396 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008397 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008398#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008399 {
8400 status = PSA_ERROR_NOT_SUPPORTED;
8401 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008402 }
8403
Przemek Stekiel0c781802022-11-29 14:53:13 +01008404 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8405 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008406 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008407 &shared_key_len);
8408
8409 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008410 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008411 }
8412
8413 status = psa_key_derivation_input_bytes(output,
8414 PSA_KEY_DERIVATION_INPUT_SECRET,
8415 shared_key,
8416 shared_key_len);
8417
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008418 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008419exit:
8420 abort_status = psa_pake_abort(operation);
8421 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008422}
8423
8424psa_status_t psa_pake_abort(
8425 psa_pake_operation_t *operation)
8426{
Przemek Stekield69dca92023-01-31 19:59:20 +01008427 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008428
Przemek Stekield69dca92023-01-31 19:59:20 +01008429 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008430 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008431 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008432
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008433 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8434 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008435 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008436 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008437 }
8438 if (operation->data.inputs.user != NULL) {
8439 mbedtls_free(operation->data.inputs.user);
8440 }
8441 if (operation->data.inputs.peer != NULL) {
8442 mbedtls_free(operation->data.inputs.peer);
8443 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008444 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008445 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008446
Przemek Stekield69dca92023-01-31 19:59:20 +01008447 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008448}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008449#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008450
Gilles Peskinee59236f2018-01-27 23:32:46 +01008451#endif /* MBEDTLS_PSA_CRYPTO_C */