blob: 028bdaaafb73ddf6edd7aa47ab6e897f7bcd6aec [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Ronald Cronafbc7ed2023-01-24 16:02:04 +010022#include "psa_crypto_core_common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010023
24#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030025
John Durkop07cc04a2020-11-16 22:08:34 -080026#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
27#include "check_crypto_config.h"
28#endif
29
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
Przemyslaw Stekiel705fb0f2021-11-24 08:47:29 +010031#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032
Ronald Crond6d28882020-12-14 14:56:02 +010033#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020036#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_ecp.h"
Przemek Stekiel359f4622022-12-05 14:11:55 +010038#include "psa_crypto_ffdh.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010040#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010042#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020044#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020045#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010046#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskineb2b64d32020-12-14 16:43:58 +010051#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010052
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#include "mbedtls/platform.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020062#include "mbedtls/chacha20.h"
63#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Dave Rodgman78701152023-08-29 14:20:18 +010067#include "mbedtls/constant_time.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000077#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020085#include "md_psa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
88 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
89 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Przemek Stekiel69c46792022-06-10 12:59:51 +020090#define BUILTIN_ALG_ANY_HKDF 1
Przemek Stekiel75fe3fb2022-06-09 14:44:55 +020091#endif
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093/****************************************************************/
94/* Global data, support functions and library management */
95/****************************************************************/
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int key_type_is_raw_bytes(psa_key_type_t type)
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_KEY_TYPE_IS_UNSTRUCTURED(type);
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200100}
101
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100102/* Values for psa_global_data_t::rng_state */
103#define RNG_NOT_INITIALIZED 0
104#define RNG_INITIALIZED 1
105#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct {
Dave Rodgman864f5942023-08-16 18:04:44 +0100108 uint8_t initialized;
109 uint8_t rng_state;
110 uint8_t drivers_initialized;
Gilles Peskine2d8a1822021-11-08 22:20:03 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112} psa_global_data_t;
113
114static psa_global_data_t global_data;
115
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100116#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
117mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
118 &global_data.rng.drbg;
119#endif
120
itayzafrir0adf0fc2018-09-06 16:24:41 +0300121#define GUARD_MODULE_INITIALIZED \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (global_data.initialized == 0) \
123 return PSA_ERROR_BAD_STATE;
itayzafrir0adf0fc2018-09-06 16:24:41 +0300124
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100125int psa_can_do_hash(psa_algorithm_t hash_alg)
126{
127 (void) hash_alg;
128 return global_data.drivers_initialized;
129}
Valerio Settia55f0422023-07-10 15:34:41 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Settia55f0422023-07-10 15:34:41 +0200132 defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekiel134cc2e2023-05-05 10:13:37 +0200133static int psa_is_dh_key_size_valid(size_t bits)
134{
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200135 if (bits != 2048 && bits != 3072 && bits != 4096 &&
136 bits != 6144 && bits != 8192) {
137 return 0;
138 }
139
140 return 1;
141}
Valerio Settia55f0422023-07-10 15:34:41 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Settia55f0422023-07-10 15:34:41 +0200144 PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146psa_status_t mbedtls_to_psa_error(int ret)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100148 /* Mbed TLS error codes can combine a high-level error code and a
149 * low-level error code. The low-level error usually reflects the
150 * root cause better, so dispatch on that preferably. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 int low_level_ret = -(-ret & 0x007f);
152 switch (low_level_ret != 0 ? low_level_ret : ret) {
Gilles Peskinee59236f2018-01-27 23:32:46 +0100153 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return PSA_SUCCESS;
Gilles Peskinea5905292018-02-07 20:59:33 +0100155
Dave Rodgman509b5672023-08-16 19:26:23 +0100156#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
158 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman09a9e582023-08-31 11:05:22 +0100160 case MBEDTLS_ERR_AES_BAD_INPUT_DATA:
161 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100162#endif
163
164#if defined(MBEDTLS_ASN1_PARSE_C) || defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
166 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
167 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
168 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
169 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200171 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200173 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100175#endif
Gilles Peskine9a944802018-06-21 09:35:35 +0200176
Dave Rodgman509b5672023-08-16 19:26:23 +0100177#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100183#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100184
Dave Rodgman509b5672023-08-16 19:26:23 +0100185#if defined(PSA_WANT_ALG_CCM)
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100190#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100191
Dave Rodgman509b5672023-08-16 19:26:23 +0100192#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100195#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200196
Dave Rodgman509b5672023-08-16 19:26:23 +0100197#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100202#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200203
Dave Rodgman509b5672023-08-16 19:26:23 +0100204#if defined(MBEDTLS_CIPHER_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100219#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
Dave Rodgman509b5672023-08-16 19:26:23 +0100234#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return PSA_ERROR_NOT_SUPPORTED;
Dave Rodgman509b5672023-08-16 19:26:23 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
Gilles Peskinee59236f2018-01-27 23:32:46 +0100239 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
240 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
241 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100243
Dave Rodgman509b5672023-08-16 19:26:23 +0100244#if defined(PSA_WANT_ALG_GCM)
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200247 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100251#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100264#endif
265
Dave Rodgman509b5672023-08-16 19:26:23 +0100266#if defined(MBEDTLS_MD_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100273#if defined(MBEDTLS_FS_IO)
Gilles Peskinea5905292018-02-07 20:59:33 +0100274 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100276#endif
277#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100278
Dave Rodgman509b5672023-08-16 19:26:23 +0100279#if defined(MBEDTLS_BIGNUM_C)
280#if defined(MBEDTLS_FS_IO)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100281 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100283#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return PSA_ERROR_INSUFFICIENT_MEMORY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100298#endif
Gilles Peskinef76aa772018-10-29 19:24:33 +0100299
Dave Rodgman509b5672023-08-16 19:26:23 +0100300#if defined(MBEDTLS_PK_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
304 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_ARGUMENT;
Dave Rodgman509b5672023-08-16 19:26:23 +0100306#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || defined(MBEDTLS_FS_IO) || \
307 defined(MBEDTLS_PSA_ITS_FILE_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_STORAGE_FAILURE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100310#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
312 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100314 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100316 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
317 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 case MBEDTLS_ERR_PK_INVALID_ALG:
322 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
323 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200327 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman509b5672023-08-16 19:26:23 +0100329#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100330
Gilles Peskineff2d2002019-05-06 15:26:23 +0200331 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200333 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200335
Dave Rodgman509b5672023-08-16 19:26:23 +0100336#if defined(MBEDTLS_RSA_C)
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100339 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100343 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
346 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Dave Rodgman509b5672023-08-16 19:26:23 +0100354#endif
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200355
Dave Rodgman509b5672023-08-16 19:26:23 +0100356#if defined(MBEDTLS_ECP_C)
itayzafrir5c753392018-05-08 11:18:38 +0300357 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300358 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300360 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
365 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300367 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100369 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100371
Dave Rodgman509b5672023-08-16 19:26:23 +0100372#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +0000373 case MBEDTLS_ERR_ECP_IN_PROGRESS:
374 return PSA_OPERATION_INCOMPLETE;
Dave Rodgman509b5672023-08-16 19:26:23 +0100375#endif
376#endif
Paul Elliott588f8ed2022-12-02 18:10:26 +0000377
Janos Follatha13b9052019-11-22 12:48:59 +0000378 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300380
Gilles Peskinee59236f2018-01-27 23:32:46 +0100381 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100383 }
384}
385
Paul Elliottdc42ca82023-02-24 18:11:59 +0000386/**
387 * \brief For output buffers which contain "tags"
388 * (outputs that may be checked for validity like
389 * hashes, MACs and signatures), fill the unused
390 * part of the output buffer (the whole buffer on
391 * error, the trailing part on success) with
392 * something that isn't a valid tag (barring an
393 * attack on the tag and deliberately-crafted
394 * input), in case the caller doesn't check the
395 * return status properly.
396 *
397 * \param output_buffer Pointer to buffer to wipe. May not be NULL
398 * unless \p output_buffer_size is zero.
399 * \param status Status of function called to generate
400 * output_buffer originally
401 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
402 * could be NULL.
403 * \param output_buffer_length Length of data written to output_buffer, must be
404 * less than \p output_buffer_size
405 */
406static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000407 size_t output_buffer_size, size_t output_buffer_length)
408{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000409 size_t offset = 0;
410
411 if (output_buffer_size == 0) {
412 /* If output_buffer_size is 0 then we have nothing to do. We must not
413 call memset because output_buffer may be NULL in this case */
414 return;
415 }
416
417 if (status == PSA_SUCCESS) {
418 offset = output_buffer_length;
419 }
420
421 memset(output_buffer + offset, '!', output_buffer_size - offset);
422}
423
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200424
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200425
426
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427/****************************************************************/
428/* Key management */
429/****************************************************************/
430
Valerio Settia9aab1a2023-06-19 13:39:54 +0200431#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200432psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
433 size_t *bits)
434{
435 switch (grpid) {
Dave Rodgman6f682032023-08-16 18:44:32 +0100436#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200437 case MBEDTLS_ECP_DP_SECP192R1:
438 *bits = 192;
439 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100440#endif
441#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200442 case MBEDTLS_ECP_DP_SECP224R1:
443 *bits = 224;
444 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100445#endif
446#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200447 case MBEDTLS_ECP_DP_SECP256R1:
448 *bits = 256;
449 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100450#endif
451#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200452 case MBEDTLS_ECP_DP_SECP384R1:
453 *bits = 384;
454 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100455#endif
456#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200457 case MBEDTLS_ECP_DP_SECP521R1:
458 *bits = 521;
459 return PSA_ECC_FAMILY_SECP_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100460#endif
461#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200462 case MBEDTLS_ECP_DP_BP256R1:
463 *bits = 256;
464 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100465#endif
466#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200467 case MBEDTLS_ECP_DP_BP384R1:
468 *bits = 384;
469 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100470#endif
471#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200472 case MBEDTLS_ECP_DP_BP512R1:
473 *bits = 512;
474 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100475#endif
476#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200477 case MBEDTLS_ECP_DP_CURVE25519:
478 *bits = 255;
479 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100480#endif
481#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200482 case MBEDTLS_ECP_DP_SECP192K1:
483 *bits = 192;
484 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100485#endif
486#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200487 case MBEDTLS_ECP_DP_SECP224K1:
488 *bits = 224;
489 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100490#endif
491#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200492 case MBEDTLS_ECP_DP_SECP256K1:
493 *bits = 256;
494 return PSA_ECC_FAMILY_SECP_K1;
Dave Rodgman6f682032023-08-16 18:44:32 +0100495#endif
496#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200497 case MBEDTLS_ECP_DP_CURVE448:
498 *bits = 448;
499 return PSA_ECC_FAMILY_MONTGOMERY;
Dave Rodgman6f682032023-08-16 18:44:32 +0100500#endif
Valerio Settibc2b1d32023-06-19 12:15:13 +0200501 default:
502 *bits = 0;
503 return 0;
504 }
505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
508 size_t bits,
509 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200510{
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100512 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100514#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100515 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100517#endif
518#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100519 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100521#endif
522#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100523 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100525#endif
526#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100527 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100529#endif
530#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100531 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100533 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (bits_is_sloppy) {
535 return MBEDTLS_ECP_DP_SECP521R1;
536 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100537 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100538#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100539 }
540 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100541
Paul Elliott8ff510a2020-06-02 17:19:28 +0100542 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100544#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100545 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100547#endif
548#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100549 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100551#endif
552#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100553 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100555#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100556 }
557 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100558
Paul Elliott8ff510a2020-06-02 17:19:28 +0100559 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100561#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100562 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100564 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (bits_is_sloppy) {
566 return MBEDTLS_ECP_DP_CURVE25519;
567 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100568 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100569#endif
570#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100571 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100573#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100574 }
575 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100576
Paul Elliott8ff510a2020-06-02 17:19:28 +0100577 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100579#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100580 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100582#endif
583#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100584 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100586#endif
587#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100588 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100590#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100591 }
592 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200593 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100594
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100595 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200597}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200598#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
601 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200602{
603 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200606 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200607 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200608 case PSA_KEY_TYPE_PASSWORD:
609 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200610 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100611#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200612 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 if (bits != 128 && bits != 192 && bits != 256) {
614 return PSA_ERROR_INVALID_ARGUMENT;
615 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200616 break;
617#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200618#if defined(PSA_WANT_KEY_TYPE_ARIA)
619 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (bits != 128 && bits != 192 && bits != 256) {
621 return PSA_ERROR_INVALID_ARGUMENT;
622 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200623 break;
624#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100625#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200626 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (bits != 128 && bits != 192 && bits != 256) {
628 return PSA_ERROR_INVALID_ARGUMENT;
629 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200630 break;
631#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100632#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200633 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (bits != 64 && bits != 128 && bits != 192) {
635 return PSA_ERROR_INVALID_ARGUMENT;
636 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 break;
638#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100639#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200640 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (bits != 256) {
642 return PSA_ERROR_INVALID_ARGUMENT;
643 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200644 break;
645#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200646 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200648 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (bits % 8 != 0) {
650 return PSA_ERROR_INVALID_ARGUMENT;
651 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200654}
655
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100656/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100657 *
Steven Cooremancd640932021-03-08 12:00:27 +0100658 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
659 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100660 *
661 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100662 * \param[in] key_type The key type of the key to be used with the
663 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100664 *
665 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100666 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100667 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100668 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100669 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100670MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100671 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (PSA_ALG_IS_HMAC(algorithm)) {
675 if (key_type == PSA_KEY_TYPE_HMAC) {
676 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100677 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100678 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
681 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
682 * key. */
683 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
684 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
685 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
686 * the block length (larger than 1) for block ciphers. */
687 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
688 return PSA_SUCCESS;
689 }
690 }
691 }
692
693 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100694}
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
697 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200698{
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (slot->key.data != NULL) {
700 return PSA_ERROR_ALREADY_EXISTS;
701 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 slot->key.data = mbedtls_calloc(1, buffer_length);
704 if (slot->key.data == NULL) {
705 return PSA_ERROR_INSUFFICIENT_MEMORY;
706 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200707
Ronald Cronea0f8a62020-11-25 17:52:23 +0100708 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200710}
711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
713 const uint8_t *data,
714 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200715{
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 psa_status_t status = psa_allocate_buffer_to_slot(slot,
717 data_length);
718 if (status != PSA_SUCCESS) {
719 return status;
720 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 memcpy(slot->key.data, data, data_length);
723 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200724}
725
Ronald Crona0fe59f2020-11-29 11:14:53 +0100726psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100727 const psa_key_attributes_t *attributes,
728 const uint8_t *data, size_t data_length,
729 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100732 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
733 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200735 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (data_length == 0) {
737 return PSA_ERROR_NOT_SUPPORTED;
738 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (key_type_is_raw_bytes(type)) {
741 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
744 *bits);
745 if (status != PSA_SUCCESS) {
746 return status;
747 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200748
Ronald Crondd04d422020-11-28 15:14:42 +0100749 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100751 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return PSA_SUCCESS;
755 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +0200756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100758 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200759 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100760 return PSA_ERROR_INVALID_ARGUMENT;
761 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200762 return mbedtls_psa_ffdh_import_key(attributes,
763 data, data_length,
764 key_buffer, key_buffer_size,
765 key_buffer_length,
766 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100767 }
Valerio Settia55f0422023-07-10 15:34:41 +0200768#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_IMPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200769 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200770#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
772 if (PSA_KEY_TYPE_IS_ECC(type)) {
773 return mbedtls_psa_ecp_import_key(attributes,
774 data, data_length,
775 key_buffer, key_buffer_size,
776 key_buffer_length,
777 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100778 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200779#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800780 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200781#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
782 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
784 if (PSA_KEY_TYPE_IS_RSA(type)) {
785 return mbedtls_psa_rsa_import_key(attributes,
786 data, data_length,
787 key_buffer, key_buffer_size,
788 key_buffer_length,
789 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200790 }
Valerio Settife478902023-07-25 12:27:19 +0200791#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
792 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800793 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100794 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100797}
798
Gilles Peskinef603c712019-01-19 13:40:11 +0100799/** Calculate the intersection of two algorithm usage policies.
800 *
801 * Return 0 (which allows no operation) on incompatibility.
802 */
803static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100804 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100805 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100807{
Gilles Peskine549ea862019-05-22 11:45:59 +0200808 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (alg1 == alg2) {
810 return alg1;
811 }
Steven Cooreman03488022021-02-18 12:07:20 +0100812 /* If the policies are from the same hash-and-sign family, check
813 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
815 PSA_ALG_IS_SIGN_HASH(alg2) &&
816 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
817 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
818 return alg2;
819 }
820 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
821 return alg1;
822 }
Steven Cooreman03488022021-02-18 12:07:20 +0100823 }
824 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100825 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100826 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
828 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
829 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
830 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
831 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100832 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100833
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100834 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
836 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
837 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
838 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100839 }
840 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
842 (alg1_len <= alg2_len)) {
843 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100844 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
846 (alg2_len <= alg1_len)) {
847 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100848 }
849 }
850 /* If the policies are from the same MAC family, check whether one
851 * of them is a minimum-MAC-length policy. Calculate the most
852 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
854 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
855 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100856 /* Validate the combination of key type and algorithm. Since the base
857 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
859 return 0;
860 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100861
Steven Cooreman31a876d2021-03-03 20:47:40 +0100862 /* Get the (exact or at-least) output lengths for both sides of the
863 * requested intersection. None of the currently supported algorithms
864 * have an output length dependent on the actual key size, so setting it
865 * to a bogus value of 0 is currently OK.
866 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100867 * Note that for at-least-this-length wildcard algorithms, the output
868 * length is set to the shortest allowed length, which allows us to
869 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
871 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100872 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873
Steven Cooremana1d83222021-02-25 10:20:29 +0100874 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
876 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
877 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100878 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100879
880 /* If only one is an at-least-this-length policy, the intersection would
881 * be the other (fixed-length) policy as long as said fixed length is
882 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
884 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100885 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
887 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100888 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100889
Steven Cooremancd640932021-03-08 12:00:27 +0100890 /* If none of them are wildcards, check whether they define the same tag
891 * length. This is still possible here when one is default-length and
892 * the other specific-length. Ensure to always return the
893 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (alg1_len == alg2_len) {
895 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
896 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100897 }
898 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100900}
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902static int psa_key_algorithm_permits(psa_key_type_t key_type,
903 psa_algorithm_t policy_alg,
904 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200905{
Gilles Peskine549ea862019-05-22 11:45:59 +0200906 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (requested_alg == policy_alg) {
908 return 1;
909 }
Steven Cooreman03488022021-02-18 12:07:20 +0100910 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
911 * and requested_alg is the same hash-and-sign family with any hash,
912 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
914 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
915 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
916 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100917 }
918 /* If policy_alg is a wildcard AEAD algorithm of the same base as
919 * the requested algorithm, check the requested tag length to be
920 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (PSA_ALG_IS_AEAD(policy_alg) &&
922 PSA_ALG_IS_AEAD(requested_alg) &&
923 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
924 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
925 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
926 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
927 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100928 }
Steven Cooremancd640932021-03-08 12:00:27 +0100929 /* If policy_alg is a MAC algorithm of the same base as the requested
930 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (PSA_ALG_IS_MAC(policy_alg) &&
932 PSA_ALG_IS_MAC(requested_alg) &&
933 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
934 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100935 /* Validate the combination of key type and algorithm. Since the policy
936 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
938 return 0;
939 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100940
Steven Cooreman31a876d2021-03-03 20:47:40 +0100941 /* Get both the requested output length for the algorithm which is to be
942 * verified, and the default output length for the base algorithm.
943 * Note that none of the currently supported algorithms have an output
944 * length dependent on actual key size, so setting it to a bogus value
945 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100946 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100948 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 key_type, 0,
950 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100951
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100952 /* If the policy is default-length, only allow an algorithm with
953 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
955 return requested_output_length == default_output_length;
956 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100957
958 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100959 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
961 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
962 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100963 }
964
Steven Cooremancd640932021-03-08 12:00:27 +0100965 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
966 * check for the requested MAC length to be equal to or longer than the
967 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
969 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
970 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100971 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200972 }
Steven Cooremance48e852020-10-05 16:02:45 +0200973 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200974 * a key derivation with that key agreement should also be allowed. This
975 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
977 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
978 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
979 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200980 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100981 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200983}
984
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100985/** Test whether a policy permits an algorithm.
986 *
987 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100988 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100989 * \note This function requires providing the key type for which the policy is
990 * being validated, since some algorithm policy definitions (e.g. MAC)
991 * have different properties depending on what kind of cipher it is
992 * combined with.
993 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100994 * \retval PSA_SUCCESS When \p alg is a specific algorithm
995 * allowed by the \p policy.
996 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
997 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
998 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
1001 psa_key_type_t key_type,
1002 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001003{
Steven Cooremand788fab2021-02-25 11:29:17 +01001004 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (alg == 0) {
1006 return PSA_ERROR_INVALID_ARGUMENT;
1007 }
Steven Cooremand788fab2021-02-25 11:29:17 +01001008
Steven Cooreman7e39f052021-02-23 14:18:32 +01001009 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (PSA_ALG_IS_WILDCARD(alg)) {
1011 return PSA_ERROR_INVALID_ARGUMENT;
1012 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
1015 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
1016 return PSA_SUCCESS;
1017 } else {
1018 return PSA_ERROR_NOT_PERMITTED;
1019 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020}
1021
Gilles Peskinef603c712019-01-19 13:40:11 +01001022/** Restrict a key policy based on a constraint.
1023 *
Steven Cooreman5a172672021-03-02 21:27:42 +01001024 * \note This function requires providing the key type for which the policy is
1025 * being restricted, since some algorithm policy definitions (e.g. MAC)
1026 * have different properties depending on what kind of cipher it is
1027 * combined with.
1028 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001029 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +01001030 * \param[in,out] policy The policy to restrict.
1031 * \param[in] constraint The policy constraint to apply.
1032 *
1033 * \retval #PSA_SUCCESS
1034 * \c *policy contains the intersection of the original value of
1035 * \c *policy and \c *constraint.
1036 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001037 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +01001038 * \c *policy is unchanged.
1039 */
1040static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01001041 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +01001042 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +01001044{
1045 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 psa_key_policy_algorithm_intersection(key_type, policy->alg,
1047 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +02001048 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
1050 constraint->alg2);
1051 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
1052 return PSA_ERROR_INVALID_ARGUMENT;
1053 }
1054 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
1055 return PSA_ERROR_INVALID_ARGUMENT;
1056 }
Gilles Peskinef603c712019-01-19 13:40:11 +01001057 policy->usage &= constraint->usage;
1058 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001059 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +01001061}
1062
Przemek Stekiel061f6942022-11-30 10:51:35 +01001063/** Get the description of a key given its identifier and policy constraints
1064 * and lock it.
1065 *
1066 * The key must have allow all the usage flags set in \p usage. If \p alg is
1067 * nonzero, the key must allow operations with this algorithm. If \p alg is
1068 * zero, the algorithm is not checked.
1069 *
1070 * In case of a persistent key, the function loads the description of the key
1071 * into a key slot if not already done.
1072 *
1073 * On success, the returned key slot is locked. It is the responsibility of
1074 * the caller to unlock the key slot when it does not access it anymore.
1075 */
1076static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001077 mbedtls_svc_key_id_t key,
1078 psa_key_slot_t **p_slot,
1079 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001081{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001083 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 status = psa_get_and_lock_key_slot(key, p_slot);
1086 if (status != PSA_SUCCESS) {
1087 return status;
1088 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001089 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001090
1091 /* Enforce that usage policy for the key slot contains all the flags
1092 * required by the usage parameter. There is one exception: public
1093 * keys can always be exported, so we treat public key objects as
1094 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001096 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001100 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001101 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001102 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001103
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001104 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (alg != 0) {
1106 status = psa_key_policy_permits(&slot->attr.policy,
1107 slot->attr.type,
1108 alg);
1109 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001110 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001112 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001115
1116error:
1117 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001121}
Darryl Green940d72c2018-07-13 13:18:51 +01001122
Ronald Cron5c522922020-11-14 16:35:34 +01001123/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001124 *
1125 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001126 * available, as opposed to a key in a secure element and/or to be used
1127 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001128 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001129 * This is a temporary function that may be used instead of
1130 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1131 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001132 *
Ronald Cron5c522922020-11-14 16:35:34 +01001133 * On success, the returned key slot is locked. It is the responsibility of the
1134 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001135 */
Ronald Cron5c522922020-11-14 16:35:34 +01001136static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1137 mbedtls_svc_key_id_t key,
1138 psa_key_slot_t **p_slot,
1139 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001141{
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1143 usage, alg);
1144 if (status != PSA_SUCCESS) {
1145 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001146 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1149 psa_unlock_key_slot(*p_slot);
1150 *p_slot = NULL;
1151 return PSA_ERROR_NOT_SUPPORTED;
1152 }
1153
1154 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001155}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001158{
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (slot->key.data != NULL) {
Tom Cosgrove52f7e182023-08-01 09:08:48 +01001160 mbedtls_zeroize_and_free(slot->key.data, slot->key.bytes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001162
Ronald Cronea0f8a62020-11-25 17:52:23 +01001163 slot->key.data = NULL;
1164 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001167}
1168
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001169/** Completely wipe a slot in memory, including its policy.
1170 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001172{
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 /*
1176 * As the return error code may not be handled in case of multiple errors,
1177 * do our best to report an unexpected lock counter. Assert with
1178 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1179 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1180 * function is called as part of the execution of a test suite, the
1181 * execution of the test suite is stopped in error if the assertion fails.
1182 */
1183 if (slot->lock_count != 1) {
1184 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001185 status = PSA_ERROR_CORRUPTION_DETECTED;
1186 }
1187
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001188 /* Multipart operations may still be using the key. This is safe
1189 * because all multipart operation objects are independent from
1190 * the key slot: if they need to access the key after the setup
1191 * phase, they have a copy of the key. Note that this means that
1192 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001193 /* At this point, key material and other type-specific content has
1194 * been wiped. Clear remaining metadata. We can call memset and not
1195 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 memset(slot, 0, sizeof(*slot));
1197 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001198}
1199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001202 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001203 psa_status_t status; /* status of the last operation */
1204 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1206 psa_se_drv_table_entry_t *driver;
1207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_svc_key_id_is_null(key)) {
1210 return PSA_SUCCESS;
1211 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001212
Ronald Cronf2911112020-10-29 17:51:10 +01001213 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001214 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001215 * key, this will load the key description from persistent memory if not
1216 * done yet. We cannot avoid this loading as without it we don't know if
1217 * the key is operated by an SE or not and this information is needed by
1218 * the current implementation.
1219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 status = psa_get_and_lock_key_slot(key, &slot);
1221 if (status != PSA_SUCCESS) {
1222 return status;
1223 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001224
Ronald Cronf2911112020-10-29 17:51:10 +01001225 /*
1226 * If the key slot containing the key description is under access by the
1227 * library (apart from the present access), the key cannot be destroyed
1228 * yet. For the time being, just return in error. Eventually (to be
1229 * implemented), the key should be destroyed when all accesses have
1230 * stopped.
1231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (slot->lock_count > 1) {
1233 psa_unlock_key_slot(slot);
1234 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001238 /* Refuse the destruction of a read-only key (which may or may not work
1239 * if we attempt it, depending on whether the key is merely read-only
1240 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001241 * Just do the best we can, which is to wipe the copy in memory
1242 * (done in this function's cleanup code). */
1243 overall_status = PSA_ERROR_NOT_PERMITTED;
1244 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001245 }
1246
Gilles Peskine354f7672019-07-12 23:46:38 +02001247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1249 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001250 /* For a key in a secure element, we need to do three things:
1251 * remove the key file in internal storage, destroy the
1252 * key inside the secure element, and update the driver's
1253 * persistent data. Start a transaction that will encompass these
1254 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001256 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001258 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 status = psa_crypto_save_transaction();
1260 if (status != PSA_SUCCESS) {
1261 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 /* We should still try to destroy the key in the secure
1263 * element and the key metadata in storage. This is especially
1264 * important if the error is that the storage is full.
1265 * But how to do it exactly without risking an inconsistent
1266 * state after a reset?
1267 * https://github.com/ARMmbed/mbed-crypto/issues/215
1268 */
1269 overall_status = status;
1270 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 }
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 status = psa_destroy_se_key(driver,
1274 psa_key_slot_get_slot_number(slot));
1275 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001276 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001278 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1280
Darryl Greend49a4992018-06-18 17:27:26 +01001281#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1283 status = psa_destroy_persistent_key(slot->attr.id);
1284 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001285 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001288 /* TODO: other slots may have a copy of the same key. We should
1289 * invalidate them.
1290 * https://github.com/ARMmbed/mbed-crypto/issues/214
1291 */
Darryl Greend49a4992018-06-18 17:27:26 +01001292 }
1293#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001294
Gilles Peskinefc762652019-07-22 19:30:34 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (driver != NULL) {
1297 status = psa_save_se_persistent_data(driver);
1298 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 }
1301 status = psa_crypto_stop_transaction();
1302 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001305 }
1306#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1307
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001310 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001312 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 }
1314 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315}
1316
Valerio Settib2bcedb2023-07-10 11:24:00 +02001317#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001318 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001319static psa_status_t psa_get_rsa_public_exponent(
1320 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001322{
1323 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001325 uint8_t *buffer = NULL;
1326 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1330 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 }
1333 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001334 /* It's the default value, which is reported as an empty string,
1335 * so there's nothing to do. */
1336 goto exit;
1337 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 buflen = mbedtls_mpi_size(&mpi);
1340 buffer = mbedtls_calloc(1, buflen);
1341 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001342 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1343 goto exit;
1344 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1346 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001347 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001349 attributes->domain_parameters = buffer;
1350 attributes->domain_parameters_size = buflen;
1351
1352exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 mbedtls_mpi_free(&mpi);
1354 if (ret != 0) {
1355 mbedtls_free(buffer);
1356 }
1357 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001358}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001359#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001360 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001361
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001362/** Retrieve all the publicly-accessible attributes of a key.
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1365 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001368 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001369 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1374 if (status != PSA_SUCCESS) {
1375 return status;
1376 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001377
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001378 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1380 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381
1382#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1384 psa_set_key_slot_number(attributes,
1385 psa_key_slot_get_slot_number(slot));
1386 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001387#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001390#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1391 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001392 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001393 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001394 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001395 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001396 * is not yet implemented.
1397 * https://github.com/ARMmbed/mbed-crypto/issues/216
1398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001400 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001401
Ronald Cron90857082020-11-25 14:58:33 +01001402 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 slot->attr.type,
1404 slot->key.data,
1405 slot->key.bytes,
1406 &rsa);
1407 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001408 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 status = psa_get_rsa_public_exponent(rsa,
1412 attributes);
1413 mbedtls_rsa_free(rsa);
1414 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001415 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001416 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001417#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1418 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001419 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001420 default:
1421 /* Nothing else to do. */
1422 break;
1423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (status != PSA_SUCCESS) {
1426 psa_reset_key_attributes(attributes);
1427 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
1433
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1435psa_status_t psa_get_key_slot_number(
1436 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438{
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001440 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return PSA_SUCCESS;
1442 } else {
1443 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001444 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001445}
1446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1449 size_t key_buffer_size,
1450 uint8_t *data,
1451 size_t data_size,
1452 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001453{
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (key_buffer_size > data_size) {
1455 return PSA_ERROR_BUFFER_TOO_SMALL;
1456 }
1457 memcpy(data, key_buffer, key_buffer_size);
1458 memset(data + key_buffer_size, 0,
1459 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001462}
1463
Ronald Cron7285cda2020-11-26 14:46:37 +01001464psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 const psa_key_attributes_t *attributes,
1466 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468{
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 psa_key_type_t type = attributes->core.type;
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (key_type_is_raw_bytes(type) ||
1472 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001473 PSA_KEY_TYPE_IS_ECC(type) ||
1474 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return psa_export_key_buffer_internal(
1476 key_buffer, key_buffer_size,
1477 data, data_size, data_length);
1478 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001479 /* This shouldn't happen in the reference implementation, but
1480 it is valid for a special-purpose implementation to omit
1481 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001483 }
1484}
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1487 uint8_t *data,
1488 size_t data_size,
1489 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001490{
1491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1492 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1493 psa_key_slot_t *slot;
1494
Ronald Crone907e552021-01-18 13:22:38 +01001495 /* Reject a zero-length output buffer now, since this can never be a
1496 * valid key representation. This way we know that data must be a valid
1497 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (data_size == 0) {
1499 return PSA_ERROR_BUFFER_TOO_SMALL;
1500 }
Ronald Crone907e552021-01-18 13:22:38 +01001501
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 /* Set the key to empty now, so that even when there are errors, we always
1503 * set data_length to a value between 0 and data_size. On error, setting
1504 * the key to empty is a good choice because an empty key representation is
1505 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001506 *data_length = 0;
1507
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508 /* Export requires the EXPORT flag. There is an exception for public keys,
1509 * which don't require any flag, but
1510 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1513 PSA_KEY_USAGE_EXPORT, 0);
1514 if (status != PSA_SUCCESS) {
1515 return status;
1516 }
mohammad160306e79202018-03-28 13:17:44 +03001517
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 psa_key_attributes_t attributes = {
1519 .core = slot->attr
1520 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 status = psa_driver_wrapper_export_key(&attributes,
1522 slot->key.data, slot->key.bytes,
1523 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528}
1529
Ronald Cron7285cda2020-11-26 14:46:37 +01001530psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001531 const psa_key_attributes_t *attributes,
1532 const uint8_t *key_buffer,
1533 size_t key_buffer_size,
1534 uint8_t *data,
1535 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001537{
Ronald Crond18b5f82020-11-25 16:46:05 +01001538 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001539
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001540 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001541 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1542 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001543 /* Exporting public -> public */
1544 return psa_export_key_buffer_internal(
1545 key_buffer, key_buffer_size,
1546 data, data_size, data_length);
1547 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1550 return mbedtls_psa_rsa_export_public_key(attributes,
1551 key_buffer,
1552 key_buffer_size,
1553 data,
1554 data_size,
1555 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001556#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001557 /* We don't know how to convert a private RSA key to public. */
1558 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001559#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001560 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001561 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001562#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001563 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1564 return mbedtls_psa_ecp_export_public_key(attributes,
1565 key_buffer,
1566 key_buffer_size,
1567 data,
1568 data_size,
1569 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001571 /* We don't know how to convert a private ECC key to public */
1572 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001573#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001574 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001575 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Settia55f0422023-07-10 15:34:41 +02001576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001577 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001578 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001579 key_buffer,
1580 key_buffer_size,
1581 data, data_size,
1582 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001583#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001584 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settia55f0422023-07-10 15:34:41 +02001585#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_EXPORT) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001586 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001588 (void) key_buffer;
1589 (void) key_buffer_size;
1590 (void) data;
1591 (void) data_size;
1592 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595}
Ronald Crond18b5f82020-11-25 16:46:05 +01001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1598 uint8_t *data,
1599 size_t data_size,
1600 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001601{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001603 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001604 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001605 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001606
Ronald Crone907e552021-01-18 13:22:38 +01001607 /* Reject a zero-length output buffer now, since this can never be a
1608 * valid key representation. This way we know that data must be a valid
1609 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (data_size == 0) {
1611 return PSA_ERROR_BUFFER_TOO_SMALL;
1612 }
Ronald Crone907e552021-01-18 13:22:38 +01001613
Darryl Greendd8fb772018-11-07 16:00:44 +00001614 /* Set the key to empty now, so that even when there are errors, we always
1615 * set data_length to a value between 0 and data_size. On error, setting
1616 * the key to empty is a good choice because an empty key representation is
1617 * unlikely to be accepted anywhere. */
1618 *data_length = 0;
1619
1620 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1622 if (status != PSA_SUCCESS) {
1623 return status;
1624 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1627 status = PSA_ERROR_INVALID_ARGUMENT;
1628 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001629 }
1630
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001631 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001632 .core = slot->attr
1633 };
Ronald Cron67227982020-11-26 15:16:05 +01001634 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001635 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001637
1638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001642}
1643
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001644MBEDTLS_STATIC_ASSERT(
1645 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1646 "One or more key attribute flag is listed as both external-only and dual-use")
1647MBEDTLS_STATIC_ASSERT(
1648 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1649 "One or more key attribute flag is listed as both internal-only and dual-use")
1650MBEDTLS_STATIC_ASSERT(
1651 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1652 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001653
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001654/** Validate that a key policy is internally well-formed.
1655 *
1656 * This function only rejects invalid policies. It does not validate the
1657 * consistency of the policy with respect to other attributes of the key
1658 * such as the key type.
1659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001660static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001661{
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1663 PSA_KEY_USAGE_COPY |
1664 PSA_KEY_USAGE_ENCRYPT |
1665 PSA_KEY_USAGE_DECRYPT |
1666 PSA_KEY_USAGE_SIGN_MESSAGE |
1667 PSA_KEY_USAGE_VERIFY_MESSAGE |
1668 PSA_KEY_USAGE_SIGN_HASH |
1669 PSA_KEY_USAGE_VERIFY_HASH |
1670 PSA_KEY_USAGE_VERIFY_DERIVATION |
1671 PSA_KEY_USAGE_DERIVE)) != 0) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676}
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate the internal consistency of key attributes.
1679 *
1680 * This function only rejects invalid attribute values. If does not
1681 * validate the consistency of the attributes with any key data that may
1682 * be involved in the creation of the key.
1683 *
1684 * Call this function early in the key creation process.
1685 *
1686 * \param[in] attributes Key attributes for the new key.
1687 * \param[out] p_drv On any return, the driver for the key, if any.
1688 * NULL for a transparent key.
1689 *
1690 */
1691static psa_status_t psa_validate_key_attributes(
1692 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001694{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1697 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 status = psa_validate_key_location(lifetime, p_drv);
1700 if (status != PSA_SUCCESS) {
1701 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001702 }
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 status = psa_validate_key_persistence(lifetime);
1705 if (status != PSA_SUCCESS) {
1706 return status;
1707 }
1708
1709 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1710 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1711 return PSA_ERROR_INVALID_ARGUMENT;
1712 }
1713 } else {
1714 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1715 return PSA_ERROR_INVALID_ARGUMENT;
1716 }
1717 }
1718
1719 status = psa_validate_key_policy(&attributes->core.policy);
1720 if (status != PSA_SUCCESS) {
1721 return status;
1722 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001723
1724 /* Refuse to create overly large keys.
1725 * Note that this doesn't trigger on import if the attributes don't
1726 * explicitly specify a size (so psa_get_key_bits returns 0), so
1727 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1729 return PSA_ERROR_NOT_SUPPORTED;
1730 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Gilles Peskine91e8c332019-08-02 19:19:39 +02001732 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1734 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1735 return PSA_ERROR_INVALID_ARGUMENT;
1736 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001739}
1740
Gilles Peskine4747d192019-04-17 15:05:45 +02001741/** Prepare a key slot to receive key material.
1742 *
1743 * This function allocates a key slot and sets its metadata.
1744 *
1745 * If this function fails, call psa_fail_key_creation().
1746 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001747 * This function is intended to be used as follows:
1748 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001749 * it with the specified attributes, and in case of a volatile key assign it
1750 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 * -# Populate the slot with the key material.
1752 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1753 * In case of failure at any step, stop the sequence and call
1754 * psa_fail_key_creation().
1755 *
Ronald Cron5c522922020-11-14 16:35:34 +01001756 * On success, the key slot is locked. It is the responsibility of the caller
1757 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001758 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001759 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[out] p_slot On success, a pointer to the prepared slot.
1762 * \param[out] p_drv On any return, the driver for the key, if any.
1763 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 *
1765 * \retval #PSA_SUCCESS
1766 * The key slot is ready to receive key material.
1767 * \return If this function fails, the key slot is an invalid state.
1768 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 */
1770static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001771 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001772 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001773 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001775{
1776 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001777 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 psa_key_slot_t *slot;
1779
Gilles Peskinedf179142019-07-15 22:02:14 +02001780 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 *p_drv = NULL;
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 status = psa_validate_key_attributes(attributes, p_drv);
1784 if (status != PSA_SUCCESS) {
1785 return status;
1786 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1789 if (status != PSA_SUCCESS) {
1790 return status;
1791 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 slot = *p_slot;
1793
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001794 /* We're storing the declared bit-size of the key. It's up to each
1795 * creation mechanism to verify that this information is correct.
1796 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001798 * is optional (import, copy). In case of a volatile key, assign it the
1799 * volatile key identifier associated to the slot returned to contain its
1800 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001801
1802 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001804#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1805 slot->attr.id = volatile_key_id;
1806#else
1807 slot->attr.id.key_id = volatile_key_id;
1808#endif
1809 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001810
Gilles Peskine91e8c332019-08-02 19:19:39 +02001811 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001812 * external-only flags, query `attributes`. Thanks to the check
1813 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001814 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001815 * may have set. */
1816 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001817
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001819 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001821 * create the key file in internal storage, create the
1822 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 * persistent data. This is done by starting a transaction that will
1824 * encompass these three actions.
1825 * For registering a volatile key, we just need to find an appropriate
1826 * slot number inside the SE. Since the key is designated volatile, creating
1827 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001828 /* The first thing to do is to find a slot number for the new key.
1829 * We save the slot number in persistent storage as part of the
1830 * transaction data. It will be needed to recover if the power
1831 * fails during the key creation process, to clean up on the secure
1832 * element side after restarting. Obtaining a slot number from the
1833 * secure element driver updates its persistent state, but we do not yet
1834 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001835 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001837 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1839 &slot_number);
1840 if (status != PSA_SUCCESS) {
1841 return status;
1842 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1845 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001847 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 status = psa_crypto_save_transaction();
1850 if (status != PSA_SUCCESS) {
1851 (void) psa_crypto_stop_transaction();
1852 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001853 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001854 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001855
1856 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001858 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001861 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001864#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001867}
Gilles Peskine4747d192019-04-17 15:05:45 +02001868
1869/** Finalize the creation of a key once its key material has been set.
1870 *
1871 * This entails writing the key to persistent storage.
1872 *
1873 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001874 * See the documentation of psa_start_key_creation() for the intended use
1875 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001876 *
Ronald Cron5c522922020-11-14 16:35:34 +01001877 * If the finalization succeeds, the function unlocks the key slot (it was
1878 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1879 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001880 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 * \param[in,out] slot Pointer to the slot with key material.
1882 * \param[in] driver The secure element driver for the key,
1883 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001884 * \param[out] key On success, identifier of the key. Note that the
1885 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001886 *
1887 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001888 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1890 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1891 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1892 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1893 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1894 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001895 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001896 * \return If this function fails, the key slot is an invalid state.
1897 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001899static psa_status_t psa_finish_key_creation(
1900 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001901 psa_se_drv_table_entry_t *driver,
1902 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001903{
1904 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001905 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001906 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001907
1908#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001910#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001912 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001913 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001915
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001916 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1917 sizeof(data.slot_number),
1918 "Slot number size does not match psa_se_key_data_storage_t");
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1921 status = psa_save_persistent_key(&slot->attr,
1922 (uint8_t *) &data,
1923 sizeof(data));
1924 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001925#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1926 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001927 /* Key material is saved in export representation in the slot, so
1928 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 status = psa_save_persistent_key(&slot->attr,
1930 slot->key.data,
1931 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001932 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001933 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001934#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1935
Gilles Peskinecbaff462019-07-12 23:46:04 +02001936#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001937 /* Finish the transaction for a key creation. This does not
1938 * happen when registering an existing key. Detect this case
1939 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001940 * creation of a persistent key in a secure element requires a transaction,
1941 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 if (driver != NULL &&
1943 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1944 status = psa_save_se_persistent_data(driver);
1945 if (status != PSA_SUCCESS) {
1946 psa_destroy_persistent_key(slot->attr.id);
1947 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001948 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001950 }
1951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001954 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 status = psa_unlock_key_slot(slot);
1956 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001957 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001959 }
Ronald Cron50972942020-11-14 11:28:25 +01001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001962}
1963
1964/** Abort the creation of a key.
1965 *
1966 * You may call this function after calling psa_start_key_creation(),
1967 * or after psa_finish_key_creation() fails. In other circumstances, this
1968 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001969 * See the documentation of psa_start_key_creation() for the intended use
1970 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001972 * \param[in,out] slot Pointer to the slot with key material.
1973 * \param[in] driver The secure element driver for the key,
1974 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976static void psa_fail_key_creation(psa_key_slot_t *slot,
1977 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001978{
Gilles Peskine011e4282019-06-26 18:34:38 +02001979 (void) driver;
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001982 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001984
1985#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001986 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001987 * element, and the failure happened later (when saving metadata
1988 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001989 * element.
1990 * https://github.com/ARMmbed/mbed-crypto/issues/217
1991 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001992
Gilles Peskined7729582019-08-05 15:55:54 +02001993 /* Abort the ongoing transaction if any (there may not be one if
1994 * the creation process failed before starting one, or if the
1995 * key creation is a registration of a key in a secure element).
1996 * Earlier functions must already have done what it takes to undo any
1997 * partial creation. All that's left is to update the transaction data
1998 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02002000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02002003}
2004
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002005/** Validate optional attributes during key creation.
2006 *
2007 * Some key attributes are optional during key creation. If they are
2008 * specified in the attributes structure, check that they are consistent
2009 * with the data in the slot.
2010 *
2011 * This function should be called near the end of key creation, after
2012 * the slot in memory is fully populated but before saving persistent data.
2013 */
2014static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017{
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 if (attributes->core.type != 0) {
2019 if (attributes->core.type != slot->attr.type) {
2020 return PSA_ERROR_INVALID_ARGUMENT;
2021 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 }
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02002025#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
2026 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
2028 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02002029 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002030 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002032
Ronald Cron90857082020-11-25 14:58:33 +01002033 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 slot->attr.type,
2035 slot->key.data,
2036 slot->key.bytes,
2037 &rsa);
2038 if (status != PSA_SUCCESS) {
2039 return status;
2040 }
Steven Cooreman75b74362020-07-28 14:30:13 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 mbedtls_mpi_init(&actual);
2043 mbedtls_mpi_init(&required);
2044 ret = mbedtls_rsa_export(rsa,
2045 NULL, NULL, NULL, NULL, &actual);
2046 mbedtls_rsa_free(rsa);
2047 mbedtls_free(rsa);
2048 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
2051 ret = mbedtls_mpi_read_binary(&required,
2052 attributes->domain_parameters,
2053 attributes->domain_parameters_size);
2054 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002055 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 }
2057 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 }
2060rsa_exit:
2061 mbedtls_mpi_free(&actual);
2062 mbedtls_mpi_free(&required);
2063 if (ret != 0) {
2064 return mbedtls_to_psa_error(ret);
2065 }
2066 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002067#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2068 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002069 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002070 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002072 }
2073 }
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (attributes->core.bits != 0) {
2076 if (attributes->core.bits != slot->attr.bits) {
2077 return PSA_ERROR_INVALID_ARGUMENT;
2078 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002079 }
2080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002082}
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2085 const uint8_t *data,
2086 size_t data_length,
2087 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002088{
2089 psa_status_t status;
2090 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002091 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002092 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302093 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002094
Ronald Cron81709fc2020-11-14 12:10:32 +01002095 *key = MBEDTLS_SVC_KEY_ID_INIT;
2096
Gilles Peskine0f84d622019-09-12 19:03:13 +02002097 /* Reject zero-length symmetric keys (including raw data key objects).
2098 * This also rejects any key which might be encoded as an empty string,
2099 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (data_length == 0) {
2101 return PSA_ERROR_INVALID_ARGUMENT;
2102 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002103
Archana449608b2021-09-08 15:36:05 +05302104 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (data_length > SIZE_MAX / 8) {
2106 return PSA_ERROR_NOT_SUPPORTED;
2107 }
Archana6ed4bda2021-08-04 10:47:15 +05302108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2110 &slot, &driver);
2111 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002115 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302116 * storage ( thus not in the case of importing a key in a secure element
2117 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2118 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (slot->key.data == NULL) {
2120 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302121 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 attributes, data, data_length, &storage_size);
2123 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 }
Archanad8a83dc2021-06-14 10:04:16 +05302126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_allocate_buffer_to_slot(slot, storage_size);
2128 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002131 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002132
2133 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 status = psa_driver_wrapper_import_key(attributes,
2135 data, data_length,
2136 slot->key.data,
2137 slot->key.bytes,
2138 &slot->key.bytes, &bits);
2139 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002144 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002146 status = PSA_ERROR_INVALID_ARGUMENT;
2147 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002148 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002149
Archana6ed4bda2021-08-04 10:47:15 +05302150 /* Enforce a size limit, and in particular ensure that the bit
2151 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302153 status = PSA_ERROR_NOT_SUPPORTED;
2154 goto exit;
2155 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 status = psa_validate_optional_attributes(slot, attributes);
2157 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002158 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (status != PSA_SUCCESS) {
2164 psa_fail_key_creation(slot, driver);
2165 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002168}
2169
Gilles Peskined7729582019-08-05 15:55:54 +02002170#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2171psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002173{
2174 psa_status_t status;
2175 psa_key_slot_t *slot = NULL;
2176 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002178
2179 /* Leaving attributes unspecified is not currently supported.
2180 * It could make sense to query the key type and size from the
2181 * secure element, but not all secure elements support this
2182 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2184 return PSA_ERROR_NOT_SUPPORTED;
2185 }
2186 if (psa_get_key_bits(attributes) == 0) {
2187 return PSA_ERROR_NOT_SUPPORTED;
2188 }
Gilles Peskined7729582019-08-05 15:55:54 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2191 &slot, &driver);
2192 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002193 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 }
Gilles Peskined7729582019-08-05 15:55:54 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002197
2198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (status != PSA_SUCCESS) {
2200 psa_fail_key_creation(slot, driver);
2201 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002202
Gilles Peskined7729582019-08-05 15:55:54 +02002203 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_close_key(key);
2205 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002206}
2207#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2210 const psa_key_attributes_t *specified_attributes,
2211 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002212{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002214 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002215 psa_key_slot_t *source_slot = NULL;
2216 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002218 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302219 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002220
Ronald Cron81709fc2020-11-14 12:10:32 +01002221 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2222
Archana8a180362021-07-05 02:18:48 +05302223 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2225 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 status = psa_validate_optional_attributes(source_slot,
2230 specified_attributes);
2231 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002234
Archana9d17bf42021-09-10 06:22:44 +05302235 /* The target key type and number of bits have been validated by
2236 * psa_validate_optional_attributes() to be either equal to zero or
2237 * equal to the ones of the source key. So it is safe to inherit
2238 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302239 * */
Archana9d17bf42021-09-10 06:22:44 +05302240 actual_attributes.core.bits = source_slot->attr.bits;
2241 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302242
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 status = psa_restrict_key_policy(source_slot->attr.type,
2245 &actual_attributes.core.policy,
2246 &source_slot->attr.policy);
2247 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002248 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2252 &target_slot, &driver);
2253 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 }
2256 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2257 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302258 /*
Archana9d17bf42021-09-10 06:22:44 +05302259 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302260 * the source key would need to be exported as plaintext and re-imported
2261 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302262 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302263 * appropriate API invocations from the application, if needed.
2264 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002265 status = PSA_ERROR_NOT_SUPPORTED;
2266 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002267 }
Archana8a180362021-07-05 02:18:48 +05302268 /*
2269 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302270 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302271 * - For opaque keys this translates to an invocation of the drivers'
2272 * copy_key entry point through the dispatch layer.
2273 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2275 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2276 &storage_size);
2277 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302278 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 }
Archana374fe5b2021-09-08 15:50:28 +05302280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2282 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302283 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 }
Archana374fe5b2021-09-08 15:50:28 +05302285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 status = psa_driver_wrapper_copy_key(&actual_attributes,
2287 source_slot->key.data,
2288 source_slot->key.bytes,
2289 target_slot->key.data,
2290 target_slot->key.bytes,
2291 &target_slot->key.bytes);
2292 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302293 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
2295 } else {
2296 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302297 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 source_slot->key.bytes);
2299 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302300 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 }
Archana8a180362021-07-05 02:18:48 +05302302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (status != PSA_SUCCESS) {
2306 psa_fail_key_creation(target_slot, driver);
2307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002312}
2313
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002314
2315
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002316/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002317/* Message digests */
2318/****************************************************************/
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002322 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (operation->id == 0) {
2324 return PSA_SUCCESS;
2325 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002328 operation->id = 0;
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2334 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002336 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002337
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002338 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002340 status = PSA_ERROR_BAD_STATE;
2341 goto exit;
2342 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002345 status = PSA_ERROR_INVALID_ARGUMENT;
2346 goto exit;
2347 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002348
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002349 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2350 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002354
2355exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (status != PSA_SUCCESS) {
2357 psa_hash_abort(operation);
2358 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359
2360 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002361}
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2364 const uint8_t *input,
2365 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002366{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370 status = PSA_ERROR_BAD_STATE;
2371 goto exit;
2372 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002373
Steven Cooremanc8288352021-03-04 14:02:19 +01002374 /* Don't require hash implementations to behave correctly on a
2375 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (input_length == 0) {
2377 return PSA_SUCCESS;
2378 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002381
2382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (status != PSA_SUCCESS) {
2384 psa_hash_abort(operation);
2385 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388}
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2391 uint8_t *hash,
2392 size_t hash_size,
2393 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002395 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (operation->id == 0) {
2397 return PSA_ERROR_BAD_STATE;
2398 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399
Steven Cooreman1e582352021-02-18 17:24:37 +01002400 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 operation, hash, hash_size, hash_length);
2402 psa_hash_abort(operation);
2403 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404}
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2407 const uint8_t *hash,
2408 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409{
Ronald Cron69a63422021-10-18 09:47:58 +02002410 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002412 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 operation,
2414 actual_hash, sizeof(actual_hash),
2415 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002418 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002422 status = PSA_ERROR_INVALID_SIGNATURE;
2423 goto exit;
2424 }
2425
Dave Rodgman78701152023-08-29 14:20:18 +01002426 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002427 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002429
2430exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2432 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002433 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437}
2438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439psa_status_t psa_hash_compute(psa_algorithm_t alg,
2440 const uint8_t *input, size_t input_length,
2441 uint8_t *hash, size_t hash_size,
2442 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002443{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002444 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (!PSA_ALG_IS_HASH(alg)) {
2446 return PSA_ERROR_INVALID_ARGUMENT;
2447 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2450 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002451}
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453psa_status_t psa_hash_compare(psa_algorithm_t alg,
2454 const uint8_t *input, size_t input_length,
2455 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002456{
Ronald Cron69a63422021-10-18 09:47:58 +02002457 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002458 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (!PSA_ALG_IS_HASH(alg)) {
2461 return PSA_ERROR_INVALID_ARGUMENT;
2462 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002463
Steven Cooreman1e582352021-02-18 17:24:37 +01002464 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 alg, input, input_length,
2466 actual_hash, sizeof(actual_hash),
2467 &actual_hash_length);
2468 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002469 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 }
2471 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002472 status = PSA_ERROR_INVALID_SIGNATURE;
2473 goto exit;
2474 }
Dave Rodgman78701152023-08-29 14:20:18 +01002475 if (mbedtls_ct_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002476 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002478
2479exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2481 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002482}
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2485 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002486{
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (source_operation->id == 0 ||
2488 target_operation->id != 0) {
2489 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002490 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2493 target_operation);
2494 if (status != PSA_SUCCESS) {
2495 psa_hash_abort(target_operation);
2496 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002499}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500
2501
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503/* MAC */
2504/****************************************************************/
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002507{
Steven Cooremane6804192021-03-19 18:28:56 +01002508 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (operation->id == 0) {
2510 return PSA_SUCCESS;
2511 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002514 operation->mac_size = 0;
2515 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002516 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002519}
2520
Ronald Cron2dff3b22021-06-17 16:33:22 +02002521static psa_status_t psa_mac_finalize_alg_and_key_validation(
2522 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002523 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 psa_key_type_t key_type = psa_get_key_type(attributes);
2528 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (!PSA_ALG_IS_MAC(alg)) {
2531 return PSA_ERROR_INVALID_ARGUMENT;
2532 }
Steven Cooremane6804192021-03-19 18:28:56 +01002533
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002534 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 status = psa_mac_key_can_do(alg, key_type);
2536 if (status != PSA_SUCCESS) {
2537 return status;
2538 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002539
Steven Cooremand1a68f12021-05-10 11:13:41 +02002540 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002544 /* A very short MAC is too short for security since it can be
2545 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2546 * so we make this our minimum, even though 32 bits is still
2547 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002549 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2552 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002553 /* It's impossible to "truncate" to a larger length than the full length
2554 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002556 }
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002559 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2560 * that is disabled in the compile-time configuration. The result can
2561 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2562 * configuration into account. In this case, force a return of
2563 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2564 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2565 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2566 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2567 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002569 }
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002572}
2573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2575 mbedtls_svc_key_id_t key,
2576 psa_algorithm_t alg,
2577 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002578{
2579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2580 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002581 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002582 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583
2584 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002586 status = PSA_ERROR_BAD_STATE;
2587 goto exit;
2588 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589
2590 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 key,
2592 &slot,
2593 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2594 alg);
2595 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002596 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002599 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002600 .core = slot->attr
2601 };
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2604 &operation->mac_size);
2605 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
2609 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002610 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (is_sign) {
2612 status = psa_driver_wrapper_mac_sign_setup(operation,
2613 &attributes,
2614 slot->key.data,
2615 slot->key.bytes,
2616 alg);
2617 } else {
2618 status = psa_driver_wrapper_mac_verify_setup(operation,
2619 &attributes,
2620 slot->key.data,
2621 slot->key.bytes,
2622 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002623 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002624
Gilles Peskinefbfac682018-07-08 20:51:54 +02002625exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (status != PSA_SUCCESS) {
2627 psa_mac_abort(operation);
2628 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633}
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2636 mbedtls_svc_key_id_t key,
2637 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002638{
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002640}
2641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2643 mbedtls_svc_key_id_t key,
2644 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002645{
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002647}
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2650 const uint8_t *input,
2651 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652{
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (operation->id == 0) {
2654 return PSA_ERROR_BAD_STATE;
2655 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002656
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002657 /* Don't require hash implementations to behave correctly on a
2658 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (input_length == 0) {
2660 return PSA_SUCCESS;
2661 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2664 input, input_length);
2665 if (status != PSA_SUCCESS) {
2666 psa_mac_abort(operation);
2667 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670}
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2673 uint8_t *mac,
2674 size_t mac_size,
2675 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002676{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2678 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002681 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002682 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002683 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002686 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002688 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002689
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002690 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2691 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002693 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002694 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002695 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002698 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002699 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002700 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 status = psa_driver_wrapper_mac_sign_finish(operation,
2703 mac, operation->mac_size,
2704 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002705
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002706exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002707 /* In case of success, set the potential excess room in the output buffer
2708 * to an invalid value, to avoid potentially leaking a longer MAC.
2709 * In case of error, set the output length and content to a safe default,
2710 * such that in case the caller misses an error check, the output would be
2711 * an unachievable MAC.
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002714 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002715 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002716 }
mohammad16036df908f2018-04-02 08:34:15 -07002717
Paul Elliottdc42ca82023-02-24 18:11:59 +00002718 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002723}
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2726 const uint8_t *mac,
2727 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2730 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002733 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002734 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002735 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002738 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002739 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002740 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002743 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002744 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002745 }
2746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 status = psa_driver_wrapper_mac_verify_finish(operation,
2748 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002749
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002750exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754}
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2757 psa_algorithm_t alg,
2758 const uint8_t *input,
2759 size_t input_length,
2760 uint8_t *mac,
2761 size_t mac_size,
2762 size_t *mac_length,
2763 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002764{
2765 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002766 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2767 psa_key_slot_t *slot;
2768 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002769 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770
Ronald Cron51131b52021-06-17 17:17:20 +02002771 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 key,
2773 &slot,
2774 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2775 alg);
2776 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002779
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002780 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002781 .core = slot->attr
2782 };
2783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2785 &operation_mac_size);
2786 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002791 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002793 }
2794
2795 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 &attributes,
2797 slot->key.data, slot->key.bytes,
2798 alg,
2799 input, input_length,
2800 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002801
2802exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002803 /* In case of success, set the potential excess room in the output buffer
2804 * to an invalid value, to avoid potentially leaking a longer MAC.
2805 * In case of error, set the output length and content to a safe default,
2806 * such that in case the caller misses an error check, the output would be
2807 * an unachievable MAC.
2808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002810 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002811 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002812 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002813
2814 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002819}
2820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002822 psa_algorithm_t alg,
2823 const uint8_t *input,
2824 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 uint8_t *mac,
2826 size_t mac_size,
2827 size_t *mac_length)
2828{
2829 return psa_mac_compute_internal(key, alg,
2830 input, input_length,
2831 mac, mac_size, mac_length, 1);
2832}
2833
2834psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2835 psa_algorithm_t alg,
2836 const uint8_t *input,
2837 size_t input_length,
2838 const uint8_t *mac,
2839 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002840{
2841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002842 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2843 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 status = psa_mac_compute_internal(key, alg,
2846 input, input_length,
2847 actual_mac, sizeof(actual_mac),
2848 &actual_mac_length, 0);
2849 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002850 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (mac_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002854 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002855 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002856 }
Dave Rodgman78701152023-08-29 14:20:18 +01002857 if (mbedtls_ct_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002858 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002859 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002860 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002861
2862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002866}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002867
Gilles Peskinee59236f2018-01-27 23:32:46 +01002868/****************************************************************/
2869/* Asymmetric cryptography */
2870/****************************************************************/
2871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2873 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002874{
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (input_is_message) {
2876 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2877 return PSA_ERROR_INVALID_ARGUMENT;
2878 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2881 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2882 return PSA_ERROR_INVALID_ARGUMENT;
2883 }
2884 }
2885 } else {
2886 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2887 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002888 }
2889 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002892}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2895 int input_is_message,
2896 psa_algorithm_t alg,
2897 const uint8_t *input,
2898 size_t input_length,
2899 uint8_t *signature,
2900 size_t signature_size,
2901 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002902{
2903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2904 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2905 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002906 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002907
2908 *signature_length = 0;
2909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 status = psa_sign_verify_check_alg(input_is_message, alg);
2911 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002912 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002914
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002915 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002916 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002917 * buffer can in principle be empty since it doesn't actually have
2918 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (signature_size == 0) {
2920 return PSA_ERROR_BUFFER_TOO_SMALL;
2921 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002922
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002923 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 key, &slot,
2925 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2926 PSA_KEY_USAGE_SIGN_HASH,
2927 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002934 status = PSA_ERROR_INVALID_ARGUMENT;
2935 goto exit;
2936 }
2937
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002938 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002940 };
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002943 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002944 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002945 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 signature, signature_size, signature_length);
2947 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002948
2949 status = psa_driver_wrapper_sign_hash(
2950 &attributes, slot->key.data, slot->key.bytes,
2951 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002953 }
2954
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002955
2956exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002957 psa_wipe_tag_output_buffer(signature, status, signature_size,
2958 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002963}
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2966 int input_is_message,
2967 psa_algorithm_t alg,
2968 const uint8_t *input,
2969 size_t input_length,
2970 const uint8_t *signature,
2971 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002972{
2973 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2974 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2975 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 status = psa_sign_verify_check_alg(input_is_message, alg);
2978 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002979 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002981
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002982 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 key, &slot,
2984 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2985 PSA_KEY_USAGE_VERIFY_HASH,
2986 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (status != PSA_SUCCESS) {
2989 return status;
2990 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002991
2992 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002994 };
2995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002997 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002998 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002999 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 signature, signature_length);
3001 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003002 status = psa_driver_wrapper_verify_hash(
3003 &attributes, slot->key.data, slot->key.bytes,
3004 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003006 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003011
3012}
3013
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003014psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003015 const psa_key_attributes_t *attributes,
3016 const uint8_t *key_buffer,
3017 size_t key_buffer_size,
3018 psa_algorithm_t alg,
3019 const uint8_t *input,
3020 size_t input_length,
3021 uint8_t *signature,
3022 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003024{
3025 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003028 size_t hash_length;
3029 uint8_t hash[PSA_HASH_MAX_SIZE];
3030
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003031 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 PSA_ALG_SIGN_GET_HASH(alg),
3033 input, input_length,
3034 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003037 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003039
3040 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 attributes, key_buffer, key_buffer_size,
3042 alg, hash, hash_length,
3043 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003044 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003047}
3048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
3050 psa_algorithm_t alg,
3051 const uint8_t *input,
3052 size_t input_length,
3053 uint8_t *signature,
3054 size_t signature_size,
3055 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003056{
3057 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003058 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003060}
3061
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003062psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003063 const psa_key_attributes_t *attributes,
3064 const uint8_t *key_buffer,
3065 size_t key_buffer_size,
3066 psa_algorithm_t alg,
3067 const uint8_t *input,
3068 size_t input_length,
3069 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003071{
3072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003075 size_t hash_length;
3076 uint8_t hash[PSA_HASH_MAX_SIZE];
3077
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003078 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 PSA_ALG_SIGN_GET_HASH(alg),
3080 input, input_length,
3081 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003084 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003086
3087 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 attributes, key_buffer, key_buffer_size,
3089 alg, hash, hash_length,
3090 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003091 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003094}
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3097 psa_algorithm_t alg,
3098 const uint8_t *input,
3099 size_t input_length,
3100 const uint8_t *signature,
3101 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003102{
3103 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003104 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003106}
3107
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003108psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003109 const psa_key_attributes_t *attributes,
3110 const uint8_t *key_buffer, size_t key_buffer_size,
3111 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003113{
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3115 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3116 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003117#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3119 return mbedtls_psa_rsa_sign_hash(
3120 attributes,
3121 key_buffer, key_buffer_size,
3122 alg, hash, hash_length,
3123 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 } else {
3127 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3130 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003131#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3133 return mbedtls_psa_ecdsa_sign_hash(
3134 attributes,
3135 key_buffer, key_buffer_size,
3136 alg, hash, hash_length,
3137 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3139 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 } else {
3141 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003142 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003143 }
Ronald Cron4501c982021-03-19 20:09:32 +01003144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 (void) key_buffer;
3146 (void) key_buffer_size;
3147 (void) hash;
3148 (void) hash_length;
3149 (void) signature;
3150 (void) signature_size;
3151 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003154}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3157 psa_algorithm_t alg,
3158 const uint8_t *hash,
3159 size_t hash_length,
3160 uint8_t *signature,
3161 size_t signature_size,
3162 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003163{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003164 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003165 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003167}
3168
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003169psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003170 const psa_key_attributes_t *attributes,
3171 const uint8_t *key_buffer, size_t key_buffer_size,
3172 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003174{
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3176 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3177 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003178#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3180 return mbedtls_psa_rsa_verify_hash(
3181 attributes,
3182 key_buffer, key_buffer_size,
3183 alg, hash, hash_length,
3184 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003185#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3186 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 } else {
3188 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3191 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003192#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3194 return mbedtls_psa_ecdsa_verify_hash(
3195 attributes,
3196 key_buffer, key_buffer_size,
3197 alg, hash, hash_length,
3198 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003199#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3200 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 } else {
3202 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003203 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003204 }
Ronald Cron4501c982021-03-19 20:09:32 +01003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 (void) key_buffer;
3207 (void) key_buffer_size;
3208 (void) hash;
3209 (void) hash_length;
3210 (void) signature;
3211 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3217 psa_algorithm_t alg,
3218 const uint8_t *hash,
3219 size_t hash_length,
3220 const uint8_t *signature,
3221 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003222{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003223 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003224 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003226}
3227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3229 psa_algorithm_t alg,
3230 const uint8_t *input,
3231 size_t input_length,
3232 const uint8_t *salt,
3233 size_t salt_length,
3234 uint8_t *output,
3235 size_t output_size,
3236 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003239 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003240 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003241 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003242
Darryl Green5cc689a2018-07-24 15:34:10 +01003243 (void) input;
3244 (void) input_length;
3245 (void) salt;
3246 (void) output;
3247 (void) output_size;
3248
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003249 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3252 return PSA_ERROR_INVALID_ARGUMENT;
3253 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003254
Ronald Cron5c522922020-11-14 16:35:34 +01003255 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3257 if (status != PSA_SUCCESS) {
3258 return status;
3259 }
3260 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3261 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262 status = PSA_ERROR_INVALID_ARGUMENT;
3263 goto exit;
3264 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003266 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003268 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003269
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003270 status = psa_driver_wrapper_asymmetric_encrypt(
3271 &attributes, slot->key.data, slot->key.bytes,
3272 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003278}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3281 psa_algorithm_t alg,
3282 const uint8_t *input,
3283 size_t input_length,
3284 const uint8_t *salt,
3285 size_t salt_length,
3286 uint8_t *output,
3287 size_t output_size,
3288 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003289{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003291 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003292 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003293 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003294
Darryl Green5cc689a2018-07-24 15:34:10 +01003295 (void) input;
3296 (void) input_length;
3297 (void) salt;
3298 (void) output;
3299 (void) output_size;
3300
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003301 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3304 return PSA_ERROR_INVALID_ARGUMENT;
3305 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003306
Ronald Cron5c522922020-11-14 16:35:34 +01003307 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3309 if (status != PSA_SUCCESS) {
3310 return status;
3311 }
3312 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313 status = PSA_ERROR_INVALID_ARGUMENT;
3314 goto exit;
3315 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003316
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003317 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003319 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003321 status = psa_driver_wrapper_asymmetric_decrypt(
3322 &attributes, slot->key.data, slot->key.bytes,
3323 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003325
3326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003330}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003331
Paul Elliott9fe12f62022-11-30 19:16:02 +00003332/****************************************************************/
3333/* Asymmetric interruptible cryptography */
3334/****************************************************************/
3335
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003336static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3337
Paul Elliott9fe12f62022-11-30 19:16:02 +00003338void psa_interruptible_set_max_ops(uint32_t max_ops)
3339{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003340 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003341}
3342
3343uint32_t psa_interruptible_get_max_ops(void)
3344{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003345 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003346}
3347
Paul Elliott9fe12f62022-11-30 19:16:02 +00003348uint32_t psa_sign_hash_get_num_ops(
3349 const psa_sign_hash_interruptible_operation_t *operation)
3350{
Paul Elliott296ede92022-12-15 17:00:30 +00003351 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003352}
3353
3354uint32_t psa_verify_hash_get_num_ops(
3355 const psa_verify_hash_interruptible_operation_t *operation)
3356{
Paul Elliott296ede92022-12-15 17:00:30 +00003357 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003358}
3359
Paul Elliott59ad9452022-12-18 15:09:02 +00003360static psa_status_t psa_sign_hash_abort_internal(
3361 psa_sign_hash_interruptible_operation_t *operation)
3362{
3363 if (operation->id == 0) {
3364 /* The object has (apparently) been initialized but it is not (yet)
3365 * in use. It's ok to call abort on such an object, and there's
3366 * nothing to do. */
3367 return PSA_SUCCESS;
3368 }
3369
3370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3371
3372 status = psa_driver_wrapper_sign_hash_abort(operation);
3373
3374 operation->id = 0;
3375
Paul Elliotte6145dc2023-02-07 12:51:21 +00003376 /* Do not clear either the error_occurred or num_ops elements here as they
3377 * only want to be cleared by the application calling abort, not by abort
3378 * being called at completion of an operation. */
3379
Paul Elliott59ad9452022-12-18 15:09:02 +00003380 return status;
3381}
3382
Paul Elliott9fe12f62022-11-30 19:16:02 +00003383psa_status_t psa_sign_hash_start(
3384 psa_sign_hash_interruptible_operation_t *operation,
3385 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3386 const uint8_t *hash, size_t hash_length)
3387{
3388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3389 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3390 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003391 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003392
Paul Elliottc9774412023-02-06 15:14:07 +00003393 /* Check that start has not been previously called, or operation has not
3394 * previously errored. */
3395 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003396 return PSA_ERROR_BAD_STATE;
3397 }
3398
Paul Elliott9fe12f62022-11-30 19:16:02 +00003399 status = psa_sign_verify_check_alg(0, alg);
3400 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003401 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003402 return status;
3403 }
3404
3405 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3406 PSA_KEY_USAGE_SIGN_HASH,
3407 alg);
3408
3409 if (status != PSA_SUCCESS) {
3410 goto exit;
3411 }
3412
3413 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3414 status = PSA_ERROR_INVALID_ARGUMENT;
3415 goto exit;
3416 }
3417
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003418 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003419 .core = slot->attr
3420 };
3421
Paul Elliott296ede92022-12-15 17:00:30 +00003422 /* Ensure ops count gets reset, in case of operation re-use. */
3423 operation->num_ops = 0;
3424
Paul Elliott9fe12f62022-11-30 19:16:02 +00003425 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3426 slot->key.data,
3427 slot->key.bytes, alg,
3428 hash, hash_length);
3429exit:
3430
3431 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003432 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003433 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003434 }
3435
3436 unlock_status = psa_unlock_key_slot(slot);
3437
Paul Elliottc9774412023-02-06 15:14:07 +00003438 if (unlock_status != PSA_SUCCESS) {
3439 operation->error_occurred = 1;
3440 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003441
Paul Elliottc9774412023-02-06 15:14:07 +00003442 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003443}
3444
3445
3446psa_status_t psa_sign_hash_complete(
3447 psa_sign_hash_interruptible_operation_t *operation,
3448 uint8_t *signature, size_t signature_size,
3449 size_t *signature_length)
3450{
3451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3452
3453 *signature_length = 0;
3454
Paul Elliottc9774412023-02-06 15:14:07 +00003455 /* Check that start has been called first, and that operation has not
3456 * previously errored. */
3457 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003458 status = PSA_ERROR_BAD_STATE;
3459 goto exit;
3460 }
3461
Paul Elliott096abc42023-02-03 18:33:23 +00003462 /* Immediately reject a zero-length signature buffer. This guarantees that
3463 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003464 if (signature_size == 0) {
3465 status = PSA_ERROR_BUFFER_TOO_SMALL;
3466 goto exit;
3467 }
3468
3469 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3470 signature_size,
3471 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003472
Paul Elliott296ede92022-12-15 17:00:30 +00003473 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003474 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003475
Paul Elliottebe225c2023-02-10 14:32:53 +00003476exit:
3477
Paul Elliott59200a22023-02-21 15:07:40 +00003478 psa_wipe_tag_output_buffer(signature, status, signature_size,
3479 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003480
Paul Elliott53bb3122023-02-10 14:22:22 +00003481 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003482 if (status != PSA_SUCCESS) {
3483 operation->error_occurred = 1;
3484 }
3485
Paul Elliott59ad9452022-12-18 15:09:02 +00003486 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003487 }
3488
3489 return status;
3490}
3491
3492psa_status_t psa_sign_hash_abort(
3493 psa_sign_hash_interruptible_operation_t *operation)
3494{
Paul Elliott59ad9452022-12-18 15:09:02 +00003495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3496
3497 status = psa_sign_hash_abort_internal(operation);
3498
3499 /* We clear the number of ops done here, so that it is not cleared when
3500 * the operation fails or succeeds, only on manual abort. */
3501 operation->num_ops = 0;
3502
Paul Elliottc9774412023-02-06 15:14:07 +00003503 /* Likewise, failure state. */
3504 operation->error_occurred = 0;
3505
Paul Elliott59ad9452022-12-18 15:09:02 +00003506 return status;
3507}
3508
3509static psa_status_t psa_verify_hash_abort_internal(
3510 psa_verify_hash_interruptible_operation_t *operation)
3511{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003512 if (operation->id == 0) {
3513 /* The object has (apparently) been initialized but it is not (yet)
3514 * in use. It's ok to call abort on such an object, and there's
3515 * nothing to do. */
3516 return PSA_SUCCESS;
3517 }
3518
Paul Elliott59ad9452022-12-18 15:09:02 +00003519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3520
3521 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003522
3523 operation->id = 0;
3524
Paul Elliotte6145dc2023-02-07 12:51:21 +00003525 /* Do not clear either the error_occurred or num_ops elements here as they
3526 * only want to be cleared by the application calling abort, not by abort
3527 * being called at completion of an operation. */
3528
Paul Elliott59ad9452022-12-18 15:09:02 +00003529 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003530}
3531
3532psa_status_t psa_verify_hash_start(
3533 psa_verify_hash_interruptible_operation_t *operation,
3534 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3535 const uint8_t *hash, size_t hash_length,
3536 const uint8_t *signature, size_t signature_length)
3537{
3538 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3539 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3540 psa_key_slot_t *slot;
3541
Paul Elliottc9774412023-02-06 15:14:07 +00003542 /* Check that start has not been previously called, or operation has not
3543 * previously errored. */
3544 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003545 return PSA_ERROR_BAD_STATE;
3546 }
3547
3548 status = psa_sign_verify_check_alg(0, alg);
3549 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003550 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003551 return status;
3552 }
3553
3554 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3555 PSA_KEY_USAGE_VERIFY_HASH,
3556 alg);
3557
3558 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003559 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003560 return status;
3561 }
3562
3563 psa_key_attributes_t attributes = {
3564 .core = slot->attr
3565 };
3566
Paul Elliott296ede92022-12-15 17:00:30 +00003567 /* Ensure ops count gets reset, in case of operation re-use. */
3568 operation->num_ops = 0;
3569
Paul Elliott9fe12f62022-11-30 19:16:02 +00003570 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3571 slot->key.data,
3572 slot->key.bytes,
3573 alg, hash, hash_length,
3574 signature, signature_length);
3575
3576 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003577 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003578 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003579 }
3580
3581 unlock_status = psa_unlock_key_slot(slot);
3582
Paul Elliottc9774412023-02-06 15:14:07 +00003583 if (unlock_status != PSA_SUCCESS) {
3584 operation->error_occurred = 1;
3585 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003586
Paul Elliottc9774412023-02-06 15:14:07 +00003587 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003588}
3589
3590psa_status_t psa_verify_hash_complete(
3591 psa_verify_hash_interruptible_operation_t *operation)
3592{
3593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3594
Paul Elliottc9774412023-02-06 15:14:07 +00003595 /* Check that start has been called first, and that operation has not
3596 * previously errored. */
3597 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003598 status = PSA_ERROR_BAD_STATE;
3599 goto exit;
3600 }
3601
3602 status = psa_driver_wrapper_verify_hash_complete(operation);
3603
Paul Elliott296ede92022-12-15 17:00:30 +00003604 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003605 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003606 operation);
3607
Paul Elliottebe225c2023-02-10 14:32:53 +00003608exit:
3609
Paul Elliott9fe12f62022-11-30 19:16:02 +00003610 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003611 if (status != PSA_SUCCESS) {
3612 operation->error_occurred = 1;
3613 }
3614
Paul Elliott59ad9452022-12-18 15:09:02 +00003615 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003616 }
3617
3618 return status;
3619}
3620
3621psa_status_t psa_verify_hash_abort(
3622 psa_verify_hash_interruptible_operation_t *operation)
3623{
Paul Elliott59ad9452022-12-18 15:09:02 +00003624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003625
Paul Elliott59ad9452022-12-18 15:09:02 +00003626 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003627
Paul Elliott59ad9452022-12-18 15:09:02 +00003628 /* We clear the number of ops done here, so that it is not cleared when
3629 * the operation fails or succeeds, only on manual abort. */
3630 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003631
Paul Elliottc9774412023-02-06 15:14:07 +00003632 /* Likewise, failure state. */
3633 operation->error_occurred = 0;
3634
Paul Elliott59ad9452022-12-18 15:09:02 +00003635 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003636}
3637
Paul Elliott588f8ed2022-12-02 18:10:26 +00003638/****************************************************************/
3639/* Asymmetric interruptible cryptography internal */
3640/* implementations */
3641/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003642
Paul Elliott588f8ed2022-12-02 18:10:26 +00003643void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3644{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003645
Paul Elliott588f8ed2022-12-02 18:10:26 +00003646#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3647 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3648 defined(MBEDTLS_ECP_RESTARTABLE)
3649
3650 /* Internal implementation uses zero to indicate infinite number max ops,
3651 * therefore avoid this value, and set to minimum possible. */
3652 if (max_ops == 0) {
3653 max_ops = 1;
3654 }
3655
Paul Elliott588f8ed2022-12-02 18:10:26 +00003656 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003657#else
3658 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003659#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3660 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3661 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3662}
3663
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003665 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003666{
3667#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3668 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3669 defined(MBEDTLS_ECP_RESTARTABLE)
3670
Paul Elliott93d9ca82023-02-15 18:14:21 +00003671 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003672#else
3673 (void) operation;
3674 return 0;
3675#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3676 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3677 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3678}
3679
3680uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003681 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003682{
3683 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3684 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3685 defined(MBEDTLS_ECP_RESTARTABLE)
3686
Paul Elliott93d9ca82023-02-15 18:14:21 +00003687 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003688#else
3689 (void) operation;
3690 return 0;
3691#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3692 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3693 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3694}
3695
3696psa_status_t mbedtls_psa_sign_hash_start(
3697 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3698 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3699 size_t key_buffer_size, psa_algorithm_t alg,
3700 const uint8_t *hash, size_t hash_length)
3701{
3702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003703 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003704
Paul Elliott068fe072023-01-16 13:59:15 +00003705 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3706 return PSA_ERROR_NOT_SUPPORTED;
3707 }
3708
3709 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003710 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003711 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712
3713#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003714 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3715 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003716
Paul Elliotta1c94092023-02-15 16:38:04 +00003717 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3718
Paul Elliott93d9ca82023-02-15 18:14:21 +00003719 /* Ensure num_ops is zero'ed in case of context re-use. */
3720 operation->num_ops = 0;
3721
Paul Elliott068fe072023-01-16 13:59:15 +00003722 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3723 attributes->core.bits,
3724 key_buffer,
3725 key_buffer_size,
3726 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003727
Paul Elliott068fe072023-01-16 13:59:15 +00003728 if (status != PSA_SUCCESS) {
3729 return status;
3730 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731
Paul Elliott1bc59df2023-02-05 13:41:57 +00003732 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003733 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003734
Paul Elliott068fe072023-01-16 13:59:15 +00003735 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003736 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003737 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738
Paul Elliott0290a762023-02-09 14:30:24 +00003739 /* We only need to store the same length of hash as the private key size
3740 * here, it would be truncated by the internal implementation anyway. */
3741 required_hash_length = (hash_length < operation->coordinate_bytes ?
3742 hash_length : operation->coordinate_bytes);
3743
Paul Elliottba70ad42023-02-15 18:23:53 +00003744 if (required_hash_length > sizeof(operation->hash)) {
3745 /* Shouldn't happen, but better safe than sorry. */
3746 return PSA_ERROR_CORRUPTION_DETECTED;
3747 }
3748
Paul Elliott0290a762023-02-09 14:30:24 +00003749 memcpy(operation->hash, hash, required_hash_length);
3750 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003751
3752 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003753
3754#else
Paul Elliott068fe072023-01-16 13:59:15 +00003755 (void) operation;
3756 (void) key_buffer;
3757 (void) key_buffer_size;
3758 (void) alg;
3759 (void) hash;
3760 (void) hash_length;
3761 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003762 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003763
Paul Elliott068fe072023-01-16 13:59:15 +00003764 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003765#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3766 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3767 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003768}
3769
3770psa_status_t mbedtls_psa_sign_hash_complete(
3771 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3772 uint8_t *signature, size_t signature_size,
3773 size_t *signature_length)
3774{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003775#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3776 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3777 defined(MBEDTLS_ECP_RESTARTABLE)
3778
Paul Elliotta1c94092023-02-15 16:38:04 +00003779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003780 mbedtls_mpi r;
3781 mbedtls_mpi s;
3782
Paul Elliott46845252023-02-03 14:59:11 +00003783 mbedtls_mpi_init(&r);
3784 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003786 /* Ensure max_ops is set to the current value (or default). */
3787 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3788
Paul Elliotta1c94092023-02-15 16:38:04 +00003789 if (signature_size < 2 * operation->coordinate_bytes) {
3790 status = PSA_ERROR_BUFFER_TOO_SMALL;
3791 goto exit;
3792 }
3793
Paul Elliott588f8ed2022-12-02 18:10:26 +00003794 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003795
Paul Elliott588f8ed2022-12-02 18:10:26 +00003796#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3797 status = mbedtls_to_psa_error(
3798 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003799 &r,
3800 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801 &operation->ctx->d,
3802 operation->hash,
3803 operation->hash_length,
3804 operation->md_alg,
3805 mbedtls_psa_get_random,
3806 MBEDTLS_PSA_RANDOM_STATE,
3807 &operation->restart_ctx));
3808#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003809 status = PSA_ERROR_NOT_SUPPORTED;
3810 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003811#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3812 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003813 status = mbedtls_to_psa_error(
3814 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003815 &r,
3816 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003817 &operation->ctx->d,
3818 operation->hash,
3819 operation->hash_length,
3820 mbedtls_psa_get_random,
3821 MBEDTLS_PSA_RANDOM_STATE,
3822 mbedtls_psa_get_random,
3823 MBEDTLS_PSA_RANDOM_STATE,
3824 &operation->restart_ctx));
3825 }
3826
Paul Elliottf8e5b562023-02-19 18:43:45 +00003827 /* Hide the fact that the restart context only holds a delta of number of
3828 * ops done during the last operation, not an absolute value. */
3829 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3830
Paul Elliott724bd252023-02-08 12:35:08 +00003831 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003832 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003833 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003834 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003835 operation->coordinate_bytes)
3836 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003837
3838 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003839 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003840 }
3841
3842 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003843 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003844 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003845 operation->coordinate_bytes,
3846 operation->coordinate_bytes)
3847 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003848
3849 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003850 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003851 }
3852
Paul Elliott1bc59df2023-02-05 13:41:57 +00003853 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003854
Paul Elliott724bd252023-02-08 12:35:08 +00003855 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003856 }
Paul Elliott724bd252023-02-08 12:35:08 +00003857
3858exit:
3859
3860 mbedtls_mpi_free(&r);
3861 mbedtls_mpi_free(&s);
3862 return status;
3863
Paul Elliott588f8ed2022-12-02 18:10:26 +00003864 #else
3865
3866 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003867 (void) signature;
3868 (void) signature_size;
3869 (void) signature_length;
3870
3871 return PSA_ERROR_NOT_SUPPORTED;
3872
3873#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3874 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3875 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3876}
3877
3878psa_status_t mbedtls_psa_sign_hash_abort(
3879 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3880{
3881
3882#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3883 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3884 defined(MBEDTLS_ECP_RESTARTABLE)
3885
3886 if (operation->ctx) {
3887 mbedtls_ecdsa_free(operation->ctx);
3888 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003889 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003890 }
3891
3892 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3893
Paul Elliott93d9ca82023-02-15 18:14:21 +00003894 operation->num_ops = 0;
3895
Paul Elliott588f8ed2022-12-02 18:10:26 +00003896 return PSA_SUCCESS;
3897
3898#else
3899
3900 (void) operation;
3901
3902 return PSA_ERROR_NOT_SUPPORTED;
3903
3904#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3905 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3906 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3907}
3908
3909psa_status_t mbedtls_psa_verify_hash_start(
3910 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3911 const psa_key_attributes_t *attributes,
3912 const uint8_t *key_buffer, size_t key_buffer_size,
3913 psa_algorithm_t alg,
3914 const uint8_t *hash, size_t hash_length,
3915 const uint8_t *signature, size_t signature_length)
3916{
3917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003918 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003919 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003920
Paul Elliott068fe072023-01-16 13:59:15 +00003921 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3922 return PSA_ERROR_NOT_SUPPORTED;
3923 }
3924
3925 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003926 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003927 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003928
3929#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003930 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3931 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932
Paul Elliotta1c94092023-02-15 16:38:04 +00003933 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3934 mbedtls_mpi_init(&operation->r);
3935 mbedtls_mpi_init(&operation->s);
3936
Paul Elliott93d9ca82023-02-15 18:14:21 +00003937 /* Ensure num_ops is zero'ed in case of context re-use. */
3938 operation->num_ops = 0;
3939
Paul Elliott068fe072023-01-16 13:59:15 +00003940 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3941 attributes->core.bits,
3942 key_buffer,
3943 key_buffer_size,
3944 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003945
Paul Elliott068fe072023-01-16 13:59:15 +00003946 if (status != PSA_SUCCESS) {
3947 return status;
3948 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949
Paul Elliottc569fc22023-02-10 13:02:54 +00003950 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003951
Paul Elliott1bc59df2023-02-05 13:41:57 +00003952 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003953 return PSA_ERROR_INVALID_SIGNATURE;
3954 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003955
Paul Elliott068fe072023-01-16 13:59:15 +00003956 status = mbedtls_to_psa_error(
3957 mbedtls_mpi_read_binary(&operation->r,
3958 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003959 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003960
Paul Elliott068fe072023-01-16 13:59:15 +00003961 if (status != PSA_SUCCESS) {
3962 return status;
3963 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003964
Paul Elliott068fe072023-01-16 13:59:15 +00003965 status = mbedtls_to_psa_error(
3966 mbedtls_mpi_read_binary(&operation->s,
3967 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003968 coordinate_bytes,
3969 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003970
Paul Elliott068fe072023-01-16 13:59:15 +00003971 if (status != PSA_SUCCESS) {
3972 return status;
3973 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003974
Paul Elliott2c9843f2023-02-15 17:32:42 +00003975 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003976
Paul Elliott2c9843f2023-02-15 17:32:42 +00003977 if (status != PSA_SUCCESS) {
3978 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003979 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003980
Paul Elliott0290a762023-02-09 14:30:24 +00003981 /* We only need to store the same length of hash as the private key size
3982 * here, it would be truncated by the internal implementation anyway. */
3983 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3984 coordinate_bytes);
3985
Paul Elliottba70ad42023-02-15 18:23:53 +00003986 if (required_hash_length > sizeof(operation->hash)) {
3987 /* Shouldn't happen, but better safe than sorry. */
3988 return PSA_ERROR_CORRUPTION_DETECTED;
3989 }
3990
Paul Elliott0290a762023-02-09 14:30:24 +00003991 memcpy(operation->hash, hash, required_hash_length);
3992 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003993
3994 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003995#else
Paul Elliott068fe072023-01-16 13:59:15 +00003996 (void) operation;
3997 (void) key_buffer;
3998 (void) key_buffer_size;
3999 (void) alg;
4000 (void) hash;
4001 (void) hash_length;
4002 (void) signature;
4003 (void) signature_length;
4004 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00004005 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00004006 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004007
Paul Elliott068fe072023-01-16 13:59:15 +00004008 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4010 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4011 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00004012}
4013
4014psa_status_t mbedtls_psa_verify_hash_complete(
4015 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4016{
4017
4018#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4019 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4020 defined(MBEDTLS_ECP_RESTARTABLE)
4021
Paul Elliottf8e5b562023-02-19 18:43:45 +00004022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4023
Paul Elliotta16ce9f2023-02-21 14:19:23 +00004024 /* Ensure max_ops is set to the current value (or default). */
4025 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
4026
Paul Elliottf8e5b562023-02-19 18:43:45 +00004027 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00004028 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
4029 operation->hash,
4030 operation->hash_length,
4031 &operation->ctx->Q,
4032 &operation->r,
4033 &operation->s,
4034 &operation->restart_ctx));
4035
Paul Elliottf8e5b562023-02-19 18:43:45 +00004036 /* Hide the fact that the restart context only holds a delta of number of
4037 * ops done during the last operation, not an absolute value. */
4038 operation->num_ops += operation->restart_ctx.ecp.ops_done;
4039
4040 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004041#else
4042 (void) operation;
4043
4044 return PSA_ERROR_NOT_SUPPORTED;
4045
4046#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4047 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4048 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4049}
4050
4051psa_status_t mbedtls_psa_verify_hash_abort(
4052 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
4053{
4054
4055#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4056 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
4057 defined(MBEDTLS_ECP_RESTARTABLE)
4058
4059 if (operation->ctx) {
4060 mbedtls_ecdsa_free(operation->ctx);
4061 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00004062 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004063 }
4064
4065 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4066
Paul Elliott93d9ca82023-02-15 18:14:21 +00004067 operation->num_ops = 0;
4068
Paul Elliott588f8ed2022-12-02 18:10:26 +00004069 mbedtls_mpi_free(&operation->r);
4070 mbedtls_mpi_free(&operation->s);
4071
4072 return PSA_SUCCESS;
4073
4074#else
4075 (void) operation;
4076
4077 return PSA_ERROR_NOT_SUPPORTED;
4078
4079#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4080 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4081 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4082}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004083
mohammad1603503973b2018-03-12 15:59:30 +02004084/****************************************************************/
4085/* Symmetric cryptography */
4086/****************************************************************/
4087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4089 mbedtls_svc_key_id_t key,
4090 psa_algorithm_t alg,
4091 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004092{
4093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4094 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004095 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4097 PSA_KEY_USAGE_ENCRYPT :
4098 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004099 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004100
4101 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004103 status = PSA_ERROR_BAD_STATE;
4104 goto exit;
4105 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004108 status = PSA_ERROR_INVALID_ARGUMENT;
4109 goto exit;
4110 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4113 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004116
Ronald Cron49fafa92021-03-10 08:34:23 +01004117 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004118 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004119 * so we only set it (in the driver wrapper) after resources have been
4120 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004121 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004123 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004125 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 }
4127 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004128
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004129 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004131 };
4132
Ronald Cronab99ac22020-10-01 16:38:28 +02004133 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (cipher_operation == MBEDTLS_ENCRYPT) {
4135 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4136 &attributes,
4137 slot->key.data,
4138 slot->key.bytes,
4139 alg);
4140 } else {
4141 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4142 &attributes,
4143 slot->key.data,
4144 slot->key.bytes,
4145 alg);
4146 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004147
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 if (status != PSA_SUCCESS) {
4150 psa_cipher_abort(operation);
4151 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004156}
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4159 mbedtls_svc_key_id_t key,
4160 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004161{
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004163}
4164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4166 mbedtls_svc_key_id_t key,
4167 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004168{
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004170}
4171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4173 uint8_t *iv,
4174 size_t iv_size,
4175 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004176{
Ronald Cron6d051732020-10-01 14:10:20 +02004177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004178 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4179 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004182 status = PSA_ERROR_BAD_STATE;
4183 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004184 }
4185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004187 status = PSA_ERROR_BAD_STATE;
4188 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004189 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004190
Ronald Cron2fb90522021-07-05 12:31:44 +02004191 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004193 status = PSA_ERROR_BUFFER_TOO_SMALL;
4194 goto exit;
4195 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004198 status = PSA_ERROR_GENERIC_ERROR;
4199 goto exit;
4200 }
4201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 status = psa_generate_random(local_iv, default_iv_length);
4203 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004204 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 }
Ronald Cron5618a392021-03-26 09:52:26 +01004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 status = psa_driver_wrapper_cipher_set_iv(operation,
4208 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004209
4210exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 if (status == PSA_SUCCESS) {
4212 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004213 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004214 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004216 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004218 }
Ronald Cron6d051732020-10-01 14:10:20 +02004219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004221}
4222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4224 const uint8_t *iv,
4225 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004226{
Ronald Cron6d051732020-10-01 14:10:20 +02004227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004230 status = PSA_ERROR_BAD_STATE;
4231 goto exit;
4232 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004235 status = PSA_ERROR_BAD_STATE;
4236 goto exit;
4237 }
Ronald Crona0d68172021-03-26 10:15:08 +01004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004240 status = PSA_ERROR_INVALID_ARGUMENT;
4241 goto exit;
4242 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 status = psa_driver_wrapper_cipher_set_iv(operation,
4245 iv,
4246 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004247
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004250 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 } else {
4252 psa_cipher_abort(operation);
4253 }
4254 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004255}
4256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4258 const uint8_t *input,
4259 size_t input_length,
4260 uint8_t *output,
4261 size_t output_size,
4262 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004263{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004267 status = PSA_ERROR_BAD_STATE;
4268 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004269 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004272 status = PSA_ERROR_BAD_STATE;
4273 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004274 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 status = psa_driver_wrapper_cipher_update(operation,
4277 input,
4278 input_length,
4279 output,
4280 output_size,
4281 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004282
4283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (status != PSA_SUCCESS) {
4285 psa_cipher_abort(operation);
4286 }
Ronald Cron6d051732020-10-01 14:10:20 +02004287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004289}
4290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4292 uint8_t *output,
4293 size_t output_size,
4294 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004295{
David Saadab4ecc272019-02-14 13:48:10 +02004296 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004297
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004299 status = PSA_ERROR_BAD_STATE;
4300 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004301 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004304 status = PSA_ERROR_BAD_STATE;
4305 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004306 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 status = psa_driver_wrapper_cipher_finish(operation,
4309 output,
4310 output_size,
4311 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004312
4313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (status == PSA_SUCCESS) {
4315 return psa_cipher_abort(operation);
4316 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004317 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004321 }
mohammad1603503973b2018-03-12 15:59:30 +02004322}
4323
Gilles Peskine449bd832023-01-11 14:50:10 +01004324psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004325{
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004327 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004328 * in use. It's ok to call abort on such an object, and there's
4329 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004331 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004334
Ronald Cron49fafa92021-03-10 08:34:23 +01004335 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004336 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004337 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004340}
4341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4343 psa_algorithm_t alg,
4344 const uint8_t *input,
4345 size_t input_length,
4346 uint8_t *output,
4347 size_t output_size,
4348 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004349{
4350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004351 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004352 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004353 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4354 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004355 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004358 status = PSA_ERROR_INVALID_ARGUMENT;
4359 goto exit;
4360 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4363 PSA_KEY_USAGE_ENCRYPT,
4364 alg);
4365 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004366 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004368
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004369 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004371 };
4372
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4374 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004375 status = PSA_ERROR_GENERIC_ERROR;
4376 goto exit;
4377 }
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (default_iv_length > 0) {
4380 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004381 status = PSA_ERROR_BUFFER_TOO_SMALL;
4382 goto exit;
4383 }
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_generate_random(local_iv, default_iv_length);
4386 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004387 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004389 }
4390
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004391 status = psa_driver_wrapper_cipher_encrypt(
4392 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004393 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004394 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004396
4397exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 unlock_status = psa_unlock_key_slot(slot);
4399 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004400 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004401 }
Ronald Cron23919522021-07-08 19:00:07 +02004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (status == PSA_SUCCESS) {
4404 if (default_iv_length > 0) {
4405 memcpy(output, local_iv, default_iv_length);
4406 }
4407 *output_length += default_iv_length;
4408 } else {
4409 *output_length = 0;
4410 }
4411
4412 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004413}
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4416 psa_algorithm_t alg,
4417 const uint8_t *input,
4418 size_t input_length,
4419 uint8_t *output,
4420 size_t output_size,
4421 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004422{
4423 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004424 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004425 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004426 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004429 status = PSA_ERROR_INVALID_ARGUMENT;
4430 goto exit;
4431 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4434 PSA_KEY_USAGE_DECRYPT,
4435 alg);
4436 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004437 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004439
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004440 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004442 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4445 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004446 status = PSA_ERROR_INVALID_ARGUMENT;
4447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004449 status = PSA_ERROR_INVALID_ARGUMENT;
4450 goto exit;
4451 }
4452
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004453 status = psa_driver_wrapper_cipher_decrypt(
4454 &attributes, slot->key.data, slot->key.bytes,
4455 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004457
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004458exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 unlock_status = psa_unlock_key_slot(slot);
4460 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004461 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004465 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 }
Ronald Cron23919522021-07-08 19:00:07 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004469}
4470
4471
mohammad16035955c982018-04-26 00:53:03 +03004472/****************************************************************/
4473/* AEAD */
4474/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004475
Paul Elliottbaff51c2021-09-28 17:44:45 +01004476/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004477static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004478{
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004480}
4481
4482/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004483static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4484 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004485{
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004489#if defined(PSA_WANT_ALG_GCM)
4490 case PSA_ALG_GCM:
4491 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 * arbitrarily large nonces. Please note that we do not generally
4493 * recommend the usage of nonces of greater length than
4494 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4495 * size, which can then lead to collisions if you encrypt a very
4496 * large number of messages.*/
4497 if (nonce_length != 0) {
4498 return PSA_SUCCESS;
4499 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004500 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004501#endif /* PSA_WANT_ALG_GCM */
4502#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004503 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (nonce_length >= 7 && nonce_length <= 13) {
4505 return PSA_SUCCESS;
4506 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004507 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004508#endif /* PSA_WANT_ALG_CCM */
4509#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004510 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 if (nonce_length == 12) {
4512 return PSA_SUCCESS;
4513 } else if (nonce_length == 8) {
4514 return PSA_ERROR_NOT_SUPPORTED;
4515 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004516 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004517#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004518 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004519 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004521 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004524}
4525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004527{
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4529 return PSA_ERROR_INVALID_ARGUMENT;
4530 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004533}
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4536 psa_algorithm_t alg,
4537 const uint8_t *nonce,
4538 size_t nonce_length,
4539 const uint8_t *additional_data,
4540 size_t additional_data_length,
4541 const uint8_t *plaintext,
4542 size_t plaintext_length,
4543 uint8_t *ciphertext,
4544 size_t ciphertext_size,
4545 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004546{
Ronald Cron215633c2021-03-16 17:15:37 +01004547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4548 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004549
mohammad1603f08a5502018-06-03 15:05:47 +03004550 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 status = psa_aead_check_algorithm(alg);
4553 if (status != PSA_SUCCESS) {
4554 return status;
4555 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004556
Ronald Cron9a986162021-03-26 12:40:07 +01004557 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4559 if (status != PSA_SUCCESS) {
4560 return status;
4561 }
mohammad16035955c982018-04-26 00:53:03 +03004562
Ronald Cron215633c2021-03-16 17:15:37 +01004563 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004565 };
mohammad16035955c982018-04-26 00:53:03 +03004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 status = psa_aead_check_nonce_length(alg, nonce_length);
4568 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004569 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004571
Ronald Cronde822812021-03-17 16:08:20 +01004572 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004573 &attributes, slot->key.data, slot->key.bytes,
4574 alg,
4575 nonce, nonce_length,
4576 additional_data, additional_data_length,
4577 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4581 memset(ciphertext, 0, ciphertext_size);
4582 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004583
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004588}
4589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4591 psa_algorithm_t alg,
4592 const uint8_t *nonce,
4593 size_t nonce_length,
4594 const uint8_t *additional_data,
4595 size_t additional_data_length,
4596 const uint8_t *ciphertext,
4597 size_t ciphertext_length,
4598 uint8_t *plaintext,
4599 size_t plaintext_size,
4600 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004601{
Ronald Cron215633c2021-03-16 17:15:37 +01004602 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4603 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004604
Gilles Peskineee652a32018-06-01 19:23:52 +02004605 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 status = psa_aead_check_algorithm(alg);
4608 if (status != PSA_SUCCESS) {
4609 return status;
4610 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004611
Ronald Cron9a986162021-03-26 12:40:07 +01004612 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4614 if (status != PSA_SUCCESS) {
4615 return status;
4616 }
mohammad16035955c982018-04-26 00:53:03 +03004617
Ronald Cron215633c2021-03-16 17:15:37 +01004618 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004620 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 status = psa_aead_check_nonce_length(alg, nonce_length);
4623 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004624 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004626
Ronald Cronde822812021-03-17 16:08:20 +01004627 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004628 &attributes, slot->key.data, slot->key.bytes,
4629 alg,
4630 nonce, nonce_length,
4631 additional_data, additional_data_length,
4632 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (status != PSA_SUCCESS && plaintext_size != 0) {
4636 memset(plaintext, 0, plaintext_size);
4637 }
mohammad160360a64d02018-06-03 17:20:42 +03004638
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004639exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 return status;
mohammad16035955c982018-04-26 00:53:03 +03004643}
4644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4646{
4647 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004650#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004652 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4654 return PSA_ERROR_INVALID_ARGUMENT;
4655 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004656 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004657#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004658
Przemek Stekiel86679c72022-10-06 17:06:56 +02004659#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004661 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4663 return PSA_ERROR_INVALID_ARGUMENT;
4664 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004665 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004666#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004667
Przemek Stekiel86679c72022-10-06 17:06:56 +02004668#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004670 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if (tag_len != 16) {
4672 return PSA_ERROR_INVALID_ARGUMENT;
4673 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004674 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004675#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004676
4677 default:
4678 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004680 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004682}
4683
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004684/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4686 int is_encrypt,
4687 mbedtls_svc_key_id_t key,
4688 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004689{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4691 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4692 psa_key_slot_t *slot = NULL;
4693 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004694 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 status = psa_aead_check_algorithm(alg);
4697 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004698 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004702 status = PSA_ERROR_BAD_STATE;
4703 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004704 }
4705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (operation->nonce_set || operation->lengths_set ||
4707 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004708 status = PSA_ERROR_BAD_STATE;
4709 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004710 }
4711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004713 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004715 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4719 alg);
4720 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004723
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004724 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004725 .core = slot->attr
4726 };
4727
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004729 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if (is_encrypt) {
4733 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4734 &attributes,
4735 slot->key.data,
4736 slot->key.bytes,
4737 alg);
4738 } else {
4739 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4740 &attributes,
4741 slot->key.data,
4742 slot->key.bytes,
4743 alg);
4744 }
4745 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004746 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004750
4751exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004755 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004757 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 } else {
4759 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004760 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004763}
4764
Paul Elliott302ff6b2021-04-20 18:10:30 +01004765/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4767 mbedtls_svc_key_id_t key,
4768 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004769{
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004771}
4772
4773/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004774psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4775 mbedtls_svc_key_id_t key,
4776 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004777{
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779}
4780
4781/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004782psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4783 uint8_t *nonce,
4784 size_t nonce_size,
4785 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004786{
4787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004788 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004789 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004791 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004794 status = PSA_ERROR_BAD_STATE;
4795 goto exit;
4796 }
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004799 status = PSA_ERROR_BAD_STATE;
4800 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004801 }
4802
Paul Elliotte193ea82021-10-01 13:00:16 +01004803 /* For CCM, this size may not be correct according to the PSA
4804 * specification. The PSA Crypto 1.0.1 specification states:
4805 *
4806 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4807 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4808 *
4809 * However this restriction that L has to be the smallest integer is not
4810 * applied in practice, and it is not implementable here since the
4811 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4813 operation->alg);
4814 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004815 status = PSA_ERROR_BUFFER_TOO_SMALL;
4816 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004817 }
4818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 status = psa_generate_random(local_nonce, required_nonce_size);
4820 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004825
Paul Elliott39dc6b82021-05-11 19:16:09 +01004826exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (status == PSA_SUCCESS) {
4828 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004829 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 } else {
4831 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004832 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004835}
4836
4837/* Set the nonce for a multipart authenticated encryption or decryption
4838 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004839psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4840 const uint8_t *nonce,
4841 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004842{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004846 status = PSA_ERROR_BAD_STATE;
4847 goto exit;
4848 }
4849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004851 status = PSA_ERROR_BAD_STATE;
4852 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004853 }
4854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4856 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004857 status = PSA_ERROR_INVALID_ARGUMENT;
4858 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004859 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4862 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004863
Paul Elliott39dc6b82021-05-11 19:16:09 +01004864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004866 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 } else {
4868 psa_aead_abort(operation);
4869 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004872}
4873
4874/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004875psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4876 size_t ad_length,
4877 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004878{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004882 status = PSA_ERROR_BAD_STATE;
4883 goto exit;
4884 }
4885
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 if (operation->lengths_set || operation->ad_started ||
4887 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004888 status = PSA_ERROR_BAD_STATE;
4889 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004890 }
4891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004893#if defined(PSA_WANT_ALG_GCM)
4894 case PSA_ALG_GCM:
4895 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 * bits. Without the guard this code will generate warnings on 32bit
4897 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004898#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (((uint64_t) ad_length) >> 61 != 0 ||
4900 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004901 status = PSA_ERROR_INVALID_ARGUMENT;
4902 goto exit;
4903 }
Paul Elliott325d3742021-09-27 17:56:28 +01004904#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004905 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004906#endif /* PSA_WANT_ALG_GCM */
4907#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004908 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004910 status = PSA_ERROR_INVALID_ARGUMENT;
4911 goto exit;
4912 }
4913 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004914#endif /* PSA_WANT_ALG_CCM */
4915#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004916 case PSA_ALG_CHACHA20_POLY1305:
4917 /* No length restrictions for ChaChaPoly. */
4918 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004919#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004920 default:
4921 break;
4922 }
Paul Elliott325d3742021-09-27 17:56:28 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4925 plaintext_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004926
Paul Elliott39dc6b82021-05-11 19:16:09 +01004927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (status == PSA_SUCCESS) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004929 operation->ad_remaining = ad_length;
4930 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004931 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 } else {
4933 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004934 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004937}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004938
4939/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004940psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4941 const uint8_t *input,
4942 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004943{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004947 status = PSA_ERROR_BAD_STATE;
4948 goto exit;
4949 }
4950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004952 status = PSA_ERROR_BAD_STATE;
4953 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004954 }
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 if (operation->lengths_set) {
4957 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004958 status = PSA_ERROR_INVALID_ARGUMENT;
4959 goto exit;
4960 }
4961
4962 operation->ad_remaining -= input_length;
4963 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004964#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004966 status = PSA_ERROR_BAD_STATE;
4967 goto exit;
4968 }
4969#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971 status = psa_driver_wrapper_aead_update_ad(operation, input,
4972 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004973
Paul Elliott39dc6b82021-05-11 19:16:09 +01004974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004976 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 } else {
4978 psa_aead_abort(operation);
4979 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004982}
4983
4984/* Encrypt or decrypt a message fragment in an active multipart AEAD
4985 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004986psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4987 const uint8_t *input,
4988 size_t input_length,
4989 uint8_t *output,
4990 size_t output_size,
4991 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004992{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004994
4995 *output_length = 0;
4996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004998 status = PSA_ERROR_BAD_STATE;
4999 goto exit;
5000 }
5001
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005003 status = PSA_ERROR_BAD_STATE;
5004 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005005 }
5006
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005008 /* Additional data length was supplied, but not all the additional
5009 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005011 status = PSA_ERROR_INVALID_ARGUMENT;
5012 goto exit;
5013 }
5014
5015 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01005017 status = PSA_ERROR_INVALID_ARGUMENT;
5018 goto exit;
5019 }
5020
5021 operation->body_remaining -= input_length;
5022 }
Paul Elliotte193ea82021-10-01 13:00:16 +01005023#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01005025 status = PSA_ERROR_BAD_STATE;
5026 goto exit;
5027 }
5028#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 status = psa_driver_wrapper_aead_update(operation, input, input_length,
5031 output, output_size,
5032 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005033
Paul Elliott39dc6b82021-05-11 19:16:09 +01005034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01005036 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 } else {
5038 psa_aead_abort(operation);
5039 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01005040
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005042}
5043
Gilles Peskine449bd832023-01-11 14:50:10 +01005044static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01005045{
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (operation->id == 0 || !operation->nonce_set) {
5047 return PSA_ERROR_BAD_STATE;
5048 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (operation->lengths_set && (operation->ad_remaining != 0 ||
5051 operation->body_remaining != 0)) {
5052 return PSA_ERROR_INVALID_ARGUMENT;
5053 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01005056}
5057
Paul Elliott302ff6b2021-04-20 18:10:30 +01005058/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005059psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
5060 uint8_t *ciphertext,
5061 size_t ciphertext_size,
5062 size_t *ciphertext_length,
5063 uint8_t *tag,
5064 size_t tag_size,
5065 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005066{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5068
Paul Elliott302ff6b2021-04-20 18:10:30 +01005069 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005070 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 status = psa_aead_final_checks(operation);
5073 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005078 status = PSA_ERROR_BAD_STATE;
5079 goto exit;
5080 }
5081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5083 ciphertext_size,
5084 ciphertext_length,
5085 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005086
Paul Elliott39dc6b82021-05-11 19:16:09 +01005087exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005088
5089
Paul Elliottf47b0952021-05-21 18:02:33 +01005090 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005091 * the zero tag size, make sure the tag is set to something implausible.
5092 * Even if the operation succeeds, make sure we clear the rest of the
5093 * buffer to prevent potential leakage of anything previously placed in
5094 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005095 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005100}
5101
5102/* Finish authenticating and decrypting a message in a multipart AEAD
5103 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005104psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5105 uint8_t *plaintext,
5106 size_t plaintext_size,
5107 size_t *plaintext_length,
5108 const uint8_t *tag,
5109 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005110{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005111 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5112
Paul Elliott302ff6b2021-04-20 18:10:30 +01005113 *plaintext_length = 0;
5114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 status = psa_aead_final_checks(operation);
5116 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005121 status = PSA_ERROR_BAD_STATE;
5122 goto exit;
5123 }
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5126 plaintext_size,
5127 plaintext_length,
5128 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005129
5130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005132
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005134}
5135
5136/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005138{
5139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005142 /* The object has (apparently) been initialized but it is not (yet)
5143 * in use. It's ok to call abort on such an object, and there's
5144 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005146 }
5147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005153}
5154
Gilles Peskinee59236f2018-01-27 23:32:46 +01005155/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156/* Generators */
5157/****************************************************************/
5158
Przemek Stekiel69c46792022-06-10 12:59:51 +02005159#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005160 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005161 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305162 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5163 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005164#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005165#endif /* At least one builtin KDF */
5166
Przemek Stekiel69c46792022-06-10 12:59:51 +02005167#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005168 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5169 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5170static psa_status_t psa_key_derivation_start_hmac(
5171 psa_mac_operation_t *operation,
5172 psa_algorithm_t hash_alg,
5173 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005175{
5176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5179 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5180 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005181
Steven Cooreman72f736a2021-05-07 14:14:37 +02005182 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 status = psa_driver_wrapper_mac_sign_setup(operation,
5186 &attributes,
5187 hmac_key, hmac_key_length,
5188 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 psa_reset_key_attributes(&attributes);
5191 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005192}
5193#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005194
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195#define HKDF_STATE_INIT 0 /* no input yet */
5196#define HKDF_STATE_STARTED 1 /* got salt */
5197#define HKDF_STATE_KEYED 2 /* got key */
5198#define HKDF_STATE_OUTPUT 3 /* output started */
5199
Gilles Peskinecbe66502019-05-16 16:59:18 +02005200static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005202{
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5204 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5205 } else {
5206 return operation->alg;
5207 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208}
5209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211{
5212 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5214 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005215 /* The object has (apparently) been initialized but it is not
5216 * in use. It's ok to call abort on such an object, and there's
5217 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005219#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5221 mbedtls_free(operation->ctx.hkdf.info);
5222 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5223 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005224#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005225#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5226 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5228 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5229 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5230 if (operation->ctx.tls12_prf.secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005231 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 operation->ctx.tls12_prf.secret_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02005233 }
5234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 if (operation->ctx.tls12_prf.seed != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005236 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.seed,
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 operation->ctx.tls12_prf.seed_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005238 }
5239
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (operation->ctx.tls12_prf.label != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005241 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.label,
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 operation->ctx.tls12_prf.label_length);
Janos Follath6a1d2622019-06-11 10:37:28 +01005243 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005244#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (operation->ctx.tls12_prf.other_secret != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005246 mbedtls_zeroize_and_free(operation->ctx.tls12_prf.other_secret,
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 operation->ctx.tls12_prf.other_secret_length);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005248 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005249#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005250 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005251
5252 /* We leave the fields Ai and output_block to be erased safely by the
5253 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005255#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5256 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005257#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5259 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5260 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5261 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005262#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305263#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5264 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305265 if (operation->ctx.pbkdf2.salt != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005266 mbedtls_zeroize_and_free(operation->ctx.pbkdf2.salt,
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305267 operation->ctx.pbkdf2.salt_length);
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305268 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305269
5270 status = PSA_SUCCESS;
5271 } else
5272#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005273 {
5274 status = PSA_ERROR_BAD_STATE;
5275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 mbedtls_platform_zeroize(operation, sizeof(*operation));
5277 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005278}
5279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282{
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005286 }
5287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005288 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290}
5291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5293 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005294{
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 if (operation->alg == 0) {
5296 return PSA_ERROR_BAD_STATE;
5297 }
5298 if (capacity > operation->capacity) {
5299 return PSA_ERROR_INVALID_ARGUMENT;
5300 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005303}
5304
Przemek Stekiel69c46792022-06-10 12:59:51 +02005305#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005306/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5308 psa_algorithm_t kdf_alg,
5309 uint8_t *output,
5310 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005311{
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5313 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005314 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005315 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005318#else
5319 const uint8_t last_block = 0xff;
5320#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 if (hkdf->state < HKDF_STATE_KEYED ||
5323 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005324#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005326#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 )) {
5328 return PSA_ERROR_BAD_STATE;
5329 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005330 hkdf->state = HKDF_STATE_OUTPUT;
5331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005333 /* Copy what remains of the current block */
5334 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005336 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 }
5338 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005339 output += n;
5340 output_length -= n;
5341 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005343 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005345 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005346 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005348 * object was corrupted or if this function is called directly
5349 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if (hkdf->block_number == last_block) {
5351 return PSA_ERROR_BAD_STATE;
5352 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005353
5354 /* We need a new block */
5355 ++hkdf->block_number;
5356 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5359 hash_alg,
5360 hkdf->prk,
5361 hash_length);
5362 if (status != PSA_SUCCESS) {
5363 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005364 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005365
5366 if (hkdf->block_number != 1) {
5367 status = psa_mac_update(&hkdf->hmac,
5368 hkdf->output_block,
5369 hash_length);
5370 if (status != PSA_SUCCESS) {
5371 return status;
5372 }
5373 }
5374 status = psa_mac_update(&hkdf->hmac,
5375 hkdf->info,
5376 hkdf->info_length);
5377 if (status != PSA_SUCCESS) {
5378 return status;
5379 }
5380 status = psa_mac_update(&hkdf->hmac,
5381 &hkdf->block_number, 1);
5382 if (status != PSA_SUCCESS) {
5383 return status;
5384 }
5385 status = psa_mac_sign_finish(&hkdf->hmac,
5386 hkdf->output_block,
5387 sizeof(hkdf->output_block),
5388 &hmac_output_length);
5389 if (status != PSA_SUCCESS) {
5390 return status;
5391 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005392 }
5393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005395}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005396#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005397
John Durkop07cc04a2020-11-16 22:08:34 -08005398#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5399 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005400static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5401 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005403{
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5405 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005406 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5407 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005408 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005409
Janos Follath7742fee2019-06-17 12:58:10 +01005410 /* We can't be wanting more output after block 0xff, otherwise
5411 * the capacity check in psa_key_derivation_output_bytes() would have
5412 * prevented this call. It could happen only if the operation
5413 * object was corrupted or if this function is called directly
5414 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (tls12_prf->block_number == 0xff) {
5416 return PSA_ERROR_CORRUPTION_DETECTED;
5417 }
Janos Follath7742fee2019-06-17 12:58:10 +01005418
5419 /* We need a new block */
5420 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005421 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005422
5423 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5424 *
5425 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5426 *
5427 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5428 * HMAC_hash(secret, A(2) + seed) +
5429 * HMAC_hash(secret, A(3) + seed) + ...
5430 *
5431 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005433 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005434 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005435 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005436 * is currently extracted as `output_block` and where i is
5437 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005438 */
5439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 status = psa_key_derivation_start_hmac(&hmac,
5441 hash_alg,
5442 tls12_prf->secret,
5443 tls12_prf->secret_length);
5444 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005447
5448 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5451 * the variable seed and in this instance means it in the context of the
5452 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 status = psa_mac_update(&hmac,
5454 tls12_prf->label,
5455 tls12_prf->label_length);
5456 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005457 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 }
5459 status = psa_mac_update(&hmac,
5460 tls12_prf->seed,
5461 tls12_prf->seed_length);
5462 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005463 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 }
5465 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005466 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5468 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005471 }
5472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_mac_sign_finish(&hmac,
5474 tls12_prf->Ai, hash_length,
5475 &hmac_output_length);
5476 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005477 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 }
5479 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005480 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005482
5483 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_key_derivation_start_hmac(&hmac,
5485 hash_alg,
5486 tls12_prf->secret,
5487 tls12_prf->secret_length);
5488 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005489 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 }
5491 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5492 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005493 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 }
5495 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5496 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005497 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 }
5499 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5500 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005501 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 }
5503 status = psa_mac_sign_finish(&hmac,
5504 tls12_prf->output_block, hash_length,
5505 &hmac_output_length);
5506 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005507 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005509
Janos Follath7742fee2019-06-17 12:58:10 +01005510
5511cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 cleanup_status = psa_mac_abort(&hmac);
5513 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005514 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005518}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005519
Janos Follath844eb0e2019-06-19 12:10:49 +01005520static psa_status_t psa_key_derivation_tls12_prf_read(
5521 psa_tls12_prf_key_derivation_t *tls12_prf,
5522 psa_algorithm_t alg,
5523 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005525{
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5527 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005528 psa_status_t status;
5529 uint8_t offset, length;
5530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005532 case PSA_TLS12_PRF_STATE_LABEL_SET:
5533 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5534 break;
5535 case PSA_TLS12_PRF_STATE_OUTPUT:
5536 break;
5537 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005539 }
5540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005542 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 if (tls12_prf->left_in_block == 0) {
5544 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5545 alg);
5546 if (status != PSA_SUCCESS) {
5547 return status;
5548 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005549
5550 continue;
5551 }
5552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005554 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005556 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005558
5559 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005561 output += length;
5562 output_length -= length;
5563 tls12_prf->left_in_block -= length;
5564 }
5565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005567}
John Durkop07cc04a2020-11-16 22:08:34 -08005568#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5569 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005570
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005571#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5572static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5573 psa_tls12_ecjpake_to_pms_t *ecjpake,
5574 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005576{
5577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005578 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (output_length != 32) {
5581 return PSA_ERROR_INVALID_ARGUMENT;
5582 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5585 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5586 &output_size);
5587 if (status != PSA_SUCCESS) {
5588 return status;
5589 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 if (output_size != output_length) {
5592 return PSA_ERROR_GENERIC_ERROR;
5593 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005596}
5597#endif
5598
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305599#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5600static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5601 psa_pbkdf2_key_derivation_t *pbkdf2,
5602 psa_algorithm_t prf_alg,
5603 uint8_t prf_output_length,
5604 psa_key_attributes_t *attributes)
5605{
5606 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305607 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305608 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305609 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305610 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305611 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305612 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305613
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305614 mac_operation.is_sign = 1;
5615 mac_operation.mac_size = prf_output_length;
5616 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305617
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305618 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5619 attributes,
5620 pbkdf2->password,
5621 pbkdf2->password_length,
5622 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305623 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305624 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305625 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305626 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5627 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305628 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305629 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305630 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305631 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305632 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305633 }
5634 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5635 &mac_output_length);
5636 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305637 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305638 }
5639
5640 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305641 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305642 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305643 }
5644
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305645 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305646
5647 for (i = 1; i < pbkdf2->input_cost; i++) {
5648 status = psa_driver_wrapper_mac_compute(attributes,
5649 pbkdf2->password,
5650 pbkdf2->password_length,
5651 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305652 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305653 &mac_output_length);
5654 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305655 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305656 }
5657
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305658 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305659 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305660
5661cleanup:
5662 /* Zeroise buffers to clear sensitive data from memory. */
5663 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5664 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305665}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305666
5667static psa_status_t psa_key_derivation_pbkdf2_read(
5668 psa_pbkdf2_key_derivation_t *pbkdf2,
5669 psa_algorithm_t kdf_alg,
5670 uint8_t *output,
5671 size_t output_length)
5672{
5673 psa_status_t status;
5674 psa_algorithm_t prf_alg;
5675 uint8_t prf_output_length;
5676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5677 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5678 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5679
5680 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5681 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5682 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5683 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305684 } else {
5685 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305686 }
5687
5688 switch (pbkdf2->state) {
5689 case PSA_PBKDF2_STATE_PASSWORD_SET:
5690 /* Initially we need a new block so bytes_used is equal to block size*/
5691 pbkdf2->bytes_used = prf_output_length;
5692 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5693 break;
5694 case PSA_PBKDF2_STATE_OUTPUT:
5695 break;
5696 default:
5697 return PSA_ERROR_BAD_STATE;
5698 }
5699
5700 while (output_length != 0) {
5701 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5702 if (n > output_length) {
5703 n = (uint8_t) output_length;
5704 }
5705 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5706 output += n;
5707 output_length -= n;
5708 pbkdf2->bytes_used += n;
5709
5710 if (output_length == 0) {
5711 break;
5712 }
5713
5714 /* We need a new block */
5715 pbkdf2->bytes_used = 0;
5716 pbkdf2->block_number++;
5717
5718 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5719 prf_output_length,
5720 &attributes);
5721 if (status != PSA_SUCCESS) {
5722 return status;
5723 }
5724 }
5725
5726 return PSA_SUCCESS;
5727}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305728#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5729
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005730psa_status_t psa_key_derivation_output_bytes(
5731 psa_key_derivation_operation_t *operation,
5732 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005734{
5735 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005741 }
5742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005745 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005747 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005748 goto exit;
5749 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005752 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005753 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5754 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005756 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005758 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005759 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005760
Przemek Stekiel69c46792022-06-10 12:59:51 +02005761#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5763 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5764 output, output_length);
5765 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005766#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005767#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5768 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5770 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5771 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5772 kdf_alg, output,
5773 output_length);
5774 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005775#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5776 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005777#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005779 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5781 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005782#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305783#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5784 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305785 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5786 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305787 } else
5788#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005789
Gilles Peskineeab56e42018-07-12 17:12:33 +02005790 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005791 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005793 }
5794
5795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005797 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005798 * This allows us to differentiate between exhausted operations and
5799 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5800 * operations. */
5801 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005803 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005805 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005807}
5808
David Brown7807bf72021-02-09 16:28:23 -07005809#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005810static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005811{
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 if (data_size >= 8) {
5813 mbedtls_des_key_set_parity(data);
5814 }
5815 if (data_size >= 16) {
5816 mbedtls_des_key_set_parity(data + 8);
5817 }
5818 if (data_size >= 24) {
5819 mbedtls_des_key_set_parity(data + 16);
5820 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005821}
David Brown7807bf72021-02-09 16:28:23 -07005822#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005823
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005824/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 * ECC keys on a Weierstrass elliptic curve require the generation
5826 * of a private key which is an integer
5827 * in the range [1, N - 1], where N is the boundary of the private key domain:
5828 * N is the prime p for Diffie-Hellman, or the order of the
5829 * curve’s base point for ECC.
5830 *
5831 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5832 * This function generates the private key using the following process:
5833 *
5834 * 1. Draw a byte string of length ceiling(m/8) bytes.
5835 * 2. If m is not a multiple of 8, set the most significant
5836 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5837 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5838 * 4. If k > N - 2, discard the result and return to step 1.
5839 * 5. Output k + 1 as the private key.
5840 *
5841 * This method allows compliance to NIST standards, specifically the methods titled
5842 * Key-Pair Generation by Testing Candidates in the following publications:
5843 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5844 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5845 * Diffie-Hellman keys.
5846 *
5847 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5848 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5849 *
5850 * Note: Function allocates memory for *data buffer, so given *data should be
5851 * always NULL.
5852 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005853#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5854#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005855static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005856 psa_key_slot_t *slot,
5857 size_t bits,
5858 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005859 uint8_t **data
5860 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005861{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005862 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005863 mbedtls_mpi k;
5864 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005866 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005867 size_t m;
5868 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 mbedtls_mpi_init(&k);
5871 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005872
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005875 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005879 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005880 goto cleanup;
5881 }
5882
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005883 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005887
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005888 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005889 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005890 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005891
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005892 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005893
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005894 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005896
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005897 /* Note: This function is always called with *data == NULL and it
5898 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 *data = mbedtls_calloc(1, m_bytes);
5900 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005901 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005902 goto cleanup;
5903 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005906 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005908 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005910
5911 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005913 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005914 * (8 * ceiling(m/8) - m) bits of the first byte in
5915 * the string to zero.
5916 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005918 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005919 }
5920
5921 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 * big-endian byte string.
5923 */
5924 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005925
5926 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 * Result of comparison is returned. When it indicates error
5928 * then this function is called again.
5929 */
5930 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005931 }
5932
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005933 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5935 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005936cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (ret != 0) {
5938 status = mbedtls_to_psa_error(ret);
5939 }
5940 if (status != PSA_SUCCESS) {
5941 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005942 *data = NULL;
5943 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 mbedtls_mpi_free(&k);
5945 mbedtls_mpi_free(&diff_N_2);
5946 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005947}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005948
5949/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5950 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5951 *
5952 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5953 * draw a 32-byte string and process it as specified in
5954 * Elliptic Curves for Security [RFC7748] §5.
5955 *
5956 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5957 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5958 *
5959 * Note: Function allocates memory for *data buffer, so given *data should be
5960 * always NULL.
5961 */
5962
5963static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5964 size_t bits,
5965 psa_key_derivation_operation_t *operation,
5966 uint8_t **data
5967 )
5968{
5969 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005970 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005973 case 255:
5974 output_length = 32;
5975 break;
5976 case 448:
5977 output_length = 56;
5978 break;
5979 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005981 break;
5982 }
5983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (*data == NULL) {
5987 return PSA_ERROR_INSUFFICIENT_MEMORY;
5988 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005993 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005997 case 255:
5998 (*data)[0] &= 248;
5999 (*data)[31] &= 127;
6000 (*data)[31] |= 64;
6001 break;
6002 case 448:
6003 (*data)[0] &= 252;
6004 (*data)[55] |= 128;
6005 break;
6006 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01006008 break;
6009 }
6010
6011 return status;
6012}
Valerio Setti86587ab2023-06-27 13:09:30 +02006013#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6014static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
6015 psa_key_slot_t *slot, size_t bits,
6016 psa_key_derivation_operation_t *operation, uint8_t **data)
6017{
6018 (void) slot;
6019 (void) bits;
6020 (void) operation;
6021 (void) data;
6022 return PSA_ERROR_NOT_SUPPORTED;
6023}
6024
6025static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
6026 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
6027{
6028 (void) bits;
6029 (void) operation;
6030 (void) data;
6031 return PSA_ERROR_NOT_SUPPORTED;
6032}
6033#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
6034#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01006035
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01006036static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006037 psa_key_slot_t *slot,
6038 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02006040{
6041 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05306043 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01006044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006045 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
6048 return PSA_ERROR_INVALID_ARGUMENT;
6049 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006050
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006051#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02006052 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
6054 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
6055 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006056 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6058 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006059 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 }
6061 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006062 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6064 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006065 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006067 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006068 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006069#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006070 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (key_type_is_raw_bytes(slot->attr.type)) {
6072 if (bits % 8 != 0) {
6073 return PSA_ERROR_INVALID_ARGUMENT;
6074 }
6075 data = mbedtls_calloc(1, bytes);
6076 if (data == NULL) {
6077 return PSA_ERROR_INSUFFICIENT_MEMORY;
6078 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 status = psa_key_derivation_output_bytes(operation, data, bytes);
6081 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 }
David Brown12ca5032021-02-11 11:02:00 -07006084#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6086 psa_des_set_key_parity(data, bytes);
6087 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006088#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 } else {
6090 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006091 }
Ronald Crondd04d422020-11-28 15:14:42 +01006092
Ronald Cronbf33c932020-11-28 18:06:53 +01006093 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006094 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006096 };
6097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6099 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6100 &storage_size);
6101 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 }
Archanad8a83dc2021-06-14 10:04:16 +05306104 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 status = psa_allocate_buffer_to_slot(slot, storage_size);
6106 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 }
Archanad8a83dc2021-06-14 10:04:16 +05306109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 status = psa_driver_wrapper_import_key(&attributes,
6111 data, bytes,
6112 slot->key.data,
6113 slot->key.bytes,
6114 &slot->key.bytes, &bits);
6115 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006116 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006118
6119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 mbedtls_free(data);
6121 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006122}
6123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6125 psa_key_derivation_operation_t *operation,
6126 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006127{
6128 psa_status_t status;
6129 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006130 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006131
Ronald Cron81709fc2020-11-14 12:10:32 +01006132 *key = MBEDTLS_SVC_KEY_ID_INIT;
6133
Gilles Peskine0f84d622019-09-12 19:03:13 +02006134 /* Reject any attempt to create a zero-length key so that we don't
6135 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (psa_get_key_bits(attributes) == 0) {
6137 return PSA_ERROR_INVALID_ARGUMENT;
6138 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if (operation->alg == PSA_ALG_NONE) {
6141 return PSA_ERROR_BAD_STATE;
6142 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 if (!operation->can_output_key) {
6145 return PSA_ERROR_NOT_PERMITTED;
6146 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6149 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006150#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006152 /* Deriving a key in a secure element is not implemented yet. */
6153 status = PSA_ERROR_NOT_SUPPORTED;
6154 }
6155#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 if (status == PSA_SUCCESS) {
6157 status = psa_generate_derived_key_internal(slot,
6158 attributes->core.bits,
6159 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (status == PSA_SUCCESS) {
6162 status = psa_finish_key_creation(slot, driver, key);
6163 }
6164 if (status != PSA_SUCCESS) {
6165 psa_fail_key_creation(slot, driver);
6166 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006169}
6170
Gilles Peskineeab56e42018-07-12 17:12:33 +02006171
6172
6173/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006174/* Key derivation */
6175/****************************************************************/
6176
Steven Cooreman932ffb72021-02-15 12:14:32 +01006177#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006178static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006179{
6180#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6182 return 1;
6183 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006184#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006185#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6187 return 1;
6188 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006189#endif
6190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6192 return 1;
6193 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006194#endif
6195#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6197 return 1;
6198 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006199#endif
6200#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6202 return 1;
6203 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006204#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006205#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6207 return 1;
6208 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006209#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306210#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6211 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6212 return 1;
6213 }
6214#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006216}
6217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006219{
6220 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 psa_status_t status = psa_hash_setup(&operation, alg);
6222 psa_hash_abort(&operation);
6223 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006224}
6225
Gilles Peskine969c5d62019-01-16 15:53:06 +01006226static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006229{
Janos Follath5fe19732019-06-20 15:09:30 +01006230 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6231 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006233
Gilles Peskine969c5d62019-01-16 15:53:06 +01006234 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 if (!is_kdf_alg_supported(kdf_alg)) {
6236 return PSA_ERROR_NOT_SUPPORTED;
6237 }
John Durkop07cc04a2020-11-16 22:08:34 -08006238
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006239 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006240 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6242 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6243 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6244 if (hash_size == 0) {
6245 return PSA_ERROR_NOT_SUPPORTED;
6246 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006247
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006248 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6249 * we might fail later, which is somewhat unfriendly and potentially
6250 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 psa_status_t status = psa_hash_try_support(hash_alg);
6252 if (status != PSA_SUCCESS) {
6253 return status;
6254 }
6255 } else {
6256 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006257 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6260 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6261 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6262 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006263 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006264#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006265 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6267 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006268 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006270#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6271 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 operation->capacity = 255 * hash_size;
6273 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006274}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006277{
6278#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if (alg == PSA_ALG_ECDH) {
6280 return PSA_SUCCESS;
6281 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006282#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006283#if defined(PSA_WANT_ALG_FFDH)
6284 if (alg == PSA_ALG_FFDH) {
6285 return PSA_SUCCESS;
6286 }
6287#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006288 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006290}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006291
6292static int psa_key_derivation_allows_free_form_secret_input(
6293 psa_algorithm_t kdf_alg)
6294{
6295#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6296 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6297 return 0;
6298 }
6299#endif
6300 (void) kdf_alg;
6301 return 1;
6302}
John Durkop07cc04a2020-11-16 22:08:34 -08006303#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6306 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006307{
6308 psa_status_t status;
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (operation->alg != 0) {
6311 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006312 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6315 return PSA_ERROR_INVALID_ARGUMENT;
6316 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6317#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6318 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6319 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6320 status = psa_key_agreement_try_support(ka_alg);
6321 if (status != PSA_SUCCESS) {
6322 return status;
6323 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006324 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6325 return PSA_ERROR_INVALID_ARGUMENT;
6326 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6328#else
6329 return PSA_ERROR_NOT_SUPPORTED;
6330#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6331 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6332#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6333 status = psa_key_derivation_setup_kdf(operation, alg);
6334#else
6335 return PSA_ERROR_NOT_SUPPORTED;
6336#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6337 } else {
6338 return PSA_ERROR_INVALID_ARGUMENT;
6339 }
6340
6341 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006342 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 }
6344 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006345}
6346
Przemek Stekiel69c46792022-06-10 12:59:51 +02006347#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006348static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6349 psa_algorithm_t kdf_alg,
6350 psa_key_derivation_step_t step,
6351 const uint8_t *data,
6352 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006353{
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006355 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006357 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6360 return PSA_ERROR_INVALID_ARGUMENT;
6361 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006362#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 if (hkdf->state != HKDF_STATE_INIT) {
6364 return PSA_ERROR_BAD_STATE;
6365 } else {
6366 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6367 hash_alg,
6368 data, data_length);
6369 if (status != PSA_SUCCESS) {
6370 return status;
6371 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006372 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006374 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006375 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006376#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006378 /* We shouldn't be in different state as HKDF_EXPAND only allows
6379 * two inputs: SECRET (this case) and INFO which does not modify
6380 * the state. It could happen only if the hkdf
6381 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 if (hkdf->state != HKDF_STATE_INIT) {
6383 return PSA_ERROR_BAD_STATE;
6384 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006385
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006386 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6388 return PSA_ERROR_INVALID_ARGUMENT;
6389 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 memcpy(hkdf->prk, data, data_length);
6392 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006393#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006394 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006395 /* HKDF: If no salt was provided, use an empty salt.
6396 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006398#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6400 return PSA_ERROR_BAD_STATE;
6401 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006402#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6404 hash_alg,
6405 NULL, 0);
6406 if (status != PSA_SUCCESS) {
6407 return status;
6408 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006409 hkdf->state = HKDF_STATE_STARTED;
6410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 if (hkdf->state != HKDF_STATE_STARTED) {
6412 return PSA_ERROR_BAD_STATE;
6413 }
6414 status = psa_mac_update(&hkdf->hmac,
6415 data, data_length);
6416 if (status != PSA_SUCCESS) {
6417 return status;
6418 }
6419 status = psa_mac_sign_finish(&hkdf->hmac,
6420 hkdf->prk,
6421 sizeof(hkdf->prk),
6422 &data_length);
6423 if (status != PSA_SUCCESS) {
6424 return status;
6425 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006426 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006427
Gilles Peskine2b522db2019-04-12 15:11:49 +02006428 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006429 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006430#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006432 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006434 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006436#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006437 {
6438 /* Block 0 is empty, and the next block will be
6439 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006441 }
6442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006444 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006445#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6447 return PSA_ERROR_INVALID_ARGUMENT;
6448 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006449#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006450#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6452 hkdf->state == HKDF_STATE_INIT) {
6453 return PSA_ERROR_BAD_STATE;
6454 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006455#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 if (hkdf->state == HKDF_STATE_OUTPUT) {
6457 return PSA_ERROR_BAD_STATE;
6458 }
6459 if (hkdf->info_set) {
6460 return PSA_ERROR_BAD_STATE;
6461 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006462 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 if (data_length != 0) {
6464 hkdf->info = mbedtls_calloc(1, data_length);
6465 if (hkdf->info == NULL) {
6466 return PSA_ERROR_INSUFFICIENT_MEMORY;
6467 }
6468 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006469 }
6470 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006472 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006474 }
6475}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006476#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006477
John Durkop07cc04a2020-11-16 22:08:34 -08006478#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6479 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006480static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6481 const uint8_t *data,
6482 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006483{
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6485 return PSA_ERROR_BAD_STATE;
6486 }
Janos Follathf08e2652019-06-13 09:05:41 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (data_length != 0) {
6489 prf->seed = mbedtls_calloc(1, data_length);
6490 if (prf->seed == NULL) {
6491 return PSA_ERROR_INSUFFICIENT_MEMORY;
6492 }
Janos Follathf08e2652019-06-13 09:05:41 +01006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006495 prf->seed_length = data_length;
6496 }
Janos Follathf08e2652019-06-13 09:05:41 +01006497
Gilles Peskine43f958b2020-12-13 14:55:14 +01006498 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006501}
6502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6504 const uint8_t *data,
6505 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006506{
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6508 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6509 return PSA_ERROR_BAD_STATE;
6510 }
Janos Follath81550542019-06-13 14:26:34 +01006511
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 if (data_length != 0) {
6513 prf->secret = mbedtls_calloc(1, data_length);
6514 if (prf->secret == NULL) {
6515 return PSA_ERROR_INSUFFICIENT_MEMORY;
6516 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006519 prf->secret_length = data_length;
6520 }
Janos Follath81550542019-06-13 14:26:34 +01006521
Gilles Peskine43f958b2020-12-13 14:55:14 +01006522 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006525}
6526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6528 const uint8_t *data,
6529 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006530{
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6532 return PSA_ERROR_BAD_STATE;
6533 }
Janos Follath63028dd2019-06-13 09:15:47 +01006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 if (data_length != 0) {
6536 prf->label = mbedtls_calloc(1, data_length);
6537 if (prf->label == NULL) {
6538 return PSA_ERROR_INSUFFICIENT_MEMORY;
6539 }
Janos Follath63028dd2019-06-13 09:15:47 +01006540
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006542 prf->label_length = data_length;
6543 }
Janos Follath63028dd2019-06-13 09:15:47 +01006544
Gilles Peskine43f958b2020-12-13 14:55:14 +01006545 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006548}
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6551 psa_key_derivation_step_t step,
6552 const uint8_t *data,
6553 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006554{
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006556 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006558 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006560 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006562 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006564 }
6565}
John Durkop07cc04a2020-11-16 22:08:34 -08006566#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6567 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6568
6569#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6570static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6571 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006572 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006574{
6575 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6577 4 + data_length + prf->other_secret_length :
6578 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006579
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6581 return PSA_ERROR_INVALID_ARGUMENT;
6582 }
John Durkop07cc04a2020-11-16 22:08:34 -08006583
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 uint8_t *pms = mbedtls_calloc(1, pms_len);
6585 if (pms == NULL) {
6586 return PSA_ERROR_INSUFFICIENT_MEMORY;
6587 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006588 uint8_t *cur = pms;
6589
6590 /* pure-PSK:
6591 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006592 *
6593 * The premaster secret is formed as follows: if the PSK is N octets
6594 * long, concatenate a uint16 with the value N, N zero octets, a second
6595 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006596 *
6597 * mixed-PSK:
6598 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6599 * follows: concatenate a uint16 with the length of the other secret,
6600 * the other secret itself, uint16 with the length of PSK, and the
6601 * PSK itself.
6602 * For details please check:
6603 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6604 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6605 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006606 */
6607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6609 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6610 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6611 if (prf->other_secret_length != 0) {
6612 memcpy(cur, prf->other_secret, prf->other_secret_length);
6613 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006614 cur += prf->other_secret_length;
6615 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 } else {
6617 *cur++ = MBEDTLS_BYTE_1(data_length);
6618 *cur++ = MBEDTLS_BYTE_0(data_length);
6619 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006620 cur += data_length;
6621 }
6622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 *cur++ = MBEDTLS_BYTE_1(data_length);
6624 *cur++ = MBEDTLS_BYTE_0(data_length);
6625 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006626 cur += data_length;
6627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006629
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006630 mbedtls_zeroize_and_free(pms, pms_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006632}
Janos Follath6660f0e2019-06-17 08:44:03 +01006633
Przemek Stekiele47201b2022-04-19 13:53:28 +02006634static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6635 psa_tls12_prf_key_derivation_t *prf,
6636 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006638{
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6640 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006641 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006642
6643 if (data_length != 0) {
6644 prf->other_secret = mbedtls_calloc(1, data_length);
6645 if (prf->other_secret == NULL) {
6646 return PSA_ERROR_INSUFFICIENT_MEMORY;
6647 }
6648
6649 memcpy(prf->other_secret, data, data_length);
6650 prf->other_secret_length = data_length;
6651 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006652 prf->other_secret_length = 0;
6653 }
6654
6655 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006658}
6659
Janos Follath6660f0e2019-06-17 08:44:03 +01006660static psa_status_t psa_tls12_prf_psk_to_ms_input(
6661 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006662 psa_key_derivation_step_t step,
6663 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006665{
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006667 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 return psa_tls12_prf_psk_to_ms_set_key(prf,
6669 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006670 break;
6671 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6673 data,
6674 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006675 break;
6676 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006678 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006679
Przemek Stekiele47201b2022-04-19 13:53:28 +02006680 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006681}
John Durkop07cc04a2020-11-16 22:08:34 -08006682#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006683
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006684#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6685static psa_status_t psa_tls12_ecjpake_to_pms_input(
6686 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006687 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006688 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006690{
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6692 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6693 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006694 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006695
6696 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 if (data[0] != 0x04) {
6698 return PSA_ERROR_INVALID_ARGUMENT;
6699 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006700
6701 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006705}
6706#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306707
6708#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6709static psa_status_t psa_pbkdf2_set_input_cost(
6710 psa_pbkdf2_key_derivation_t *pbkdf2,
6711 psa_key_derivation_step_t step,
6712 uint64_t data)
6713{
6714 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6715 return PSA_ERROR_INVALID_ARGUMENT;
6716 }
6717
6718 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6719 return PSA_ERROR_BAD_STATE;
6720 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306721
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306722 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306723 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306724 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306725
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306726 if (data == 0) {
6727 return PSA_ERROR_INVALID_ARGUMENT;
6728 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306729
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306730 pbkdf2->input_cost = data;
6731 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6732
6733 return PSA_SUCCESS;
6734}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306735
6736static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6737 const uint8_t *data,
6738 size_t data_length)
6739{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306740 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6741 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6742 return PSA_ERROR_BAD_STATE;
6743 }
6744
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306745 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6746 pbkdf2->salt = mbedtls_calloc(1, data_length);
6747 if (pbkdf2->salt == NULL) {
6748 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306749 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306750
6751 memcpy(pbkdf2->salt, data, data_length);
6752 pbkdf2->salt_length = data_length;
6753 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306754 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306755
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306756 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6757 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306758 return PSA_ERROR_INSUFFICIENT_MEMORY;
6759 }
6760
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306761 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6762 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306763 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306764 mbedtls_free(pbkdf2->salt);
6765 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306766 }
6767
6768 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6769
6770 return PSA_SUCCESS;
6771}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306772
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306773static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306774 const uint8_t *input,
6775 size_t input_len,
6776 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306777 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306778{
6779 psa_status_t status = PSA_SUCCESS;
6780 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6781 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306782 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306783 } else {
6784 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306785 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306786 }
6787 return status;
6788}
6789
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306790static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306791 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306792 const uint8_t *data,
6793 size_t data_length)
6794{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306795 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306796 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6797 return PSA_ERROR_BAD_STATE;
6798 }
6799
6800 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306801 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6802 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6803 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6804 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306805 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306806 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306807 }
6808
6809 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6810
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306811 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306812}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306813
6814static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306815 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306816 psa_key_derivation_step_t step,
6817 const uint8_t *data,
6818 size_t data_length)
6819{
6820 switch (step) {
6821 case PSA_KEY_DERIVATION_INPUT_SALT:
6822 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6823 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306824 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306825 default:
6826 return PSA_ERROR_INVALID_ARGUMENT;
6827 }
6828}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306829#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6830
Gilles Peskineb8965192019-09-24 16:21:10 +02006831/** Check whether the given key type is acceptable for the given
6832 * input step of a key derivation.
6833 *
6834 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6835 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6836 * Both secret and non-secret inputs can alternatively have the type
6837 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6838 * that the input was passed as a buffer rather than via a key object.
6839 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006840static int psa_key_derivation_check_input_type(
6841 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006843{
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006845 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 if (key_type == PSA_KEY_TYPE_DERIVE) {
6847 return PSA_SUCCESS;
6848 }
6849 if (key_type == PSA_KEY_TYPE_NONE) {
6850 return PSA_SUCCESS;
6851 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006852 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006853 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 if (key_type == PSA_KEY_TYPE_DERIVE) {
6855 return PSA_SUCCESS;
6856 }
6857 if (key_type == PSA_KEY_TYPE_NONE) {
6858 return PSA_SUCCESS;
6859 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006860 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006861 case PSA_KEY_DERIVATION_INPUT_LABEL:
6862 case PSA_KEY_DERIVATION_INPUT_SALT:
6863 case PSA_KEY_DERIVATION_INPUT_INFO:
6864 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6866 return PSA_SUCCESS;
6867 }
6868 if (key_type == PSA_KEY_TYPE_NONE) {
6869 return PSA_SUCCESS;
6870 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006871 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306872 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6873 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6874 return PSA_SUCCESS;
6875 }
6876 if (key_type == PSA_KEY_TYPE_DERIVE) {
6877 return PSA_SUCCESS;
6878 }
6879 if (key_type == PSA_KEY_TYPE_NONE) {
6880 return PSA_SUCCESS;
6881 }
6882 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006883 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006885}
6886
Janos Follathb80a94e2019-06-12 15:54:46 +01006887static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006888 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006889 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006890 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006891 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006893{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006894 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 status = psa_key_derivation_check_input_type(step, key_type);
6898 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006899 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006901
Przemek Stekiel69c46792022-06-10 12:59:51 +02006902#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6904 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6905 step, data, data_length);
6906 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006907#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006908#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6910 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6911 step, data, data_length);
6912 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006913#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6914#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6916 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6917 step, data, data_length);
6918 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006919#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006920#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006922 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6924 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006925#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306926#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6927 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306928 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6929 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306930 } else
6931#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006932 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006933 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006934 (void) data;
6935 (void) data_length;
6936 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006938 }
6939
Gilles Peskine224b0d62019-09-23 18:13:17 +02006940exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (status != PSA_SUCCESS) {
6942 psa_key_derivation_abort(operation);
6943 }
6944 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006945}
6946
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306947static psa_status_t psa_key_derivation_input_integer_internal(
6948 psa_key_derivation_operation_t *operation,
6949 psa_key_derivation_step_t step,
6950 uint64_t value)
6951{
6952 psa_status_t status;
6953 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6954
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306955#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6956 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6957 status = psa_pbkdf2_set_input_cost(
6958 &operation->ctx.pbkdf2, step, value);
6959 } else
6960#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306961 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306962 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306963 (void) value;
6964 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306965 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306966 }
6967
6968 if (status != PSA_SUCCESS) {
6969 psa_key_derivation_abort(operation);
6970 }
6971 return status;
6972}
6973
Janos Follath51f4a0f2019-06-14 11:35:55 +01006974psa_status_t psa_key_derivation_input_bytes(
6975 psa_key_derivation_operation_t *operation,
6976 psa_key_derivation_step_t step,
6977 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006979{
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 return psa_key_derivation_input_internal(operation, step,
6981 PSA_KEY_TYPE_NONE,
6982 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006983}
6984
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306985psa_status_t psa_key_derivation_input_integer(
6986 psa_key_derivation_operation_t *operation,
6987 psa_key_derivation_step_t step,
6988 uint64_t value)
6989{
6990 return psa_key_derivation_input_integer_internal(operation, step, value);
6991}
6992
Janos Follath51f4a0f2019-06-14 11:35:55 +01006993psa_status_t psa_key_derivation_input_key(
6994 psa_key_derivation_operation_t *operation,
6995 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006997{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006999 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007000 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007001
Ronald Cron5c522922020-11-14 16:35:34 +01007002 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7004 if (status != PSA_SUCCESS) {
7005 psa_key_derivation_abort(operation);
7006 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02007007 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007008
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05307009 /* Passing a key object as a SECRET or PASSWORD input unlocks the
7010 * permission to output to a key object. */
7011 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
7012 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007013 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02007015
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 status = psa_key_derivation_input_internal(operation,
7017 step, slot->attr.type,
7018 slot->key.data,
7019 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007020
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007024}
Gilles Peskineea0fb492018-07-12 17:17:20 +02007025
7026
7027
7028/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02007029/* Key agreement */
7030/****************************************************************/
7031
Gilles Peskine449bd832023-01-11 14:50:10 +01007032psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
7033 const uint8_t *key_buffer,
7034 size_t key_buffer_size,
7035 psa_algorithm_t alg,
7036 const uint8_t *peer_key,
7037 size_t peer_key_length,
7038 uint8_t *shared_secret,
7039 size_t shared_secret_size,
7040 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02007041{
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08007043#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007044 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
7046 key_buffer_size, alg,
7047 peer_key, peer_key_length,
7048 shared_secret,
7049 shared_secret_size,
7050 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007051#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007052
7053#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
7054 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02007055 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01007056 peer_key,
7057 peer_key_length,
7058 key_buffer,
7059 key_buffer_size,
7060 shared_secret,
7061 shared_secret_size,
7062 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007063#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7064
Gilles Peskine01d718c2018-09-18 12:01:02 +02007065 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007066 (void) attributes;
7067 (void) key_buffer;
7068 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007069 (void) peer_key;
7070 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007071 (void) shared_secret;
7072 (void) shared_secret_size;
7073 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007075 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007076}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007077
Aditya Deshpande17845b82022-10-13 17:21:01 +01007078/** Internal function for raw key agreement
7079 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007080 * to the driver's implementation if a driver is present.
7081 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007082 * (psa_key_agreement_raw_builtin).
7083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7085 psa_key_slot_t *private_key,
7086 const uint8_t *peer_key,
7087 size_t peer_key_length,
7088 uint8_t *shared_secret,
7089 size_t shared_secret_size,
7090 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007091{
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7093 return PSA_ERROR_NOT_SUPPORTED;
7094 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007095
7096 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007098 };
7099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 return psa_driver_wrapper_key_agreement(&attributes,
7101 private_key->key.data,
7102 private_key->key.bytes, alg,
7103 peer_key, peer_key_length,
7104 shared_secret,
7105 shared_secret_size,
7106 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007107}
7108
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007109/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007110 * to potentially free embedded data structures and wipe confidential data.
7111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007112static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7113 psa_key_derivation_step_t step,
7114 psa_key_slot_t *private_key,
7115 const uint8_t *peer_key,
7116 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007117{
7118 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007119 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007120 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007122
7123 /* Step 1: run the secret agreement algorithm to generate the shared
7124 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 status = psa_key_agreement_raw_internal(ka_alg,
7126 private_key,
7127 peer_key, peer_key_length,
7128 shared_secret,
7129 sizeof(shared_secret),
7130 &shared_secret_length);
7131 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007132 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007134
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007135 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007136 * the shared secret. A shared secret is permitted wherever a key
7137 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 status = psa_key_derivation_input_internal(operation, step,
7139 PSA_KEY_TYPE_DERIVE,
7140 shared_secret,
7141 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7144 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007145}
7146
Gilles Peskine449bd832023-01-11 14:50:10 +01007147psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7148 psa_key_derivation_step_t step,
7149 mbedtls_svc_key_id_t private_key,
7150 const uint8_t *peer_key,
7151 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007152{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007154 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007155 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007156
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7158 return PSA_ERROR_INVALID_ARGUMENT;
7159 }
Ronald Cron5c522922020-11-14 16:35:34 +01007160 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7162 if (status != PSA_SUCCESS) {
7163 return status;
7164 }
7165 status = psa_key_agreement_internal(operation, step,
7166 slot,
7167 peer_key, peer_key_length);
7168 if (status != PSA_SUCCESS) {
7169 psa_key_derivation_abort(operation);
7170 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007171 /* If a private key has been added as SECRET, we allow the derived
7172 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007174 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007176 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007181}
7182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7184 mbedtls_svc_key_id_t private_key,
7185 const uint8_t *peer_key,
7186 size_t peer_key_length,
7187 uint8_t *output,
7188 size_t output_size,
7189 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007190{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007192 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007193 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007194 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007197 status = PSA_ERROR_INVALID_ARGUMENT;
7198 goto exit;
7199 }
Ronald Cron5c522922020-11-14 16:35:34 +01007200 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007201 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7202 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007205
Gilles Peskine3e561302022-04-14 00:17:15 +02007206 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7207 * for the output size. The PSA specification only guarantees that this
7208 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7209 * but it might be nice to allow smaller buffers if the output fits.
7210 * At the time of writing this comment, with only ECDH implemented,
7211 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7212 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7213 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007214 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7216 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007217 status = PSA_ERROR_BUFFER_TOO_SMALL;
7218 goto exit;
7219 }
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 status = psa_key_agreement_raw_internal(alg, slot,
7222 peer_key, peer_key_length,
7223 output, output_size,
7224 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007225
7226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007228 /* If an error happens and is not handled properly, the output
7229 * may be used as a key to protect sensitive data. Arrange for such
7230 * a key to be random, which is likely to result in decryption or
7231 * verification errors. This is better than filling the buffer with
7232 * some constant data such as zeros, which would result in the data
7233 * being protected with a reproducible, easily knowable key.
7234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007236 *output_length = output_size;
7237 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007242}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007243
7244
Gilles Peskine30524eb2020-11-13 17:02:26 +01007245
Gilles Peskine86a440b2018-11-12 18:39:40 +01007246/****************************************************************/
7247/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007248/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007249
Gilles Peskine672a7712023-04-28 21:00:28 +02007250#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7251#include "entropy_poll.h"
7252#endif
7253
Gilles Peskine30524eb2020-11-13 17:02:26 +01007254/** Initialize the PSA random generator.
7255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007256static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007257{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007258#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007260#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7261
Gilles Peskine30524eb2020-11-13 17:02:26 +01007262 /* Set default configuration if
7263 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007265 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 }
7267 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007268 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007272#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7273 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7274 /* The PSA entropy injection feature depends on using NV seed as an entropy
7275 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 mbedtls_entropy_add_source(&rng->entropy,
7277 mbedtls_nv_seed_poll, NULL,
7278 MBEDTLS_ENTROPY_BLOCK_SIZE,
7279 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007280#endif
7281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007283#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007284}
7285
7286/** Deinitialize the PSA random generator.
7287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007288static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007289{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007290#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007292#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7294 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007295#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007296}
7297
7298/** Seed the PSA random generator.
7299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007300static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007301{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007302#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7303 /* Do nothing: the external RNG seeds itself. */
7304 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007306#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007307 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7309 drbg_seed, sizeof(drbg_seed) - 1);
7310 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007311#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007312}
7313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314psa_status_t psa_generate_random(uint8_t *output,
7315 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007316{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007317 GUARD_MODULE_INITIALIZED;
7318
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007319#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7320
7321 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7323 output, output_size,
7324 &output_length);
7325 if (status != PSA_SUCCESS) {
7326 return status;
7327 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007328 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007329 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 if (output_length != output_size) {
7331 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7332 }
7333 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007334
7335#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007338 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7340 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7341 output_size);
7342 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7343 output, request_size);
7344 if (ret != 0) {
7345 return mbedtls_to_psa_error(ret);
7346 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007347 output_size -= request_size;
7348 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007351#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007352}
7353
Gilles Peskine8814fc42020-12-14 15:33:44 +01007354/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007355 *
7356 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7357 * `psa_generate_random(...)`. The state parameter is ignored since the
7358 * PSA API doesn't support passing an explicit state.
7359 *
7360 * In the non-external case, psa_generate_random() calls an
7361 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7362 * and semantics as mbedtls_psa_get_random(). As an optimization,
7363 * instead of doing this back-and-forth between the PSA API and the
7364 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7365 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007367#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7368int mbedtls_psa_get_random(void *p_rng,
7369 unsigned char *output,
7370 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007371{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007372 /* This function takes a pointer to the RNG state because that's what
7373 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7374 * its own state internally and doesn't let the caller access that state.
7375 * So we just ignore the state parameter, and in practice we'll pass
7376 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007377 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 psa_status_t status = psa_generate_random(output, output_size);
7379 if (status == PSA_SUCCESS) {
7380 return 0;
7381 } else {
7382 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7383 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007384}
7385#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7386
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007387#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007388psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7389 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007390{
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 if (global_data.initialized) {
7392 return PSA_ERROR_NOT_PERMITTED;
7393 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7396 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7397 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7398 return PSA_ERROR_INVALID_ARGUMENT;
7399 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007402}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007403#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007404
Ronald Cron3772afe2021-02-08 16:10:05 +01007405/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007406 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007407 * \param type The key type
7408 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007409 *
7410 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007411 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007412 * \retval #PSA_ERROR_INVALID_ARGUMENT
7413 * The size in bits of the key is not valid.
7414 * \retval #PSA_ERROR_NOT_SUPPORTED
7415 * The type and/or the size in bits of the key or the combination of
7416 * the two is not supported.
7417 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007418static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007420{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (key_type_is_raw_bytes(type)) {
7424 status = psa_validate_unstructured_key_bit_size(type, bits);
7425 if (status != PSA_SUCCESS) {
7426 return status;
7427 }
7428 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007429#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7431 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7432 return PSA_ERROR_NOT_SUPPORTED;
7433 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007434 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007435 return PSA_ERROR_NOT_SUPPORTED;
7436 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007437
Ronald Cron01b2aba2020-10-05 09:42:02 +02007438 /* Accept only byte-aligned keys, for the same reasons as
7439 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 if (bits % 8 != 0) {
7441 return PSA_ERROR_NOT_SUPPORTED;
7442 }
7443 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007444#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007445
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007446#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007448 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 return PSA_SUCCESS;
7450 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007451#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007452
Valerio Settia55f0422023-07-10 15:34:41 +02007453#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007454 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007455 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007456 return PSA_ERROR_NOT_SUPPORTED;
7457 }
7458 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007459#endif /* defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007460 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007462 }
7463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007465}
7466
Ronald Cron55ed0592020-10-05 10:30:40 +02007467psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007468 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007470{
7471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007472 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007473
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 if ((attributes->domain_parameters == NULL) &&
7475 (attributes->domain_parameters_size != 0)) {
7476 return PSA_ERROR_INVALID_ARGUMENT;
7477 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if (key_type_is_raw_bytes(type)) {
7480 status = psa_generate_random(key_buffer, key_buffer_size);
7481 if (status != PSA_SUCCESS) {
7482 return status;
7483 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007484
David Brown12ca5032021-02-11 11:02:00 -07007485#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (type == PSA_KEY_TYPE_DES) {
7487 psa_des_set_key_parity(key_buffer, key_buffer_size);
7488 }
David Brown8107e312021-02-12 08:08:46 -07007489#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007491
Valerio Setti76df8c12023-07-11 14:11:28 +02007492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7494 return mbedtls_psa_rsa_generate_key(attributes,
7495 key_buffer,
7496 key_buffer_size,
7497 key_buffer_length);
7498 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007499#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007500
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007501#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7503 return mbedtls_psa_ecp_generate_key(attributes,
7504 key_buffer,
7505 key_buffer_size,
7506 key_buffer_length);
7507 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007508#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007509
Valerio Settia55f0422023-07-10 15:34:41 +02007510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007511 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7512 return mbedtls_psa_ffdh_generate_key(attributes,
7513 key_buffer,
7514 key_buffer_size,
7515 key_buffer_length);
7516 } else
Valerio Settia55f0422023-07-10 15:34:41 +02007517#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_GENERATE) */
Steven Cooreman29149862020-08-05 15:43:42 +02007518 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 (void) key_buffer_length;
7520 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007521 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007524}
Darryl Green0c6575a2018-11-07 16:05:30 +00007525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7527 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007528{
7529 psa_status_t status;
7530 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007531 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007532 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007533
Ronald Cron81709fc2020-11-14 12:10:32 +01007534 *key = MBEDTLS_SVC_KEY_ID_INIT;
7535
Gilles Peskine0f84d622019-09-12 19:03:13 +02007536 /* Reject any attempt to create a zero-length key so that we don't
7537 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 if (psa_get_key_bits(attributes) == 0) {
7539 return PSA_ERROR_INVALID_ARGUMENT;
7540 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007541
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007542 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7544 return PSA_ERROR_INVALID_ARGUMENT;
7545 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007546
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7548 &slot, &driver);
7549 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007550 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 }
Gilles Peskine11792082019-08-06 18:36:36 +02007552
Ronald Cron977c2472020-10-13 08:32:21 +02007553 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307554 * storage ( thus not in the case of generating a key in a secure element
7555 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7556 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 if (slot->key.data == NULL) {
7558 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7559 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007560 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 attributes->core.type, attributes->core.bits);
7562 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007565
7566 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 attributes->core.type,
7568 attributes->core.bits);
7569 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007570 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 attributes, &key_buffer_size);
7572 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 }
Ronald Cron977c2472020-10-13 08:32:21 +02007575 }
Ronald Cron977c2472020-10-13 08:32:21 +02007576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7578 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 }
Ronald Cron977c2472020-10-13 08:32:21 +02007581 }
7582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 status = psa_driver_wrapper_generate_key(attributes,
7584 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 if (status != PSA_SUCCESS) {
7587 psa_remove_key_data_from_memory(slot);
7588 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007589
Gilles Peskine11792082019-08-06 18:36:36 +02007590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if (status == PSA_SUCCESS) {
7592 status = psa_finish_key_creation(slot, driver, key);
7593 }
7594 if (status != PSA_SUCCESS) {
7595 psa_fail_key_creation(slot, driver);
7596 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007599}
7600
Gilles Peskinee59236f2018-01-27 23:32:46 +01007601/****************************************************************/
7602/* Module setup */
7603/****************************************************************/
7604
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007605#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007606psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 void (* entropy_init)(mbedtls_entropy_context *ctx),
7608 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007609{
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7611 return PSA_ERROR_BAD_STATE;
7612 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007613 global_data.rng.entropy_init = entropy_init;
7614 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007616}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007617#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007620{
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 psa_wipe_all_key_slots();
7622 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7623 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007624 }
7625 /* Wipe all remaining data, including configuration.
7626 * In particular, this sets all state indicator to the value
7627 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007629
7630 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007632}
7633
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007634#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7635/** Recover a transaction that was interrupted by a power failure.
7636 *
7637 * This function is called during initialization, before psa_crypto_init()
7638 * returns. If this function returns a failure status, the initialization
7639 * fails.
7640 */
7641static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007643{
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007645 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7646 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 /* TODO - fall through to the failure case until this
7648 * is implemented.
7649 * https://github.com/ARMmbed/mbed-crypto/issues/218
7650 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007651 default:
7652 /* We found an unsupported transaction in the storage.
7653 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007655 }
7656}
7657#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007660{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007661 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007662
Gilles Peskinec6b69072018-11-20 21:42:52 +01007663 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 if (global_data.initialized != 0) {
7665 return PSA_SUCCESS;
7666 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007667
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007668 /* Init drivers */
7669 status = psa_driver_wrapper_init();
7670 if (status != PSA_SUCCESS) {
7671 goto exit;
7672 }
7673 global_data.drivers_initialized = 1;
7674
Gilles Peskine30524eb2020-11-13 17:02:26 +01007675 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007677 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 status = mbedtls_psa_random_seed(&global_data.rng);
7679 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007680 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007682 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 status = psa_initialize_key_slots();
7685 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007686 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007688
Gilles Peskine4b734222019-07-24 15:56:31 +02007689#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 status = psa_crypto_load_transaction();
7691 if (status == PSA_SUCCESS) {
7692 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7693 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007694 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 }
7696 status = psa_crypto_stop_transaction();
7697 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007698 /* There's no transaction to complete. It's all good. */
7699 status = PSA_SUCCESS;
7700 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007701#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007702
Gilles Peskinec6b69072018-11-20 21:42:52 +01007703 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007704 global_data.initialized = 1;
7705
7706exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 if (status != PSA_SUCCESS) {
7708 mbedtls_psa_crypto_free();
7709 }
7710 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007711}
7712
Yanray Wangffc3c482023-07-11 12:01:04 +08007713#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007714psa_status_t psa_crypto_driver_pake_get_password_len(
7715 const psa_crypto_driver_pake_inputs_t *inputs,
7716 size_t *password_len)
7717{
7718 if (inputs->password_len == 0) {
7719 return PSA_ERROR_BAD_STATE;
7720 }
7721
7722 *password_len = inputs->password_len;
7723
7724 return PSA_SUCCESS;
7725}
7726
7727psa_status_t psa_crypto_driver_pake_get_password(
7728 const psa_crypto_driver_pake_inputs_t *inputs,
7729 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7730{
7731 if (inputs->password_len == 0) {
7732 return PSA_ERROR_BAD_STATE;
7733 }
7734
7735 if (buffer_size < inputs->password_len) {
7736 return PSA_ERROR_BUFFER_TOO_SMALL;
7737 }
7738
7739 memcpy(buffer, inputs->password, inputs->password_len);
7740 *buffer_length = inputs->password_len;
7741
7742 return PSA_SUCCESS;
7743}
7744
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007745psa_status_t psa_crypto_driver_pake_get_user_len(
7746 const psa_crypto_driver_pake_inputs_t *inputs,
7747 size_t *user_len)
7748{
7749 if (inputs->user_len == 0) {
7750 return PSA_ERROR_BAD_STATE;
7751 }
7752
7753 *user_len = inputs->user_len;
7754
7755 return PSA_SUCCESS;
7756}
7757
7758psa_status_t psa_crypto_driver_pake_get_user(
7759 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007760 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007761{
7762 if (inputs->user_len == 0) {
7763 return PSA_ERROR_BAD_STATE;
7764 }
7765
Przemek Stekielc0e62502023-03-14 11:49:36 +01007766 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007767 return PSA_ERROR_BUFFER_TOO_SMALL;
7768 }
7769
Przemek Stekielc0e62502023-03-14 11:49:36 +01007770 memcpy(user_id, inputs->user, inputs->user_len);
7771 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007772
7773 return PSA_SUCCESS;
7774}
7775
7776psa_status_t psa_crypto_driver_pake_get_peer_len(
7777 const psa_crypto_driver_pake_inputs_t *inputs,
7778 size_t *peer_len)
7779{
7780 if (inputs->peer_len == 0) {
7781 return PSA_ERROR_BAD_STATE;
7782 }
7783
7784 *peer_len = inputs->peer_len;
7785
7786 return PSA_SUCCESS;
7787}
7788
7789psa_status_t psa_crypto_driver_pake_get_peer(
7790 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007791 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007792{
7793 if (inputs->peer_len == 0) {
7794 return PSA_ERROR_BAD_STATE;
7795 }
7796
Przemek Stekielc0e62502023-03-14 11:49:36 +01007797 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007798 return PSA_ERROR_BUFFER_TOO_SMALL;
7799 }
7800
Przemek Stekielc0e62502023-03-14 11:49:36 +01007801 memcpy(peer_id, inputs->peer, inputs->peer_len);
7802 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007803
7804 return PSA_SUCCESS;
7805}
7806
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007807psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7808 const psa_crypto_driver_pake_inputs_t *inputs,
7809 psa_pake_cipher_suite_t *cipher_suite)
7810{
7811 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7812 return PSA_ERROR_BAD_STATE;
7813 }
7814
7815 *cipher_suite = inputs->cipher_suite;
7816
7817 return PSA_SUCCESS;
7818}
7819
Neil Armstronga7d08c32022-06-01 18:21:20 +02007820psa_status_t psa_pake_setup(
7821 psa_pake_operation_t *operation,
7822 const psa_pake_cipher_suite_t *cipher_suite)
7823{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007824 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007825
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007826 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007827 status = PSA_ERROR_BAD_STATE;
7828 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007829 }
7830
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007831 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007832 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007833 status = PSA_ERROR_INVALID_ARGUMENT;
7834 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007835 }
7836
Przemek Stekiel51eac532022-12-07 11:04:51 +01007837 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7838
Przemek Stekiele12ed362022-12-21 12:54:46 +01007839 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007840 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7841 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007842 operation->data.inputs.cipher_suite = *cipher_suite;
7843
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007844#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007846 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007847 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007848
David Horstmann16f01512023-06-14 17:21:07 +01007849 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007850 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007851 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007852#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007853 {
7854 status = PSA_ERROR_NOT_SUPPORTED;
7855 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007856 }
7857
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007858 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7859
Przemek Stekiel51eac532022-12-07 11:04:51 +01007860 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007861exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007862 psa_pake_abort(operation);
7863 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007864}
7865
7866psa_status_t psa_pake_set_password_key(
7867 psa_pake_operation_t *operation,
7868 mbedtls_svc_key_id_t password)
7869{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007871 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7872 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007873 psa_key_attributes_t attributes;
7874 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007875
Przemek Stekiel51eac532022-12-07 11:04:51 +01007876 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007877 status = PSA_ERROR_BAD_STATE;
7878 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007879 }
7880
Przemek Stekiel6c764412022-11-22 14:05:12 +01007881 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7882 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007883 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007884 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007885 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007886 }
7887
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007888 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007889 .core = slot->attr
7890 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007891
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007892 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007893
Przemek Stekiel51eac532022-12-07 11:04:51 +01007894 if (type != PSA_KEY_TYPE_PASSWORD &&
7895 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7896 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007897 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007898 }
7899
Przemek Stekiel51eac532022-12-07 11:04:51 +01007900 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7901 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007902 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007903 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007904 }
7905
7906 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7907 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007908 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007909exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007910 if (status != PSA_SUCCESS) {
7911 psa_pake_abort(operation);
7912 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007913 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007914 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007915}
7916
7917psa_status_t psa_pake_set_user(
7918 psa_pake_operation_t *operation,
7919 const uint8_t *user_id,
7920 size_t user_id_len)
7921{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007923
7924 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007925 status = PSA_ERROR_BAD_STATE;
7926 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007927 }
7928
Przemek Stekiel51eac532022-12-07 11:04:51 +01007929 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007930 status = PSA_ERROR_INVALID_ARGUMENT;
7931 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007932 }
7933
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007934 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007935 status = PSA_ERROR_BAD_STATE;
7936 goto exit;
7937 }
7938
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007939 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7940 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007941 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7942 goto exit;
7943 }
7944
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007945 memcpy(operation->data.inputs.user, user_id, user_id_len);
7946 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007947
7948 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007949exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007950 psa_pake_abort(operation);
7951 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007952}
7953
7954psa_status_t psa_pake_set_peer(
7955 psa_pake_operation_t *operation,
7956 const uint8_t *peer_id,
7957 size_t peer_id_len)
7958{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007960
7961 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007962 status = PSA_ERROR_BAD_STATE;
7963 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007964 }
7965
Przemek Stekiel51eac532022-12-07 11:04:51 +01007966 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007967 status = PSA_ERROR_INVALID_ARGUMENT;
7968 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007969 }
7970
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007971 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007972 status = PSA_ERROR_BAD_STATE;
7973 goto exit;
7974 }
7975
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007976 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7977 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007978 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7979 goto exit;
7980 }
7981
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007982 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7983 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007984
7985 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007986exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007987 psa_pake_abort(operation);
7988 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007989}
7990
7991psa_status_t psa_pake_set_role(
7992 psa_pake_operation_t *operation,
7993 psa_pake_role_t role)
7994{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007996
Przemek Stekiel51eac532022-12-07 11:04:51 +01007997 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007998 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007999 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008000 }
8001
Przemek Stekiel09104b82023-03-06 14:11:51 +01008002 switch (operation->alg) {
8003#if defined(PSA_WANT_ALG_JPAKE)
8004 case PSA_ALG_JPAKE:
8005 if (role == PSA_PAKE_ROLE_NONE) {
8006 return PSA_SUCCESS;
8007 }
8008 status = PSA_ERROR_INVALID_ARGUMENT;
8009 break;
8010#endif
8011 default:
8012 (void) role;
8013 status = PSA_ERROR_NOT_SUPPORTED;
8014 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008015 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008016exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008017 psa_pake_abort(operation);
8018 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008019}
8020
David Horstmanne7f21e62023-05-12 18:17:21 +01008021/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008022#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008023static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01008024 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01008025{
David Horstmann74a3d8c2023-06-14 18:28:19 +01008026 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01008027 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008028 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01008029
David Horstmann024e5c52023-06-14 15:48:21 +01008030 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008031 is_x1 = (stage->outputs < 1);
8032 } else {
8033 is_x1 = (stage->inputs < 1);
8034 }
8035
David Horstmann74a3d8c2023-06-14 18:28:19 +01008036 key_share_step = is_x1 ?
8037 PSA_JPAKE_X1_STEP_KEY_SHARE :
8038 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01008039 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01008040 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
8041 PSA_JPAKE_X2S_STEP_KEY_SHARE :
8042 PSA_JPAKE_X4S_STEP_KEY_SHARE;
8043 } else {
8044 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01008045 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01008046 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01008047}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008048#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01008049
Przemek Stekiel2797d372022-12-22 11:19:22 +01008050static psa_status_t psa_pake_complete_inputs(
8051 psa_pake_operation_t *operation)
8052{
Przemek Stekiel2797d372022-12-22 11:19:22 +01008053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01008054 /* Create copy of the inputs on stack as inputs share memory
8055 with the driver context which will be setup by the driver. */
8056 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008057
Przemek Stekielfde11282023-03-13 16:06:09 +01008058 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008059 return PSA_ERROR_BAD_STATE;
8060 }
8061
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008062 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008063 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8064 return PSA_ERROR_BAD_STATE;
8065 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008066 }
8067
Przemek Stekiel18620a32023-01-17 16:34:52 +01008068 /* Clear driver context */
8069 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8070
8071 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008072
8073 /* Driver is responsible for creating its own copy of the password. */
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008074 mbedtls_zeroize_and_free(inputs.password, inputs.password_len);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008075
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008076 /* User and peer are translated to role. */
8077 mbedtls_free(inputs.user);
8078 mbedtls_free(inputs.peer);
8079
Przemek Stekiel2797d372022-12-22 11:19:22 +01008080 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008081#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008082 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008083 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008084 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008085#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008086 {
8087 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008088 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008089 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008090 return status;
8091}
8092
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008093#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008094static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008095 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008097 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008098{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008099 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8100 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8101 step != PSA_PAKE_STEP_ZK_PROOF) {
8102 return PSA_ERROR_INVALID_ARGUMENT;
8103 }
8104
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008105 psa_jpake_computation_stage_t *computation_stage =
8106 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008107
David Horstmann5da95602023-06-08 15:37:12 +01008108 if (computation_stage->round != PSA_JPAKE_FIRST &&
8109 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008110 return PSA_ERROR_BAD_STATE;
8111 }
8112
David Horstmanne7f21e62023-05-12 18:17:21 +01008113 /* Check that the step we are given is the one we were expecting */
8114 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008115 return PSA_ERROR_BAD_STATE;
8116 }
8117
David Horstmanne7f21e62023-05-12 18:17:21 +01008118 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8119 computation_stage->inputs == 0 &&
8120 computation_stage->outputs == 0) {
8121 /* Start of the round, so function decides whether we are inputting
8122 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008123 computation_stage->io_mode = io_mode;
8124 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008125 /* Middle of the round so the mode we are in must match the function
8126 * called by the user */
8127 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008128 }
8129
8130 return PSA_SUCCESS;
8131}
8132
David Horstmanne7f21e62023-05-12 18:17:21 +01008133static psa_status_t psa_jpake_epilogue(
8134 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008135 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008136{
David Horstmanne7f21e62023-05-12 18:17:21 +01008137 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008138 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008139
David Horstmanne7f21e62023-05-12 18:17:21 +01008140 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8141 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008142 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008143 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008144 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008145 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008146 }
8147 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008148 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008149 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008150 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008151 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008152 }
8153 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008154 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8155 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008156 /* End of a round, move to the next round */
8157 stage->inputs = 0;
8158 stage->outputs = 0;
8159 stage->round++;
8160 }
8161 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008162 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008163 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008165 return PSA_SUCCESS;
8166}
David Horstmanne7f21e62023-05-12 18:17:21 +01008167
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008168#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008169
Neil Armstronga7d08c32022-06-01 18:21:20 +02008170psa_status_t psa_pake_output(
8171 psa_pake_operation_t *operation,
8172 psa_pake_step_t step,
8173 uint8_t *output,
8174 size_t output_size,
8175 size_t *output_length)
8176{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008178 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008179 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008180
8181 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008182 status = psa_pake_complete_inputs(operation);
8183 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008184 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008185 }
8186 }
8187
8188 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008189 status = PSA_ERROR_BAD_STATE;
8190 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008191 }
8192
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008193 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008194 status = PSA_ERROR_INVALID_ARGUMENT;
8195 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008196 }
8197
Przemek Stekiele12ed362022-12-21 12:54:46 +01008198 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008199#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008200 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008201 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008202 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008203 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008204 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008205 driver_step = convert_jpake_computation_stage_to_driver_step(
8206 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008207 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008208#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008209 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008210 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008211 status = PSA_ERROR_NOT_SUPPORTED;
8212 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008213 }
8214
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008215 status = psa_driver_wrapper_pake_output(operation, driver_step,
8216 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217
8218 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008219 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008220 }
8221
8222 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008223#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008225 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008227 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008228 }
8229 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008230#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008231 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008232 status = PSA_ERROR_NOT_SUPPORTED;
8233 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008234 }
8235
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008236 return PSA_SUCCESS;
8237exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008238 psa_pake_abort(operation);
8239 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008240}
8241
8242psa_status_t psa_pake_input(
8243 psa_pake_operation_t *operation,
8244 psa_pake_step_t step,
8245 const uint8_t *input,
8246 size_t input_length)
8247{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008249 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008250 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8251 operation->primitive,
8252 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008253
8254 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008255 status = psa_pake_complete_inputs(operation);
8256 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008257 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008258 }
8259 }
8260
8261 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008262 status = PSA_ERROR_BAD_STATE;
8263 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008264 }
8265
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008266 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008267 status = PSA_ERROR_INVALID_ARGUMENT;
8268 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008269 }
8270
Przemek Stekiele12ed362022-12-21 12:54:46 +01008271 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008272#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008273 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008274 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008275 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008276 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008277 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008278 driver_step = convert_jpake_computation_stage_to_driver_step(
8279 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008281#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008282 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008283 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008284 status = PSA_ERROR_NOT_SUPPORTED;
8285 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008286 }
8287
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008288 status = psa_driver_wrapper_pake_input(operation, driver_step,
8289 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008290
8291 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008292 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008293 }
8294
8295 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008296#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008297 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008298 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008299 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008300 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008301 }
8302 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008303#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008304 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008305 status = PSA_ERROR_NOT_SUPPORTED;
8306 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008307 }
8308
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008309 return PSA_SUCCESS;
8310exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008311 psa_pake_abort(operation);
8312 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008313}
8314
8315psa_status_t psa_pake_get_implicit_key(
8316 psa_pake_operation_t *operation,
8317 psa_key_derivation_operation_t *output)
8318{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008319 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008320 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008321 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008322 size_t shared_key_len = 0;
8323
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008324 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008325 status = PSA_ERROR_BAD_STATE;
8326 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008327 }
8328
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008329#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008330 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008331 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008332 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008333 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008334 status = PSA_ERROR_BAD_STATE;
8335 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008336 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008337 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008338#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008339 {
8340 status = PSA_ERROR_NOT_SUPPORTED;
8341 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008342 }
8343
Przemek Stekiel0c781802022-11-29 14:53:13 +01008344 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8345 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008346 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008347 &shared_key_len);
8348
8349 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008350 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008351 }
8352
8353 status = psa_key_derivation_input_bytes(output,
8354 PSA_KEY_DERIVATION_INPUT_SECRET,
8355 shared_key,
8356 shared_key_len);
8357
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008358 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008359exit:
8360 abort_status = psa_pake_abort(operation);
8361 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008362}
8363
8364psa_status_t psa_pake_abort(
8365 psa_pake_operation_t *operation)
8366{
Przemek Stekield69dca92023-01-31 19:59:20 +01008367 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008368
Przemek Stekield69dca92023-01-31 19:59:20 +01008369 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008370 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008371 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008372
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008373 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8374 if (operation->data.inputs.password != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008375 mbedtls_zeroize_and_free(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008376 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008377 }
8378 if (operation->data.inputs.user != NULL) {
8379 mbedtls_free(operation->data.inputs.user);
8380 }
8381 if (operation->data.inputs.peer != NULL) {
8382 mbedtls_free(operation->data.inputs.peer);
8383 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008384 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008385 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008386
Przemek Stekield69dca92023-01-31 19:59:20 +01008387 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008388}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008389#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008390
Gilles Peskinee59236f2018-01-27 23:32:46 +01008391#endif /* MBEDTLS_PSA_CRYPTO_C */