blob: 1eb69605489497bb230aee591e0b821df53c4fe4 [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"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020068#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010069#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/error.h"
72#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000075#include "md_wrap.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 {
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +0100110 unsigned drivers_initialized : 1;
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 Setti8bb57632023-05-26 13:48:07 +0200130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel53410502023-04-28 13:18:43 +0200131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200132 defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
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 Setti8bb57632023-05-26 13:48:07 +0200142#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY ||
Przemek Stekiel53410502023-04-28 13:18:43 +0200143 MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY ||
Valerio Setti8bb57632023-05-26 13:48:07 +0200144 MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY */
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
156 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
157 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine9a944802018-06-21 09:35:35 +0200165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine9a944802018-06-21 09:35:35 +0200167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskine9a944802018-06-21 09:35:35 +0200169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100175
176 case MBEDTLS_ERR_CCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
Gilles Peskine26869f22019-05-06 15:25:00 +0200181 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine26869f22019-05-06 15:25:00 +0200183
184 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return PSA_ERROR_BAD_STATE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine26869f22019-05-06 15:25:00 +0200188
Gilles Peskinea5905292018-02-07 20:59:33 +0100189 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100195 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100199 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204#if !(defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
205 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE))
Gilles Peskinebee96c82020-11-23 21:00:09 +0100206 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
207 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
211 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100215#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100216
217 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100219
Gilles Peskinee59236f2018-01-27 23:32:46 +0100220 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
221 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
222 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100224
225 case MBEDTLS_ERR_GCM_AUTH_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return PSA_ERROR_INVALID_SIGNATURE;
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200227 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine82e57d12020-11-13 21:31:17 +0100243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_ERROR_STORAGE_FAILURE;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return PSA_ERROR_NOT_PERMITTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200294 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100296
Gilles Peskineff2d2002019-05-06 15:26:23 +0200297 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200299 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301
Gilles Peskinea5905292018-02-07 20:59:33 +0100302 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100304 case MBEDTLS_ERR_RSA_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return PSA_ERROR_INVALID_PADDING;
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_ERROR_HARDWARE_FAILURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
311 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return PSA_ERROR_BUFFER_TOO_SMALL;
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return PSA_ERROR_INVALID_ARGUMENT;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return PSA_ERROR_BUFFER_TOO_SMALL;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return PSA_ERROR_NOT_SUPPORTED;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return PSA_ERROR_INVALID_SIGNATURE;
itayzafrir7b30f8b2018-05-09 16:07:36 +0300330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return PSA_ERROR_INSUFFICIENT_ENTROPY;
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Paul Elliott588f8ed2022-12-02 18:10:26 +0000335 case MBEDTLS_ERR_ECP_IN_PROGRESS:
336 return PSA_OPERATION_INCOMPLETE;
337
Janos Follatha13b9052019-11-22 12:48:59 +0000338 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_CORRUPTION_DETECTED;
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 return PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100343 }
344}
345
Paul Elliottdc42ca82023-02-24 18:11:59 +0000346/**
347 * \brief For output buffers which contain "tags"
348 * (outputs that may be checked for validity like
349 * hashes, MACs and signatures), fill the unused
350 * part of the output buffer (the whole buffer on
351 * error, the trailing part on success) with
352 * something that isn't a valid tag (barring an
353 * attack on the tag and deliberately-crafted
354 * input), in case the caller doesn't check the
355 * return status properly.
356 *
357 * \param output_buffer Pointer to buffer to wipe. May not be NULL
358 * unless \p output_buffer_size is zero.
359 * \param status Status of function called to generate
360 * output_buffer originally
361 * \param output_buffer_size Size of output buffer. If zero, \p output_buffer
362 * could be NULL.
363 * \param output_buffer_length Length of data written to output_buffer, must be
364 * less than \p output_buffer_size
365 */
366static void psa_wipe_tag_output_buffer(uint8_t *output_buffer, psa_status_t status,
Paul Elliott7118d172023-02-26 16:57:05 +0000367 size_t output_buffer_size, size_t output_buffer_length)
368{
Paul Elliottdc42ca82023-02-24 18:11:59 +0000369 size_t offset = 0;
370
371 if (output_buffer_size == 0) {
372 /* If output_buffer_size is 0 then we have nothing to do. We must not
373 call memset because output_buffer may be NULL in this case */
374 return;
375 }
376
377 if (status == PSA_SUCCESS) {
378 offset = output_buffer_length;
379 }
380
381 memset(output_buffer + offset, '!', output_buffer_size - offset);
382}
383
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200385
386
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100387/****************************************************************/
388/* Key management */
389/****************************************************************/
390
Valerio Settia9aab1a2023-06-19 13:39:54 +0200391#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Settibc2b1d32023-06-19 12:15:13 +0200392psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
393 size_t *bits)
394{
395 switch (grpid) {
396 case MBEDTLS_ECP_DP_SECP192R1:
397 *bits = 192;
398 return PSA_ECC_FAMILY_SECP_R1;
399 case MBEDTLS_ECP_DP_SECP224R1:
400 *bits = 224;
401 return PSA_ECC_FAMILY_SECP_R1;
402 case MBEDTLS_ECP_DP_SECP256R1:
403 *bits = 256;
404 return PSA_ECC_FAMILY_SECP_R1;
405 case MBEDTLS_ECP_DP_SECP384R1:
406 *bits = 384;
407 return PSA_ECC_FAMILY_SECP_R1;
408 case MBEDTLS_ECP_DP_SECP521R1:
409 *bits = 521;
410 return PSA_ECC_FAMILY_SECP_R1;
411 case MBEDTLS_ECP_DP_BP256R1:
412 *bits = 256;
413 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
414 case MBEDTLS_ECP_DP_BP384R1:
415 *bits = 384;
416 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
417 case MBEDTLS_ECP_DP_BP512R1:
418 *bits = 512;
419 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
420 case MBEDTLS_ECP_DP_CURVE25519:
421 *bits = 255;
422 return PSA_ECC_FAMILY_MONTGOMERY;
423 case MBEDTLS_ECP_DP_SECP192K1:
424 *bits = 192;
425 return PSA_ECC_FAMILY_SECP_K1;
426 case MBEDTLS_ECP_DP_SECP224K1:
427 *bits = 224;
428 return PSA_ECC_FAMILY_SECP_K1;
429 case MBEDTLS_ECP_DP_SECP256K1:
430 *bits = 256;
431 return PSA_ECC_FAMILY_SECP_K1;
432 case MBEDTLS_ECP_DP_CURVE448:
433 *bits = 448;
434 return PSA_ECC_FAMILY_MONTGOMERY;
435 default:
436 *bits = 0;
437 return 0;
438 }
439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
442 size_t bits,
443 int bits_is_sloppy)
Gilles Peskine12313cd2018-06-20 00:20:32 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100446 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100448#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return MBEDTLS_ECP_DP_SECP192R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100451#endif
452#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return MBEDTLS_ECP_DP_SECP224R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100455#endif
456#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return MBEDTLS_ECP_DP_SECP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ECP_DP_SECP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ECP_DP_SECP521R1;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100467 case 528:
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (bits_is_sloppy) {
469 return MBEDTLS_ECP_DP_SECP521R1;
470 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Paul Elliott8ff510a2020-06-02 17:19:28 +0100476 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100478#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100479 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return MBEDTLS_ECP_DP_BP256R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return MBEDTLS_ECP_DP_BP384R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
486#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100487 case 512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return MBEDTLS_ECP_DP_BP512R1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100489#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100490 }
491 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100492
Paul Elliott8ff510a2020-06-02 17:19:28 +0100493 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 case 255:
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ECP_DP_CURVE25519;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100498 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (bits_is_sloppy) {
500 return MBEDTLS_ECP_DP_CURVE25519;
501 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100502 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
504#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100505 case 448:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ECP_DP_CURVE448;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100507#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100508 }
509 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100510
Paul Elliott8ff510a2020-06-02 17:19:28 +0100511 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (bits) {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100513#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100514 case 192:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return MBEDTLS_ECP_DP_SECP192K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100516#endif
517#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100518 case 224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_ECP_DP_SECP224K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100520#endif
521#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100522 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_ECP_DP_SECP256K1;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100524#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100525 }
526 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200527 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100528
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100529 (void) bits_is_sloppy;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_ECP_DP_NONE;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200531}
Valerio Settia9aab1a2023-06-19 13:39:54 +0200532#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
535 size_t bits)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200536{
537 /* Check that the bit size is acceptable for the key type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 switch (type) {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 case PSA_KEY_TYPE_DERIVE:
Neil Armstrong4b5710f2022-05-25 11:30:27 +0200542 case PSA_KEY_TYPE_PASSWORD:
543 case PSA_KEY_TYPE_PASSWORD_HASH:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200544 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100545#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200546 case PSA_KEY_TYPE_AES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (bits != 128 && bits != 192 && bits != 256) {
548 return PSA_ERROR_INVALID_ARGUMENT;
549 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200550 break;
551#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200552#if defined(PSA_WANT_KEY_TYPE_ARIA)
553 case PSA_KEY_TYPE_ARIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (bits != 128 && bits != 192 && bits != 256) {
555 return PSA_ERROR_INVALID_ARGUMENT;
556 }
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200557 break;
558#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100559#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 case PSA_KEY_TYPE_CAMELLIA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (bits != 128 && bits != 192 && bits != 256) {
562 return PSA_ERROR_INVALID_ARGUMENT;
563 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200564 break;
565#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100566#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200567 case PSA_KEY_TYPE_DES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (bits != 64 && bits != 128 && bits != 192) {
569 return PSA_ERROR_INVALID_ARGUMENT;
570 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200571 break;
572#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100573#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200574 case PSA_KEY_TYPE_CHACHA20:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (bits != 256) {
576 return PSA_ERROR_INVALID_ARGUMENT;
577 }
Gilles Peskine26869f22019-05-06 15:25:00 +0200578 break;
579#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200580 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200582 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (bits % 8 != 0) {
584 return PSA_ERROR_INVALID_ARGUMENT;
585 }
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return PSA_SUCCESS;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200588}
589
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100590/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100591 *
Steven Cooremancd640932021-03-08 12:00:27 +0100592 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
593 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100594 *
595 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100596 * \param[in] key_type The key type of the key to be used with the
597 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100598 *
599 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100600 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100601 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100602 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100603 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100604MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100605 psa_algorithm_t algorithm,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_key_type_t key_type)
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (PSA_ALG_IS_HMAC(algorithm)) {
609 if (key_type == PSA_KEY_TYPE_HMAC) {
610 return PSA_SUCCESS;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100611 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100612 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (PSA_ALG_IS_BLOCK_CIPHER_MAC(algorithm)) {
615 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
616 * key. */
617 if ((key_type & PSA_KEY_TYPE_CATEGORY_MASK) ==
618 PSA_KEY_TYPE_CATEGORY_SYMMETRIC) {
619 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
620 * the block length (larger than 1) for block ciphers. */
621 if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1) {
622 return PSA_SUCCESS;
623 }
624 }
625 }
626
627 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
631 size_t buffer_length)
Steven Cooreman75b74362020-07-28 14:30:13 +0200632{
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (slot->key.data != NULL) {
634 return PSA_ERROR_ALREADY_EXISTS;
635 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 slot->key.data = mbedtls_calloc(1, buffer_length);
638 if (slot->key.data == NULL) {
639 return PSA_ERROR_INSUFFICIENT_MEMORY;
640 }
Steven Cooreman75b74362020-07-28 14:30:13 +0200641
Ronald Cronea0f8a62020-11-25 17:52:23 +0100642 slot->key.bytes = buffer_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return PSA_SUCCESS;
Steven Cooreman75b74362020-07-28 14:30:13 +0200644}
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
647 const uint8_t *data,
648 size_t data_length)
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 psa_status_t status = psa_allocate_buffer_to_slot(slot,
651 data_length);
652 if (status != PSA_SUCCESS) {
653 return status;
654 }
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(slot->key.data, data, data_length);
657 return PSA_SUCCESS;
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200658}
659
Ronald Crona0fe59f2020-11-29 11:14:53 +0100660psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100661 const psa_key_attributes_t *attributes,
662 const uint8_t *data, size_t data_length,
663 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 size_t *key_buffer_length, size_t *bits)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
667 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200669 /* zero-length keys are never supported. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (data_length == 0) {
671 return PSA_ERROR_NOT_SUPPORTED;
672 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (key_type_is_raw_bytes(type)) {
675 *bits = PSA_BYTES_TO_BITS(data_length);
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 status = psa_validate_unstructured_key_bit_size(attributes->core.type,
678 *bits);
679 if (status != PSA_SUCCESS) {
680 return status;
681 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200682
Ronald Crondd04d422020-11-28 15:14:42 +0100683 /* Copy the key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memcpy(key_buffer, data, data_length);
Ronald Crondd04d422020-11-28 15:14:42 +0100685 *key_buffer_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 (void) key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return PSA_SUCCESS;
689 } else if (PSA_KEY_TYPE_IS_ASYMMETRIC(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +0200690#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200691 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100692 if (PSA_KEY_TYPE_IS_DH(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +0200693 if (psa_is_dh_key_size_valid(PSA_BYTES_TO_BITS(data_length)) == 0) {
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100694 return PSA_ERROR_INVALID_ARGUMENT;
695 }
Przemek Stekiel33c91eb2023-05-30 15:16:35 +0200696 return mbedtls_psa_ffdh_import_key(attributes,
697 data, data_length,
698 key_buffer, key_buffer_size,
699 key_buffer_length,
700 bits);
Przemek Stekiel472b3f32022-12-01 14:38:49 +0100701 }
Valerio Setti8bb57632023-05-26 13:48:07 +0200702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +0200703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Valerio Setti27c501a2023-06-27 16:58:52 +0200704#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
706 if (PSA_KEY_TYPE_IS_ECC(type)) {
707 return mbedtls_psa_ecp_import_key(attributes,
708 data, data_length,
709 key_buffer, key_buffer_size,
710 key_buffer_length,
711 bits);
Steven Cooreman40120f62020-10-29 11:42:22 +0100712 }
Valerio Setti27c501a2023-06-27 16:58:52 +0200713#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_IMPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -0800714 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Valerio Settife478902023-07-25 12:27:19 +0200715#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
716 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
718 if (PSA_KEY_TYPE_IS_RSA(type)) {
719 return mbedtls_psa_rsa_import_key(attributes,
720 data, data_length,
721 key_buffer, key_buffer_size,
722 key_buffer_length,
723 bits);
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200724 }
Valerio Settife478902023-07-25 12:27:19 +0200725#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
726 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -0800727 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100728 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 return PSA_ERROR_NOT_SUPPORTED;
Darryl Green06fd18d2018-07-16 11:21:11 +0100731}
732
Gilles Peskinef603c712019-01-19 13:40:11 +0100733/** Calculate the intersection of two algorithm usage policies.
734 *
735 * Return 0 (which allows no operation) on incompatibility.
736 */
737static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100738 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100739 psa_algorithm_t alg1,
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 psa_algorithm_t alg2)
Gilles Peskinef603c712019-01-19 13:40:11 +0100741{
Gilles Peskine549ea862019-05-22 11:45:59 +0200742 /* Common case: both sides actually specify the same policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (alg1 == alg2) {
744 return alg1;
745 }
Steven Cooreman03488022021-02-18 12:07:20 +0100746 /* If the policies are from the same hash-and-sign family, check
747 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (PSA_ALG_IS_SIGN_HASH(alg1) &&
749 PSA_ALG_IS_SIGN_HASH(alg2) &&
750 (alg1 & ~PSA_ALG_HASH_MASK) == (alg2 & ~PSA_ALG_HASH_MASK)) {
751 if (PSA_ALG_SIGN_GET_HASH(alg1) == PSA_ALG_ANY_HASH) {
752 return alg2;
753 }
754 if (PSA_ALG_SIGN_GET_HASH(alg2) == PSA_ALG_ANY_HASH) {
755 return alg1;
756 }
Steven Cooreman03488022021-02-18 12:07:20 +0100757 }
758 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100759 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100760 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (PSA_ALG_IS_AEAD(alg1) && PSA_ALG_IS_AEAD(alg2) &&
762 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg1, 0) ==
763 PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg2, 0))) {
764 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg1);
765 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100766 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100767
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100768 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
770 ((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
771 return PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
772 alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100773 }
774 /* If only one is a wildcard, return specific algorithm if compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (((alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
776 (alg1_len <= alg2_len)) {
777 return alg2;
Steven Cooreman03488022021-02-18 12:07:20 +0100778 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (((alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
780 (alg2_len <= alg1_len)) {
781 return alg1;
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
783 }
784 /* If the policies are from the same MAC family, check whether one
785 * of them is a minimum-MAC-length policy. Calculate the most
786 * restrictive tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (PSA_ALG_IS_MAC(alg1) && PSA_ALG_IS_MAC(alg2) &&
788 (PSA_ALG_FULL_LENGTH_MAC(alg1) ==
789 PSA_ALG_FULL_LENGTH_MAC(alg2))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* Validate the combination of key type and algorithm. Since the base
791 * algorithm of alg1 and alg2 are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (PSA_SUCCESS != psa_mac_key_can_do(alg1, key_type)) {
793 return 0;
794 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100795
Steven Cooreman31a876d2021-03-03 20:47:40 +0100796 /* Get the (exact or at-least) output lengths for both sides of the
797 * requested intersection. None of the currently supported algorithms
798 * have an output length dependent on the actual key size, so setting it
799 * to a bogus value of 0 is currently OK.
800 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100801 * Note that for at-least-this-length wildcard algorithms, the output
802 * length is set to the shortest allowed length, which allows us to
803 * calculate the most restrictive tag length for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 size_t alg1_len = PSA_MAC_LENGTH(key_type, 0, alg1);
805 size_t alg2_len = PSA_MAC_LENGTH(key_type, 0, alg2);
Steven Cooremancd640932021-03-08 12:00:27 +0100806 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100807
Steven Cooremana1d83222021-02-25 10:20:29 +0100808 /* If both are wildcards, return most restrictive wildcard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) &&
810 ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
811 return PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg1, restricted_len);
Steven Cooreman03488022021-02-18 12:07:20 +0100812 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100813
814 /* If only one is an at-least-this-length policy, the intersection would
815 * be the other (fixed-length) policy as long as said fixed length is
816 * equal to or larger than the shortest allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if ((alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
818 return (alg1_len <= alg2_len) ? alg2 : 0;
Steven Cooreman03488022021-02-18 12:07:20 +0100819 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if ((alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
821 return (alg2_len <= alg1_len) ? alg1 : 0;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100822 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100823
Steven Cooremancd640932021-03-08 12:00:27 +0100824 /* If none of them are wildcards, check whether they define the same tag
825 * length. This is still possible here when one is default-length and
826 * the other specific-length. Ensure to always return the
827 * specific-length version for the intersection. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (alg1_len == alg2_len) {
829 return PSA_ALG_TRUNCATED_MAC(alg1, alg1_len);
830 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100831 }
832 /* If the policies are incompatible, allow nothing. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
Gilles Peskinef603c712019-01-19 13:40:11 +0100834}
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static int psa_key_algorithm_permits(psa_key_type_t key_type,
837 psa_algorithm_t policy_alg,
838 psa_algorithm_t requested_alg)
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839{
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 /* Common case: the policy only allows requested_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (requested_alg == policy_alg) {
842 return 1;
843 }
Steven Cooreman03488022021-02-18 12:07:20 +0100844 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
845 * and requested_alg is the same hash-and-sign family with any hash,
846 * then requested_alg is compliant with policy_alg. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (PSA_ALG_IS_SIGN_HASH(requested_alg) &&
848 PSA_ALG_SIGN_GET_HASH(policy_alg) == PSA_ALG_ANY_HASH) {
849 return (policy_alg & ~PSA_ALG_HASH_MASK) ==
850 (requested_alg & ~PSA_ALG_HASH_MASK);
Steven Cooreman03488022021-02-18 12:07:20 +0100851 }
852 /* If policy_alg is a wildcard AEAD algorithm of the same base as
853 * the requested algorithm, check the requested tag length to be
854 * equal-length or longer than the wildcard-specified length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (PSA_ALG_IS_AEAD(policy_alg) &&
856 PSA_ALG_IS_AEAD(requested_alg) &&
857 (PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, 0) ==
858 PSA_ALG_AEAD_WITH_SHORTENED_TAG(requested_alg, 0)) &&
859 ((policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) != 0)) {
860 return PSA_ALG_AEAD_GET_TAG_LENGTH(policy_alg) <=
861 PSA_ALG_AEAD_GET_TAG_LENGTH(requested_alg);
Steven Cooreman03488022021-02-18 12:07:20 +0100862 }
Steven Cooremancd640932021-03-08 12:00:27 +0100863 /* If policy_alg is a MAC algorithm of the same base as the requested
864 * algorithm, check whether their MAC lengths are compatible. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (PSA_ALG_IS_MAC(policy_alg) &&
866 PSA_ALG_IS_MAC(requested_alg) &&
867 (PSA_ALG_FULL_LENGTH_MAC(policy_alg) ==
868 PSA_ALG_FULL_LENGTH_MAC(requested_alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100869 /* Validate the combination of key type and algorithm. Since the policy
870 * and requested algorithms are the same, we only need this once. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (PSA_SUCCESS != psa_mac_key_can_do(policy_alg, key_type)) {
872 return 0;
873 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100874
Steven Cooreman31a876d2021-03-03 20:47:40 +0100875 /* Get both the requested output length for the algorithm which is to be
876 * verified, and the default output length for the base algorithm.
877 * Note that none of the currently supported algorithms have an output
878 * length dependent on actual key size, so setting it to a bogus value
879 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100880 size_t requested_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 key_type, 0, requested_alg);
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100882 size_t default_output_length = PSA_MAC_LENGTH(
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 key_type, 0,
884 PSA_ALG_FULL_LENGTH_MAC(requested_alg));
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100885
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100886 /* If the policy is default-length, only allow an algorithm with
887 * a declared exact-length matching the default. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (PSA_MAC_TRUNCATED_LENGTH(policy_alg) == 0) {
889 return requested_output_length == default_output_length;
890 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100891
892 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100893 * length exactly matches the default length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (PSA_MAC_TRUNCATED_LENGTH(requested_alg) == 0 &&
895 PSA_MAC_TRUNCATED_LENGTH(policy_alg) == default_output_length) {
896 return 1;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100897 }
898
Steven Cooremancd640932021-03-08 12:00:27 +0100899 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
900 * check for the requested MAC length to be equal to or longer than the
901 * minimum allowed length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) != 0) {
903 return PSA_MAC_TRUNCATED_LENGTH(policy_alg) <=
904 requested_output_length;
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100905 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200906 }
Steven Cooremance48e852020-10-05 16:02:45 +0200907 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200908 * a key derivation with that key agreement should also be allowed. This
909 * behaviour is expected to be defined in a future specification version. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(policy_alg) &&
911 PSA_ALG_IS_KEY_AGREEMENT(requested_alg)) {
912 return PSA_ALG_KEY_AGREEMENT_GET_BASE(requested_alg) ==
913 policy_alg;
Steven Cooremance48e852020-10-05 16:02:45 +0200914 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100915 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 return 0;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200917}
918
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100919/** Test whether a policy permits an algorithm.
920 *
921 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100922 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100923 * \note This function requires providing the key type for which the policy is
924 * being validated, since some algorithm policy definitions (e.g. MAC)
925 * have different properties depending on what kind of cipher it is
926 * combined with.
927 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928 * \retval PSA_SUCCESS When \p alg is a specific algorithm
929 * allowed by the \p policy.
930 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
931 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
932 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static psa_status_t psa_key_policy_permits(const psa_key_policy_t *policy,
935 psa_key_type_t key_type,
936 psa_algorithm_t alg)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100937{
Steven Cooremand788fab2021-02-25 11:29:17 +0100938 /* '0' is not a valid algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (alg == 0) {
940 return PSA_ERROR_INVALID_ARGUMENT;
941 }
Steven Cooremand788fab2021-02-25 11:29:17 +0100942
Steven Cooreman7e39f052021-02-23 14:18:32 +0100943 /* A requested algorithm cannot be a wildcard. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (PSA_ALG_IS_WILDCARD(alg)) {
945 return PSA_ERROR_INVALID_ARGUMENT;
946 }
Steven Cooreman7e39f052021-02-23 14:18:32 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (psa_key_algorithm_permits(key_type, policy->alg, alg) ||
949 psa_key_algorithm_permits(key_type, policy->alg2, alg)) {
950 return PSA_SUCCESS;
951 } else {
952 return PSA_ERROR_NOT_PERMITTED;
953 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100954}
955
Gilles Peskinef603c712019-01-19 13:40:11 +0100956/** Restrict a key policy based on a constraint.
957 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100958 * \note This function requires providing the key type for which the policy is
959 * being restricted, since some algorithm policy definitions (e.g. MAC)
960 * have different properties depending on what kind of cipher it is
961 * combined with.
962 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100963 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100964 * \param[in,out] policy The policy to restrict.
965 * \param[in] constraint The policy constraint to apply.
966 *
967 * \retval #PSA_SUCCESS
968 * \c *policy contains the intersection of the original value of
969 * \c *policy and \c *constraint.
970 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100971 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100972 * \c *policy is unchanged.
973 */
974static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100975 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100976 psa_key_policy_t *policy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 const psa_key_policy_t *constraint)
Gilles Peskinef603c712019-01-19 13:40:11 +0100978{
979 psa_algorithm_t intersection_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 psa_key_policy_algorithm_intersection(key_type, policy->alg,
981 constraint->alg);
Gilles Peskined6f371b2019-05-10 19:33:38 +0200982 psa_algorithm_t intersection_alg2 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 psa_key_policy_algorithm_intersection(key_type, policy->alg2,
984 constraint->alg2);
985 if (intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0) {
986 return PSA_ERROR_INVALID_ARGUMENT;
987 }
988 if (intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0) {
989 return PSA_ERROR_INVALID_ARGUMENT;
990 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100991 policy->usage &= constraint->usage;
992 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200993 policy->alg2 = intersection_alg2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return PSA_SUCCESS;
Gilles Peskinef603c712019-01-19 13:40:11 +0100995}
996
Przemek Stekiel061f6942022-11-30 10:51:35 +0100997/** Get the description of a key given its identifier and policy constraints
998 * and lock it.
999 *
1000 * The key must have allow all the usage flags set in \p usage. If \p alg is
1001 * nonzero, the key must allow operations with this algorithm. If \p alg is
1002 * zero, the algorithm is not checked.
1003 *
1004 * In case of a persistent key, the function loads the description of the key
1005 * into a key slot if not already done.
1006 *
1007 * On success, the returned key slot is locked. It is the responsibility of
1008 * the caller to unlock the key slot when it does not access it anymore.
1009 */
1010static psa_status_t psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01001011 mbedtls_svc_key_id_t key,
1012 psa_key_slot_t **p_slot,
1013 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 psa_algorithm_t alg)
Darryl Green06fd18d2018-07-16 11:21:11 +01001015{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01001017 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 status = psa_get_and_lock_key_slot(key, p_slot);
1020 if (status != PSA_SUCCESS) {
1021 return status;
1022 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001023 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001024
1025 /* Enforce that usage policy for the key slot contains all the flags
1026 * required by the usage parameter. There is one exception: public
1027 * keys can always be exported, so we treat public key objects as
1028 * if they had the export flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
Darryl Green06fd18d2018-07-16 11:21:11 +01001030 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if ((slot->attr.policy.usage & usage) != usage) {
Steven Cooreman328f11c2021-03-02 11:44:51 +01001034 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001035 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001036 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001037
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001038 /* Enforce that the usage policy permits the requested algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (alg != 0) {
1040 status = psa_key_policy_permits(&slot->attr.policy,
1041 slot->attr.type,
1042 alg);
1043 if (status != PSA_SUCCESS) {
Steven Cooreman7e39f052021-02-23 14:18:32 +01001044 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 }
Steven Cooreman7e39f052021-02-23 14:18:32 +01001046 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_SUCCESS;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001049
1050error:
1051 *p_slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return status;
Darryl Green06fd18d2018-07-16 11:21:11 +01001055}
Darryl Green940d72c2018-07-13 13:18:51 +01001056
Ronald Cron5c522922020-11-14 16:35:34 +01001057/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001058 *
1059 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +02001060 * available, as opposed to a key in a secure element and/or to be used
1061 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001062 *
Ronald Cronddae0f52021-08-24 15:39:44 +02001063 * This is a temporary function that may be used instead of
1064 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1065 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001066 *
Ronald Cron5c522922020-11-14 16:35:34 +01001067 * On success, the returned key slot is locked. It is the responsibility of the
1068 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001069 */
Ronald Cron5c522922020-11-14 16:35:34 +01001070static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1071 mbedtls_svc_key_id_t key,
1072 psa_key_slot_t **p_slot,
1073 psa_key_usage_t usage,
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_algorithm_t alg)
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075{
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 psa_status_t status = psa_get_and_lock_key_slot_with_policy(key, p_slot,
1077 usage, alg);
1078 if (status != PSA_SUCCESS) {
1079 return status;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001080 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (psa_key_lifetime_is_external((*p_slot)->attr.lifetime)) {
1083 psa_unlock_key_slot(*p_slot);
1084 *p_slot = NULL;
1085 return PSA_ERROR_NOT_SUPPORTED;
1086 }
1087
1088 return PSA_SUCCESS;
Gilles Peskine28f8f302019-07-24 13:30:31 +02001089}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
Darryl Green40225ba2018-11-15 14:48:15 +00001092{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001093 /* Data pointer will always be either a valid pointer or NULL in an
1094 * initialized slot, so we can just free it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (slot->key.data != NULL) {
1096 mbedtls_platform_zeroize(slot->key.data, slot->key.bytes);
1097 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 mbedtls_free(slot->key.data);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001100 slot->key.data = NULL;
1101 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return PSA_SUCCESS;
Darryl Green40225ba2018-11-15 14:48:15 +00001104}
1105
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001106/** Completely wipe a slot in memory, including its policy.
1107 * Persistent storage is not affected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001109{
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 psa_status_t status = psa_remove_key_data_from_memory(slot);
Ronald Cronddd3d052020-10-30 14:07:07 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 /*
1113 * As the return error code may not be handled in case of multiple errors,
1114 * do our best to report an unexpected lock counter. Assert with
1115 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
1116 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
1117 * function is called as part of the execution of a test suite, the
1118 * execution of the test suite is stopped in error if the assertion fails.
1119 */
1120 if (slot->lock_count != 1) {
1121 MBEDTLS_TEST_HOOK_TEST_ASSERT(slot->lock_count == 1);
Ronald Cronddd3d052020-10-30 14:07:07 +01001122 status = PSA_ERROR_CORRUPTION_DETECTED;
1123 }
1124
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001125 /* Multipart operations may still be using the key. This is safe
1126 * because all multipart operation objects are independent from
1127 * the key slot: if they need to access the key after the setup
1128 * phase, they have a copy of the key. Note that this means that
1129 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001130 /* At this point, key material and other type-specific content has
1131 * been wiped. Clear remaining metadata. We can call memset and not
1132 * zeroize because the metadata is not particularly sensitive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 memset(slot, 0, sizeof(*slot));
1134 return status;
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001135}
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001139 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001140 psa_status_t status; /* status of the last operation */
1141 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1143 psa_se_drv_table_entry_t *driver;
1144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (mbedtls_svc_key_id_is_null(key)) {
1147 return PSA_SUCCESS;
1148 }
Gilles Peskine1841cf42019-10-08 15:48:25 +02001149
Ronald Cronf2911112020-10-29 17:51:10 +01001150 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001151 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001152 * key, this will load the key description from persistent memory if not
1153 * done yet. We cannot avoid this loading as without it we don't know if
1154 * the key is operated by an SE or not and this information is needed by
1155 * the current implementation.
1156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 status = psa_get_and_lock_key_slot(key, &slot);
1158 if (status != PSA_SUCCESS) {
1159 return status;
1160 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001161
Ronald Cronf2911112020-10-29 17:51:10 +01001162 /*
1163 * If the key slot containing the key description is under access by the
1164 * library (apart from the present access), the key cannot be destroyed
1165 * yet. For the time being, just return in error. Eventually (to be
1166 * implemented), the key should be destroyed when all accesses have
1167 * stopped.
1168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (slot->lock_count > 1) {
1170 psa_unlock_key_slot(slot);
1171 return PSA_ERROR_GENERIC_ERROR;
Ronald Cronf2911112020-10-29 17:51:10 +01001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (PSA_KEY_LIFETIME_IS_READ_ONLY(slot->attr.lifetime)) {
Gilles Peskine6687cd02021-04-21 22:32:05 +02001175 /* Refuse the destruction of a read-only key (which may or may not work
1176 * if we attempt it, depending on whether the key is merely read-only
1177 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001178 * Just do the best we can, which is to wipe the copy in memory
1179 * (done in this function's cleanup code). */
1180 overall_status = PSA_ERROR_NOT_PERMITTED;
1181 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001182 }
1183
Gilles Peskine354f7672019-07-12 23:46:38 +02001184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 driver = psa_get_se_driver_entry(slot->attr.lifetime);
1186 if (driver != NULL) {
Gilles Peskine60450a42019-07-25 11:32:45 +02001187 /* For a key in a secure element, we need to do three things:
1188 * remove the key file in internal storage, destroy the
1189 * key inside the secure element, and update the driver's
1190 * persistent data. Start a transaction that will encompass these
1191 * three actions. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_DESTROY_KEY);
Gilles Peskine8e338702019-07-30 20:06:31 +02001193 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number(slot);
Gilles Peskine8e338702019-07-30 20:06:31 +02001195 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 status = psa_crypto_save_transaction();
1197 if (status != PSA_SUCCESS) {
1198 (void) psa_crypto_stop_transaction();
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 /* We should still try to destroy the key in the secure
1200 * element and the key metadata in storage. This is especially
1201 * important if the error is that the storage is full.
1202 * But how to do it exactly without risking an inconsistent
1203 * state after a reset?
1204 * https://github.com/ARMmbed/mbed-crypto/issues/215
1205 */
1206 overall_status = status;
1207 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001208 }
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 status = psa_destroy_se_key(driver,
1211 psa_key_slot_get_slot_number(slot));
1212 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001213 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001215 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1217
Darryl Greend49a4992018-06-18 17:27:26 +01001218#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1220 status = psa_destroy_persistent_key(slot->attr.id);
1221 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 }
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001225 /* TODO: other slots may have a copy of the same key. We should
1226 * invalidate them.
1227 * https://github.com/ARMmbed/mbed-crypto/issues/214
1228 */
Darryl Greend49a4992018-06-18 17:27:26 +01001229 }
1230#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001231
Gilles Peskinefc762652019-07-22 19:30:34 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (driver != NULL) {
1234 status = psa_save_se_persistent_data(driver);
1235 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001236 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
1238 status = psa_crypto_stop_transaction();
1239 if (overall_status == PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001240 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001242 }
1243#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1244
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001245exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 status = psa_wipe_key_slot(slot);
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001247 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (status != PSA_SUCCESS) {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001249 overall_status = status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 }
1251 return overall_status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252}
1253
Valerio Settib2bcedb2023-07-10 11:24:00 +02001254#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
John Durkop0e005192020-10-31 22:06:54 -07001255 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001256static psa_status_t psa_get_rsa_public_exponent(
1257 const mbedtls_rsa_context *rsa,
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 psa_key_attributes_t *attributes)
Gilles Peskineb699f072019-04-26 16:06:02 +02001259{
1260 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001262 uint8_t *buffer = NULL;
1263 size_t buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 mbedtls_mpi_init(&mpi);
Gilles Peskineb699f072019-04-26 16:06:02 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &mpi);
1267 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001268 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 }
1270 if (mbedtls_mpi_cmp_int(&mpi, 65537) == 0) {
Gilles Peskine772c8b12019-04-26 17:37:21 +02001271 /* It's the default value, which is reported as an empty string,
1272 * so there's nothing to do. */
1273 goto exit;
1274 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 buflen = mbedtls_mpi_size(&mpi);
1277 buffer = mbedtls_calloc(1, buflen);
1278 if (buffer == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001279 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1280 goto exit;
1281 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 ret = mbedtls_mpi_write_binary(&mpi, buffer, buflen);
1283 if (ret != 0) {
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001286 attributes->domain_parameters = buffer;
1287 attributes->domain_parameters_size = buflen;
1288
1289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 mbedtls_mpi_free(&mpi);
1291 if (ret != 0) {
1292 mbedtls_free(buffer);
1293 }
1294 return mbedtls_to_psa_error(ret);
Gilles Peskineb699f072019-04-26 16:06:02 +02001295}
Valerio Settib2bcedb2023-07-10 11:24:00 +02001296#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001297 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001298
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001299/** Retrieve all the publicly-accessible attributes of a key.
1300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
1302 psa_key_attributes_t *attributes)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001305 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001306 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 psa_reset_key_attributes(attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1311 if (status != PSA_SUCCESS) {
1312 return status;
1313 }
Gilles Peskineb870b182018-07-06 16:02:09 +02001314
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001315 attributes->core = slot->attr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 attributes->core.flags &= (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1317 MBEDTLS_PSA_KA_MASK_DUAL_USE);
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318
1319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (psa_get_se_driver_entry(slot->attr.lifetime) != NULL) {
1321 psa_set_key_slot_number(attributes,
1322 psa_key_slot_get_slot_number(slot));
1323 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 switch (slot->attr.type) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001327#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1328 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
John Durkop0e005192020-10-31 22:06:54 -07001329 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001330 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001332 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001333 * is not yet implemented.
1334 * https://github.com/ARMmbed/mbed-crypto/issues/216
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001337 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001338
Ronald Cron90857082020-11-25 14:58:33 +01001339 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 slot->attr.type,
1341 slot->key.data,
1342 slot->key.bytes,
1343 &rsa);
1344 if (status != PSA_SUCCESS) {
Steven Cooremana01795d2020-07-24 22:48:15 +02001345 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 }
Steven Cooremana01795d2020-07-24 22:48:15 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 status = psa_get_rsa_public_exponent(rsa,
1349 attributes);
1350 mbedtls_rsa_free(rsa);
1351 mbedtls_free(rsa);
Steven Cooremana01795d2020-07-24 22:48:15 +02001352 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001353 break;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001354#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1355 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08001356 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001357 default:
1358 /* Nothing else to do. */
1359 break;
1360 }
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (status != PSA_SUCCESS) {
1363 psa_reset_key_attributes(attributes);
1364 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369}
1370
Gilles Peskinec8000c02019-08-02 20:15:51 +02001371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1372psa_status_t psa_get_key_slot_number(
1373 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 psa_key_slot_number_t *slot_number)
Gilles Peskinec8000c02019-08-02 20:15:51 +02001375{
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER) {
Gilles Peskinec8000c02019-08-02 20:15:51 +02001377 *slot_number = attributes->slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return PSA_SUCCESS;
1379 } else {
1380 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001381 }
Gilles Peskinec8000c02019-08-02 20:15:51 +02001382}
1383#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385static psa_status_t psa_export_key_buffer_internal(const uint8_t *key_buffer,
1386 size_t key_buffer_size,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length)
Steven Cooremana01795d2020-07-24 22:48:15 +02001390{
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if (key_buffer_size > data_size) {
1392 return PSA_ERROR_BUFFER_TOO_SMALL;
1393 }
1394 memcpy(data, key_buffer, key_buffer_size);
1395 memset(data + key_buffer_size, 0,
1396 data_size - key_buffer_size);
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 *data_length = key_buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 return PSA_SUCCESS;
Steven Cooremana01795d2020-07-24 22:48:15 +02001399}
1400
Ronald Cron7285cda2020-11-26 14:46:37 +01001401psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 const psa_key_attributes_t *attributes,
1403 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 uint8_t *data, size_t data_size, size_t *data_length)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405{
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 psa_key_type_t type = attributes->core.type;
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (key_type_is_raw_bytes(type) ||
1409 PSA_KEY_TYPE_IS_RSA(type) ||
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001410 PSA_KEY_TYPE_IS_ECC(type) ||
1411 PSA_KEY_TYPE_IS_DH(type)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return psa_export_key_buffer_internal(
1413 key_buffer, key_buffer_size,
1414 data, data_size, data_length);
1415 } else {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416 /* This shouldn't happen in the reference implementation, but
1417 it is valid for a special-purpose implementation to omit
1418 support for exporting certain key types. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001420 }
1421}
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
1424 uint8_t *data,
1425 size_t data_size,
1426 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001427{
1428 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1429 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1430 psa_key_slot_t *slot;
1431
Ronald Crone907e552021-01-18 13:22:38 +01001432 /* Reject a zero-length output buffer now, since this can never be a
1433 * valid key representation. This way we know that data must be a valid
1434 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (data_size == 0) {
1436 return PSA_ERROR_BUFFER_TOO_SMALL;
1437 }
Ronald Crone907e552021-01-18 13:22:38 +01001438
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439 /* Set the key to empty now, so that even when there are errors, we always
1440 * set data_length to a value between 0 and data_size. On error, setting
1441 * the key to empty is a good choice because an empty key representation is
1442 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001443 *data_length = 0;
1444
Ronald Cron9486f9d2020-11-26 13:36:23 +01001445 /* Export requires the EXPORT flag. There is an exception for public keys,
1446 * which don't require any flag, but
1447 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
1450 PSA_KEY_USAGE_EXPORT, 0);
1451 if (status != PSA_SUCCESS) {
1452 return status;
1453 }
mohammad160306e79202018-03-28 13:17:44 +03001454
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 psa_key_attributes_t attributes = {
1456 .core = slot->attr
1457 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 status = psa_driver_wrapper_export_key(&attributes,
1459 slot->key.data, slot->key.bytes,
1460 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return (status == PSA_SUCCESS) ? unlock_status : status;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001465}
1466
Ronald Cron7285cda2020-11-26 14:46:37 +01001467psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001468 const psa_key_attributes_t *attributes,
1469 const uint8_t *key_buffer,
1470 size_t key_buffer_size,
1471 uint8_t *data,
1472 size_t data_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 size_t *data_length)
Ronald Cron9486f9d2020-11-26 13:36:23 +01001474{
Ronald Crond18b5f82020-11-25 16:46:05 +01001475 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001476
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001477 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) &&
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001478 (PSA_KEY_TYPE_IS_RSA(type) || PSA_KEY_TYPE_IS_ECC(type) ||
1479 PSA_KEY_TYPE_IS_DH(type))) {
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001480 /* Exporting public -> public */
1481 return psa_export_key_buffer_internal(
1482 key_buffer, key_buffer_size,
1483 data, data_size, data_length);
1484 } else if (PSA_KEY_TYPE_IS_RSA(type)) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001485#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001486 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1487 return mbedtls_psa_rsa_export_public_key(attributes,
1488 key_buffer,
1489 key_buffer_size,
1490 data,
1491 data_size,
1492 data_length);
Darryl Green9e2d7a02018-07-24 16:33:30 +01001493#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001494 /* We don't know how to convert a private RSA key to public. */
1495 return PSA_ERROR_NOT_SUPPORTED;
Valerio Settib2bcedb2023-07-10 11:24:00 +02001496#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001497 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001498 } else if (PSA_KEY_TYPE_IS_ECC(type)) {
Valerio Setti27c501a2023-06-27 16:58:52 +02001499#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) || \
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001500 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
1501 return mbedtls_psa_ecp_export_public_key(attributes,
1502 key_buffer,
1503 key_buffer_size,
1504 data,
1505 data_size,
1506 data_length);
Steven Cooreman560c28a2020-07-24 23:20:24 +02001507#else
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001508 /* We don't know how to convert a private ECC key to public */
1509 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti27c501a2023-06-27 16:58:52 +02001510#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_EXPORT) ||
John Durkop9814fa22020-11-04 12:28:15 -08001511 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Przemek Stekiel6fd72b62023-04-27 10:20:56 +02001512 } else if (PSA_KEY_TYPE_IS_DH(type)) {
Valerio Setti8bb57632023-05-26 13:48:07 +02001513#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) || \
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001514 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY)
Przemek Stekiel152bb462023-06-01 11:52:39 +02001515 return mbedtls_psa_ffdh_export_public_key(attributes,
Przemek Stekiel134cc2e2023-05-05 10:13:37 +02001516 key_buffer,
1517 key_buffer_size,
1518 data, data_size,
1519 data_length);
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001520#else
Przemek Stekiel472b3f32022-12-01 14:38:49 +01001521 return PSA_ERROR_NOT_SUPPORTED;
Valerio Setti8bb57632023-05-26 13:48:07 +02001522#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) ||
Przemek Stekiel6d85afa2023-04-28 11:42:17 +02001523 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_PUBLIC_KEY) */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 } else {
Przemek Stekiel0b11ee02023-05-16 13:26:06 +02001525 (void) key_buffer;
1526 (void) key_buffer_size;
1527 (void) data;
1528 (void) data_size;
1529 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001531 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532}
Ronald Crond18b5f82020-11-25 16:46:05 +01001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
1535 uint8_t *data,
1536 size_t data_size,
1537 size_t *data_length)
Moran Pekerdd4ea382018-04-03 15:30:03 +03001538{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001540 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001541 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001542 psa_key_attributes_t attributes;
Darryl Greendd8fb772018-11-07 16:00:44 +00001543
Ronald Crone907e552021-01-18 13:22:38 +01001544 /* Reject a zero-length output buffer now, since this can never be a
1545 * valid key representation. This way we know that data must be a valid
1546 * pointer and we can do things like memset(data, ..., data_size). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (data_size == 0) {
1548 return PSA_ERROR_BUFFER_TOO_SMALL;
1549 }
Ronald Crone907e552021-01-18 13:22:38 +01001550
Darryl Greendd8fb772018-11-07 16:00:44 +00001551 /* Set the key to empty now, so that even when there are errors, we always
1552 * set data_length to a value between 0 and data_size. On error, setting
1553 * the key to empty is a good choice because an empty key representation is
1554 * unlikely to be accepted anywhere. */
1555 *data_length = 0;
1556
1557 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 status = psa_get_and_lock_key_slot_with_policy(key, &slot, 0, 0);
1559 if (status != PSA_SUCCESS) {
1560 return status;
1561 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
1564 status = PSA_ERROR_INVALID_ARGUMENT;
1565 goto exit;
Ronald Cron9486f9d2020-11-26 13:36:23 +01001566 }
1567
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01001568 attributes = (psa_key_attributes_t) {
Ronald Crond18b5f82020-11-25 16:46:05 +01001569 .core = slot->attr
1570 };
Ronald Cron67227982020-11-26 15:16:05 +01001571 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001572 &attributes, slot->key.data, slot->key.bytes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 data, data_size, data_length);
Ronald Cron9486f9d2020-11-26 13:36:23 +01001574
1575exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return (status == PSA_SUCCESS) ? unlock_status : status;
Moran Pekerdd4ea382018-04-03 15:30:03 +03001579}
1580
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001581MBEDTLS_STATIC_ASSERT(
1582 (MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1583 "One or more key attribute flag is listed as both external-only and dual-use")
1584MBEDTLS_STATIC_ASSERT(
1585 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE) == 0,
1586 "One or more key attribute flag is listed as both internal-only and dual-use")
1587MBEDTLS_STATIC_ASSERT(
1588 (PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY) == 0,
1589 "One or more key attribute flag is listed as both internal-only and external-only")
Gilles Peskine91e8c332019-08-02 19:19:39 +02001590
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001591/** Validate that a key policy is internally well-formed.
1592 *
1593 * This function only rejects invalid policies. It does not validate the
1594 * consistency of the policy with respect to other attributes of the key
1595 * such as the key type.
1596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597static psa_status_t psa_validate_key_policy(const psa_key_policy_t *policy)
Gilles Peskine4747d192019-04-17 15:05:45 +02001598{
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if ((policy->usage & ~(PSA_KEY_USAGE_EXPORT |
1600 PSA_KEY_USAGE_COPY |
1601 PSA_KEY_USAGE_ENCRYPT |
1602 PSA_KEY_USAGE_DECRYPT |
1603 PSA_KEY_USAGE_SIGN_MESSAGE |
1604 PSA_KEY_USAGE_VERIFY_MESSAGE |
1605 PSA_KEY_USAGE_SIGN_HASH |
1606 PSA_KEY_USAGE_VERIFY_HASH |
1607 PSA_KEY_USAGE_VERIFY_DERIVATION |
1608 PSA_KEY_USAGE_DERIVE)) != 0) {
1609 return PSA_ERROR_INVALID_ARGUMENT;
1610 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return PSA_SUCCESS;
Gilles Peskine4747d192019-04-17 15:05:45 +02001613}
1614
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001615/** Validate the internal consistency of key attributes.
1616 *
1617 * This function only rejects invalid attribute values. If does not
1618 * validate the consistency of the attributes with any key data that may
1619 * be involved in the creation of the key.
1620 *
1621 * Call this function early in the key creation process.
1622 *
1623 * \param[in] attributes Key attributes for the new key.
1624 * \param[out] p_drv On any return, the driver for the key, if any.
1625 * NULL for a transparent key.
1626 *
1627 */
1628static psa_status_t psa_validate_key_attributes(
1629 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 psa_se_drv_table_entry_t **p_drv)
Darryl Green0c6575a2018-11-07 16:05:30 +00001631{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001632 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 psa_key_lifetime_t lifetime = psa_get_key_lifetime(attributes);
1634 mbedtls_svc_key_id_t key = psa_get_key_id(attributes);
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 status = psa_validate_key_location(lifetime, p_drv);
1637 if (status != PSA_SUCCESS) {
1638 return status;
Ronald Crond2ed4812020-07-17 16:11:30 +02001639 }
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 status = psa_validate_key_persistence(lifetime);
1642 if (status != PSA_SUCCESS) {
1643 return status;
1644 }
1645
1646 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
1647 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key) != 0) {
1648 return PSA_ERROR_INVALID_ARGUMENT;
1649 }
1650 } else {
1651 if (!psa_is_valid_key_id(psa_get_key_id(attributes), 0)) {
1652 return PSA_ERROR_INVALID_ARGUMENT;
1653 }
1654 }
1655
1656 status = psa_validate_key_policy(&attributes->core.policy);
1657 if (status != PSA_SUCCESS) {
1658 return status;
1659 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001660
1661 /* Refuse to create overly large keys.
1662 * Note that this doesn't trigger on import if the attributes don't
1663 * explicitly specify a size (so psa_get_key_bits returns 0), so
1664 * psa_import_key() needs its own checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (psa_get_key_bits(attributes) > PSA_MAX_KEY_BITS) {
1666 return PSA_ERROR_NOT_SUPPORTED;
1667 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001668
Gilles Peskine91e8c332019-08-02 19:19:39 +02001669 /* Reject invalid flags. These should not be reachable through the API. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if (attributes->core.flags & ~(MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1671 MBEDTLS_PSA_KA_MASK_DUAL_USE)) {
1672 return PSA_ERROR_INVALID_ARGUMENT;
1673 }
Gilles Peskine91e8c332019-08-02 19:19:39 +02001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return PSA_SUCCESS;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001676}
1677
Gilles Peskine4747d192019-04-17 15:05:45 +02001678/** Prepare a key slot to receive key material.
1679 *
1680 * This function allocates a key slot and sets its metadata.
1681 *
1682 * If this function fails, call psa_fail_key_creation().
1683 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001684 * This function is intended to be used as follows:
1685 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001686 * it with the specified attributes, and in case of a volatile key assign it
1687 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001688 * -# Populate the slot with the key material.
1689 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1690 * In case of failure at any step, stop the sequence and call
1691 * psa_fail_key_creation().
1692 *
Ronald Cron5c522922020-11-14 16:35:34 +01001693 * On success, the key slot is locked. It is the responsibility of the caller
1694 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001695 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001696 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001697 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001698 * \param[out] p_slot On success, a pointer to the prepared slot.
1699 * \param[out] p_drv On any return, the driver for the key, if any.
1700 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001701 *
1702 * \retval #PSA_SUCCESS
1703 * The key slot is ready to receive key material.
1704 * \return If this function fails, the key slot is an invalid state.
1705 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 */
1707static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001708 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001710 psa_key_slot_t **p_slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 psa_se_drv_table_entry_t **p_drv)
Gilles Peskine4747d192019-04-17 15:05:45 +02001712{
1713 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001714 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 psa_key_slot_t *slot;
1716
Gilles Peskinedf179142019-07-15 22:02:14 +02001717 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001718 *p_drv = NULL;
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 status = psa_validate_key_attributes(attributes, p_drv);
1721 if (status != PSA_SUCCESS) {
1722 return status;
1723 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 status = psa_get_empty_key_slot(&volatile_key_id, p_slot);
1726 if (status != PSA_SUCCESS) {
1727 return status;
1728 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001729 slot = *p_slot;
1730
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001731 /* We're storing the declared bit-size of the key. It's up to each
1732 * creation mechanism to verify that this information is correct.
1733 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001734 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001735 * is optional (import, copy). In case of a volatile key, assign it the
1736 * volatile key identifier associated to the slot returned to contain its
1737 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001738
1739 slot->attr = attributes->core;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Ronald Cronc4d1b512020-07-31 11:26:37 +02001741#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1742 slot->attr.id = volatile_key_id;
1743#else
1744 slot->attr.id.key_id = volatile_key_id;
1745#endif
1746 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001747
Gilles Peskine91e8c332019-08-02 19:19:39 +02001748 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001749 * external-only flags, query `attributes`. Thanks to the check
1750 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001751 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001752 * may have set. */
1753 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001754
Gilles Peskinecbaff462019-07-12 23:46:04 +02001755#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001756 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001757 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001758 * create the key file in internal storage, create the
1759 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001760 * persistent data. This is done by starting a transaction that will
1761 * encompass these three actions.
1762 * For registering a volatile key, we just need to find an appropriate
1763 * slot number inside the SE. Since the key is designated volatile, creating
1764 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001765 /* The first thing to do is to find a slot number for the new key.
1766 * We save the slot number in persistent storage as part of the
1767 * transaction data. It will be needed to recover if the power
1768 * fails during the key creation process, to clean up on the secure
1769 * element side after restarting. Obtaining a slot number from the
1770 * secure element driver updates its persistent state, but we do not yet
1771 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001772 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (*p_drv != NULL) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001774 psa_key_slot_number_t slot_number;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 status = psa_find_se_slot_for_key(attributes, method, *p_drv,
1776 &slot_number);
1777 if (status != PSA_SUCCESS) {
1778 return status;
1779 }
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (!PSA_KEY_LIFETIME_IS_VOLATILE(attributes->core.lifetime)) {
1782 psa_crypto_prepare_transaction(PSA_CRYPTO_TRANSACTION_CREATE_KEY);
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001783 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001784 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001785 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 status = psa_crypto_save_transaction();
1787 if (status != PSA_SUCCESS) {
1788 (void) psa_crypto_stop_transaction();
1789 return status;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001790 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001791 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001792
1793 status = psa_copy_key_material_into_slot(
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 slot, (uint8_t *) (&slot_number), sizeof(slot_number));
Darryl Green0c6575a2018-11-07 16:05:30 +00001795 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (*p_drv == NULL && method == PSA_KEY_CREATION_REGISTER) {
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001798 /* Key registration only makes sense with a secure element. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001800 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001801#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 return PSA_SUCCESS;
Darryl Green0c6575a2018-11-07 16:05:30 +00001804}
Gilles Peskine4747d192019-04-17 15:05:45 +02001805
1806/** Finalize the creation of a key once its key material has been set.
1807 *
1808 * This entails writing the key to persistent storage.
1809 *
1810 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001811 * See the documentation of psa_start_key_creation() for the intended use
1812 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001813 *
Ronald Cron5c522922020-11-14 16:35:34 +01001814 * If the finalization succeeds, the function unlocks the key slot (it was
1815 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1816 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001817 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001818 * \param[in,out] slot Pointer to the slot with key material.
1819 * \param[in] driver The secure element driver for the key,
1820 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001821 * \param[out] key On success, identifier of the key. Note that the
1822 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001823 *
1824 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001825 * The key was successfully created.
Gilles Peskineed733552023-02-14 19:21:09 +01001826 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1827 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
1828 * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
1829 * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1830 * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1831 * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001832 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001833 * \return If this function fails, the key slot is an invalid state.
1834 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001836static psa_status_t psa_finish_key_creation(
1837 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001838 psa_se_drv_table_entry_t *driver,
1839 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001840{
1841 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001842 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001843 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001844
1845#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001847#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 if (driver != NULL) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001849 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001850 psa_key_slot_number_t slot_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 psa_key_slot_get_slot_number(slot);
Ronald Cronea0f8a62020-11-25 17:52:23 +01001852
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001853 MBEDTLS_STATIC_ASSERT(sizeof(slot_number) ==
1854 sizeof(data.slot_number),
1855 "Slot number size does not match psa_se_key_data_storage_t");
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 memcpy(&data.slot_number, &slot_number, sizeof(slot_number));
1858 status = psa_save_persistent_key(&slot->attr,
1859 (uint8_t *) &data,
1860 sizeof(data));
1861 } else
Gilles Peskine1df83d42019-07-23 16:13:14 +02001862#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1863 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001864 /* Key material is saved in export representation in the slot, so
1865 * just pass the slot buffer for storage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 status = psa_save_persistent_key(&slot->attr,
1867 slot->key.data,
1868 slot->key.bytes);
Gilles Peskine1df83d42019-07-23 16:13:14 +02001869 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001871#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1872
Gilles Peskinecbaff462019-07-12 23:46:04 +02001873#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001874 /* Finish the transaction for a key creation. This does not
1875 * happen when registering an existing key. Detect this case
1876 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001877 * creation of a persistent key in a secure element requires a transaction,
1878 * but registration or volatile key creation doesn't use one). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (driver != NULL &&
1880 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY) {
1881 status = psa_save_se_persistent_data(driver);
1882 if (status != PSA_SUCCESS) {
1883 psa_destroy_persistent_key(slot->attr.id);
1884 return status;
Gilles Peskinecbaff462019-07-12 23:46:04 +02001885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 status = psa_crypto_stop_transaction();
Gilles Peskinecbaff462019-07-12 23:46:04 +02001887 }
1888#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (status == PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001891 *key = slot->attr.id;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 status = psa_unlock_key_slot(slot);
1893 if (status != PSA_SUCCESS) {
Ronald Cron81709fc2020-11-14 12:10:32 +01001894 *key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 }
Ronald Cron81709fc2020-11-14 12:10:32 +01001896 }
Ronald Cron50972942020-11-14 11:28:25 +01001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02001899}
1900
1901/** Abort the creation of a key.
1902 *
1903 * You may call this function after calling psa_start_key_creation(),
1904 * or after psa_finish_key_creation() fails. In other circumstances, this
1905 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001906 * See the documentation of psa_start_key_creation() for the intended use
1907 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001908 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001909 * \param[in,out] slot Pointer to the slot with key material.
1910 * \param[in] driver The secure element driver for the key,
1911 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913static void psa_fail_key_creation(psa_key_slot_t *slot,
1914 psa_se_drv_table_entry_t *driver)
Gilles Peskine4747d192019-04-17 15:05:45 +02001915{
Gilles Peskine011e4282019-06-26 18:34:38 +02001916 (void) driver;
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (slot == NULL) {
Gilles Peskine4747d192019-04-17 15:05:45 +02001919 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 }
Gilles Peskine011e4282019-06-26 18:34:38 +02001921
1922#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001923 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001924 * element, and the failure happened later (when saving metadata
1925 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001926 * element.
1927 * https://github.com/ARMmbed/mbed-crypto/issues/217
1928 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001929
Gilles Peskined7729582019-08-05 15:55:54 +02001930 /* Abort the ongoing transaction if any (there may not be one if
1931 * the creation process failed before starting one, or if the
1932 * key creation is a registration of a key in a secure element).
1933 * Earlier functions must already have done what it takes to undo any
1934 * partial creation. All that's left is to update the transaction data
1935 * itself. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 (void) psa_crypto_stop_transaction();
Gilles Peskine011e4282019-06-26 18:34:38 +02001937#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 psa_wipe_key_slot(slot);
Gilles Peskine4747d192019-04-17 15:05:45 +02001940}
1941
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001942/** Validate optional attributes during key creation.
1943 *
1944 * Some key attributes are optional during key creation. If they are
1945 * specified in the attributes structure, check that they are consistent
1946 * with the data in the slot.
1947 *
1948 * This function should be called near the end of key creation, after
1949 * the slot in memory is fully populated but before saving persistent data.
1950 */
1951static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952 const psa_key_slot_t *slot,
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 const psa_key_attributes_t *attributes)
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954{
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (attributes->core.type != 0) {
1956 if (attributes->core.type != slot->attr.type) {
1957 return PSA_ERROR_INVALID_ARGUMENT;
1958 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959 }
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (attributes->domain_parameters_size != 0) {
Valerio Settib2bcedb2023-07-10 11:24:00 +02001962#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
1963 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
1965 if (PSA_KEY_TYPE_IS_RSA(slot->attr.type)) {
Steven Cooremana2371e52020-07-28 14:30:39 +02001966 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001967 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001969
Ronald Cron90857082020-11-25 14:58:33 +01001970 psa_status_t status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 slot->attr.type,
1972 slot->key.data,
1973 slot->key.bytes,
1974 &rsa);
1975 if (status != PSA_SUCCESS) {
1976 return status;
1977 }
Steven Cooreman75b74362020-07-28 14:30:13 +02001978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 mbedtls_mpi_init(&actual);
1980 mbedtls_mpi_init(&required);
1981 ret = mbedtls_rsa_export(rsa,
1982 NULL, NULL, NULL, NULL, &actual);
1983 mbedtls_rsa_free(rsa);
1984 mbedtls_free(rsa);
1985 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
1988 ret = mbedtls_mpi_read_binary(&required,
1989 attributes->domain_parameters,
1990 attributes->domain_parameters_size);
1991 if (ret != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 }
1994 if (mbedtls_mpi_cmp_mpi(&actual, &required) != 0) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001995 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 }
1997rsa_exit:
1998 mbedtls_mpi_free(&actual);
1999 mbedtls_mpi_free(&required);
2000 if (ret != 0) {
2001 return mbedtls_to_psa_error(ret);
2002 }
2003 } else
Valerio Settib2bcedb2023-07-10 11:24:00 +02002004#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
2005 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
John Durkop9814fa22020-11-04 12:28:15 -08002006 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 }
2010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (attributes->core.bits != 0) {
2013 if (attributes->core.bits != slot->attr.bits) {
2014 return PSA_ERROR_INVALID_ARGUMENT;
2015 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016 }
2017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 return PSA_SUCCESS;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019}
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
2022 const uint8_t *data,
2023 size_t data_length,
2024 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002025{
2026 psa_status_t status;
2027 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002028 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002029 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05302030 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031
Ronald Cron81709fc2020-11-14 12:10:32 +01002032 *key = MBEDTLS_SVC_KEY_ID_INIT;
2033
Gilles Peskine0f84d622019-09-12 19:03:13 +02002034 /* Reject zero-length symmetric keys (including raw data key objects).
2035 * This also rejects any key which might be encoded as an empty string,
2036 * which is never valid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (data_length == 0) {
2038 return PSA_ERROR_INVALID_ARGUMENT;
2039 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02002040
Archana449608b2021-09-08 15:36:05 +05302041 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (data_length > SIZE_MAX / 8) {
2043 return PSA_ERROR_NOT_SUPPORTED;
2044 }
Archana6ed4bda2021-08-04 10:47:15 +05302045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
2047 &slot, &driver);
2048 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002052 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05302053 * storage ( thus not in the case of importing a key in a secure element
2054 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
2055 * buffer to hold the imported key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (slot->key.data == NULL) {
2057 if (psa_key_lifetime_is_external(attributes->core.lifetime)) {
Archana449608b2021-09-08 15:36:05 +05302058 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 attributes, data, data_length, &storage_size);
2060 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05302061 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 }
Archanad8a83dc2021-06-14 10:04:16 +05302063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 status = psa_allocate_buffer_to_slot(slot, storage_size);
2065 if (status != PSA_SUCCESS) {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002066 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002068 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002069
2070 bits = slot->attr.bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 status = psa_driver_wrapper_import_key(attributes,
2072 data, data_length,
2073 slot->key.data,
2074 slot->key.bytes,
2075 &slot->key.bytes, &bits);
2076 if (status != PSA_SUCCESS) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (slot->attr.bits == 0) {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002081 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 } else if (bits != slot->attr.bits) {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002083 status = PSA_ERROR_INVALID_ARGUMENT;
2084 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002085 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002086
Archana6ed4bda2021-08-04 10:47:15 +05302087 /* Enforce a size limit, and in particular ensure that the bit
2088 * size fits in its representation type.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (bits > PSA_MAX_KEY_BITS) {
Archana6ed4bda2021-08-04 10:47:15 +05302090 status = PSA_ERROR_NOT_SUPPORTED;
2091 goto exit;
2092 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 status = psa_validate_optional_attributes(slot, attributes);
2094 if (status != PSA_SUCCESS) {
Gilles Peskine18017402019-07-24 20:25:59 +02002095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 status = psa_finish_key_creation(slot, driver, key);
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if (status != PSA_SUCCESS) {
2101 psa_fail_key_creation(slot, driver);
2102 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return status;
Gilles Peskine4747d192019-04-17 15:05:45 +02002105}
2106
Gilles Peskined7729582019-08-05 15:55:54 +02002107#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2108psa_status_t mbedtls_psa_register_se_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 const psa_key_attributes_t *attributes)
Gilles Peskined7729582019-08-05 15:55:54 +02002110{
2111 psa_status_t status;
2112 psa_key_slot_t *slot = NULL;
2113 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002115
2116 /* Leaving attributes unspecified is not currently supported.
2117 * It could make sense to query the key type and size from the
2118 * secure element, but not all secure elements support this
2119 * and the driver HAL doesn't currently support it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (psa_get_key_type(attributes) == PSA_KEY_TYPE_NONE) {
2121 return PSA_ERROR_NOT_SUPPORTED;
2122 }
2123 if (psa_get_key_bits(attributes) == 0) {
2124 return PSA_ERROR_NOT_SUPPORTED;
2125 }
Gilles Peskined7729582019-08-05 15:55:54 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 status = psa_start_key_creation(PSA_KEY_CREATION_REGISTER, attributes,
2128 &slot, &driver);
2129 if (status != PSA_SUCCESS) {
Gilles Peskined7729582019-08-05 15:55:54 +02002130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Gilles Peskined7729582019-08-05 15:55:54 +02002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_finish_key_creation(slot, driver, &key);
Gilles Peskined7729582019-08-05 15:55:54 +02002134
2135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (status != PSA_SUCCESS) {
2137 psa_fail_key_creation(slot, driver);
2138 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002139
Gilles Peskined7729582019-08-05 15:55:54 +02002140 /* Registration doesn't keep the key in RAM. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 psa_close_key(key);
2142 return status;
Gilles Peskined7729582019-08-05 15:55:54 +02002143}
2144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
2147 const psa_key_attributes_t *specified_attributes,
2148 mbedtls_svc_key_id_t *target_key)
Gilles Peskinef603c712019-01-19 13:40:11 +01002149{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002151 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002152 psa_key_slot_t *source_slot = NULL;
2153 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002154 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002155 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302156 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002157
Ronald Cron81709fc2020-11-14 12:10:32 +01002158 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2159
Archana8a180362021-07-05 02:18:48 +05302160 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0);
2162 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002163 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 status = psa_validate_optional_attributes(source_slot,
2167 specified_attributes);
2168 if (status != PSA_SUCCESS) {
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002169 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 }
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002171
Archana9d17bf42021-09-10 06:22:44 +05302172 /* The target key type and number of bits have been validated by
2173 * psa_validate_optional_attributes() to be either equal to zero or
2174 * equal to the ones of the source key. So it is safe to inherit
2175 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302176 * */
Archana9d17bf42021-09-10 06:22:44 +05302177 actual_attributes.core.bits = source_slot->attr.bits;
2178 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302179
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 status = psa_restrict_key_policy(source_slot->attr.type,
2182 &actual_attributes.core.policy,
2183 &source_slot->attr.policy);
2184 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 status = psa_start_key_creation(PSA_KEY_CREATION_COPY, &actual_attributes,
2189 &target_slot, &driver);
2190 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 }
2193 if (PSA_KEY_LIFETIME_GET_LOCATION(target_slot->attr.lifetime) !=
2194 PSA_KEY_LIFETIME_GET_LOCATION(source_slot->attr.lifetime)) {
Archana8a180362021-07-05 02:18:48 +05302195 /*
Archana9d17bf42021-09-10 06:22:44 +05302196 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302197 * the source key would need to be exported as plaintext and re-imported
2198 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302199 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302200 * appropriate API invocations from the application, if needed.
2201 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002202 status = PSA_ERROR_NOT_SUPPORTED;
2203 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002204 }
Archana8a180362021-07-05 02:18:48 +05302205 /*
2206 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302207 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302208 * - For opaque keys this translates to an invocation of the drivers'
2209 * copy_key entry point through the dispatch layer.
2210 * */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (psa_key_lifetime_is_external(actual_attributes.core.lifetime)) {
2212 status = psa_driver_wrapper_get_key_buffer_size(&actual_attributes,
2213 &storage_size);
2214 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 }
Archana374fe5b2021-09-08 15:50:28 +05302217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 status = psa_allocate_buffer_to_slot(target_slot, storage_size);
2219 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 }
Archana374fe5b2021-09-08 15:50:28 +05302222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 status = psa_driver_wrapper_copy_key(&actual_attributes,
2224 source_slot->key.data,
2225 source_slot->key.bytes,
2226 target_slot->key.data,
2227 target_slot->key.bytes,
2228 &target_slot->key.bytes);
2229 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 }
2232 } else {
2233 status = psa_copy_key_material_into_slot(target_slot,
Archana9d17bf42021-09-10 06:22:44 +05302234 source_slot->key.data,
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 source_slot->key.bytes);
2236 if (status != PSA_SUCCESS) {
Archana8a180362021-07-05 02:18:48 +05302237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 }
Archana8a180362021-07-05 02:18:48 +05302239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 status = psa_finish_key_creation(target_slot, driver, target_key);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 if (status != PSA_SUCCESS) {
2243 psa_fail_key_creation(target_slot, driver);
2244 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 unlock_status = psa_unlock_key_slot(source_slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002249}
2250
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002251
2252
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002253/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002254/* Message digests */
2255/****************************************************************/
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257psa_status_t psa_hash_abort(psa_hash_operation_t *operation)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002259 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (operation->id == 0) {
2261 return PSA_SUCCESS;
2262 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 psa_status_t status = psa_driver_wrapper_hash_abort(operation);
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002265 operation->id = 0;
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268}
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
2271 psa_algorithm_t alg)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002273 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002274
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002275 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (operation->id != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002277 status = PSA_ERROR_BAD_STATE;
2278 goto exit;
2279 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (!PSA_ALG_IS_HASH(alg)) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002282 status = PSA_ERROR_INVALID_ARGUMENT;
2283 goto exit;
2284 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002285
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002286 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2287 * directly zeroes the int-sized dummy member of the context union. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 memset(&operation->ctx, 0, sizeof(operation->ctx));
Steven Cooreman893232f2021-03-15 12:23:37 +01002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 status = psa_driver_wrapper_hash_setup(operation, alg);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002291
2292exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (status != PSA_SUCCESS) {
2294 psa_hash_abort(operation);
2295 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002296
2297 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298}
2299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2301 const uint8_t *input,
2302 size_t input_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002304 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (operation->id == 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002307 status = PSA_ERROR_BAD_STATE;
2308 goto exit;
2309 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002310
Steven Cooremanc8288352021-03-04 14:02:19 +01002311 /* Don't require hash implementations to behave correctly on a
2312 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (input_length == 0) {
2314 return PSA_SUCCESS;
2315 }
Steven Cooremanc8288352021-03-04 14:02:19 +01002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 status = psa_driver_wrapper_hash_update(operation, input, input_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002318
2319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (status != PSA_SUCCESS) {
2321 psa_hash_abort(operation);
2322 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325}
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2328 uint8_t *hash,
2329 size_t hash_size,
2330 size_t *hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002332 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (operation->id == 0) {
2334 return PSA_ERROR_BAD_STATE;
2335 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336
Steven Cooreman1e582352021-02-18 17:24:37 +01002337 psa_status_t status = psa_driver_wrapper_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 operation, hash, hash_size, hash_length);
2339 psa_hash_abort(operation);
2340 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341}
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2344 const uint8_t *hash,
2345 size_t hash_length)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346{
Ronald Cron69a63422021-10-18 09:47:58 +02002347 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002349 psa_status_t status = psa_hash_finish(
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 operation,
2351 actual_hash, sizeof(actual_hash),
2352 &actual_hash_length);
Dave Rodgman685b6a72021-06-24 11:49:14 +01002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002355 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (actual_hash_length != hash_length) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002359 status = PSA_ERROR_INVALID_SIGNATURE;
2360 goto exit;
2361 }
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002364 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002366
2367exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2369 if (status != PSA_SUCCESS) {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002370 psa_hash_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 }
Dave Rodgman685b6a72021-06-24 11:49:14 +01002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374}
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376psa_status_t psa_hash_compute(psa_algorithm_t alg,
2377 const uint8_t *input, size_t input_length,
2378 uint8_t *hash, size_t hash_size,
2379 size_t *hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002380{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002381 *hash_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (!PSA_ALG_IS_HASH(alg)) {
2383 return PSA_ERROR_INVALID_ARGUMENT;
2384 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return psa_driver_wrapper_hash_compute(alg, input, input_length,
2387 hash, hash_size, hash_length);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002388}
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390psa_status_t psa_hash_compare(psa_algorithm_t alg,
2391 const uint8_t *input, size_t input_length,
2392 const uint8_t *hash, size_t hash_length)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002393{
Ronald Cron69a63422021-10-18 09:47:58 +02002394 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002395 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (!PSA_ALG_IS_HASH(alg)) {
2398 return PSA_ERROR_INVALID_ARGUMENT;
2399 }
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002400
Steven Cooreman1e582352021-02-18 17:24:37 +01002401 psa_status_t status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 alg, input, input_length,
2403 actual_hash, sizeof(actual_hash),
2404 &actual_hash_length);
2405 if (status != PSA_SUCCESS) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002406 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 }
2408 if (actual_hash_length != hash_length) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002409 status = PSA_ERROR_INVALID_SIGNATURE;
2410 goto exit;
2411 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (mbedtls_psa_safer_memcmp(hash, actual_hash, actual_hash_length) != 0) {
Gilles Peskine60aebec2021-12-13 12:33:18 +01002413 status = PSA_ERROR_INVALID_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 }
Gilles Peskine60aebec2021-12-13 12:33:18 +01002415
2416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 mbedtls_platform_zeroize(actual_hash, sizeof(actual_hash));
2418 return status;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002419}
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
2422 psa_hash_operation_t *target_operation)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002423{
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (source_operation->id == 0 ||
2425 target_operation->id != 0) {
2426 return PSA_ERROR_BAD_STATE;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002427 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 psa_status_t status = psa_driver_wrapper_hash_clone(source_operation,
2430 target_operation);
2431 if (status != PSA_SUCCESS) {
2432 psa_hash_abort(target_operation);
2433 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 return status;
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002436}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437
2438
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002440/* MAC */
2441/****************************************************************/
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443psa_status_t psa_mac_abort(psa_mac_operation_t *operation)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002444{
Steven Cooremane6804192021-03-19 18:28:56 +01002445 /* Aborting a non-active operation is allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 if (operation->id == 0) {
2447 return PSA_SUCCESS;
2448 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 psa_status_t status = psa_driver_wrapper_mac_abort(operation);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002451 operation->mac_size = 0;
2452 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002453 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return status;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002456}
2457
Ronald Cron2dff3b22021-06-17 16:33:22 +02002458static psa_status_t psa_mac_finalize_alg_and_key_validation(
2459 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002460 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 uint8_t *mac_size)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002462{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 psa_key_type_t key_type = psa_get_key_type(attributes);
2465 size_t key_bits = psa_get_key_bits(attributes);
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (!PSA_ALG_IS_MAC(alg)) {
2468 return PSA_ERROR_INVALID_ARGUMENT;
2469 }
Steven Cooremane6804192021-03-19 18:28:56 +01002470
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002471 /* Validate the combination of key type and algorithm */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 status = psa_mac_key_can_do(alg, key_type);
2473 if (status != PSA_SUCCESS) {
2474 return status;
2475 }
Steven Cooreman15472f82021-03-02 16:16:22 +01002476
Steven Cooremand1a68f12021-05-10 11:13:41 +02002477 /* Get the output length for the algorithm and key combination */
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 *mac_size = PSA_MAC_LENGTH(key_type, key_bits, alg);
Steven Cooreman15472f82021-03-02 16:16:22 +01002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (*mac_size < 4) {
Steven Cooreman15472f82021-03-02 16:16:22 +01002481 /* A very short MAC is too short for security since it can be
2482 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2483 * so we make this our minimum, even though 32 bits is still
2484 * too small for security. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002486 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (*mac_size > PSA_MAC_LENGTH(key_type, key_bits,
2489 PSA_ALG_FULL_LENGTH_MAC(alg))) {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002490 /* It's impossible to "truncate" to a larger length than the full length
2491 * of the algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002493 }
2494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (*mac_size > PSA_MAC_MAX_SIZE) {
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002496 /* PSA_MAC_LENGTH returns the correct length even for a MAC algorithm
2497 * that is disabled in the compile-time configuration. The result can
2498 * therefore be larger than PSA_MAC_MAX_SIZE, which does take the
2499 * configuration into account. In this case, force a return of
2500 * PSA_ERROR_NOT_SUPPORTED here. Otherwise psa_mac_verify(), or
2501 * psa_mac_compute(mac_size=PSA_MAC_MAX_SIZE), would return
2502 * PSA_ERROR_BUFFER_TOO_SMALL for an unsupported algorithm whose MAC size
2503 * is larger than PSA_MAC_MAX_SIZE, which is misleading and which breaks
2504 * systematically generated tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinea9b6c802022-03-16 13:54:49 +01002506 }
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 return PSA_SUCCESS;
Ronald Cron2dff3b22021-06-17 16:33:22 +02002509}
2510
Gilles Peskine449bd832023-01-11 14:50:10 +01002511static psa_status_t psa_mac_setup(psa_mac_operation_t *operation,
2512 mbedtls_svc_key_id_t key,
2513 psa_algorithm_t alg,
2514 int is_sign)
Gilles Peskinee59236f2018-01-27 23:32:46 +01002515{
2516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2517 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002518 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002519 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520
2521 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01002523 status = PSA_ERROR_BAD_STATE;
2524 goto exit;
2525 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002526
2527 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 key,
2529 &slot,
2530 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2531 alg);
2532 if (status != PSA_SUCCESS) {
Dave Rodgman54648242021-06-24 11:49:45 +01002533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002536 attributes = (psa_key_attributes_t) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 .core = slot->attr
2538 };
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2541 &operation->mac_size);
2542 if (status != PSA_SUCCESS) {
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545
2546 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002547 /* Dispatch the MAC setup call with validated input */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (is_sign) {
2549 status = psa_driver_wrapper_mac_sign_setup(operation,
2550 &attributes,
2551 slot->key.data,
2552 slot->key.bytes,
2553 alg);
2554 } else {
2555 status = psa_driver_wrapper_mac_verify_setup(operation,
2556 &attributes,
2557 slot->key.data,
2558 slot->key.bytes,
2559 alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002560 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002561
Gilles Peskinefbfac682018-07-08 20:51:54 +02002562exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (status != PSA_SUCCESS) {
2564 psa_mac_abort(operation);
2565 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570}
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
2573 mbedtls_svc_key_id_t key,
2574 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002575{
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 return psa_mac_setup(operation, key, alg, 1);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002577}
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2580 mbedtls_svc_key_id_t key,
2581 psa_algorithm_t alg)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002582{
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 return psa_mac_setup(operation, key, alg, 0);
Gilles Peskine89167cb2018-07-08 20:12:23 +02002584}
2585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2587 const uint8_t *input,
2588 size_t input_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002589{
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (operation->id == 0) {
2591 return PSA_ERROR_BAD_STATE;
2592 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002593
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002594 /* Don't require hash implementations to behave correctly on a
2595 * zero-length input, which may have an invalid pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (input_length == 0) {
2597 return PSA_SUCCESS;
2598 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 psa_status_t status = psa_driver_wrapper_mac_update(operation,
2601 input, input_length);
2602 if (status != PSA_SUCCESS) {
2603 psa_mac_abort(operation);
2604 }
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607}
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2610 uint8_t *mac,
2611 size_t mac_size,
2612 size_t *mac_length)
mohammad16036df908f2018-04-02 08:34:15 -07002613{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002614 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2615 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002618 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002619 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002620 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002621
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (!operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002623 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002624 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002625 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002626
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002627 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2628 * once all the error checks are done. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (operation->mac_size == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002630 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002631 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002632 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (mac_size < operation->mac_size) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002635 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002636 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002637 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 status = psa_driver_wrapper_mac_sign_finish(operation,
2640 mac, operation->mac_size,
2641 mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002642
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002643exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002644 /* In case of success, set the potential excess room in the output buffer
2645 * to an invalid value, to avoid potentially leaking a longer MAC.
2646 * In case of error, set the output length and content to a safe default,
2647 * such that in case the caller misses an error check, the output would be
2648 * an unachievable MAC.
2649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (status != PSA_SUCCESS) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002651 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002652 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002653 }
mohammad16036df908f2018-04-02 08:34:15 -07002654
Paul Elliottdc42ca82023-02-24 18:11:59 +00002655 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 abort_status = psa_mac_abort(operation);
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 return status == PSA_SUCCESS ? abort_status : status;
mohammad16036df908f2018-04-02 08:34:15 -07002660}
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2663 const uint8_t *mac,
2664 size_t mac_length)
Gilles Peskine8c9def32018-02-08 10:02:12 +01002665{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2667 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002670 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002671 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002672 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 if (operation->is_sign) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002675 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002676 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002677 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (operation->mac_size != mac_length) {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002680 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002681 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002682 }
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 status = psa_driver_wrapper_mac_verify_finish(operation,
2685 mac, mac_length);
Steven Cooreman72f736a2021-05-07 14:14:37 +02002686
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002687exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 abort_status = psa_mac_abort(operation);
mohammad16036df908f2018-04-02 08:34:15 -07002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return status == PSA_SUCCESS ? abort_status : status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691}
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693static psa_status_t psa_mac_compute_internal(mbedtls_svc_key_id_t key,
2694 psa_algorithm_t alg,
2695 const uint8_t *input,
2696 size_t input_length,
2697 uint8_t *mac,
2698 size_t mac_size,
2699 size_t *mac_length,
2700 int is_sign)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002701{
2702 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002703 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2704 psa_key_slot_t *slot;
2705 uint8_t operation_mac_size = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002706 psa_key_attributes_t attributes;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707
Ronald Cron51131b52021-06-17 17:17:20 +02002708 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 key,
2710 &slot,
2711 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
2712 alg);
2713 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002716
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002717 attributes = (psa_key_attributes_t) {
Ronald Cron51131b52021-06-17 17:17:20 +02002718 .core = slot->attr
2719 };
2720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 status = psa_mac_finalize_alg_and_key_validation(alg, &attributes,
2722 &operation_mac_size);
2723 if (status != PSA_SUCCESS) {
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 if (mac_size < operation_mac_size) {
Ronald Cron51131b52021-06-17 17:17:20 +02002728 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002729 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002730 }
2731
2732 status = psa_driver_wrapper_mac_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 &attributes,
2734 slot->key.data, slot->key.bytes,
2735 alg,
2736 input, input_length,
2737 mac, operation_mac_size, mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002738
2739exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002740 /* In case of success, set the potential excess room in the output buffer
2741 * to an invalid value, to avoid potentially leaking a longer MAC.
2742 * In case of error, set the output length and content to a safe default,
2743 * such that in case the caller misses an error check, the output would be
2744 * an unachievable MAC.
2745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if (status != PSA_SUCCESS) {
Ronald Cron51131b52021-06-17 17:17:20 +02002747 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002748 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002749 }
Paul Elliottdc42ca82023-02-24 18:11:59 +00002750
2751 psa_wipe_tag_output_buffer(mac, status, mac_size, *mac_length);
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 unlock_status = psa_unlock_key_slot(slot);
Ronald Cron51131b52021-06-17 17:17:20 +02002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002756}
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002759 psa_algorithm_t alg,
2760 const uint8_t *input,
2761 size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 uint8_t *mac,
2763 size_t mac_size,
2764 size_t *mac_length)
2765{
2766 return psa_mac_compute_internal(key, alg,
2767 input, input_length,
2768 mac, mac_size, mac_length, 1);
2769}
2770
2771psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
2772 psa_algorithm_t alg,
2773 const uint8_t *input,
2774 size_t input_length,
2775 const uint8_t *mac,
2776 size_t mac_length)
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002777{
2778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002779 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2780 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 status = psa_mac_compute_internal(key, alg,
2783 input, input_length,
2784 actual_mac, sizeof(actual_mac),
2785 &actual_mac_length, 0);
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_length != actual_mac_length) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002791 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002792 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002793 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (mbedtls_psa_safer_memcmp(mac, actual_mac, actual_mac_length) != 0) {
Ronald Crona587cbc2021-06-18 14:51:29 +02002795 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002796 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002797 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002798
2799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 mbedtls_platform_zeroize(actual_mac, sizeof(actual_mac));
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 return status;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002803}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002804
Gilles Peskinee59236f2018-01-27 23:32:46 +01002805/****************************************************************/
2806/* Asymmetric cryptography */
2807/****************************************************************/
2808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809static psa_status_t psa_sign_verify_check_alg(int input_is_message,
2810 psa_algorithm_t alg)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002811{
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (input_is_message) {
2813 if (!PSA_ALG_IS_SIGN_MESSAGE(alg)) {
2814 return PSA_ERROR_INVALID_ARGUMENT;
2815 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 if (PSA_ALG_IS_SIGN_HASH(alg)) {
2818 if (!PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(alg))) {
2819 return PSA_ERROR_INVALID_ARGUMENT;
2820 }
2821 }
2822 } else {
2823 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
2824 return PSA_ERROR_INVALID_ARGUMENT;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002825 }
2826 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 return PSA_SUCCESS;
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002829}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831static psa_status_t psa_sign_internal(mbedtls_svc_key_id_t key,
2832 int input_is_message,
2833 psa_algorithm_t alg,
2834 const uint8_t *input,
2835 size_t input_length,
2836 uint8_t *signature,
2837 size_t signature_size,
2838 size_t *signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002839{
2840 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2841 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2842 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002843 psa_key_attributes_t attributes;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002844
2845 *signature_length = 0;
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 status = psa_sign_verify_check_alg(input_is_message, alg);
2848 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002849 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002851
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002852 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002853 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002854 * buffer can in principle be empty since it doesn't actually have
2855 * to be a hash.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (signature_size == 0) {
2857 return PSA_ERROR_BUFFER_TOO_SMALL;
2858 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002859
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002860 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 key, &slot,
2862 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2863 PSA_KEY_USAGE_SIGN_HASH,
2864 alg);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (status != PSA_SUCCESS) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002867 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002871 status = PSA_ERROR_INVALID_ARGUMENT;
2872 goto exit;
2873 }
2874
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01002875 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002877 };
2878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002880 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002881 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002882 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 signature, signature_size, signature_length);
2884 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002885
2886 status = psa_driver_wrapper_sign_hash(
2887 &attributes, slot->key.data, slot->key.bytes,
2888 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 signature, signature_size, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002890 }
2891
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002892
2893exit:
Paul Elliott59200a22023-02-21 15:07:40 +00002894 psa_wipe_tag_output_buffer(signature, status, signature_size,
2895 *signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002898
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002900}
2901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902static psa_status_t psa_verify_internal(mbedtls_svc_key_id_t key,
2903 int input_is_message,
2904 psa_algorithm_t alg,
2905 const uint8_t *input,
2906 size_t input_length,
2907 const uint8_t *signature,
2908 size_t signature_length)
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002909{
2910 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2911 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2912 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 status = psa_sign_verify_check_alg(input_is_message, alg);
2915 if (status != PSA_SUCCESS) {
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002916 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 }
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002918
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002919 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 key, &slot,
2921 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2922 PSA_KEY_USAGE_VERIFY_HASH,
2923 alg);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (status != PSA_SUCCESS) {
2926 return status;
2927 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002928
2929 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 .core = slot->attr
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002931 };
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (input_is_message) {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002934 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002935 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002936 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 signature, signature_length);
2938 } else {
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002939 status = psa_driver_wrapper_verify_hash(
2940 &attributes, slot->key.data, slot->key.bytes,
2941 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 signature, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002943 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 unlock_status = psa_unlock_key_slot(slot);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 return (status == PSA_SUCCESS) ? unlock_status : status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002948
2949}
2950
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002951psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002952 const psa_key_attributes_t *attributes,
2953 const uint8_t *key_buffer,
2954 size_t key_buffer_size,
2955 psa_algorithm_t alg,
2956 const uint8_t *input,
2957 size_t input_length,
2958 uint8_t *signature,
2959 size_t signature_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 size_t *signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002961{
2962 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002965 size_t hash_length;
2966 uint8_t hash[PSA_HASH_MAX_SIZE];
2967
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002968 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ALG_SIGN_GET_HASH(alg),
2970 input, input_length,
2971 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002974 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002976
2977 return psa_driver_wrapper_sign_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 attributes, key_buffer, key_buffer_size,
2979 alg, hash, hash_length,
2980 signature, signature_size, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002981 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002984}
2985
Gilles Peskine449bd832023-01-11 14:50:10 +01002986psa_status_t psa_sign_message(mbedtls_svc_key_id_t key,
2987 psa_algorithm_t alg,
2988 const uint8_t *input,
2989 size_t input_length,
2990 uint8_t *signature,
2991 size_t signature_size,
2992 size_t *signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002993{
2994 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002995 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 signature, signature_size, signature_length);
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002997}
2998
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002999psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003000 const psa_key_attributes_t *attributes,
3001 const uint8_t *key_buffer,
3002 size_t key_buffer_size,
3003 psa_algorithm_t alg,
3004 const uint8_t *input,
3005 size_t input_length,
3006 const uint8_t *signature,
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 size_t signature_length)
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003008{
3009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 if (PSA_ALG_IS_SIGN_HASH(alg)) {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003012 size_t hash_length;
3013 uint8_t hash[PSA_HASH_MAX_SIZE];
3014
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003015 status = psa_driver_wrapper_hash_compute(
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 PSA_ALG_SIGN_GET_HASH(alg),
3017 input, input_length,
3018 hash, sizeof(hash), &hash_length);
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (status != PSA_SUCCESS) {
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003021 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 }
gabor-mezei-armf9820f92021-05-03 16:26:20 +02003023
3024 return psa_driver_wrapper_verify_hash(
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 attributes, key_buffer, key_buffer_size,
3026 alg, hash, hash_length,
3027 signature, signature_length);
gabor-mezei-arm0f622402021-04-29 16:48:44 +02003028 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02003031}
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033psa_status_t psa_verify_message(mbedtls_svc_key_id_t key,
3034 psa_algorithm_t alg,
3035 const uint8_t *input,
3036 size_t input_length,
3037 const uint8_t *signature,
3038 size_t signature_length)
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003039{
3040 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003041 key, 1, alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 signature, signature_length);
gabor-mezei-arm4a210192021-04-14 21:14:28 +02003043}
3044
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003045psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003046 const psa_key_attributes_t *attributes,
3047 const uint8_t *key_buffer, size_t key_buffer_size,
3048 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 uint8_t *signature, size_t signature_size, size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003050{
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
3052 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3053 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003054#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3056 return mbedtls_psa_rsa_sign_hash(
3057 attributes,
3058 key_buffer, key_buffer_size,
3059 alg, hash, hash_length,
3060 signature, signature_size, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003061#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3062 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 } else {
3064 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003065 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3067 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003068#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3070 return mbedtls_psa_ecdsa_sign_hash(
3071 attributes,
3072 key_buffer, key_buffer_size,
3073 alg, hash, hash_length,
3074 signature, signature_size, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3076 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 } else {
3078 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003079 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003080 }
Ronald Cron4501c982021-03-19 20:09:32 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 (void) key_buffer;
3083 (void) key_buffer_size;
3084 (void) hash;
3085 (void) hash_length;
3086 (void) signature;
3087 (void) signature_size;
3088 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003091}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
3094 psa_algorithm_t alg,
3095 const uint8_t *hash,
3096 size_t hash_length,
3097 uint8_t *signature,
3098 size_t signature_size,
3099 size_t *signature_length)
Gilles Peskinee59236f2018-01-27 23:32:46 +01003100{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003101 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003102 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 signature, signature_size, signature_length);
itayzafrir5c753392018-05-08 11:18:38 +03003104}
3105
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02003106psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003107 const psa_key_attributes_t *attributes,
3108 const uint8_t *key_buffer, size_t key_buffer_size,
3109 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 const uint8_t *signature, size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003111{
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
3113 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
3114 PSA_ALG_IS_RSA_PSS(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003115#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3117 return mbedtls_psa_rsa_verify_hash(
3118 attributes,
3119 key_buffer, key_buffer_size,
3120 alg, hash, hash_length,
3121 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003122#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3123 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 } else {
3125 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanacda8342020-07-24 23:09:52 +02003126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 } else if (PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3128 if (PSA_ALG_IS_ECDSA(alg)) {
Ronald Cron4501c982021-03-19 20:09:32 +01003129#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3131 return mbedtls_psa_ecdsa_verify_hash(
3132 attributes,
3133 key_buffer, key_buffer_size,
3134 alg, hash, hash_length,
3135 signature, signature_length);
Ronald Cron4501c982021-03-19 20:09:32 +01003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3137 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 } else {
3139 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron4501c982021-03-19 20:09:32 +01003140 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003141 }
Ronald Cron4501c982021-03-19 20:09:32 +01003142
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 (void) key_buffer;
3144 (void) key_buffer_size;
3145 (void) hash;
3146 (void) hash_length;
3147 (void) signature;
3148 (void) signature_length;
Ronald Cron4501c982021-03-19 20:09:32 +01003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron18659932020-12-08 16:32:23 +01003151}
3152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
3154 psa_algorithm_t alg,
3155 const uint8_t *hash,
3156 size_t hash_length,
3157 const uint8_t *signature,
3158 size_t signature_length)
itayzafrir5c753392018-05-08 11:18:38 +03003159{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003160 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003161 key, 0, alg, hash, hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 signature, signature_length);
Gilles Peskinee59236f2018-01-27 23:32:46 +01003163}
3164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
3166 psa_algorithm_t alg,
3167 const uint8_t *input,
3168 size_t input_length,
3169 const uint8_t *salt,
3170 size_t salt_length,
3171 uint8_t *output,
3172 size_t output_size,
3173 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003174{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003176 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003177 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003178 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003179
Darryl Green5cc689a2018-07-24 15:34:10 +01003180 (void) input;
3181 (void) input_length;
3182 (void) salt;
3183 (void) output;
3184 (void) output_size;
3185
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003186 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3189 return PSA_ERROR_INVALID_ARGUMENT;
3190 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003191
Ronald Cron5c522922020-11-14 16:35:34 +01003192 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
3194 if (status != PSA_SUCCESS) {
3195 return status;
3196 }
3197 if (!(PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type) ||
3198 PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type))) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003199 status = PSA_ERROR_INVALID_ARGUMENT;
3200 goto exit;
3201 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003203 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 .core = slot->attr
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003205 };
Steven Cooremana2371e52020-07-28 14:30:39 +02003206
Przemyslaw Stekiel19e61422021-12-09 11:09:11 +01003207 status = psa_driver_wrapper_asymmetric_encrypt(
3208 &attributes, slot->key.data, slot->key.bytes,
3209 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003211exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003215}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
3218 psa_algorithm_t alg,
3219 const uint8_t *input,
3220 size_t input_length,
3221 const uint8_t *salt,
3222 size_t salt_length,
3223 uint8_t *output,
3224 size_t output_size,
3225 size_t *output_length)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003228 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003229 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003230 psa_key_attributes_t attributes;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003231
Darryl Green5cc689a2018-07-24 15:34:10 +01003232 (void) input;
3233 (void) input_length;
3234 (void) salt;
3235 (void) output;
3236 (void) output_size;
3237
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003238 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (!PSA_ALG_IS_RSA_OAEP(alg) && salt_length != 0) {
3241 return PSA_ERROR_INVALID_ARGUMENT;
3242 }
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003243
Ronald Cron5c522922020-11-14 16:35:34 +01003244 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
3246 if (status != PSA_SUCCESS) {
3247 return status;
3248 }
3249 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003250 status = PSA_ERROR_INVALID_ARGUMENT;
3251 goto exit;
3252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003254 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 .core = slot->attr
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003256 };
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257
Przemyslaw Stekiel8d45c002021-12-13 08:58:19 +01003258 status = psa_driver_wrapper_asymmetric_decrypt(
3259 &attributes, slot->key.data, slot->key.bytes,
3260 alg, input, input_length, salt, salt_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 output, output_size, output_length);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262
3263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003267}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003268
Paul Elliott9fe12f62022-11-30 19:16:02 +00003269/****************************************************************/
3270/* Asymmetric interruptible cryptography */
3271/****************************************************************/
3272
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003273static uint32_t psa_interruptible_max_ops = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
3274
Paul Elliott9fe12f62022-11-30 19:16:02 +00003275void psa_interruptible_set_max_ops(uint32_t max_ops)
3276{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003277 psa_interruptible_max_ops = max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003278}
3279
3280uint32_t psa_interruptible_get_max_ops(void)
3281{
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003282 return psa_interruptible_max_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003283}
3284
Paul Elliott9fe12f62022-11-30 19:16:02 +00003285uint32_t psa_sign_hash_get_num_ops(
3286 const psa_sign_hash_interruptible_operation_t *operation)
3287{
Paul Elliott296ede92022-12-15 17:00:30 +00003288 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003289}
3290
3291uint32_t psa_verify_hash_get_num_ops(
3292 const psa_verify_hash_interruptible_operation_t *operation)
3293{
Paul Elliott296ede92022-12-15 17:00:30 +00003294 return operation->num_ops;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003295}
3296
Paul Elliott59ad9452022-12-18 15:09:02 +00003297static psa_status_t psa_sign_hash_abort_internal(
3298 psa_sign_hash_interruptible_operation_t *operation)
3299{
3300 if (operation->id == 0) {
3301 /* The object has (apparently) been initialized but it is not (yet)
3302 * in use. It's ok to call abort on such an object, and there's
3303 * nothing to do. */
3304 return PSA_SUCCESS;
3305 }
3306
3307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3308
3309 status = psa_driver_wrapper_sign_hash_abort(operation);
3310
3311 operation->id = 0;
3312
Paul Elliotte6145dc2023-02-07 12:51:21 +00003313 /* Do not clear either the error_occurred or num_ops elements here as they
3314 * only want to be cleared by the application calling abort, not by abort
3315 * being called at completion of an operation. */
3316
Paul Elliott59ad9452022-12-18 15:09:02 +00003317 return status;
3318}
3319
Paul Elliott9fe12f62022-11-30 19:16:02 +00003320psa_status_t psa_sign_hash_start(
3321 psa_sign_hash_interruptible_operation_t *operation,
3322 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3323 const uint8_t *hash, size_t hash_length)
3324{
3325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3326 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3327 psa_key_slot_t *slot;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003328 psa_key_attributes_t attributes;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003329
Paul Elliottc9774412023-02-06 15:14:07 +00003330 /* Check that start has not been previously called, or operation has not
3331 * previously errored. */
3332 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003333 return PSA_ERROR_BAD_STATE;
3334 }
3335
Paul Elliott9fe12f62022-11-30 19:16:02 +00003336 status = psa_sign_verify_check_alg(0, alg);
3337 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003338 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003339 return status;
3340 }
3341
3342 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3343 PSA_KEY_USAGE_SIGN_HASH,
3344 alg);
3345
3346 if (status != PSA_SUCCESS) {
3347 goto exit;
3348 }
3349
3350 if (!PSA_KEY_TYPE_IS_KEY_PAIR(slot->attr.type)) {
3351 status = PSA_ERROR_INVALID_ARGUMENT;
3352 goto exit;
3353 }
3354
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01003355 attributes = (psa_key_attributes_t) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003356 .core = slot->attr
3357 };
3358
Paul Elliott296ede92022-12-15 17:00:30 +00003359 /* Ensure ops count gets reset, in case of operation re-use. */
3360 operation->num_ops = 0;
3361
Paul Elliott9fe12f62022-11-30 19:16:02 +00003362 status = psa_driver_wrapper_sign_hash_start(operation, &attributes,
3363 slot->key.data,
3364 slot->key.bytes, alg,
3365 hash, hash_length);
3366exit:
3367
3368 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003369 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003370 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003371 }
3372
3373 unlock_status = psa_unlock_key_slot(slot);
3374
Paul Elliottc9774412023-02-06 15:14:07 +00003375 if (unlock_status != PSA_SUCCESS) {
3376 operation->error_occurred = 1;
3377 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003378
Paul Elliottc9774412023-02-06 15:14:07 +00003379 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003380}
3381
3382
3383psa_status_t psa_sign_hash_complete(
3384 psa_sign_hash_interruptible_operation_t *operation,
3385 uint8_t *signature, size_t signature_size,
3386 size_t *signature_length)
3387{
3388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3389
3390 *signature_length = 0;
3391
Paul Elliottc9774412023-02-06 15:14:07 +00003392 /* Check that start has been called first, and that operation has not
3393 * previously errored. */
3394 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003395 status = PSA_ERROR_BAD_STATE;
3396 goto exit;
3397 }
3398
Paul Elliott096abc42023-02-03 18:33:23 +00003399 /* Immediately reject a zero-length signature buffer. This guarantees that
3400 * signature must be a valid pointer. */
Paul Elliott9fe12f62022-11-30 19:16:02 +00003401 if (signature_size == 0) {
3402 status = PSA_ERROR_BUFFER_TOO_SMALL;
3403 goto exit;
3404 }
3405
3406 status = psa_driver_wrapper_sign_hash_complete(operation, signature,
3407 signature_size,
3408 signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003409
Paul Elliott296ede92022-12-15 17:00:30 +00003410 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003411 operation->num_ops = psa_driver_wrapper_sign_hash_get_num_ops(operation);
Paul Elliott296ede92022-12-15 17:00:30 +00003412
Paul Elliottebe225c2023-02-10 14:32:53 +00003413exit:
3414
Paul Elliott59200a22023-02-21 15:07:40 +00003415 psa_wipe_tag_output_buffer(signature, status, signature_size,
3416 *signature_length);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003417
Paul Elliott53bb3122023-02-10 14:22:22 +00003418 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003419 if (status != PSA_SUCCESS) {
3420 operation->error_occurred = 1;
3421 }
3422
Paul Elliott59ad9452022-12-18 15:09:02 +00003423 psa_sign_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003424 }
3425
3426 return status;
3427}
3428
3429psa_status_t psa_sign_hash_abort(
3430 psa_sign_hash_interruptible_operation_t *operation)
3431{
Paul Elliott59ad9452022-12-18 15:09:02 +00003432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3433
3434 status = psa_sign_hash_abort_internal(operation);
3435
3436 /* We clear the number of ops done here, so that it is not cleared when
3437 * the operation fails or succeeds, only on manual abort. */
3438 operation->num_ops = 0;
3439
Paul Elliottc9774412023-02-06 15:14:07 +00003440 /* Likewise, failure state. */
3441 operation->error_occurred = 0;
3442
Paul Elliott59ad9452022-12-18 15:09:02 +00003443 return status;
3444}
3445
3446static psa_status_t psa_verify_hash_abort_internal(
3447 psa_verify_hash_interruptible_operation_t *operation)
3448{
Paul Elliott9fe12f62022-11-30 19:16:02 +00003449 if (operation->id == 0) {
3450 /* The object has (apparently) been initialized but it is not (yet)
3451 * in use. It's ok to call abort on such an object, and there's
3452 * nothing to do. */
3453 return PSA_SUCCESS;
3454 }
3455
Paul Elliott59ad9452022-12-18 15:09:02 +00003456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3457
3458 status = psa_driver_wrapper_verify_hash_abort(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003459
3460 operation->id = 0;
3461
Paul Elliotte6145dc2023-02-07 12:51:21 +00003462 /* Do not clear either the error_occurred or num_ops elements here as they
3463 * only want to be cleared by the application calling abort, not by abort
3464 * being called at completion of an operation. */
3465
Paul Elliott59ad9452022-12-18 15:09:02 +00003466 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003467}
3468
3469psa_status_t psa_verify_hash_start(
3470 psa_verify_hash_interruptible_operation_t *operation,
3471 mbedtls_svc_key_id_t key, psa_algorithm_t alg,
3472 const uint8_t *hash, size_t hash_length,
3473 const uint8_t *signature, size_t signature_length)
3474{
3475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3476 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3477 psa_key_slot_t *slot;
3478
Paul Elliottc9774412023-02-06 15:14:07 +00003479 /* Check that start has not been previously called, or operation has not
3480 * previously errored. */
3481 if (operation->id != 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003482 return PSA_ERROR_BAD_STATE;
3483 }
3484
3485 status = psa_sign_verify_check_alg(0, alg);
3486 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003487 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003488 return status;
3489 }
3490
3491 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
3492 PSA_KEY_USAGE_VERIFY_HASH,
3493 alg);
3494
3495 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003496 operation->error_occurred = 1;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003497 return status;
3498 }
3499
3500 psa_key_attributes_t attributes = {
3501 .core = slot->attr
3502 };
3503
Paul Elliott296ede92022-12-15 17:00:30 +00003504 /* Ensure ops count gets reset, in case of operation re-use. */
3505 operation->num_ops = 0;
3506
Paul Elliott9fe12f62022-11-30 19:16:02 +00003507 status = psa_driver_wrapper_verify_hash_start(operation, &attributes,
3508 slot->key.data,
3509 slot->key.bytes,
3510 alg, hash, hash_length,
3511 signature, signature_length);
3512
3513 if (status != PSA_SUCCESS) {
Paul Elliottc9774412023-02-06 15:14:07 +00003514 operation->error_occurred = 1;
Paul Elliott59ad9452022-12-18 15:09:02 +00003515 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003516 }
3517
3518 unlock_status = psa_unlock_key_slot(slot);
3519
Paul Elliottc9774412023-02-06 15:14:07 +00003520 if (unlock_status != PSA_SUCCESS) {
3521 operation->error_occurred = 1;
3522 }
Paul Elliott9fe12f62022-11-30 19:16:02 +00003523
Paul Elliottc9774412023-02-06 15:14:07 +00003524 return (status == PSA_SUCCESS) ? unlock_status : status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003525}
3526
3527psa_status_t psa_verify_hash_complete(
3528 psa_verify_hash_interruptible_operation_t *operation)
3529{
3530 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3531
Paul Elliottc9774412023-02-06 15:14:07 +00003532 /* Check that start has been called first, and that operation has not
3533 * previously errored. */
3534 if (operation->id == 0 || operation->error_occurred) {
Paul Elliott9fe12f62022-11-30 19:16:02 +00003535 status = PSA_ERROR_BAD_STATE;
3536 goto exit;
3537 }
3538
3539 status = psa_driver_wrapper_verify_hash_complete(operation);
3540
Paul Elliott296ede92022-12-15 17:00:30 +00003541 /* Update ops count with work done. */
Paul Elliottde1114c2023-02-07 12:43:11 +00003542 operation->num_ops = psa_driver_wrapper_verify_hash_get_num_ops(
Paul Elliott296ede92022-12-15 17:00:30 +00003543 operation);
3544
Paul Elliottebe225c2023-02-10 14:32:53 +00003545exit:
3546
Paul Elliott9fe12f62022-11-30 19:16:02 +00003547 if (status != PSA_OPERATION_INCOMPLETE) {
Paul Elliottc9774412023-02-06 15:14:07 +00003548 if (status != PSA_SUCCESS) {
3549 operation->error_occurred = 1;
3550 }
3551
Paul Elliott59ad9452022-12-18 15:09:02 +00003552 psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003553 }
3554
3555 return status;
3556}
3557
3558psa_status_t psa_verify_hash_abort(
3559 psa_verify_hash_interruptible_operation_t *operation)
3560{
Paul Elliott59ad9452022-12-18 15:09:02 +00003561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003562
Paul Elliott59ad9452022-12-18 15:09:02 +00003563 status = psa_verify_hash_abort_internal(operation);
Paul Elliott9fe12f62022-11-30 19:16:02 +00003564
Paul Elliott59ad9452022-12-18 15:09:02 +00003565 /* We clear the number of ops done here, so that it is not cleared when
3566 * the operation fails or succeeds, only on manual abort. */
3567 operation->num_ops = 0;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003568
Paul Elliottc9774412023-02-06 15:14:07 +00003569 /* Likewise, failure state. */
3570 operation->error_occurred = 0;
3571
Paul Elliott59ad9452022-12-18 15:09:02 +00003572 return status;
Paul Elliott9fe12f62022-11-30 19:16:02 +00003573}
3574
Paul Elliott588f8ed2022-12-02 18:10:26 +00003575/****************************************************************/
3576/* Asymmetric interruptible cryptography internal */
3577/* implementations */
3578/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003579
Paul Elliott588f8ed2022-12-02 18:10:26 +00003580void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
3581{
Paul Elliott7cc4e812023-01-10 17:14:11 +00003582
Paul Elliott588f8ed2022-12-02 18:10:26 +00003583#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3584 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3585 defined(MBEDTLS_ECP_RESTARTABLE)
3586
3587 /* Internal implementation uses zero to indicate infinite number max ops,
3588 * therefore avoid this value, and set to minimum possible. */
3589 if (max_ops == 0) {
3590 max_ops = 1;
3591 }
3592
Paul Elliott588f8ed2022-12-02 18:10:26 +00003593 mbedtls_ecp_set_max_ops(max_ops);
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003594#else
3595 (void) max_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003596#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3597 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3598 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3599}
3600
Paul Elliott588f8ed2022-12-02 18:10:26 +00003601uint32_t mbedtls_psa_sign_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003602 const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003603{
3604#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3605 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3606 defined(MBEDTLS_ECP_RESTARTABLE)
3607
Paul Elliott93d9ca82023-02-15 18:14:21 +00003608 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003609#else
3610 (void) operation;
3611 return 0;
3612#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3613 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3614 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3615}
3616
3617uint32_t mbedtls_psa_verify_hash_get_num_ops(
Paul Elliottf8e5b562023-02-19 18:43:45 +00003618 const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003619{
3620 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3621 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3622 defined(MBEDTLS_ECP_RESTARTABLE)
3623
Paul Elliott93d9ca82023-02-15 18:14:21 +00003624 return operation->num_ops;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003625#else
3626 (void) operation;
3627 return 0;
3628#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3629 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3630 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3631}
3632
3633psa_status_t mbedtls_psa_sign_hash_start(
3634 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3635 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
3636 size_t key_buffer_size, psa_algorithm_t alg,
3637 const uint8_t *hash, size_t hash_length)
3638{
3639 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott0290a762023-02-09 14:30:24 +00003640 size_t required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003641
Paul Elliott068fe072023-01-16 13:59:15 +00003642 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3643 return PSA_ERROR_NOT_SUPPORTED;
3644 }
3645
3646 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003647 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003648 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003649
3650#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003651 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3652 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003653
Paul Elliotta1c94092023-02-15 16:38:04 +00003654 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3655
Paul Elliott93d9ca82023-02-15 18:14:21 +00003656 /* Ensure num_ops is zero'ed in case of context re-use. */
3657 operation->num_ops = 0;
3658
Paul Elliott068fe072023-01-16 13:59:15 +00003659 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3660 attributes->core.bits,
3661 key_buffer,
3662 key_buffer_size,
3663 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003664
Paul Elliott068fe072023-01-16 13:59:15 +00003665 if (status != PSA_SUCCESS) {
3666 return status;
3667 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003668
Paul Elliott1bc59df2023-02-05 13:41:57 +00003669 operation->coordinate_bytes = PSA_BITS_TO_BYTES(
Paul Elliottc569fc22023-02-10 13:02:54 +00003670 operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003671
Paul Elliott068fe072023-01-16 13:59:15 +00003672 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02003673 operation->md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Paul Elliott068fe072023-01-16 13:59:15 +00003674 operation->alg = alg;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003675
Paul Elliott0290a762023-02-09 14:30:24 +00003676 /* We only need to store the same length of hash as the private key size
3677 * here, it would be truncated by the internal implementation anyway. */
3678 required_hash_length = (hash_length < operation->coordinate_bytes ?
3679 hash_length : operation->coordinate_bytes);
3680
Paul Elliottba70ad42023-02-15 18:23:53 +00003681 if (required_hash_length > sizeof(operation->hash)) {
3682 /* Shouldn't happen, but better safe than sorry. */
3683 return PSA_ERROR_CORRUPTION_DETECTED;
3684 }
3685
Paul Elliott0290a762023-02-09 14:30:24 +00003686 memcpy(operation->hash, hash, required_hash_length);
3687 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003688
3689 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003690
3691#else
Paul Elliott068fe072023-01-16 13:59:15 +00003692 (void) operation;
3693 (void) key_buffer;
3694 (void) key_buffer_size;
3695 (void) alg;
3696 (void) hash;
3697 (void) hash_length;
3698 (void) status;
Paul Elliott0290a762023-02-09 14:30:24 +00003699 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003700
Paul Elliott068fe072023-01-16 13:59:15 +00003701 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003702#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3703 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3704 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003705}
3706
3707psa_status_t mbedtls_psa_sign_hash_complete(
3708 mbedtls_psa_sign_hash_interruptible_operation_t *operation,
3709 uint8_t *signature, size_t signature_size,
3710 size_t *signature_length)
3711{
Paul Elliott588f8ed2022-12-02 18:10:26 +00003712#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3713 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3714 defined(MBEDTLS_ECP_RESTARTABLE)
3715
Paul Elliotta1c94092023-02-15 16:38:04 +00003716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1243f932023-02-07 11:21:10 +00003717 mbedtls_mpi r;
3718 mbedtls_mpi s;
3719
Paul Elliott46845252023-02-03 14:59:11 +00003720 mbedtls_mpi_init(&r);
3721 mbedtls_mpi_init(&s);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003722
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003723 /* Ensure max_ops is set to the current value (or default). */
3724 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3725
Paul Elliotta1c94092023-02-15 16:38:04 +00003726 if (signature_size < 2 * operation->coordinate_bytes) {
3727 status = PSA_ERROR_BUFFER_TOO_SMALL;
3728 goto exit;
3729 }
3730
Paul Elliott588f8ed2022-12-02 18:10:26 +00003731 if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
Paul Elliott46845252023-02-03 14:59:11 +00003732
Paul Elliott588f8ed2022-12-02 18:10:26 +00003733#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3734 status = mbedtls_to_psa_error(
3735 mbedtls_ecdsa_sign_det_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003736 &r,
3737 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003738 &operation->ctx->d,
3739 operation->hash,
3740 operation->hash_length,
3741 operation->md_alg,
3742 mbedtls_psa_get_random,
3743 MBEDTLS_PSA_RANDOM_STATE,
3744 &operation->restart_ctx));
3745#else /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Paul Elliott724bd252023-02-08 12:35:08 +00003746 status = PSA_ERROR_NOT_SUPPORTED;
3747 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003748#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3749 } else {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003750 status = mbedtls_to_psa_error(
3751 mbedtls_ecdsa_sign_restartable(&operation->ctx->grp,
Paul Elliott46845252023-02-03 14:59:11 +00003752 &r,
3753 &s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003754 &operation->ctx->d,
3755 operation->hash,
3756 operation->hash_length,
3757 mbedtls_psa_get_random,
3758 MBEDTLS_PSA_RANDOM_STATE,
3759 mbedtls_psa_get_random,
3760 MBEDTLS_PSA_RANDOM_STATE,
3761 &operation->restart_ctx));
3762 }
3763
Paul Elliottf8e5b562023-02-19 18:43:45 +00003764 /* Hide the fact that the restart context only holds a delta of number of
3765 * ops done during the last operation, not an absolute value. */
3766 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3767
Paul Elliott724bd252023-02-08 12:35:08 +00003768 if (status == PSA_SUCCESS) {
Paul Elliott588f8ed2022-12-02 18:10:26 +00003769 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003770 mbedtls_mpi_write_binary(&r,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003771 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003772 operation->coordinate_bytes)
3773 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003774
3775 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003776 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003777 }
3778
3779 status = mbedtls_to_psa_error(
Paul Elliott46845252023-02-03 14:59:11 +00003780 mbedtls_mpi_write_binary(&s,
Paul Elliott588f8ed2022-12-02 18:10:26 +00003781 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003782 operation->coordinate_bytes,
3783 operation->coordinate_bytes)
3784 );
Paul Elliott588f8ed2022-12-02 18:10:26 +00003785
3786 if (status != PSA_SUCCESS) {
Paul Elliott724bd252023-02-08 12:35:08 +00003787 goto exit;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003788 }
3789
Paul Elliott1bc59df2023-02-05 13:41:57 +00003790 *signature_length = operation->coordinate_bytes * 2;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003791
Paul Elliott724bd252023-02-08 12:35:08 +00003792 status = PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003793 }
Paul Elliott724bd252023-02-08 12:35:08 +00003794
3795exit:
3796
3797 mbedtls_mpi_free(&r);
3798 mbedtls_mpi_free(&s);
3799 return status;
3800
Paul Elliott588f8ed2022-12-02 18:10:26 +00003801 #else
3802
3803 (void) operation;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003804 (void) signature;
3805 (void) signature_size;
3806 (void) signature_length;
3807
3808 return PSA_ERROR_NOT_SUPPORTED;
3809
3810#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3811 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3812 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3813}
3814
3815psa_status_t mbedtls_psa_sign_hash_abort(
3816 mbedtls_psa_sign_hash_interruptible_operation_t *operation)
3817{
3818
3819#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3820 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3821 defined(MBEDTLS_ECP_RESTARTABLE)
3822
3823 if (operation->ctx) {
3824 mbedtls_ecdsa_free(operation->ctx);
3825 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003826 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003827 }
3828
3829 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
3830
Paul Elliott93d9ca82023-02-15 18:14:21 +00003831 operation->num_ops = 0;
3832
Paul Elliott588f8ed2022-12-02 18:10:26 +00003833 return PSA_SUCCESS;
3834
3835#else
3836
3837 (void) operation;
3838
3839 return PSA_ERROR_NOT_SUPPORTED;
3840
3841#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3842 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3843 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3844}
3845
3846psa_status_t mbedtls_psa_verify_hash_start(
3847 mbedtls_psa_verify_hash_interruptible_operation_t *operation,
3848 const psa_key_attributes_t *attributes,
3849 const uint8_t *key_buffer, size_t key_buffer_size,
3850 psa_algorithm_t alg,
3851 const uint8_t *hash, size_t hash_length,
3852 const uint8_t *signature, size_t signature_length)
3853{
3854 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott1bc59df2023-02-05 13:41:57 +00003855 size_t coordinate_bytes = 0;
Paul Elliott0290a762023-02-09 14:30:24 +00003856 size_t required_hash_length = 0;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003857
Paul Elliott068fe072023-01-16 13:59:15 +00003858 if (!PSA_KEY_TYPE_IS_ECC(attributes->core.type)) {
3859 return PSA_ERROR_NOT_SUPPORTED;
3860 }
3861
3862 if (!PSA_ALG_IS_ECDSA(alg)) {
Paul Elliott813f9cd2023-02-05 15:28:46 +00003863 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott068fe072023-01-16 13:59:15 +00003864 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003865
3866#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
Paul Elliott068fe072023-01-16 13:59:15 +00003867 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3868 defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott588f8ed2022-12-02 18:10:26 +00003869
Paul Elliotta1c94092023-02-15 16:38:04 +00003870 mbedtls_ecdsa_restart_init(&operation->restart_ctx);
3871 mbedtls_mpi_init(&operation->r);
3872 mbedtls_mpi_init(&operation->s);
3873
Paul Elliott93d9ca82023-02-15 18:14:21 +00003874 /* Ensure num_ops is zero'ed in case of context re-use. */
3875 operation->num_ops = 0;
3876
Paul Elliott068fe072023-01-16 13:59:15 +00003877 status = mbedtls_psa_ecp_load_representation(attributes->core.type,
3878 attributes->core.bits,
3879 key_buffer,
3880 key_buffer_size,
3881 &operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003882
Paul Elliott068fe072023-01-16 13:59:15 +00003883 if (status != PSA_SUCCESS) {
3884 return status;
3885 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003886
Paul Elliottc569fc22023-02-10 13:02:54 +00003887 coordinate_bytes = PSA_BITS_TO_BYTES(operation->ctx->grp.nbits);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003888
Paul Elliott1bc59df2023-02-05 13:41:57 +00003889 if (signature_length != 2 * coordinate_bytes) {
Paul Elliott068fe072023-01-16 13:59:15 +00003890 return PSA_ERROR_INVALID_SIGNATURE;
3891 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003892
Paul Elliott068fe072023-01-16 13:59:15 +00003893 status = mbedtls_to_psa_error(
3894 mbedtls_mpi_read_binary(&operation->r,
3895 signature,
Paul Elliott1bc59df2023-02-05 13:41:57 +00003896 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003897
Paul Elliott068fe072023-01-16 13:59:15 +00003898 if (status != PSA_SUCCESS) {
3899 return status;
3900 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003901
Paul Elliott068fe072023-01-16 13:59:15 +00003902 status = mbedtls_to_psa_error(
3903 mbedtls_mpi_read_binary(&operation->s,
3904 signature +
Paul Elliott1bc59df2023-02-05 13:41:57 +00003905 coordinate_bytes,
3906 coordinate_bytes));
Paul Elliott588f8ed2022-12-02 18:10:26 +00003907
Paul Elliott068fe072023-01-16 13:59:15 +00003908 if (status != PSA_SUCCESS) {
3909 return status;
3910 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003911
Paul Elliott2c9843f2023-02-15 17:32:42 +00003912 status = mbedtls_psa_ecp_load_public_part(operation->ctx);
Paul Elliott588f8ed2022-12-02 18:10:26 +00003913
Paul Elliott2c9843f2023-02-15 17:32:42 +00003914 if (status != PSA_SUCCESS) {
3915 return status;
Paul Elliott068fe072023-01-16 13:59:15 +00003916 }
Paul Elliott588f8ed2022-12-02 18:10:26 +00003917
Paul Elliott0290a762023-02-09 14:30:24 +00003918 /* We only need to store the same length of hash as the private key size
3919 * here, it would be truncated by the internal implementation anyway. */
3920 required_hash_length = (hash_length < coordinate_bytes ? hash_length :
3921 coordinate_bytes);
3922
Paul Elliottba70ad42023-02-15 18:23:53 +00003923 if (required_hash_length > sizeof(operation->hash)) {
3924 /* Shouldn't happen, but better safe than sorry. */
3925 return PSA_ERROR_CORRUPTION_DETECTED;
3926 }
3927
Paul Elliott0290a762023-02-09 14:30:24 +00003928 memcpy(operation->hash, hash, required_hash_length);
3929 operation->hash_length = required_hash_length;
Paul Elliott068fe072023-01-16 13:59:15 +00003930
3931 return PSA_SUCCESS;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003932#else
Paul Elliott068fe072023-01-16 13:59:15 +00003933 (void) operation;
3934 (void) key_buffer;
3935 (void) key_buffer_size;
3936 (void) alg;
3937 (void) hash;
3938 (void) hash_length;
3939 (void) signature;
3940 (void) signature_length;
3941 (void) status;
Paul Elliott1243f932023-02-07 11:21:10 +00003942 (void) coordinate_bytes;
Paul Elliott0290a762023-02-09 14:30:24 +00003943 (void) required_hash_length;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003944
Paul Elliott068fe072023-01-16 13:59:15 +00003945 return PSA_ERROR_NOT_SUPPORTED;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003946#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3947 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3948 * defined( MBEDTLS_ECP_RESTARTABLE ) */
Paul Elliott588f8ed2022-12-02 18:10:26 +00003949}
3950
3951psa_status_t mbedtls_psa_verify_hash_complete(
3952 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3953{
3954
3955#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3956 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3957 defined(MBEDTLS_ECP_RESTARTABLE)
3958
Paul Elliottf8e5b562023-02-19 18:43:45 +00003959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3960
Paul Elliotta16ce9f2023-02-21 14:19:23 +00003961 /* Ensure max_ops is set to the current value (or default). */
3962 mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());
3963
Paul Elliottf8e5b562023-02-19 18:43:45 +00003964 status = mbedtls_to_psa_error(
Paul Elliott588f8ed2022-12-02 18:10:26 +00003965 mbedtls_ecdsa_verify_restartable(&operation->ctx->grp,
3966 operation->hash,
3967 operation->hash_length,
3968 &operation->ctx->Q,
3969 &operation->r,
3970 &operation->s,
3971 &operation->restart_ctx));
3972
Paul Elliottf8e5b562023-02-19 18:43:45 +00003973 /* Hide the fact that the restart context only holds a delta of number of
3974 * ops done during the last operation, not an absolute value. */
3975 operation->num_ops += operation->restart_ctx.ecp.ops_done;
3976
3977 return status;
Paul Elliott588f8ed2022-12-02 18:10:26 +00003978#else
3979 (void) operation;
3980
3981 return PSA_ERROR_NOT_SUPPORTED;
3982
3983#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3984 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
3985 * defined( MBEDTLS_ECP_RESTARTABLE ) */
3986}
3987
3988psa_status_t mbedtls_psa_verify_hash_abort(
3989 mbedtls_psa_verify_hash_interruptible_operation_t *operation)
3990{
3991
3992#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3993 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
3994 defined(MBEDTLS_ECP_RESTARTABLE)
3995
3996 if (operation->ctx) {
3997 mbedtls_ecdsa_free(operation->ctx);
3998 mbedtls_free(operation->ctx);
Paul Elliottf9c91a72023-02-05 18:06:38 +00003999 operation->ctx = NULL;
Paul Elliott588f8ed2022-12-02 18:10:26 +00004000 }
4001
4002 mbedtls_ecdsa_restart_free(&operation->restart_ctx);
4003
Paul Elliott93d9ca82023-02-15 18:14:21 +00004004 operation->num_ops = 0;
4005
Paul Elliott588f8ed2022-12-02 18:10:26 +00004006 mbedtls_mpi_free(&operation->r);
4007 mbedtls_mpi_free(&operation->s);
4008
4009 return PSA_SUCCESS;
4010
4011#else
4012 (void) operation;
4013
4014 return PSA_ERROR_NOT_SUPPORTED;
4015
4016#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4017 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
4018 * defined( MBEDTLS_ECP_RESTARTABLE ) */
4019}
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004020
mohammad1603503973b2018-03-12 15:59:30 +02004021/****************************************************************/
4022/* Symmetric cryptography */
4023/****************************************************************/
4024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025static psa_status_t psa_cipher_setup(psa_cipher_operation_t *operation,
4026 mbedtls_svc_key_id_t key,
4027 psa_algorithm_t alg,
4028 mbedtls_operation_t cipher_operation)
Ronald Cronab99ac22020-10-01 16:38:28 +02004029{
4030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4031 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01004032 psa_key_slot_t *slot = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 psa_key_usage_t usage = (cipher_operation == MBEDTLS_ENCRYPT ?
4034 PSA_KEY_USAGE_ENCRYPT :
4035 PSA_KEY_USAGE_DECRYPT);
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004036 psa_key_attributes_t attributes;
Ronald Cronab99ac22020-10-01 16:38:28 +02004037
4038 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 if (operation->id != 0) {
Dave Rodgman54648242021-06-24 11:49:45 +01004040 status = PSA_ERROR_BAD_STATE;
4041 goto exit;
4042 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 if (!PSA_ALG_IS_CIPHER(alg)) {
Dave Rodgman54648242021-06-24 11:49:45 +01004045 status = PSA_ERROR_INVALID_ARGUMENT;
4046 goto exit;
4047 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 status = psa_get_and_lock_key_slot_with_policy(key, &slot, usage, alg);
4050 if (status != PSA_SUCCESS) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004051 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004053
Ronald Cron49fafa92021-03-10 08:34:23 +01004054 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02004055 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01004056 * so we only set it (in the driver wrapper) after resources have been
4057 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02004058 operation->iv_set = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 if (alg == PSA_ALG_ECB_NO_PADDING) {
Ronald Cronab99ac22020-10-01 16:38:28 +02004060 operation->iv_required = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 } else {
Ronald Cronab99ac22020-10-01 16:38:28 +02004062 operation->iv_required = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 }
4064 operation->default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
Ronald Cronab99ac22020-10-01 16:38:28 +02004065
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004066 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 .core = slot->attr
Ronald Crona4af55f2020-12-14 14:36:06 +01004068 };
4069
Ronald Cronab99ac22020-10-01 16:38:28 +02004070 /* Try doing the operation through a driver before using software fallback. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 if (cipher_operation == MBEDTLS_ENCRYPT) {
4072 status = psa_driver_wrapper_cipher_encrypt_setup(operation,
4073 &attributes,
4074 slot->key.data,
4075 slot->key.bytes,
4076 alg);
4077 } else {
4078 status = psa_driver_wrapper_cipher_decrypt_setup(operation,
4079 &attributes,
4080 slot->key.data,
4081 slot->key.bytes,
4082 alg);
4083 }
Ronald Cronab99ac22020-10-01 16:38:28 +02004084
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (status != PSA_SUCCESS) {
4087 psa_cipher_abort(operation);
4088 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 return (status == PSA_SUCCESS) ? unlock_status : status;
mohammad1603503973b2018-03-12 15:59:30 +02004093}
4094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
4096 mbedtls_svc_key_id_t key,
4097 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004098{
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 return psa_cipher_setup(operation, key, alg, MBEDTLS_ENCRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004100}
4101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
4103 mbedtls_svc_key_id_t key,
4104 psa_algorithm_t alg)
mohammad16038481e742018-03-18 13:57:31 +02004105{
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 return psa_cipher_setup(operation, key, alg, MBEDTLS_DECRYPT);
mohammad16038481e742018-03-18 13:57:31 +02004107}
4108
Gilles Peskine449bd832023-01-11 14:50:10 +01004109psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
4110 uint8_t *iv,
4111 size_t iv_size,
4112 size_t *iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004113{
Ronald Cron6d051732020-10-01 14:10:20 +02004114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2fb90522021-07-05 12:31:44 +02004115 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4116 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004119 status = PSA_ERROR_BAD_STATE;
4120 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004121 }
4122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004124 status = PSA_ERROR_BAD_STATE;
4125 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004126 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004127
Ronald Cron2fb90522021-07-05 12:31:44 +02004128 default_iv_length = operation->default_iv_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 if (iv_size < default_iv_length) {
Ronald Cron5618a392021-03-26 09:52:26 +01004130 status = PSA_ERROR_BUFFER_TOO_SMALL;
4131 goto exit;
4132 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cron2fb90522021-07-05 12:31:44 +02004135 status = PSA_ERROR_GENERIC_ERROR;
4136 goto exit;
4137 }
4138
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 status = psa_generate_random(local_iv, default_iv_length);
4140 if (status != PSA_SUCCESS) {
Ronald Cron5618a392021-03-26 09:52:26 +01004141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 }
Ronald Cron5618a392021-03-26 09:52:26 +01004143
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 status = psa_driver_wrapper_cipher_set_iv(operation,
4145 local_iv, default_iv_length);
Ronald Cron5618a392021-03-26 09:52:26 +01004146
4147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (status == PSA_SUCCESS) {
4149 memcpy(iv, local_iv, default_iv_length);
Ronald Cron2fb90522021-07-05 12:31:44 +02004150 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02004151 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 } else {
Ronald Cron2fb90522021-07-05 12:31:44 +02004153 *iv_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 psa_cipher_abort(operation);
Ronald Cron2fb90522021-07-05 12:31:44 +02004155 }
Ronald Cron6d051732020-10-01 14:10:20 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004158}
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
4161 const uint8_t *iv,
4162 size_t iv_length)
mohammad1603503973b2018-03-12 15:59:30 +02004163{
Ronald Cron6d051732020-10-01 14:10:20 +02004164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004167 status = PSA_ERROR_BAD_STATE;
4168 goto exit;
4169 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 if (operation->iv_set || !operation->iv_required) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004172 status = PSA_ERROR_BAD_STATE;
4173 goto exit;
4174 }
Ronald Crona0d68172021-03-26 10:15:08 +01004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004177 status = PSA_ERROR_INVALID_ARGUMENT;
4178 goto exit;
4179 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 status = psa_driver_wrapper_cipher_set_iv(operation,
4182 iv,
4183 iv_length);
Steven Cooremand3feccd2020-09-01 15:56:14 +02004184
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (status == PSA_SUCCESS) {
itayzafrir534bd7c2018-08-02 13:56:32 +03004187 operation->iv_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 } else {
4189 psa_cipher_abort(operation);
4190 }
4191 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004192}
4193
Gilles Peskine449bd832023-01-11 14:50:10 +01004194psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
4195 const uint8_t *input,
4196 size_t input_length,
4197 uint8_t *output,
4198 size_t output_size,
4199 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004200{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004204 status = PSA_ERROR_BAD_STATE;
4205 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004206 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004209 status = PSA_ERROR_BAD_STATE;
4210 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004211 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 status = psa_driver_wrapper_cipher_update(operation,
4214 input,
4215 input_length,
4216 output,
4217 output_size,
4218 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004219
4220exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (status != PSA_SUCCESS) {
4222 psa_cipher_abort(operation);
4223 }
Ronald Cron6d051732020-10-01 14:10:20 +02004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 return status;
mohammad1603503973b2018-03-12 15:59:30 +02004226}
4227
Gilles Peskine449bd832023-01-11 14:50:10 +01004228psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
4229 uint8_t *output,
4230 size_t output_size,
4231 size_t *output_length)
mohammad1603503973b2018-03-12 15:59:30 +02004232{
David Saadab4ecc272019-02-14 13:48:10 +02004233 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02004234
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 if (operation->id == 0) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004236 status = PSA_ERROR_BAD_STATE;
4237 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004238 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 if (operation->iv_required && !operation->iv_set) {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004241 status = PSA_ERROR_BAD_STATE;
4242 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02004243 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 status = psa_driver_wrapper_cipher_finish(operation,
4246 output,
4247 output_size,
4248 output_length);
Dave Rodgman38e62ae2021-06-23 11:38:39 +01004249
4250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (status == PSA_SUCCESS) {
4252 return psa_cipher_abort(operation);
4253 } else {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004254 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 (void) psa_cipher_abort(operation);
Janos Follath315b51c2018-07-09 16:04:51 +01004256
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 return status;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004258 }
mohammad1603503973b2018-03-12 15:59:30 +02004259}
4260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation)
Gilles Peskinee553c652018-06-04 16:22:46 +02004262{
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 if (operation->id == 0) {
Steven Cooremana07b9972020-09-10 14:54:14 +02004264 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004265 * in use. It's ok to call abort on such an object, and there's
4266 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 return PSA_SUCCESS;
Gilles Peskine81736312018-06-26 15:04:31 +02004268 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 psa_driver_wrapper_cipher_abort(operation);
Gilles Peskinee553c652018-06-04 16:22:46 +02004271
Ronald Cron49fafa92021-03-10 08:34:23 +01004272 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004273 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004274 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 return PSA_SUCCESS;
mohammad1603503973b2018-03-12 15:59:30 +02004277}
4278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
4280 psa_algorithm_t alg,
4281 const uint8_t *input,
4282 size_t input_length,
4283 uint8_t *output,
4284 size_t output_size,
4285 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004286{
4287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004288 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004289 psa_key_slot_t *slot = NULL;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004290 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
4291 size_t default_iv_length = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004292 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004295 status = PSA_ERROR_INVALID_ARGUMENT;
4296 goto exit;
4297 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4300 PSA_KEY_USAGE_ENCRYPT,
4301 alg);
4302 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004303 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004305
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004306 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004308 };
4309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 default_iv_length = PSA_CIPHER_IV_LENGTH(slot->attr.type, alg);
4311 if (default_iv_length > PSA_CIPHER_IV_MAX_SIZE) {
Ronald Cronc6e6f502021-07-09 18:44:58 +02004312 status = PSA_ERROR_GENERIC_ERROR;
4313 goto exit;
4314 }
4315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (default_iv_length > 0) {
4317 if (output_size < default_iv_length) {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004318 status = PSA_ERROR_BUFFER_TOO_SMALL;
4319 goto exit;
4320 }
4321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 status = psa_generate_random(local_iv, default_iv_length);
4323 if (status != PSA_SUCCESS) {
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004324 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004326 }
4327
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004328 status = psa_driver_wrapper_cipher_encrypt(
4329 &attributes, slot->key.data, slot->key.bytes,
Ronald Cronc6e6f502021-07-09 18:44:58 +02004330 alg, local_iv, default_iv_length, input, input_length,
Ronald Cronafbc7ed2023-01-24 16:02:04 +01004331 psa_crypto_buffer_offset(output, default_iv_length),
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 output_size - default_iv_length, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004333
4334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 unlock_status = psa_unlock_key_slot(slot);
4336 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004337 status = unlock_status;
Ronald Cronc6e6f502021-07-09 18:44:58 +02004338 }
Ronald Cron23919522021-07-08 19:00:07 +02004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (status == PSA_SUCCESS) {
4341 if (default_iv_length > 0) {
4342 memcpy(output, local_iv, default_iv_length);
4343 }
4344 *output_length += default_iv_length;
4345 } else {
4346 *output_length = 0;
4347 }
4348
4349 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004350}
4351
Gilles Peskine449bd832023-01-11 14:50:10 +01004352psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
4353 psa_algorithm_t alg,
4354 const uint8_t *input,
4355 size_t input_length,
4356 uint8_t *output,
4357 size_t output_size,
4358 size_t *output_length)
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004359{
4360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004361 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron23919522021-07-08 19:00:07 +02004362 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004363 psa_key_attributes_t attributes;
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (!PSA_ALG_IS_CIPHER(alg)) {
Ronald Cron23919522021-07-08 19:00:07 +02004366 status = PSA_ERROR_INVALID_ARGUMENT;
4367 goto exit;
4368 }
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 status = psa_get_and_lock_key_slot_with_policy(key, &slot,
4371 PSA_KEY_USAGE_DECRYPT,
4372 alg);
4373 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004376
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004377 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 .core = slot->attr
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004379 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (alg == PSA_ALG_CCM_STAR_NO_TAG &&
4382 input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH(slot->attr.type)) {
Mateusz Starzyk594215b2021-10-14 12:23:06 +02004383 status = PSA_ERROR_INVALID_ARGUMENT;
4384 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 } else if (input_length < PSA_CIPHER_IV_LENGTH(slot->attr.type, alg)) {
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004386 status = PSA_ERROR_INVALID_ARGUMENT;
4387 goto exit;
4388 }
4389
gabor-mezei-arma9449a02021-03-25 11:17:10 +01004390 status = psa_driver_wrapper_cipher_decrypt(
4391 &attributes, slot->key.data, slot->key.bytes,
4392 alg, input, input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 output, output_size, output_length);
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004394
gabor-mezei-arm258ae072021-06-25 15:25:38 +02004395exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 unlock_status = psa_unlock_key_slot(slot);
4397 if (status == PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004398 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 }
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (status != PSA_SUCCESS) {
Ronald Cron23919522021-07-08 19:00:07 +02004402 *output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 }
Ronald Cron23919522021-07-08 19:00:07 +02004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 return status;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01004406}
4407
4408
mohammad16035955c982018-04-26 00:53:03 +03004409/****************************************************************/
4410/* AEAD */
4411/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004412
Paul Elliottbaff51c2021-09-28 17:44:45 +01004413/* Helper function to get the base algorithm from its variants. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004414static psa_algorithm_t psa_aead_get_base_algorithm(psa_algorithm_t alg)
Paul Elliottbaff51c2021-09-28 17:44:45 +01004415{
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004417}
4418
4419/* Helper function to perform common nonce length checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004420static psa_status_t psa_aead_check_nonce_length(psa_algorithm_t alg,
4421 size_t nonce_length)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004422{
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 psa_algorithm_t base_alg = psa_aead_get_base_algorithm(alg);
Paul Elliottbaff51c2021-09-28 17:44:45 +01004424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 switch (base_alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004426#if defined(PSA_WANT_ALG_GCM)
4427 case PSA_ALG_GCM:
4428 /* Not checking max nonce size here as GCM spec allows almost
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 * arbitrarily large nonces. Please note that we do not generally
4430 * recommend the usage of nonces of greater length than
4431 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
4432 * size, which can then lead to collisions if you encrypt a very
4433 * large number of messages.*/
4434 if (nonce_length != 0) {
4435 return PSA_SUCCESS;
4436 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004437 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004438#endif /* PSA_WANT_ALG_GCM */
4439#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004440 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (nonce_length >= 7 && nonce_length <= 13) {
4442 return PSA_SUCCESS;
4443 }
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004444 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004445#endif /* PSA_WANT_ALG_CCM */
4446#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004447 case PSA_ALG_CHACHA20_POLY1305:
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 if (nonce_length == 12) {
4449 return PSA_SUCCESS;
4450 } else if (nonce_length == 8) {
4451 return PSA_ERROR_NOT_SUPPORTED;
4452 }
Bence Szépkúti6d48e202021-11-15 20:04:15 +01004453 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004454#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004455 default:
Przemek Stekiel4c499272022-09-27 13:55:37 +02004456 (void) nonce_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004458 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return PSA_ERROR_INVALID_ARGUMENT;
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004461}
4462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463static psa_status_t psa_aead_check_algorithm(psa_algorithm_t alg)
Bence Szépkútiaa3a6e42022-01-13 16:26:03 +01004464{
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 if (!PSA_ALG_IS_AEAD(alg) || PSA_ALG_IS_WILDCARD(alg)) {
4466 return PSA_ERROR_INVALID_ARGUMENT;
4467 }
Bence Szépkúti08f34652021-12-08 21:07:13 +01004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 return PSA_SUCCESS;
Bence Szépkúti08f34652021-12-08 21:07:13 +01004470}
4471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
4473 psa_algorithm_t alg,
4474 const uint8_t *nonce,
4475 size_t nonce_length,
4476 const uint8_t *additional_data,
4477 size_t additional_data_length,
4478 const uint8_t *plaintext,
4479 size_t plaintext_length,
4480 uint8_t *ciphertext,
4481 size_t ciphertext_size,
4482 size_t *ciphertext_length)
mohammad16035955c982018-04-26 00:53:03 +03004483{
Ronald Cron215633c2021-03-16 17:15:37 +01004484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4485 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004486
mohammad1603f08a5502018-06-03 15:05:47 +03004487 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004488
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 status = psa_aead_check_algorithm(alg);
4490 if (status != PSA_SUCCESS) {
4491 return status;
4492 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004493
Ronald Cron9a986162021-03-26 12:40:07 +01004494 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg);
4496 if (status != PSA_SUCCESS) {
4497 return status;
4498 }
mohammad16035955c982018-04-26 00:53:03 +03004499
Ronald Cron215633c2021-03-16 17:15:37 +01004500 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004502 };
mohammad16035955c982018-04-26 00:53:03 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 status = psa_aead_check_nonce_length(alg, nonce_length);
4505 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004506 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004508
Ronald Cronde822812021-03-17 16:08:20 +01004509 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004510 &attributes, slot->key.data, slot->key.bytes,
4511 alg,
4512 nonce, nonce_length,
4513 additional_data, additional_data_length,
4514 plaintext, plaintext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 ciphertext, ciphertext_size, ciphertext_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (status != PSA_SUCCESS && ciphertext_size != 0) {
4518 memset(ciphertext, 0, ciphertext_size);
4519 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004520
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004521exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 psa_unlock_key_slot(slot);
mohammad16035955c982018-04-26 00:53:03 +03004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 return status;
Gilles Peskineee652a32018-06-01 19:23:52 +02004525}
4526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
4528 psa_algorithm_t alg,
4529 const uint8_t *nonce,
4530 size_t nonce_length,
4531 const uint8_t *additional_data,
4532 size_t additional_data_length,
4533 const uint8_t *ciphertext,
4534 size_t ciphertext_length,
4535 uint8_t *plaintext,
4536 size_t plaintext_size,
4537 size_t *plaintext_length)
mohammad16035955c982018-04-26 00:53:03 +03004538{
Ronald Cron215633c2021-03-16 17:15:37 +01004539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4540 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02004541
Gilles Peskineee652a32018-06-01 19:23:52 +02004542 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 status = psa_aead_check_algorithm(alg);
4545 if (status != PSA_SUCCESS) {
4546 return status;
4547 }
Steven Cooremanea7ab132021-03-17 16:28:00 +01004548
Ronald Cron9a986162021-03-26 12:40:07 +01004549 status = psa_get_and_lock_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 key, &slot, PSA_KEY_USAGE_DECRYPT, alg);
4551 if (status != PSA_SUCCESS) {
4552 return status;
4553 }
mohammad16035955c982018-04-26 00:53:03 +03004554
Ronald Cron215633c2021-03-16 17:15:37 +01004555 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 .core = slot->attr
Ronald Cron215633c2021-03-16 17:15:37 +01004557 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 status = psa_aead_check_nonce_length(alg, nonce_length);
4560 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004561 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004563
Ronald Cronde822812021-03-17 16:08:20 +01004564 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01004565 &attributes, slot->key.data, slot->key.bytes,
4566 alg,
4567 nonce, nonce_length,
4568 additional_data, additional_data_length,
4569 ciphertext, ciphertext_length,
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 plaintext, plaintext_size, plaintext_length);
Gilles Peskinea40d7742018-06-01 16:28:30 +02004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (status != PSA_SUCCESS && plaintext_size != 0) {
4573 memset(plaintext, 0, plaintext_size);
4574 }
mohammad160360a64d02018-06-03 17:20:42 +03004575
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 psa_unlock_key_slot(slot);
Ronald Cron215633c2021-03-16 17:15:37 +01004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 return status;
mohammad16035955c982018-04-26 00:53:03 +03004580}
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582static psa_status_t psa_validate_tag_length(psa_algorithm_t alg)
4583{
4584 const uint8_t tag_len = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
Andrzej Kurekf8816012021-12-19 17:00:12 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) {
Przemek Stekiel86679c72022-10-06 17:06:56 +02004587#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004589 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 if (tag_len < 4 || tag_len > 16 || tag_len % 2) {
4591 return PSA_ERROR_INVALID_ARGUMENT;
4592 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004593 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004594#endif /* PSA_WANT_ALG_CCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004595
Przemek Stekiel86679c72022-10-06 17:06:56 +02004596#if defined(PSA_WANT_ALG_GCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004598 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if (tag_len != 4 && tag_len != 8 && (tag_len < 12 || tag_len > 16)) {
4600 return PSA_ERROR_INVALID_ARGUMENT;
4601 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004602 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004603#endif /* PSA_WANT_ALG_GCM */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004604
Przemek Stekiel86679c72022-10-06 17:06:56 +02004605#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
Andrzej Kurekf8816012021-12-19 17:00:12 +01004607 /* We only support the default tag length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if (tag_len != 16) {
4609 return PSA_ERROR_INVALID_ARGUMENT;
4610 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004611 break;
Przemek Stekiel86679c72022-10-06 17:06:56 +02004612#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Andrzej Kurekf8816012021-12-19 17:00:12 +01004613
4614 default:
4615 (void) tag_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 return PSA_ERROR_NOT_SUPPORTED;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004617 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 return PSA_SUCCESS;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004619}
4620
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004621/* Set the key for a multipart authenticated operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622static psa_status_t psa_aead_setup(psa_aead_operation_t *operation,
4623 int is_encrypt,
4624 mbedtls_svc_key_id_t key,
4625 psa_algorithm_t alg)
Paul Elliottc2b71442021-06-24 18:17:52 +01004626{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4628 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
4629 psa_key_slot_t *slot = NULL;
4630 psa_key_usage_t key_usage = 0;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004631 psa_key_attributes_t attributes;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 status = psa_aead_check_algorithm(alg);
4634 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 }
Paul Elliottc2b71442021-06-24 18:17:52 +01004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 if (operation->id != 0) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004639 status = PSA_ERROR_BAD_STATE;
4640 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004641 }
4642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 if (operation->nonce_set || operation->lengths_set ||
4644 operation->ad_started || operation->body_started) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004645 status = PSA_ERROR_BAD_STATE;
4646 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01004647 }
4648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if (is_encrypt) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004650 key_usage = PSA_KEY_USAGE_ENCRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 } else {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004652 key_usage = PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 status = psa_get_and_lock_key_slot_with_policy(key, &slot, key_usage,
4656 alg);
4657 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004660
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01004661 attributes = (psa_key_attributes_t) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004662 .core = slot->attr
4663 };
4664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if ((status = psa_validate_tag_length(alg)) != PSA_SUCCESS) {
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 }
Przemek Stekiel6ab50762022-10-08 17:54:30 +02004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if (is_encrypt) {
4670 status = psa_driver_wrapper_aead_encrypt_setup(operation,
4671 &attributes,
4672 slot->key.data,
4673 slot->key.bytes,
4674 alg);
4675 } else {
4676 status = psa_driver_wrapper_aead_decrypt_setup(operation,
4677 &attributes,
4678 slot->key.data,
4679 slot->key.bytes,
4680 alg);
4681 }
4682 if (status != PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 operation->key_type = psa_get_key_type(&attributes);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004687
4688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 unlock_status = psa_unlock_key_slot(slot);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if (status == PSA_SUCCESS) {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004692 status = unlock_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 operation->alg = psa_aead_get_base_algorithm(alg);
Paul Elliottd9343f22021-08-23 18:59:49 +01004694 operation->is_encrypt = is_encrypt;
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 } else {
4696 psa_aead_abort(operation);
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004697 }
Paul Elliotte0a12bd2021-08-19 18:55:56 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 return status;
Paul Elliottc2b71442021-06-24 18:17:52 +01004700}
4701
Paul Elliott302ff6b2021-04-20 18:10:30 +01004702/* Set the key for a multipart authenticated encryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004703psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
4704 mbedtls_svc_key_id_t key,
4705 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004706{
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 return psa_aead_setup(operation, 1, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004708}
4709
4710/* Set the key for a multipart authenticated decryption operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004711psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
4712 mbedtls_svc_key_id_t key,
4713 psa_algorithm_t alg)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004714{
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 return psa_aead_setup(operation, 0, key, alg);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004716}
4717
4718/* Generate a random nonce / IV for multipart AEAD operation */
Gilles Peskine449bd832023-01-11 14:50:10 +01004719psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
4720 uint8_t *nonce,
4721 size_t nonce_size,
4722 size_t *nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004723{
4724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Croncae59092021-11-26 18:53:58 +01004725 uint8_t local_nonce[PSA_AEAD_NONCE_MAX_SIZE];
Paul Elliottb91da712021-05-20 14:43:47 +01004726 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004727
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004728 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004731 status = PSA_ERROR_BAD_STATE;
4732 goto exit;
4733 }
4734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (operation->nonce_set || !operation->is_encrypt) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004736 status = PSA_ERROR_BAD_STATE;
4737 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004738 }
4739
Paul Elliotte193ea82021-10-01 13:00:16 +01004740 /* For CCM, this size may not be correct according to the PSA
4741 * specification. The PSA Crypto 1.0.1 specification states:
4742 *
4743 * CCM encodes the plaintext length pLen in L octets, with L the smallest
4744 * integer >= 2 where pLen < 2^(8L). The nonce length is then 15 - L bytes.
4745 *
4746 * However this restriction that L has to be the smallest integer is not
4747 * applied in practice, and it is not implementable here since the
4748 * plaintext length may or may not be known at this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 required_nonce_size = PSA_AEAD_NONCE_LENGTH(operation->key_type,
4750 operation->alg);
4751 if (nonce_size < required_nonce_size) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004752 status = PSA_ERROR_BUFFER_TOO_SMALL;
4753 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004754 }
4755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 status = psa_generate_random(local_nonce, required_nonce_size);
4757 if (status != PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004758 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 }
Paul Elliott302ff6b2021-04-20 18:10:30 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 status = psa_aead_set_nonce(operation, local_nonce, required_nonce_size);
Paul Elliott302ff6b2021-04-20 18:10:30 +01004762
Paul Elliott39dc6b82021-05-11 19:16:09 +01004763exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (status == PSA_SUCCESS) {
4765 memcpy(nonce, local_nonce, required_nonce_size);
Paul Elliott39dc6b82021-05-11 19:16:09 +01004766 *nonce_length = required_nonce_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 } else {
4768 psa_aead_abort(operation);
Ronald Croncae59092021-11-26 18:53:58 +01004769 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004772}
4773
4774/* Set the nonce for a multipart authenticated encryption or decryption
4775 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004776psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
4777 const uint8_t *nonce,
4778 size_t nonce_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004779{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004783 status = PSA_ERROR_BAD_STATE;
4784 goto exit;
4785 }
4786
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004788 status = PSA_ERROR_BAD_STATE;
4789 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004790 }
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_aead_check_nonce_length(operation->alg, nonce_length);
4793 if (status != PSA_SUCCESS) {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01004794 status = PSA_ERROR_INVALID_ARGUMENT;
4795 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01004796 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01004797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 status = psa_driver_wrapper_aead_set_nonce(operation, nonce,
4799 nonce_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004800
Paul Elliott39dc6b82021-05-11 19:16:09 +01004801exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (status == PSA_SUCCESS) {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004803 operation->nonce_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 } else {
4805 psa_aead_abort(operation);
4806 }
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004809}
4810
4811/* Declare the lengths of the message and additional data for multipart AEAD. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
4813 size_t ad_length,
4814 size_t plaintext_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004815{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004816 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004819 status = PSA_ERROR_BAD_STATE;
4820 goto exit;
4821 }
4822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (operation->lengths_set || operation->ad_started ||
4824 operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004825 status = PSA_ERROR_BAD_STATE;
4826 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004827 }
4828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 switch (operation->alg) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004830#if defined(PSA_WANT_ALG_GCM)
4831 case PSA_ALG_GCM:
4832 /* Lengths can only be too large for GCM if size_t is bigger than 32
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 * bits. Without the guard this code will generate warnings on 32bit
4834 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01004835#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (((uint64_t) ad_length) >> 61 != 0 ||
4837 ((uint64_t) plaintext_length) > 0xFFFFFFFE0ull) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004838 status = PSA_ERROR_INVALID_ARGUMENT;
4839 goto exit;
4840 }
Paul Elliott325d3742021-09-27 17:56:28 +01004841#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004842 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004843#endif /* PSA_WANT_ALG_GCM */
4844#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004845 case PSA_ALG_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (ad_length > 0xFF00) {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004847 status = PSA_ERROR_INVALID_ARGUMENT;
4848 goto exit;
4849 }
4850 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004851#endif /* PSA_WANT_ALG_CCM */
4852#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004853 case PSA_ALG_CHACHA20_POLY1305:
4854 /* No length restrictions for ChaChaPoly. */
4855 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004856#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004857 default:
4858 break;
4859 }
Paul Elliott325d3742021-09-27 17:56:28 +01004860
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 status = psa_driver_wrapper_aead_set_lengths(operation, ad_length,
4862 plaintext_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 Elliottee4ffe02021-05-20 17:25:06 +01004866 operation->ad_remaining = ad_length;
4867 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004868 operation->lengths_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 } else {
4870 psa_aead_abort(operation);
Paul Elliottee4ffe02021-05-20 17:25:06 +01004871 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004874}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004875
4876/* Pass additional data to an active multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
4878 const uint8_t *input,
4879 size_t input_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004880{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004884 status = PSA_ERROR_BAD_STATE;
4885 goto exit;
4886 }
4887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (!operation->nonce_set || operation->body_started) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004889 status = PSA_ERROR_BAD_STATE;
4890 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004891 }
4892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 if (operation->lengths_set) {
4894 if (operation->ad_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004895 status = PSA_ERROR_INVALID_ARGUMENT;
4896 goto exit;
4897 }
4898
4899 operation->ad_remaining -= input_length;
4900 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004901#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004903 status = PSA_ERROR_BAD_STATE;
4904 goto exit;
4905 }
4906#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 status = psa_driver_wrapper_aead_update_ad(operation, input,
4909 input_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004910
Paul Elliott39dc6b82021-05-11 19:16:09 +01004911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004913 operation->ad_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 } else {
4915 psa_aead_abort(operation);
4916 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004919}
4920
4921/* Encrypt or decrypt a message fragment in an active multipart AEAD
4922 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004923psa_status_t psa_aead_update(psa_aead_operation_t *operation,
4924 const uint8_t *input,
4925 size_t input_length,
4926 uint8_t *output,
4927 size_t output_size,
4928 size_t *output_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01004929{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004931
4932 *output_length = 0;
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (operation->id == 0) {
Paul Elliottcee785c2021-05-20 14:29:20 +01004935 status = PSA_ERROR_BAD_STATE;
4936 goto exit;
4937 }
4938
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (!operation->nonce_set) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004940 status = PSA_ERROR_BAD_STATE;
4941 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004942 }
4943
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 if (operation->lengths_set) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004945 /* Additional data length was supplied, but not all the additional
4946 data was supplied.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (operation->ad_remaining != 0) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004948 status = PSA_ERROR_INVALID_ARGUMENT;
4949 goto exit;
4950 }
4951
4952 /* Too much data provided. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (operation->body_remaining < input_length) {
Paul Elliottee4ffe02021-05-20 17:25:06 +01004954 status = PSA_ERROR_INVALID_ARGUMENT;
4955 goto exit;
4956 }
4957
4958 operation->body_remaining -= input_length;
4959 }
Paul Elliotte193ea82021-10-01 13:00:16 +01004960#if defined(PSA_WANT_ALG_CCM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 else if (operation->alg == PSA_ALG_CCM) {
Paul Elliotte193ea82021-10-01 13:00:16 +01004962 status = PSA_ERROR_BAD_STATE;
4963 goto exit;
4964 }
4965#endif /* PSA_WANT_ALG_CCM */
Paul Elliottee4ffe02021-05-20 17:25:06 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 status = psa_driver_wrapper_aead_update(operation, input, input_length,
4968 output, output_size,
4969 output_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004970
Paul Elliott39dc6b82021-05-11 19:16:09 +01004971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 if (status == PSA_SUCCESS) {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004973 operation->body_started = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 } else {
4975 psa_aead_abort(operation);
4976 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004979}
4980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981static psa_status_t psa_aead_final_checks(const psa_aead_operation_t *operation)
Paul Elliottad53dcc2021-06-23 08:50:14 +01004982{
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (operation->id == 0 || !operation->nonce_set) {
4984 return PSA_ERROR_BAD_STATE;
4985 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (operation->lengths_set && (operation->ad_remaining != 0 ||
4988 operation->body_remaining != 0)) {
4989 return PSA_ERROR_INVALID_ARGUMENT;
4990 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01004991
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 return PSA_SUCCESS;
Paul Elliottad53dcc2021-06-23 08:50:14 +01004993}
4994
Paul Elliott302ff6b2021-04-20 18:10:30 +01004995/* Finish encrypting a message in a multipart AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004996psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
4997 uint8_t *ciphertext,
4998 size_t ciphertext_size,
4999 size_t *ciphertext_length,
5000 uint8_t *tag,
5001 size_t tag_size,
5002 size_t *tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005003{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5005
Paul Elliott302ff6b2021-04-20 18:10:30 +01005006 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01005007 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 status = psa_aead_final_checks(operation);
5010 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005011 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005013
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (!operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005015 status = PSA_ERROR_BAD_STATE;
5016 goto exit;
5017 }
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 status = psa_driver_wrapper_aead_finish(operation, ciphertext,
5020 ciphertext_size,
5021 ciphertext_length,
5022 tag, tag_size, tag_length);
Paul Elliottcbbde5f2021-05-10 18:19:46 +01005023
Paul Elliott39dc6b82021-05-11 19:16:09 +01005024exit:
Paul Elliottdc42ca82023-02-24 18:11:59 +00005025
5026
Paul Elliottf47b0952021-05-21 18:02:33 +01005027 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01005028 * the zero tag size, make sure the tag is set to something implausible.
5029 * Even if the operation succeeds, make sure we clear the rest of the
5030 * buffer to prevent potential leakage of anything previously placed in
5031 * the same buffer.*/
Paul Elliottdc42ca82023-02-24 18:11:59 +00005032 psa_wipe_tag_output_buffer(tag, status, tag_size, *tag_length);
Paul Elliottf47b0952021-05-21 18:02:33 +01005033
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005037}
5038
5039/* Finish authenticating and decrypting a message in a multipart AEAD
5040 operation.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005041psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
5042 uint8_t *plaintext,
5043 size_t plaintext_size,
5044 size_t *plaintext_length,
5045 const uint8_t *tag,
5046 size_t tag_length)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005047{
Paul Elliott39dc6b82021-05-11 19:16:09 +01005048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5049
Paul Elliott302ff6b2021-04-20 18:10:30 +01005050 *plaintext_length = 0;
5051
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 status = psa_aead_final_checks(operation);
5053 if (status != PSA_SUCCESS) {
Paul Elliottad53dcc2021-06-23 08:50:14 +01005054 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 }
Paul Elliottad53dcc2021-06-23 08:50:14 +01005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (operation->is_encrypt) {
Paul Elliottcee785c2021-05-20 14:29:20 +01005058 status = PSA_ERROR_BAD_STATE;
5059 goto exit;
5060 }
5061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 status = psa_driver_wrapper_aead_verify(operation, plaintext,
5063 plaintext_size,
5064 plaintext_length,
5065 tag, tag_length);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005066
5067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 psa_aead_abort(operation);
Paul Elliott39dc6b82021-05-11 19:16:09 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005071}
5072
5073/* Abort an AEAD operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074psa_status_t psa_aead_abort(psa_aead_operation_t *operation)
Paul Elliott302ff6b2021-04-20 18:10:30 +01005075{
5076 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (operation->id == 0) {
Paul Elliott302ff6b2021-04-20 18:10:30 +01005079 /* The object has (apparently) been initialized but it is not (yet)
5080 * in use. It's ok to call abort on such an object, and there's
5081 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 return PSA_SUCCESS;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005083 }
5084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 status = psa_driver_wrapper_aead_abort(operation);
Paul Elliott302ff6b2021-04-20 18:10:30 +01005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 memset(operation, 0, sizeof(*operation));
Paul Elliott302ff6b2021-04-20 18:10:30 +01005088
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 return status;
Paul Elliott302ff6b2021-04-20 18:10:30 +01005090}
5091
Gilles Peskinee59236f2018-01-27 23:32:46 +01005092/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093/* Generators */
5094/****************************************************************/
5095
Przemek Stekiel69c46792022-06-10 12:59:51 +02005096#if defined(BUILTIN_ALG_ANY_HKDF) || \
John Durkop07cc04a2020-11-16 22:08:34 -08005097 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005098 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) || \
Kusumit Ghoderaoaf0b5342023-05-03 11:58:46 +05305099 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) || \
5100 defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
John Durkop07cc04a2020-11-16 22:08:34 -08005101#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02005102#endif /* At least one builtin KDF */
5103
Przemek Stekiel69c46792022-06-10 12:59:51 +02005104#if defined(BUILTIN_ALG_ANY_HKDF) || \
Steven Cooreman094a77e2021-05-06 17:58:36 +02005105 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5106 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5107static psa_status_t psa_key_derivation_start_hmac(
5108 psa_mac_operation_t *operation,
5109 psa_algorithm_t hash_alg,
5110 const uint8_t *hmac_key,
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 size_t hmac_key_length)
Steven Cooreman094a77e2021-05-06 17:58:36 +02005112{
5113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
5116 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(hmac_key_length));
5117 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
Steven Cooreman094a77e2021-05-06 17:58:36 +02005118
Steven Cooreman72f736a2021-05-07 14:14:37 +02005119 operation->is_sign = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 operation->mac_size = PSA_HASH_LENGTH(hash_alg);
Steven Cooreman72f736a2021-05-07 14:14:37 +02005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 status = psa_driver_wrapper_mac_sign_setup(operation,
5123 &attributes,
5124 hmac_key, hmac_key_length,
5125 PSA_ALG_HMAC(hash_alg));
Steven Cooreman094a77e2021-05-06 17:58:36 +02005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 psa_reset_key_attributes(&attributes);
5128 return status;
Steven Cooreman094a77e2021-05-06 17:58:36 +02005129}
5130#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08005131
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132#define HKDF_STATE_INIT 0 /* no input yet */
5133#define HKDF_STATE_STARTED 1 /* got salt */
5134#define HKDF_STATE_KEYED 2 /* got key */
5135#define HKDF_STATE_OUTPUT 3 /* output started */
5136
Gilles Peskinecbe66502019-05-16 16:59:18 +02005137static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 const psa_key_derivation_operation_t *operation)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005139{
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 if (PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
5141 return PSA_ALG_KEY_AGREEMENT_GET_KDF(operation->alg);
5142 } else {
5143 return operation->alg;
5144 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01005145}
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005148{
5149 psa_status_t status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
5151 if (kdf_alg == 0) {
Gilles Peskineeab56e42018-07-12 17:12:33 +02005152 /* The object has (apparently) been initialized but it is not
5153 * in use. It's ok to call abort on such an object, and there's
5154 * nothing to do. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005156#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5158 mbedtls_free(operation->ctx.hkdf.info);
5159 status = psa_mac_abort(&operation->ctx.hkdf.hmac);
5160 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005161#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005162#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5163 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5165 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
5166 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5167 if (operation->ctx.tls12_prf.secret != NULL) {
5168 mbedtls_platform_zeroize(operation->ctx.tls12_prf.secret,
5169 operation->ctx.tls12_prf.secret_length);
5170 mbedtls_free(operation->ctx.tls12_prf.secret);
Steven Cooremana6df6042021-04-29 19:32:25 +02005171 }
5172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (operation->ctx.tls12_prf.seed != NULL) {
5174 mbedtls_platform_zeroize(operation->ctx.tls12_prf.seed,
5175 operation->ctx.tls12_prf.seed_length);
5176 mbedtls_free(operation->ctx.tls12_prf.seed);
Janos Follath6a1d2622019-06-11 10:37:28 +01005177 }
5178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (operation->ctx.tls12_prf.label != NULL) {
5180 mbedtls_platform_zeroize(operation->ctx.tls12_prf.label,
5181 operation->ctx.tls12_prf.label_length);
5182 mbedtls_free(operation->ctx.tls12_prf.label);
Janos Follath6a1d2622019-06-11 10:37:28 +01005183 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005184#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (operation->ctx.tls12_prf.other_secret != NULL) {
5186 mbedtls_platform_zeroize(operation->ctx.tls12_prf.other_secret,
5187 operation->ctx.tls12_prf.other_secret_length);
5188 mbedtls_free(operation->ctx.tls12_prf.other_secret);
Przemek Stekiele3ee2212022-04-07 14:29:56 +02005189 }
Przemek Stekiel6c764412022-11-22 14:05:12 +01005190#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Steven Cooremana6df6042021-04-29 19:32:25 +02005191 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01005192
5193 /* We leave the fields Ai and output_block to be erased safely by the
5194 * mbedtls_platform_zeroize() in the end of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005196#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5197 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005198#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
5200 mbedtls_platform_zeroize(operation->ctx.tls12_ecjpake_to_pms.data,
5201 sizeof(operation->ctx.tls12_ecjpake_to_pms.data));
5202 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005203#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS) */
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305204#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5205 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305206 if (operation->ctx.pbkdf2.salt != NULL) {
5207 mbedtls_platform_zeroize(operation->ctx.pbkdf2.salt,
5208 operation->ctx.pbkdf2.salt_length);
5209 mbedtls_free(operation->ctx.pbkdf2.salt);
5210 }
Kusumit Ghoderaof5fedf12023-05-03 12:29:48 +05305211
5212 status = PSA_SUCCESS;
5213 } else
5214#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005215 {
5216 status = PSA_ERROR_BAD_STATE;
5217 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 mbedtls_platform_zeroize(operation, sizeof(*operation));
5219 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220}
5221
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 size_t *capacity)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224{
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 /* This is a blank key derivation operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005228 }
5229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 *capacity = operation->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232}
5233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation,
5235 size_t capacity)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236{
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (operation->alg == 0) {
5238 return PSA_ERROR_BAD_STATE;
5239 }
5240 if (capacity > operation->capacity) {
5241 return PSA_ERROR_INVALID_ARGUMENT;
5242 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 operation->capacity = capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 return PSA_SUCCESS;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005245}
5246
Przemek Stekiel69c46792022-06-10 12:59:51 +02005247#if defined(BUILTIN_ALG_ANY_HKDF)
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005248/* Read some bytes from an HKDF-based operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249static psa_status_t psa_key_derivation_hkdf_read(psa_hkdf_key_derivation_t *hkdf,
5250 psa_algorithm_t kdf_alg,
5251 uint8_t *output,
5252 size_t output_length)
Gilles Peskinebef7f142018-07-12 17:22:21 +02005253{
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
5255 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005256 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005257 psa_status_t status;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005258#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ? 0 : 0xff;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005260#else
5261 const uint8_t last_block = 0xff;
5262#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (hkdf->state < HKDF_STATE_KEYED ||
5265 (!hkdf->info_set
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 && !PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02005268#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 )) {
5270 return PSA_ERROR_BAD_STATE;
5271 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005272 hkdf->state = HKDF_STATE_OUTPUT;
5273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 while (output_length != 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005275 /* Copy what remains of the current block */
5276 uint8_t n = hash_length - hkdf->offset_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 if (n > output_length) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005278 n = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 }
5280 memcpy(output, hkdf->output_block + hkdf->offset_in_block, n);
Gilles Peskinebef7f142018-07-12 17:22:21 +02005281 output += n;
5282 output_length -= n;
5283 hkdf->offset_in_block += n;
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (output_length == 0) {
Gilles Peskinebef7f142018-07-12 17:22:21 +02005285 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 }
Przemek Stekiel03d948c2022-05-19 11:45:20 +02005287 /* We can't be wanting more output after the last block, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005288 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005290 * object was corrupted or if this function is called directly
5291 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (hkdf->block_number == last_block) {
5293 return PSA_ERROR_BAD_STATE;
5294 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295
5296 /* We need a new block */
5297 ++hkdf->block_number;
5298 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 status = psa_key_derivation_start_hmac(&hkdf->hmac,
5301 hash_alg,
5302 hkdf->prk,
5303 hash_length);
5304 if (status != PSA_SUCCESS) {
5305 return status;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005306 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005307
5308 if (hkdf->block_number != 1) {
5309 status = psa_mac_update(&hkdf->hmac,
5310 hkdf->output_block,
5311 hash_length);
5312 if (status != PSA_SUCCESS) {
5313 return status;
5314 }
5315 }
5316 status = psa_mac_update(&hkdf->hmac,
5317 hkdf->info,
5318 hkdf->info_length);
5319 if (status != PSA_SUCCESS) {
5320 return status;
5321 }
5322 status = psa_mac_update(&hkdf->hmac,
5323 &hkdf->block_number, 1);
5324 if (status != PSA_SUCCESS) {
5325 return status;
5326 }
5327 status = psa_mac_sign_finish(&hkdf->hmac,
5328 hkdf->output_block,
5329 sizeof(hkdf->output_block),
5330 &hmac_output_length);
5331 if (status != PSA_SUCCESS) {
5332 return status;
5333 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005334 }
5335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 return PSA_SUCCESS;
Gilles Peskinebef7f142018-07-12 17:22:21 +02005337}
Przemek Stekiel69c46792022-06-10 12:59:51 +02005338#endif /* BUILTIN_ALG_ANY_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005339
John Durkop07cc04a2020-11-16 22:08:34 -08005340#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5341 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005342static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5343 psa_tls12_prf_key_derivation_t *tls12_prf,
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 psa_algorithm_t alg)
Janos Follath7742fee2019-06-17 12:58:10 +01005345{
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(alg);
5347 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Steven Cooremana6df6042021-04-29 19:32:25 +02005348 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
5349 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01005350 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005351
Janos Follath7742fee2019-06-17 12:58:10 +01005352 /* We can't be wanting more output after block 0xff, otherwise
5353 * the capacity check in psa_key_derivation_output_bytes() would have
5354 * prevented this call. It could happen only if the operation
5355 * object was corrupted or if this function is called directly
5356 * inside the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 if (tls12_prf->block_number == 0xff) {
5358 return PSA_ERROR_CORRUPTION_DETECTED;
5359 }
Janos Follath7742fee2019-06-17 12:58:10 +01005360
5361 /* We need a new block */
5362 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005363 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005364
5365 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5366 *
5367 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5368 *
5369 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5370 * HMAC_hash(secret, A(2) + seed) +
5371 * HMAC_hash(secret, A(3) + seed) + ...
5372 *
5373 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005375 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005376 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005377 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005378 * is currently extracted as `output_block` and where i is
5379 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005380 */
5381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 status = psa_key_derivation_start_hmac(&hmac,
5383 hash_alg,
5384 tls12_prf->secret,
5385 tls12_prf->secret_length);
5386 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005387 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005389
5390 /* Calculate A(i) where i = tls12_prf->block_number. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 if (tls12_prf->block_number == 1) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005392 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5393 * the variable seed and in this instance means it in the context of the
5394 * P_hash function, where seed = label + seed.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_mac_update(&hmac,
5396 tls12_prf->label,
5397 tls12_prf->label_length);
5398 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005399 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 }
5401 status = psa_mac_update(&hmac,
5402 tls12_prf->seed,
5403 tls12_prf->seed_length);
5404 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005405 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 }
5407 } else {
Janos Follathea29bfb2019-06-19 12:21:20 +01005408 /* A(i) = HMAC_hash(secret, A(i-1)) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5410 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005411 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005413 }
5414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 status = psa_mac_sign_finish(&hmac,
5416 tls12_prf->Ai, hash_length,
5417 &hmac_output_length);
5418 if (hmac_output_length != hash_length) {
Steven Cooremana6df6042021-04-29 19:32:25 +02005419 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 }
5421 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005422 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005424
5425 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 status = psa_key_derivation_start_hmac(&hmac,
5427 hash_alg,
5428 tls12_prf->secret,
5429 tls12_prf->secret_length);
5430 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 }
5433 status = psa_mac_update(&hmac, tls12_prf->Ai, hash_length);
5434 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005435 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 }
5437 status = psa_mac_update(&hmac, tls12_prf->label, tls12_prf->label_length);
5438 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 }
5441 status = psa_mac_update(&hmac, tls12_prf->seed, tls12_prf->seed_length);
5442 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005443 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 }
5445 status = psa_mac_sign_finish(&hmac,
5446 tls12_prf->output_block, hash_length,
5447 &hmac_output_length);
5448 if (status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005449 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005451
Janos Follath7742fee2019-06-17 12:58:10 +01005452
5453cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 cleanup_status = psa_mac_abort(&hmac);
5455 if (status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS) {
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 status = cleanup_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 }
Janos Follathea29bfb2019-06-19 12:21:20 +01005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 return status;
Janos Follath7742fee2019-06-17 12:58:10 +01005460}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005461
Janos Follath844eb0e2019-06-19 12:10:49 +01005462static psa_status_t psa_key_derivation_tls12_prf_read(
5463 psa_tls12_prf_key_derivation_t *tls12_prf,
5464 psa_algorithm_t alg,
5465 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 size_t output_length)
Janos Follath844eb0e2019-06-19 12:10:49 +01005467{
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH(alg);
5469 uint8_t hash_length = PSA_HASH_LENGTH(hash_alg);
Janos Follath844eb0e2019-06-19 12:10:49 +01005470 psa_status_t status;
5471 uint8_t offset, length;
5472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 switch (tls12_prf->state) {
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005474 case PSA_TLS12_PRF_STATE_LABEL_SET:
5475 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
5476 break;
5477 case PSA_TLS12_PRF_STATE_OUTPUT:
5478 break;
5479 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 return PSA_ERROR_BAD_STATE;
Gilles Peskineb1edaec2021-06-11 22:41:46 +02005481 }
5482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 while (output_length != 0) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005484 /* Check if we have fully processed the current block. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 if (tls12_prf->left_in_block == 0) {
5486 status = psa_key_derivation_tls12_prf_generate_next_block(tls12_prf,
5487 alg);
5488 if (status != PSA_SUCCESS) {
5489 return status;
5490 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005491
5492 continue;
5493 }
5494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (tls12_prf->left_in_block > output_length) {
Janos Follath844eb0e2019-06-19 12:10:49 +01005496 length = (uint8_t) output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 } else {
Janos Follath844eb0e2019-06-19 12:10:49 +01005498 length = tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 }
Janos Follath844eb0e2019-06-19 12:10:49 +01005500
5501 offset = hash_length - tls12_prf->left_in_block;
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 memcpy(output, tls12_prf->output_block + offset, length);
Janos Follath844eb0e2019-06-19 12:10:49 +01005503 output += length;
5504 output_length -= length;
5505 tls12_prf->left_in_block -= length;
5506 }
5507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 return PSA_SUCCESS;
Janos Follath844eb0e2019-06-19 12:10:49 +01005509}
John Durkop07cc04a2020-11-16 22:08:34 -08005510#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5511 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005512
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005513#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
5514static psa_status_t psa_key_derivation_tls12_ecjpake_to_pms_read(
5515 psa_tls12_ecjpake_to_pms_t *ecjpake,
5516 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 size_t output_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005518{
5519 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04005520 size_t output_size = 0;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 if (output_length != 32) {
5523 return PSA_ERROR_INVALID_ARGUMENT;
5524 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 status = psa_hash_compute(PSA_ALG_SHA_256, ecjpake->data,
5527 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE, output, output_length,
5528 &output_size);
5529 if (status != PSA_SUCCESS) {
5530 return status;
5531 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 if (output_size != output_length) {
5534 return PSA_ERROR_GENERIC_ERROR;
5535 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005538}
5539#endif
5540
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305541#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5542static psa_status_t psa_key_derivation_pbkdf2_generate_block(
5543 psa_pbkdf2_key_derivation_t *pbkdf2,
5544 psa_algorithm_t prf_alg,
5545 uint8_t prf_output_length,
5546 psa_key_attributes_t *attributes)
5547{
5548 psa_status_t status;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305549 psa_mac_operation_t mac_operation = PSA_MAC_OPERATION_INIT;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305550 size_t mac_output_length;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305551 uint8_t U_i[PSA_MAC_MAX_SIZE];
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305552 uint8_t *U_accumulator = pbkdf2->output_block;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305553 uint64_t i;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305554 uint8_t block_counter[4];
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305555
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305556 mac_operation.is_sign = 1;
5557 mac_operation.mac_size = prf_output_length;
5558 MBEDTLS_PUT_UINT32_BE(pbkdf2->block_number, block_counter, 0);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305559
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305560 status = psa_driver_wrapper_mac_sign_setup(&mac_operation,
5561 attributes,
5562 pbkdf2->password,
5563 pbkdf2->password_length,
5564 prf_alg);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305565 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305566 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305567 }
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305568 status = psa_mac_update(&mac_operation, pbkdf2->salt, pbkdf2->salt_length);
5569 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305570 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305571 }
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305572 status = psa_mac_update(&mac_operation, block_counter, sizeof(block_counter));
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305573 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305574 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305575 }
5576 status = psa_mac_sign_finish(&mac_operation, U_i, sizeof(U_i),
5577 &mac_output_length);
5578 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305579 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305580 }
5581
5582 if (mac_output_length != prf_output_length) {
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305583 status = PSA_ERROR_CORRUPTION_DETECTED;
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305584 goto cleanup;
Kusumit Ghoderaob821a5f2023-06-08 16:35:55 +05305585 }
5586
Kusumit Ghoderao257ea002023-06-14 15:55:11 +05305587 memcpy(U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305588
5589 for (i = 1; i < pbkdf2->input_cost; i++) {
5590 status = psa_driver_wrapper_mac_compute(attributes,
5591 pbkdf2->password,
5592 pbkdf2->password_length,
5593 prf_alg, U_i, prf_output_length,
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305594 U_i, sizeof(U_i),
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305595 &mac_output_length);
5596 if (status != PSA_SUCCESS) {
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305597 goto cleanup;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305598 }
5599
Kusumit Ghoderao109ee3d2023-06-08 16:36:45 +05305600 mbedtls_xor(U_accumulator, U_accumulator, U_i, prf_output_length);
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305601 }
Kusumit Ghoderao246e51f2023-06-15 22:15:43 +05305602
5603cleanup:
5604 /* Zeroise buffers to clear sensitive data from memory. */
5605 mbedtls_platform_zeroize(U_i, PSA_MAC_MAX_SIZE);
5606 return status;
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305607}
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305608
5609static psa_status_t psa_key_derivation_pbkdf2_read(
5610 psa_pbkdf2_key_derivation_t *pbkdf2,
5611 psa_algorithm_t kdf_alg,
5612 uint8_t *output,
5613 size_t output_length)
5614{
5615 psa_status_t status;
5616 psa_algorithm_t prf_alg;
5617 uint8_t prf_output_length;
5618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5619 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(pbkdf2->password_length));
5620 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
5621
5622 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
5623 prf_alg = PSA_ALG_HMAC(PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg));
5624 prf_output_length = PSA_HASH_LENGTH(prf_alg);
5625 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Kusumit Ghoderaod9ec1af2023-06-08 20:19:51 +05305626 } else {
5627 return PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305628 }
5629
5630 switch (pbkdf2->state) {
5631 case PSA_PBKDF2_STATE_PASSWORD_SET:
5632 /* Initially we need a new block so bytes_used is equal to block size*/
5633 pbkdf2->bytes_used = prf_output_length;
5634 pbkdf2->state = PSA_PBKDF2_STATE_OUTPUT;
5635 break;
5636 case PSA_PBKDF2_STATE_OUTPUT:
5637 break;
5638 default:
5639 return PSA_ERROR_BAD_STATE;
5640 }
5641
5642 while (output_length != 0) {
5643 uint8_t n = prf_output_length - pbkdf2->bytes_used;
5644 if (n > output_length) {
5645 n = (uint8_t) output_length;
5646 }
5647 memcpy(output, pbkdf2->output_block + pbkdf2->bytes_used, n);
5648 output += n;
5649 output_length -= n;
5650 pbkdf2->bytes_used += n;
5651
5652 if (output_length == 0) {
5653 break;
5654 }
5655
5656 /* We need a new block */
5657 pbkdf2->bytes_used = 0;
5658 pbkdf2->block_number++;
5659
5660 status = psa_key_derivation_pbkdf2_generate_block(pbkdf2, prf_alg,
5661 prf_output_length,
5662 &attributes);
5663 if (status != PSA_SUCCESS) {
5664 return status;
5665 }
5666 }
5667
5668 return PSA_SUCCESS;
5669}
Kusumit Ghoderaoa4346cd2023-06-05 14:50:20 +05305670#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
5671
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005672psa_status_t psa_key_derivation_output_bytes(
5673 psa_key_derivation_operation_t *operation,
5674 uint8_t *output,
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 size_t output_length)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005676{
5677 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 if (operation->alg == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 /* This is a blank operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 return PSA_ERROR_BAD_STATE;
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005683 }
5684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 if (output_length > operation->capacity) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005687 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005688 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005689 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005690 goto exit;
5691 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 if (output_length == 0 && operation->capacity == 0) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005694 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005695 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5696 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005698 * output_length > 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 return PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005700 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005702
Przemek Stekiel69c46792022-06-10 12:59:51 +02005703#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
5705 status = psa_key_derivation_hkdf_read(&operation->ctx.hkdf, kdf_alg,
5706 output, output_length);
5707 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02005708#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08005709#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5710 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 if (PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
5712 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
5713 status = psa_key_derivation_tls12_prf_read(&operation->ctx.tls12_prf,
5714 kdf_alg, output,
5715 output_length);
5716 } else
John Durkop07cc04a2020-11-16 22:08:34 -08005717#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5718 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005719#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005721 status = psa_key_derivation_tls12_ecjpake_to_pms_read(
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 &operation->ctx.tls12_ecjpake_to_pms, output, output_length);
5723 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005724#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305725#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
5726 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaof6a0d572023-06-05 14:55:56 +05305727 status = psa_key_derivation_pbkdf2_read(&operation->ctx.pbkdf2, kdf_alg,
5728 output, output_length);
Kusumit Ghoderao056f0c52023-05-03 15:58:34 +05305729 } else
5730#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04005731
Gilles Peskineeab56e42018-07-12 17:12:33 +02005732 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005733 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 return PSA_ERROR_BAD_STATE;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005735 }
5736
5737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 if (status != PSA_SUCCESS) {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005739 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005740 * This allows us to differentiate between exhausted operations and
5741 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5742 * operations. */
5743 psa_algorithm_t alg = operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 psa_key_derivation_abort(operation);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 memset(output, '!', output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02005747 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005749}
5750
David Brown7807bf72021-02-09 16:28:23 -07005751#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005752static void psa_des_set_key_parity(uint8_t *data, size_t data_size)
Gilles Peskine08542d82018-07-19 17:05:42 +02005753{
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 if (data_size >= 8) {
5755 mbedtls_des_key_set_parity(data);
5756 }
5757 if (data_size >= 16) {
5758 mbedtls_des_key_set_parity(data + 8);
5759 }
5760 if (data_size >= 24) {
5761 mbedtls_des_key_set_parity(data + 16);
5762 }
Gilles Peskine08542d82018-07-19 17:05:42 +02005763}
David Brown7807bf72021-02-09 16:28:23 -07005764#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005765
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005766/*
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 * ECC keys on a Weierstrass elliptic curve require the generation
5768 * of a private key which is an integer
5769 * in the range [1, N - 1], where N is the boundary of the private key domain:
5770 * N is the prime p for Diffie-Hellman, or the order of the
5771 * curve’s base point for ECC.
5772 *
5773 * Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
5774 * This function generates the private key using the following process:
5775 *
5776 * 1. Draw a byte string of length ceiling(m/8) bytes.
5777 * 2. If m is not a multiple of 8, set the most significant
5778 * (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
5779 * 3. Convert the string to integer k by decoding it as a big-endian byte string.
5780 * 4. If k > N - 2, discard the result and return to step 1.
5781 * 5. Output k + 1 as the private key.
5782 *
5783 * This method allows compliance to NIST standards, specifically the methods titled
5784 * Key-Pair Generation by Testing Candidates in the following publications:
5785 * - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
5786 * Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
5787 * Diffie-Hellman keys.
5788 *
5789 * - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
5790 * Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
5791 *
5792 * Note: Function allocates memory for *data buffer, so given *data should be
5793 * always NULL.
5794 */
Valerio Setti86587ab2023-06-27 13:09:30 +02005795#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
5796#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005797static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005798 psa_key_slot_t *slot,
5799 size_t bits,
5800 psa_key_derivation_operation_t *operation,
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005801 uint8_t **data
5802 )
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005803{
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005804 unsigned key_out_of_range = 1;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005805 mbedtls_mpi k;
5806 mbedtls_mpi diff_N_2;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005808 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005809 size_t m;
5810 size_t m_bytes;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 mbedtls_mpi_init(&k);
5813 mbedtls_mpi_init(&diff_N_2);
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01005814
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005815 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 slot->attr.type);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005817 mbedtls_ecp_group_id grp_id =
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 mbedtls_ecc_group_of_psa(curve, bits, 0);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (grp_id == MBEDTLS_ECP_DP_NONE) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005821 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
Przemyslaw Stekiel871a3362021-12-02 08:46:39 +01005822 goto cleanup;
5823 }
5824
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005825 mbedtls_ecp_group ecp_group;
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 mbedtls_ecp_group_init(&ecp_group);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ecp_group, grp_id));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005829
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005830 /* N is the boundary of the private key domain (ecp_group.N). */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005831 /* Let m be the bit size of N. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005832 m = ecp_group.nbits;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005833
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005834 m_bytes = PSA_BITS_TO_BYTES(m);
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005835
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005836 /* Calculate N - 2 - it will be needed later. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&diff_N_2, &ecp_group.N, 2));
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005838
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005839 /* Note: This function is always called with *data == NULL and it
5840 * allocates memory for the data buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 *data = mbedtls_calloc(1, m_bytes);
5842 if (*data == NULL) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005843 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005844 goto cleanup;
5845 }
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 while (key_out_of_range) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005848 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 if ((status = psa_key_derivation_output_bytes(operation, *data, m_bytes)) != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005850 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 }
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005852
5853 /* 2. If m is not a multiple of 8 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (m % 8 != 0) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005855 /* Set the most significant
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005856 * (8 * ceiling(m/8) - m) bits of the first byte in
5857 * the string to zero.
5858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 uint8_t clear_bit_mask = (1 << (m % 8)) - 1;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005860 (*data)[0] &= clear_bit_mask;
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005861 }
5862
5863 /* 3. Convert the string to integer k by decoding it as a
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 * big-endian byte string.
5865 */
5866 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&k, *data, m_bytes));
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01005867
5868 /* 4. If k > N - 2, discard the result and return to step 1.
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 * Result of comparison is returned. When it indicates error
5870 * then this function is called again.
5871 */
5872 MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(&diff_N_2, &k, &key_out_of_range));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005873 }
5874
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005875 /* 5. Output k + 1 as the private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&k, &k, 1));
5877 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&k, *data, m_bytes));
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005878cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (ret != 0) {
5880 status = mbedtls_to_psa_error(ret);
5881 }
5882 if (status != PSA_SUCCESS) {
5883 mbedtls_free(*data);
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005884 *data = NULL;
5885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 mbedtls_mpi_free(&k);
5887 mbedtls_mpi_free(&diff_N_2);
5888 return status;
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005889}
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005890
5891/* ECC keys on a Montgomery elliptic curve draws a byte string whose length
5892 * is determined by the curve, and sets the mandatory bits accordingly. That is:
5893 *
5894 * - Curve25519 (PSA_ECC_FAMILY_MONTGOMERY, 255 bits):
5895 * draw a 32-byte string and process it as specified in
5896 * Elliptic Curves for Security [RFC7748] §5.
5897 *
5898 * - Curve448 (PSA_ECC_FAMILY_MONTGOMERY, 448 bits):
5899 * draw a 56-byte string and process it as specified in [RFC7748] §5.
5900 *
5901 * Note: Function allocates memory for *data buffer, so given *data should be
5902 * always NULL.
5903 */
5904
5905static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5906 size_t bits,
5907 psa_key_derivation_operation_t *operation,
5908 uint8_t **data
5909 )
5910{
5911 size_t output_length;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005915 case 255:
5916 output_length = 32;
5917 break;
5918 case 448:
5919 output_length = 56;
5920 break;
5921 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 return PSA_ERROR_INVALID_ARGUMENT;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005923 break;
5924 }
5925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 *data = mbedtls_calloc(1, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (*data == NULL) {
5929 return PSA_ERROR_INSUFFICIENT_MEMORY;
5930 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 status = psa_key_derivation_output_bytes(operation, *data, output_length);
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if (status != PSA_SUCCESS) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005935 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 }
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 switch (bits) {
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005939 case 255:
5940 (*data)[0] &= 248;
5941 (*data)[31] &= 127;
5942 (*data)[31] |= 64;
5943 break;
5944 case 448:
5945 (*data)[0] &= 252;
5946 (*data)[55] |= 128;
5947 break;
5948 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 return PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielaeaa4f02022-02-21 08:17:43 +01005950 break;
5951 }
5952
5953 return status;
5954}
Valerio Setti86587ab2023-06-27 13:09:30 +02005955#else /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5956static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
5957 psa_key_slot_t *slot, size_t bits,
5958 psa_key_derivation_operation_t *operation, uint8_t **data)
5959{
5960 (void) slot;
5961 (void) bits;
5962 (void) operation;
5963 (void) data;
5964 return PSA_ERROR_NOT_SUPPORTED;
5965}
5966
5967static psa_status_t psa_generate_derived_ecc_key_montgomery_helper(
5968 size_t bits, psa_key_derivation_operation_t *operation, uint8_t **data)
5969{
5970 (void) bits;
5971 (void) operation;
5972 (void) data;
5973 return PSA_ERROR_NOT_SUPPORTED;
5974}
5975#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
5976#endif /* PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE */
Przemyslaw Stekield8cdcba2021-11-15 12:38:53 +01005977
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005978static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005979 psa_key_slot_t *slot,
5980 size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 psa_key_derivation_operation_t *operation)
Gilles Peskineeab56e42018-07-12 17:12:33 +02005982{
5983 uint8_t *data = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 size_t bytes = PSA_BITS_TO_BYTES(bits);
Archanad8a83dc2021-06-14 10:04:16 +05305985 size_t storage_size = bytes;
Przemek Stekiela81aed22022-03-01 15:13:30 +01005986 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01005987 psa_key_attributes_t attributes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(slot->attr.type)) {
5990 return PSA_ERROR_INVALID_ARGUMENT;
5991 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02005992
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02005993#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) || \
Valerio Settidd24f292023-06-26 12:21:17 +02005994 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE)
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (PSA_KEY_TYPE_IS_ECC(slot->attr.type)) {
5996 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(slot->attr.type);
5997 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(curve)) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01005998 /* Weierstrass elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data);
6000 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel6d3d18b2022-01-20 22:41:17 +01006001 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 }
6003 } else {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006004 /* Montgomery elliptic curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 status = psa_generate_derived_ecc_key_montgomery_helper(bits, operation, &data);
6006 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006007 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 }
Przemyslaw Stekiel58ce8d82021-11-25 13:53:28 +01006009 }
Przemyslaw Stekiel1dfd1222021-11-18 13:35:00 +01006010 } else
Valerio Setti8ffdb5d2023-06-20 13:14:08 +02006011#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_DERIVE) ||
Valerio Settidd24f292023-06-26 12:21:17 +02006012 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_DERIVE) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 if (key_type_is_raw_bytes(slot->attr.type)) {
6014 if (bits % 8 != 0) {
6015 return PSA_ERROR_INVALID_ARGUMENT;
6016 }
6017 data = mbedtls_calloc(1, bytes);
6018 if (data == NULL) {
6019 return PSA_ERROR_INSUFFICIENT_MEMORY;
6020 }
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 status = psa_key_derivation_output_bytes(operation, data, bytes);
6023 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 }
David Brown12ca5032021-02-11 11:02:00 -07006026#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (slot->attr.type == PSA_KEY_TYPE_DES) {
6028 psa_des_set_key_parity(data, bytes);
6029 }
Przemek Stekielf110dc02022-03-01 14:48:05 +01006030#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 } else {
6032 return PSA_ERROR_NOT_SUPPORTED;
Przemyslaw Stekiel1608e332021-11-09 10:46:40 +01006033 }
Ronald Crondd04d422020-11-28 15:14:42 +01006034
Ronald Cronbf33c932020-11-28 18:06:53 +01006035 slot->attr.bits = (psa_key_bits_t) bits;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01006036 attributes = (psa_key_attributes_t) {
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 .core = slot->attr
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01006038 };
6039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (psa_key_lifetime_is_external(attributes.core.lifetime)) {
6041 status = psa_driver_wrapper_get_key_buffer_size(&attributes,
6042 &storage_size);
6043 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306044 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 }
Archanad8a83dc2021-06-14 10:04:16 +05306046 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 status = psa_allocate_buffer_to_slot(slot, storage_size);
6048 if (status != PSA_SUCCESS) {
Archanad8a83dc2021-06-14 10:04:16 +05306049 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 }
Archanad8a83dc2021-06-14 10:04:16 +05306051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 status = psa_driver_wrapper_import_key(&attributes,
6053 data, bytes,
6054 slot->key.data,
6055 slot->key.bytes,
6056 &slot->key.bytes, &bits);
6057 if (bits != slot->attr.bits) {
Ronald Cronbf33c932020-11-28 18:06:53 +01006058 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 }
Gilles Peskineeab56e42018-07-12 17:12:33 +02006060
6061exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 mbedtls_free(data);
6063 return status;
Gilles Peskineeab56e42018-07-12 17:12:33 +02006064}
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
6067 psa_key_derivation_operation_t *operation,
6068 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006069{
6070 psa_status_t status;
6071 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006072 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02006073
Ronald Cron81709fc2020-11-14 12:10:32 +01006074 *key = MBEDTLS_SVC_KEY_ID_INIT;
6075
Gilles Peskine0f84d622019-09-12 19:03:13 +02006076 /* Reject any attempt to create a zero-length key so that we don't
6077 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 if (psa_get_key_bits(attributes) == 0) {
6079 return PSA_ERROR_INVALID_ARGUMENT;
6080 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (operation->alg == PSA_ALG_NONE) {
6083 return PSA_ERROR_BAD_STATE;
6084 }
Dave Rodgmand69da6c2021-11-16 10:32:48 +00006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 if (!operation->can_output_key) {
6087 return PSA_ERROR_NOT_PERMITTED;
6088 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 status = psa_start_key_creation(PSA_KEY_CREATION_DERIVE, attributes,
6091 &slot, &driver);
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006092#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (driver != NULL) {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006094 /* Deriving a key in a secure element is not implemented yet. */
6095 status = PSA_ERROR_NOT_SUPPORTED;
6096 }
6097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 if (status == PSA_SUCCESS) {
6099 status = psa_generate_derived_key_internal(slot,
6100 attributes->core.bits,
6101 operation);
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (status == PSA_SUCCESS) {
6104 status = psa_finish_key_creation(slot, driver, key);
6105 }
6106 if (status != PSA_SUCCESS) {
6107 psa_fail_key_creation(slot, driver);
6108 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 return status;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006111}
6112
Gilles Peskineeab56e42018-07-12 17:12:33 +02006113
6114
6115/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02006116/* Key derivation */
6117/****************************************************************/
6118
Steven Cooreman932ffb72021-02-15 12:14:32 +01006119#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120static int is_kdf_alg_supported(psa_algorithm_t kdf_alg)
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006121{
6122#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 if (PSA_ALG_IS_HKDF(kdf_alg)) {
6124 return 1;
6125 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006126#endif
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006127#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6129 return 1;
6130 }
Przemek Stekielb57a44b2022-06-06 08:33:45 +02006131#endif
6132#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6134 return 1;
6135 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006136#endif
6137#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6139 return 1;
6140 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006141#endif
6142#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6144 return 1;
6145 }
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006146#endif
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006147#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6149 return 1;
6150 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006151#endif
Kusumit Ghoderaod132cac2023-05-03 12:00:27 +05306152#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6153 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6154 return 1;
6155 }
6156#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 return 0;
Gilles Peskine9efde4f2021-04-29 21:10:00 +02006158}
6159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160static psa_status_t psa_hash_try_support(psa_algorithm_t alg)
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006161{
6162 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 psa_status_t status = psa_hash_setup(&operation, alg);
6164 psa_hash_abort(&operation);
6165 return status;
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006166}
6167
Gilles Peskine969c5d62019-01-16 15:53:06 +01006168static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006169 psa_key_derivation_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 psa_algorithm_t kdf_alg)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006171{
Janos Follath5fe19732019-06-20 15:09:30 +01006172 /* Make sure that operation->ctx is properly zero-initialised. (Macro
6173 * initialisers for this union leave some bytes unspecified.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 memset(&operation->ctx, 0, sizeof(operation->ctx));
Janos Follath5fe19732019-06-20 15:09:30 +01006175
Gilles Peskine969c5d62019-01-16 15:53:06 +01006176 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 if (!is_kdf_alg_supported(kdf_alg)) {
6178 return PSA_ERROR_NOT_SUPPORTED;
6179 }
John Durkop07cc04a2020-11-16 22:08:34 -08006180
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006181 /* All currently supported key derivation algorithms (apart from
Andrzej Kurek702776f2022-09-16 06:22:44 -04006182 * ecjpake to pms) are based on a hash algorithm. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
6184 size_t hash_size = PSA_HASH_LENGTH(hash_alg);
6185 if (kdf_alg != PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6186 if (hash_size == 0) {
6187 return PSA_ERROR_NOT_SUPPORTED;
6188 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006189
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006190 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
6191 * we might fail later, which is somewhat unfriendly and potentially
6192 * risk-prone. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 psa_status_t status = psa_hash_try_support(hash_alg);
6194 if (status != PSA_SUCCESS) {
6195 return status;
6196 }
6197 } else {
6198 hash_size = PSA_HASH_LENGTH(PSA_ALG_SHA_256);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006199 }
Gilles Peskine0cc417d2021-04-29 21:18:14 +02006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if ((PSA_ALG_IS_TLS12_PRF(kdf_alg) ||
6202 PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) &&
6203 !(hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384)) {
6204 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006205 }
Andrzej Kurek77638292022-09-16 12:24:52 -04006206#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006207 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg) ||
6209 (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS)) {
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006210 operation->capacity = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 } else
Andrzej Kurekb510cd22022-09-26 10:50:22 -04006212#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
6213 MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 operation->capacity = 255 * hash_size;
6215 return PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006216}
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218static psa_status_t psa_key_agreement_try_support(psa_algorithm_t alg)
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006219{
6220#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 if (alg == PSA_ALG_ECDH) {
6222 return PSA_SUCCESS;
6223 }
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006224#endif
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006225#if defined(PSA_WANT_ALG_FFDH)
6226 if (alg == PSA_ALG_FFDH) {
6227 return PSA_SUCCESS;
6228 }
6229#endif
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006230 (void) alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine0c3a0712021-04-29 21:34:33 +02006232}
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006233
6234static int psa_key_derivation_allows_free_form_secret_input(
6235 psa_algorithm_t kdf_alg)
6236{
6237#if defined(PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS)
6238 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
6239 return 0;
6240 }
6241#endif
6242 (void) kdf_alg;
6243 return 1;
6244}
John Durkop07cc04a2020-11-16 22:08:34 -08006245#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
6248 psa_algorithm_t alg)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006249{
6250 psa_status_t status;
6251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 if (operation->alg != 0) {
6253 return PSA_ERROR_BAD_STATE;
Gilles Peskine969c5d62019-01-16 15:53:06 +01006254 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
6257 return PSA_ERROR_INVALID_ARGUMENT;
6258 } else if (PSA_ALG_IS_KEY_AGREEMENT(alg)) {
6259#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6260 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
6261 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
6262 status = psa_key_agreement_try_support(ka_alg);
6263 if (status != PSA_SUCCESS) {
6264 return status;
6265 }
Gilles Peskinebb3814c2022-12-16 01:12:12 +01006266 if (!psa_key_derivation_allows_free_form_secret_input(kdf_alg)) {
6267 return PSA_ERROR_INVALID_ARGUMENT;
6268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 status = psa_key_derivation_setup_kdf(operation, kdf_alg);
6270#else
6271 return PSA_ERROR_NOT_SUPPORTED;
6272#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6273 } else if (PSA_ALG_IS_KEY_DERIVATION(alg)) {
6274#if defined(AT_LEAST_ONE_BUILTIN_KDF)
6275 status = psa_key_derivation_setup_kdf(operation, alg);
6276#else
6277 return PSA_ERROR_NOT_SUPPORTED;
6278#endif /* AT_LEAST_ONE_BUILTIN_KDF */
6279 } else {
6280 return PSA_ERROR_INVALID_ARGUMENT;
6281 }
6282
6283 if (status == PSA_SUCCESS) {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006284 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 }
6286 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006287}
6288
Przemek Stekiel69c46792022-06-10 12:59:51 +02006289#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006290static psa_status_t psa_hkdf_input(psa_hkdf_key_derivation_t *hkdf,
6291 psa_algorithm_t kdf_alg,
6292 psa_key_derivation_step_t step,
6293 const uint8_t *data,
6294 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006295{
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH(kdf_alg);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006297 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 switch (step) {
Gilles Peskine03410b52019-05-16 16:05:19 +02006299 case PSA_KEY_DERIVATION_INPUT_SALT:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006300#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
6302 return PSA_ERROR_INVALID_ARGUMENT;
6303 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006304#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 if (hkdf->state != HKDF_STATE_INIT) {
6306 return PSA_ERROR_BAD_STATE;
6307 } else {
6308 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6309 hash_alg,
6310 data, data_length);
6311 if (status != PSA_SUCCESS) {
6312 return status;
6313 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006314 hkdf->state = HKDF_STATE_STARTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 return PSA_SUCCESS;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02006316 }
Gilles Peskine03410b52019-05-16 16:05:19 +02006317 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006318#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg)) {
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006320 /* We shouldn't be in different state as HKDF_EXPAND only allows
6321 * two inputs: SECRET (this case) and INFO which does not modify
6322 * the state. It could happen only if the hkdf
6323 * object was corrupted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 if (hkdf->state != HKDF_STATE_INIT) {
6325 return PSA_ERROR_BAD_STATE;
6326 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006327
Przemek Stekiel2fb0dcd2022-05-19 10:34:37 +02006328 /* Allow only input that fits expected prk size */
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 if (data_length != PSA_HASH_LENGTH(hash_alg)) {
6330 return PSA_ERROR_INVALID_ARGUMENT;
6331 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 memcpy(hkdf->prk, data, data_length);
6334 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006335#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006336 {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006337 /* HKDF: If no salt was provided, use an empty salt.
6338 * HKDF-EXTRACT: salt is mandatory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 if (hkdf->state == HKDF_STATE_INIT) {
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006340#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6342 return PSA_ERROR_BAD_STATE;
6343 }
Przemek Stekiel0586f4c2022-06-03 16:00:25 +02006344#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 status = psa_key_derivation_start_hmac(&hkdf->hmac,
6346 hash_alg,
6347 NULL, 0);
6348 if (status != PSA_SUCCESS) {
6349 return status;
6350 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006351 hkdf->state = HKDF_STATE_STARTED;
6352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 if (hkdf->state != HKDF_STATE_STARTED) {
6354 return PSA_ERROR_BAD_STATE;
6355 }
6356 status = psa_mac_update(&hkdf->hmac,
6357 data, data_length);
6358 if (status != PSA_SUCCESS) {
6359 return status;
6360 }
6361 status = psa_mac_sign_finish(&hkdf->hmac,
6362 hkdf->prk,
6363 sizeof(hkdf->prk),
6364 &data_length);
6365 if (status != PSA_SUCCESS) {
6366 return status;
6367 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006368 }
Przemek Stekiel17520fe2022-05-10 13:53:33 +02006369
Gilles Peskine2b522db2019-04-12 15:11:49 +02006370 hkdf->state = HKDF_STATE_KEYED;
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006371 hkdf->block_number = 0;
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006372#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006374 /* The only block of output is the PRK. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 memcpy(hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006376 hkdf->offset_in_block = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 } else
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006378#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006379 {
6380 /* Block 0 is empty, and the next block will be
6381 * generated by psa_key_derivation_hkdf_read(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 hkdf->offset_in_block = PSA_HASH_LENGTH(hash_alg);
Przemek Stekiel03d948c2022-05-19 11:45:20 +02006383 }
6384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 return PSA_SUCCESS;
Gilles Peskine03410b52019-05-16 16:05:19 +02006386 case PSA_KEY_DERIVATION_INPUT_INFO:
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006387#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT)
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 if (PSA_ALG_IS_HKDF_EXTRACT(kdf_alg)) {
6389 return PSA_ERROR_INVALID_ARGUMENT;
6390 }
Przemek Stekiel3e8249c2022-06-03 14:05:07 +02006391#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Przemek Stekielcde3f782022-06-03 16:12:27 +02006392#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (PSA_ALG_IS_HKDF_EXPAND(kdf_alg) &&
6394 hkdf->state == HKDF_STATE_INIT) {
6395 return PSA_ERROR_BAD_STATE;
6396 }
Przemek Stekielcde3f782022-06-03 16:12:27 +02006397#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT */
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 if (hkdf->state == HKDF_STATE_OUTPUT) {
6399 return PSA_ERROR_BAD_STATE;
6400 }
6401 if (hkdf->info_set) {
6402 return PSA_ERROR_BAD_STATE;
6403 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006404 hkdf->info_length = data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (data_length != 0) {
6406 hkdf->info = mbedtls_calloc(1, data_length);
6407 if (hkdf->info == NULL) {
6408 return PSA_ERROR_INSUFFICIENT_MEMORY;
6409 }
6410 memcpy(hkdf->info, data, data_length);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006411 }
6412 hkdf->info_set = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 return PSA_SUCCESS;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006414 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006416 }
6417}
Przemek Stekiel69c46792022-06-10 12:59:51 +02006418#endif /* BUILTIN_ALG_ANY_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01006419
John Durkop07cc04a2020-11-16 22:08:34 -08006420#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
6421 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006422static psa_status_t psa_tls12_prf_set_seed(psa_tls12_prf_key_derivation_t *prf,
6423 const uint8_t *data,
6424 size_t data_length)
Janos Follathf08e2652019-06-13 09:05:41 +01006425{
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 if (prf->state != PSA_TLS12_PRF_STATE_INIT) {
6427 return PSA_ERROR_BAD_STATE;
6428 }
Janos Follathf08e2652019-06-13 09:05:41 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 if (data_length != 0) {
6431 prf->seed = mbedtls_calloc(1, data_length);
6432 if (prf->seed == NULL) {
6433 return PSA_ERROR_INSUFFICIENT_MEMORY;
6434 }
Janos Follathf08e2652019-06-13 09:05:41 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 memcpy(prf->seed, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006437 prf->seed_length = data_length;
6438 }
Janos Follathf08e2652019-06-13 09:05:41 +01006439
Gilles Peskine43f958b2020-12-13 14:55:14 +01006440 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 return PSA_SUCCESS;
Janos Follathf08e2652019-06-13 09:05:41 +01006443}
6444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445static psa_status_t psa_tls12_prf_set_key(psa_tls12_prf_key_derivation_t *prf,
6446 const uint8_t *data,
6447 size_t data_length)
Janos Follath81550542019-06-13 14:26:34 +01006448{
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET &&
6450 prf->state != PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6451 return PSA_ERROR_BAD_STATE;
6452 }
Janos Follath81550542019-06-13 14:26:34 +01006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 if (data_length != 0) {
6455 prf->secret = mbedtls_calloc(1, data_length);
6456 if (prf->secret == NULL) {
6457 return PSA_ERROR_INSUFFICIENT_MEMORY;
6458 }
Steven Cooremana6df6042021-04-29 19:32:25 +02006459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 memcpy(prf->secret, data, data_length);
Steven Cooremana6df6042021-04-29 19:32:25 +02006461 prf->secret_length = data_length;
6462 }
Janos Follath81550542019-06-13 14:26:34 +01006463
Gilles Peskine43f958b2020-12-13 14:55:14 +01006464 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 return PSA_SUCCESS;
Janos Follath81550542019-06-13 14:26:34 +01006467}
6468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469static psa_status_t psa_tls12_prf_set_label(psa_tls12_prf_key_derivation_t *prf,
6470 const uint8_t *data,
6471 size_t data_length)
Janos Follath63028dd2019-06-13 09:15:47 +01006472{
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 if (prf->state != PSA_TLS12_PRF_STATE_KEY_SET) {
6474 return PSA_ERROR_BAD_STATE;
6475 }
Janos Follath63028dd2019-06-13 09:15:47 +01006476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 if (data_length != 0) {
6478 prf->label = mbedtls_calloc(1, data_length);
6479 if (prf->label == NULL) {
6480 return PSA_ERROR_INSUFFICIENT_MEMORY;
6481 }
Janos Follath63028dd2019-06-13 09:15:47 +01006482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 memcpy(prf->label, data, data_length);
Janos Follathd6dce9f2019-07-04 09:11:38 +01006484 prf->label_length = data_length;
6485 }
Janos Follath63028dd2019-06-13 09:15:47 +01006486
Gilles Peskine43f958b2020-12-13 14:55:14 +01006487 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return PSA_SUCCESS;
Janos Follath63028dd2019-06-13 09:15:47 +01006490}
6491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492static psa_status_t psa_tls12_prf_input(psa_tls12_prf_key_derivation_t *prf,
6493 psa_key_derivation_step_t step,
6494 const uint8_t *data,
6495 size_t data_length)
Janos Follathb03233e2019-06-11 15:30:30 +01006496{
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 switch (step) {
Janos Follathf08e2652019-06-13 09:05:41 +01006498 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 return psa_tls12_prf_set_seed(prf, data, data_length);
Janos Follath81550542019-06-13 14:26:34 +01006500 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 return psa_tls12_prf_set_key(prf, data, data_length);
Janos Follath63028dd2019-06-13 09:15:47 +01006502 case PSA_KEY_DERIVATION_INPUT_LABEL:
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 return psa_tls12_prf_set_label(prf, data, data_length);
Janos Follathb03233e2019-06-11 15:30:30 +01006504 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 return PSA_ERROR_INVALID_ARGUMENT;
Janos Follathb03233e2019-06-11 15:30:30 +01006506 }
6507}
John Durkop07cc04a2020-11-16 22:08:34 -08006508#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
6509 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
6510
6511#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6512static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
6513 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08006514 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 size_t data_length)
John Durkop07cc04a2020-11-16 22:08:34 -08006516{
6517 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 const size_t pms_len = (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET ?
6519 4 + data_length + prf->other_secret_length :
6520 4 + 2 * data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 if (data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE) {
6523 return PSA_ERROR_INVALID_ARGUMENT;
6524 }
John Durkop07cc04a2020-11-16 22:08:34 -08006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 uint8_t *pms = mbedtls_calloc(1, pms_len);
6527 if (pms == NULL) {
6528 return PSA_ERROR_INSUFFICIENT_MEMORY;
6529 }
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006530 uint8_t *cur = pms;
6531
6532 /* pure-PSK:
6533 * Quoting RFC 4279, Section 2:
John Durkop07cc04a2020-11-16 22:08:34 -08006534 *
6535 * The premaster secret is formed as follows: if the PSK is N octets
6536 * long, concatenate a uint16 with the value N, N zero octets, a second
6537 * uint16 with the value N, and the PSK itself.
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006538 *
6539 * mixed-PSK:
6540 * In a DHE-PSK, RSA-PSK, ECDHE-PSK the premaster secret is formed as
6541 * follows: concatenate a uint16 with the length of the other secret,
6542 * the other secret itself, uint16 with the length of PSK, and the
6543 * PSK itself.
6544 * For details please check:
6545 * - RFC 4279, Section 4 for the definition of RSA-PSK,
6546 * - RFC 4279, Section 3 for the definition of DHE-PSK,
6547 * - RFC 5489 for the definition of ECDHE-PSK.
John Durkop07cc04a2020-11-16 22:08:34 -08006548 */
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 if (prf->state == PSA_TLS12_PRF_STATE_OTHER_KEY_SET) {
6551 *cur++ = MBEDTLS_BYTE_1(prf->other_secret_length);
6552 *cur++ = MBEDTLS_BYTE_0(prf->other_secret_length);
6553 if (prf->other_secret_length != 0) {
6554 memcpy(cur, prf->other_secret, prf->other_secret_length);
6555 mbedtls_platform_zeroize(prf->other_secret, prf->other_secret_length);
Przemek Stekiel2503f7e2022-04-12 12:08:01 +02006556 cur += prf->other_secret_length;
6557 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 } else {
6559 *cur++ = MBEDTLS_BYTE_1(data_length);
6560 *cur++ = MBEDTLS_BYTE_0(data_length);
6561 memset(cur, 0, data_length);
Przemek Stekielc8fa5a12022-04-07 14:17:13 +02006562 cur += data_length;
6563 }
6564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 *cur++ = MBEDTLS_BYTE_1(data_length);
6566 *cur++ = MBEDTLS_BYTE_0(data_length);
6567 memcpy(cur, data, data_length);
John Durkop07cc04a2020-11-16 22:08:34 -08006568 cur += data_length;
6569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 status = psa_tls12_prf_set_key(prf, pms, cur - pms);
John Durkop07cc04a2020-11-16 22:08:34 -08006571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 mbedtls_platform_zeroize(pms, pms_len);
6573 mbedtls_free(pms);
6574 return status;
John Durkop07cc04a2020-11-16 22:08:34 -08006575}
Janos Follath6660f0e2019-06-17 08:44:03 +01006576
Przemek Stekiele47201b2022-04-19 13:53:28 +02006577static psa_status_t psa_tls12_prf_psk_to_ms_set_other_key(
6578 psa_tls12_prf_key_derivation_t *prf,
6579 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 size_t data_length)
Przemek Stekiele47201b2022-04-19 13:53:28 +02006581{
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 if (prf->state != PSA_TLS12_PRF_STATE_SEED_SET) {
6583 return PSA_ERROR_BAD_STATE;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006584 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006585
6586 if (data_length != 0) {
6587 prf->other_secret = mbedtls_calloc(1, data_length);
6588 if (prf->other_secret == NULL) {
6589 return PSA_ERROR_INSUFFICIENT_MEMORY;
6590 }
6591
6592 memcpy(prf->other_secret, data, data_length);
6593 prf->other_secret_length = data_length;
6594 } else {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006595 prf->other_secret_length = 0;
6596 }
6597
6598 prf->state = PSA_TLS12_PRF_STATE_OTHER_KEY_SET;
6599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 return PSA_SUCCESS;
Przemek Stekiele47201b2022-04-19 13:53:28 +02006601}
6602
Janos Follath6660f0e2019-06-17 08:44:03 +01006603static psa_status_t psa_tls12_prf_psk_to_ms_input(
6604 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01006605 psa_key_derivation_step_t step,
6606 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 size_t data_length)
Janos Follath6660f0e2019-06-17 08:44:03 +01006608{
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 switch (step) {
Przemek Stekiele47201b2022-04-19 13:53:28 +02006610 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 return psa_tls12_prf_psk_to_ms_set_key(prf,
6612 data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006613 break;
6614 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 return psa_tls12_prf_psk_to_ms_set_other_key(prf,
6616 data,
6617 data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006618 break;
6619 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 return psa_tls12_prf_input(prf, step, data, data_length);
Przemek Stekiele47201b2022-04-19 13:53:28 +02006621 break;
Janos Follath6660f0e2019-06-17 08:44:03 +01006622
Przemek Stekiele47201b2022-04-19 13:53:28 +02006623 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006624}
John Durkop07cc04a2020-11-16 22:08:34 -08006625#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006626
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006627#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
6628static psa_status_t psa_tls12_ecjpake_to_pms_input(
6629 psa_tls12_ecjpake_to_pms_t *ecjpake,
Andrzej Kurek702776f2022-09-16 06:22:44 -04006630 psa_key_derivation_step_t step,
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006631 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 size_t data_length)
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006633{
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (data_length != PSA_TLS12_ECJPAKE_TO_PMS_INPUT_SIZE ||
6635 step != PSA_KEY_DERIVATION_INPUT_SECRET) {
6636 return PSA_ERROR_INVALID_ARGUMENT;
Andrzej Kurek5603efd2022-09-26 10:49:16 -04006637 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006638
6639 /* Check if the passed point is in an uncompressed form */
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 if (data[0] != 0x04) {
6641 return PSA_ERROR_INVALID_ARGUMENT;
6642 }
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006643
6644 /* Only K.X has to be extracted - bytes 1 to 32 inclusive. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 memcpy(ecjpake->data, data + 1, PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return PSA_SUCCESS;
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006648}
6649#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306650
6651#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6652static psa_status_t psa_pbkdf2_set_input_cost(
6653 psa_pbkdf2_key_derivation_t *pbkdf2,
6654 psa_key_derivation_step_t step,
6655 uint64_t data)
6656{
6657 if (step != PSA_KEY_DERIVATION_INPUT_COST) {
6658 return PSA_ERROR_INVALID_ARGUMENT;
6659 }
6660
6661 if (pbkdf2->state != PSA_PBKDF2_STATE_INIT) {
6662 return PSA_ERROR_BAD_STATE;
6663 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306664
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306665 if (data > PSA_VENDOR_PBKDF2_MAX_ITERATIONS) {
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306666 return PSA_ERROR_NOT_SUPPORTED;
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306667 }
Kusumit Ghoderao3fc4ca72023-05-08 15:57:41 +05306668
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306669 if (data == 0) {
6670 return PSA_ERROR_INVALID_ARGUMENT;
6671 }
Kusumit Ghoderaoe66a8ad2023-05-24 12:30:43 +05306672
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306673 pbkdf2->input_cost = data;
6674 pbkdf2->state = PSA_PBKDF2_STATE_INPUT_COST_SET;
6675
6676 return PSA_SUCCESS;
6677}
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306678
6679static psa_status_t psa_pbkdf2_set_salt(psa_pbkdf2_key_derivation_t *pbkdf2,
6680 const uint8_t *data,
6681 size_t data_length)
6682{
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306683 if (pbkdf2->state != PSA_PBKDF2_STATE_INPUT_COST_SET &&
6684 pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6685 return PSA_ERROR_BAD_STATE;
6686 }
6687
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306688 if (pbkdf2->state == PSA_PBKDF2_STATE_INPUT_COST_SET) {
6689 pbkdf2->salt = mbedtls_calloc(1, data_length);
6690 if (pbkdf2->salt == NULL) {
6691 return PSA_ERROR_INSUFFICIENT_MEMORY;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306692 }
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306693
6694 memcpy(pbkdf2->salt, data, data_length);
6695 pbkdf2->salt_length = data_length;
6696 } else if (pbkdf2->state == PSA_PBKDF2_STATE_SALT_SET) {
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306697 uint8_t *next_salt;
Kusumit Ghoderaob538bb72023-05-24 12:32:14 +05306698
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306699 next_salt = mbedtls_calloc(1, data_length + pbkdf2->salt_length);
6700 if (next_salt == NULL) {
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306701 return PSA_ERROR_INSUFFICIENT_MEMORY;
6702 }
6703
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306704 memcpy(next_salt, pbkdf2->salt, pbkdf2->salt_length);
6705 memcpy(next_salt + pbkdf2->salt_length, data, data_length);
Kusumit Ghoderaod0422f32023-05-08 15:56:19 +05306706 pbkdf2->salt_length += data_length;
Kusumit Ghoderaod7a3f802023-05-24 22:19:47 +05306707 mbedtls_free(pbkdf2->salt);
6708 pbkdf2->salt = next_salt;
Kusumit Ghoderao547a6c62023-05-03 12:18:19 +05306709 }
6710
6711 pbkdf2->state = PSA_PBKDF2_STATE_SALT_SET;
6712
6713 return PSA_SUCCESS;
6714}
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306715
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306716static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306717 const uint8_t *input,
6718 size_t input_len,
6719 uint8_t *output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306720 size_t *output_len)
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306721{
6722 psa_status_t status = PSA_SUCCESS;
6723 if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
6724 status = psa_hash_compute(hash_alg, input, input_len, output,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306725 PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306726 } else {
6727 memcpy(output, input, input_len);
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306728 *output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306729 }
6730 return status;
6731}
6732
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306733static psa_status_t psa_pbkdf2_set_password(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306734 psa_algorithm_t kdf_alg,
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306735 const uint8_t *data,
6736 size_t data_length)
6737{
Kusumit Ghoderaoaac9a582023-05-24 14:19:17 +05306738 psa_status_t status = PSA_SUCCESS;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306739 if (pbkdf2->state != PSA_PBKDF2_STATE_SALT_SET) {
6740 return PSA_ERROR_BAD_STATE;
6741 }
6742
6743 if (data_length != 0) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306744 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6745 psa_algorithm_t hash_alg = PSA_ALG_PBKDF2_HMAC_GET_HASH(kdf_alg);
6746 status = psa_pbkdf2_hmac_set_password(hash_alg, data, data_length,
6747 pbkdf2->password,
Kusumit Ghoderao91f99f52023-05-24 22:21:48 +05306748 &pbkdf2->password_length);
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306749 }
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306750 }
6751
6752 pbkdf2->state = PSA_PBKDF2_STATE_PASSWORD_SET;
6753
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306754 return status;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306755}
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306756
6757static psa_status_t psa_pbkdf2_input(psa_pbkdf2_key_derivation_t *pbkdf2,
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306758 psa_algorithm_t kdf_alg,
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306759 psa_key_derivation_step_t step,
6760 const uint8_t *data,
6761 size_t data_length)
6762{
6763 switch (step) {
6764 case PSA_KEY_DERIVATION_INPUT_SALT:
6765 return psa_pbkdf2_set_salt(pbkdf2, data, data_length);
6766 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306767 return psa_pbkdf2_set_password(pbkdf2, kdf_alg, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306768 default:
6769 return PSA_ERROR_INVALID_ARGUMENT;
6770 }
6771}
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306772#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
6773
Gilles Peskineb8965192019-09-24 16:21:10 +02006774/** Check whether the given key type is acceptable for the given
6775 * input step of a key derivation.
6776 *
6777 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6778 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6779 * Both secret and non-secret inputs can alternatively have the type
6780 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6781 * that the input was passed as a buffer rather than via a key object.
6782 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006783static int psa_key_derivation_check_input_type(
6784 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 psa_key_type_t key_type)
Gilles Peskine224b0d62019-09-23 18:13:17 +02006786{
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 switch (step) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006788 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 if (key_type == PSA_KEY_TYPE_DERIVE) {
6790 return PSA_SUCCESS;
6791 }
6792 if (key_type == PSA_KEY_TYPE_NONE) {
6793 return PSA_SUCCESS;
6794 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006795 break;
Przemek Stekiela7695a22022-04-07 15:39:01 +02006796 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 if (key_type == PSA_KEY_TYPE_DERIVE) {
6798 return PSA_SUCCESS;
6799 }
6800 if (key_type == PSA_KEY_TYPE_NONE) {
6801 return PSA_SUCCESS;
6802 }
Przemek Stekiela7695a22022-04-07 15:39:01 +02006803 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006804 case PSA_KEY_DERIVATION_INPUT_LABEL:
6805 case PSA_KEY_DERIVATION_INPUT_SALT:
6806 case PSA_KEY_DERIVATION_INPUT_INFO:
6807 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 if (key_type == PSA_KEY_TYPE_RAW_DATA) {
6809 return PSA_SUCCESS;
6810 }
6811 if (key_type == PSA_KEY_TYPE_NONE) {
6812 return PSA_SUCCESS;
6813 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006814 break;
Kusumit Ghoderaof4fe3ee2023-05-03 12:21:07 +05306815 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
6816 if (key_type == PSA_KEY_TYPE_PASSWORD) {
6817 return PSA_SUCCESS;
6818 }
6819 if (key_type == PSA_KEY_TYPE_DERIVE) {
6820 return PSA_SUCCESS;
6821 }
6822 if (key_type == PSA_KEY_TYPE_NONE) {
6823 return PSA_SUCCESS;
6824 }
6825 break;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 return PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine224b0d62019-09-23 18:13:17 +02006828}
6829
Janos Follathb80a94e2019-06-12 15:54:46 +01006830static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006831 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006832 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006833 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006834 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 size_t data_length)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006836{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006837 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 status = psa_key_derivation_check_input_type(step, key_type);
6841 if (status != PSA_SUCCESS) {
Gilles Peskine224b0d62019-09-23 18:13:17 +02006842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 }
Gilles Peskine224b0d62019-09-23 18:13:17 +02006844
Przemek Stekiel69c46792022-06-10 12:59:51 +02006845#if defined(BUILTIN_ALG_ANY_HKDF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 if (PSA_ALG_IS_ANY_HKDF(kdf_alg)) {
6847 status = psa_hkdf_input(&operation->ctx.hkdf, kdf_alg,
6848 step, data, data_length);
6849 } else
Przemek Stekiel69c46792022-06-10 12:59:51 +02006850#endif /* BUILTIN_ALG_ANY_HKDF */
John Durkop07cc04a2020-11-16 22:08:34 -08006851#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 if (PSA_ALG_IS_TLS12_PRF(kdf_alg)) {
6853 status = psa_tls12_prf_input(&operation->ctx.tls12_prf,
6854 step, data, data_length);
6855 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006856#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6857#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 if (PSA_ALG_IS_TLS12_PSK_TO_MS(kdf_alg)) {
6859 status = psa_tls12_prf_psk_to_ms_input(&operation->ctx.tls12_prf,
6860 step, data, data_length);
6861 } else
John Durkop07cc04a2020-11-16 22:08:34 -08006862#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006863#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (kdf_alg == PSA_ALG_TLS12_ECJPAKE_TO_PMS) {
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006865 status = psa_tls12_ecjpake_to_pms_input(
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 &operation->ctx.tls12_ecjpake_to_pms, step, data, data_length);
6867 } else
Andrzej Kurek08d34b82022-07-29 10:00:16 -04006868#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306869#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6870 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
Kusumit Ghoderaobd6cefb2023-05-24 12:36:34 +05306871 status = psa_pbkdf2_input(&operation->ctx.pbkdf2, kdf_alg,
6872 step, data, data_length);
Kusumit Ghoderao24b38952023-05-03 12:25:26 +05306873 } else
6874#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006875 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006876 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01006877 (void) data;
6878 (void) data_length;
6879 (void) kdf_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 return PSA_ERROR_BAD_STATE;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006881 }
6882
Gilles Peskine224b0d62019-09-23 18:13:17 +02006883exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if (status != PSA_SUCCESS) {
6885 psa_key_derivation_abort(operation);
6886 }
6887 return status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006888}
6889
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306890static psa_status_t psa_key_derivation_input_integer_internal(
6891 psa_key_derivation_operation_t *operation,
6892 psa_key_derivation_step_t step,
6893 uint64_t value)
6894{
6895 psa_status_t status;
6896 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
6897
Kusumit Ghoderao944bba12023-05-03 12:14:03 +05306898#if defined(MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC)
6899 if (PSA_ALG_IS_PBKDF2_HMAC(kdf_alg)) {
6900 status = psa_pbkdf2_set_input_cost(
6901 &operation->ctx.pbkdf2, step, value);
6902 } else
6903#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306904 {
Kusumit Ghoderao3a18dee2023-04-07 16:16:27 +05306905 (void) step;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306906 (void) value;
6907 (void) kdf_alg;
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +05306908 status = PSA_ERROR_INVALID_ARGUMENT;
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306909 }
6910
6911 if (status != PSA_SUCCESS) {
6912 psa_key_derivation_abort(operation);
6913 }
6914 return status;
6915}
6916
Janos Follath51f4a0f2019-06-14 11:35:55 +01006917psa_status_t psa_key_derivation_input_bytes(
6918 psa_key_derivation_operation_t *operation,
6919 psa_key_derivation_step_t step,
6920 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 size_t data_length)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006922{
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 return psa_key_derivation_input_internal(operation, step,
6924 PSA_KEY_TYPE_NONE,
6925 data, data_length);
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006926}
6927
Kusumit Ghoderao50e0e112023-04-06 17:47:25 +05306928psa_status_t psa_key_derivation_input_integer(
6929 psa_key_derivation_operation_t *operation,
6930 psa_key_derivation_step_t step,
6931 uint64_t value)
6932{
6933 return psa_key_derivation_input_integer_internal(operation, step, value);
6934}
6935
Janos Follath51f4a0f2019-06-14 11:35:55 +01006936psa_status_t psa_key_derivation_input_key(
6937 psa_key_derivation_operation_t *operation,
6938 psa_key_derivation_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 mbedtls_svc_key_id_t key)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006940{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006942 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006943 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006944
Ronald Cron5c522922020-11-14 16:35:34 +01006945 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
6947 if (status != PSA_SUCCESS) {
6948 psa_key_derivation_abort(operation);
6949 return status;
Gilles Peskine593773d2019-09-23 18:17:40 +02006950 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006951
Kusumit Ghoderao3128c5d2023-05-03 12:27:57 +05306952 /* Passing a key object as a SECRET or PASSWORD input unlocks the
6953 * permission to output to a key object. */
6954 if (step == PSA_KEY_DERIVATION_INPUT_SECRET ||
6955 step == PSA_KEY_DERIVATION_INPUT_PASSWORD) {
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006956 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 status = psa_key_derivation_input_internal(operation,
6960 step, slot->attr.type,
6961 slot->key.data,
6962 slot->key.bytes);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006967}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006968
6969
6970
6971/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006972/* Key agreement */
6973/****************************************************************/
6974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes,
6976 const uint8_t *key_buffer,
6977 size_t key_buffer_size,
6978 psa_algorithm_t alg,
6979 const uint8_t *peer_key,
6980 size_t peer_key_length,
6981 uint8_t *shared_secret,
6982 size_t shared_secret_size,
6983 size_t *shared_secret_length)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006984{
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 switch (alg) {
John Durkop6ba40d12020-11-10 08:50:04 -08006986#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006987 case PSA_ALG_ECDH:
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 return mbedtls_psa_key_agreement_ecdh(attributes, key_buffer,
6989 key_buffer_size, alg,
6990 peer_key, peer_key_length,
6991 shared_secret,
6992 shared_secret_size,
6993 shared_secret_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02006994#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Przemek Stekielfb3dd542022-12-01 14:59:15 +01006995
6996#if defined(MBEDTLS_PSA_BUILTIN_ALG_FFDH)
6997 case PSA_ALG_FFDH:
Przemek Stekiel152bb462023-06-01 11:52:39 +02006998 return mbedtls_psa_ffdh_key_agreement(attributes,
Przemek Stekiel359f4622022-12-05 14:11:55 +01006999 peer_key,
7000 peer_key_length,
7001 key_buffer,
7002 key_buffer_size,
7003 shared_secret,
7004 shared_secret_size,
7005 shared_secret_length);
Przemek Stekielfb3dd542022-12-01 14:59:15 +01007006#endif /* MBEDTLS_PSA_BUILTIN_ALG_FFDH */
7007
Gilles Peskine01d718c2018-09-18 12:01:02 +02007008 default:
Aditya Deshpande17845b82022-10-13 17:21:01 +01007009 (void) attributes;
7010 (void) key_buffer;
7011 (void) key_buffer_size;
Gilles Peskine93f85002018-11-16 16:43:31 +01007012 (void) peer_key;
7013 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007014 (void) shared_secret;
7015 (void) shared_secret_size;
7016 (void) shared_secret_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 return PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007018 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007019}
Gilles Peskine01d718c2018-09-18 12:01:02 +02007020
Aditya Deshpande17845b82022-10-13 17:21:01 +01007021/** Internal function for raw key agreement
7022 * Calls the driver wrapper which will hand off key agreement task
Aditya Deshpande40c05cc2022-10-14 16:41:40 +01007023 * to the driver's implementation if a driver is present.
7024 * Fallback specified in the driver wrapper is built-in raw key agreement
Aditya Deshpande17845b82022-10-13 17:21:01 +01007025 * (psa_key_agreement_raw_builtin).
7026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007027static psa_status_t psa_key_agreement_raw_internal(psa_algorithm_t alg,
7028 psa_key_slot_t *private_key,
7029 const uint8_t *peer_key,
7030 size_t peer_key_length,
7031 uint8_t *shared_secret,
7032 size_t shared_secret_size,
7033 size_t *shared_secret_length)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007034{
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (!PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
7036 return PSA_ERROR_NOT_SUPPORTED;
7037 }
Aditya Deshpande17845b82022-10-13 17:21:01 +01007038
7039 psa_key_attributes_t attributes = {
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 .core = private_key->attr
Aditya Deshpande17845b82022-10-13 17:21:01 +01007041 };
7042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 return psa_driver_wrapper_key_agreement(&attributes,
7044 private_key->key.data,
7045 private_key->key.bytes, alg,
7046 peer_key, peer_key_length,
7047 shared_secret,
7048 shared_secret_size,
7049 shared_secret_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007050}
7051
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007052/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02007053 * to potentially free embedded data structures and wipe confidential data.
7054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007055static psa_status_t psa_key_agreement_internal(psa_key_derivation_operation_t *operation,
7056 psa_key_derivation_step_t step,
7057 psa_key_slot_t *private_key,
7058 const uint8_t *peer_key,
7059 size_t peer_key_length)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007060{
7061 psa_status_t status;
Aditya Deshpande2f7fd762022-11-22 11:10:34 +00007062 uint8_t shared_secret[PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE];
Gilles Peskine01d718c2018-09-18 12:01:02 +02007063 size_t shared_secret_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(operation->alg);
Gilles Peskine01d718c2018-09-18 12:01:02 +02007065
7066 /* Step 1: run the secret agreement algorithm to generate the shared
7067 * secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 status = psa_key_agreement_raw_internal(ka_alg,
7069 private_key,
7070 peer_key, peer_key_length,
7071 shared_secret,
7072 sizeof(shared_secret),
7073 &shared_secret_length);
7074 if (status != PSA_SUCCESS) {
Gilles Peskine12313cd2018-06-20 00:20:32 +02007075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02007077
Gilles Peskineb0b255c2018-07-06 17:01:38 +02007078 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02007079 * the shared secret. A shared secret is permitted wherever a key
7080 * of type DERIVE is permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 status = psa_key_derivation_input_internal(operation, step,
7082 PSA_KEY_TYPE_DERIVE,
7083 shared_secret,
7084 shared_secret_length);
Gilles Peskine12313cd2018-06-20 00:20:32 +02007085exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 mbedtls_platform_zeroize(shared_secret, shared_secret_length);
7087 return status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007088}
7089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *operation,
7091 psa_key_derivation_step_t step,
7092 mbedtls_svc_key_id_t private_key,
7093 const uint8_t *peer_key,
7094 size_t peer_key_length)
Gilles Peskine12313cd2018-06-20 00:20:32 +02007095{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007097 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01007098 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 if (!PSA_ALG_IS_KEY_AGREEMENT(operation->alg)) {
7101 return PSA_ERROR_INVALID_ARGUMENT;
7102 }
Ronald Cron5c522922020-11-14 16:35:34 +01007103 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg);
7105 if (status != PSA_SUCCESS) {
7106 return status;
7107 }
7108 status = psa_key_agreement_internal(operation, step,
7109 slot,
7110 peer_key, peer_key_length);
7111 if (status != PSA_SUCCESS) {
7112 psa_key_derivation_abort(operation);
7113 } else {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007114 /* If a private key has been added as SECRET, we allow the derived
7115 * key material to be used as a key in PSA Crypto. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 if (step == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007117 operation->can_output_key = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 }
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007119 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007122
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02007124}
7125
Gilles Peskine449bd832023-01-11 14:50:10 +01007126psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
7127 mbedtls_svc_key_id_t private_key,
7128 const uint8_t *peer_key,
7129 size_t peer_key_length,
7130 uint8_t *output,
7131 size_t output_size,
7132 size_t *output_length)
Gilles Peskine0216fe12019-04-11 21:23:21 +02007133{
Ronald Cronf95a2b12020-10-22 15:24:49 +02007134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01007135 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02007136 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007137 size_t expected_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 if (!PSA_ALG_IS_KEY_AGREEMENT(alg)) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007140 status = PSA_ERROR_INVALID_ARGUMENT;
7141 goto exit;
7142 }
Ronald Cron5c522922020-11-14 16:35:34 +01007143 status = psa_get_and_lock_transparent_key_slot_with_policy(
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg);
7145 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02007148
Gilles Peskine3e561302022-04-14 00:17:15 +02007149 /* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is in general an upper bound
7150 * for the output size. The PSA specification only guarantees that this
7151 * function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
7152 * but it might be nice to allow smaller buffers if the output fits.
7153 * At the time of writing this comment, with only ECDH implemented,
7154 * PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
7155 * If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
7156 * be exact for it as well. */
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007157 expected_length =
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
7159 if (output_size < expected_length) {
Gilles Peskine3e561302022-04-14 00:17:15 +02007160 status = PSA_ERROR_BUFFER_TOO_SMALL;
7161 goto exit;
7162 }
7163
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 status = psa_key_agreement_raw_internal(alg, slot,
7165 peer_key, peer_key_length,
7166 output, output_size,
7167 output_length);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007168
7169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 if (status != PSA_SUCCESS) {
Gilles Peskine0216fe12019-04-11 21:23:21 +02007171 /* If an error happens and is not handled properly, the output
7172 * may be used as a key to protect sensitive data. Arrange for such
7173 * a key to be random, which is likely to result in decryption or
7174 * verification errors. This is better than filling the buffer with
7175 * some constant data such as zeros, which would result in the data
7176 * being protected with a reproducible, easily knowable key.
7177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 psa_generate_random(output, output_size);
Gilles Peskine0216fe12019-04-11 21:23:21 +02007179 *output_length = output_size;
7180 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007181
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 unlock_status = psa_unlock_key_slot(slot);
Ronald Cronf95a2b12020-10-22 15:24:49 +02007183
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 return (status == PSA_SUCCESS) ? unlock_status : status;
Gilles Peskine0216fe12019-04-11 21:23:21 +02007185}
Gilles Peskine86a440b2018-11-12 18:39:40 +01007186
7187
Gilles Peskine30524eb2020-11-13 17:02:26 +01007188
Gilles Peskine86a440b2018-11-12 18:39:40 +01007189/****************************************************************/
7190/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02007191/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02007192
Gilles Peskine672a7712023-04-28 21:00:28 +02007193#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
7194#include "entropy_poll.h"
7195#endif
7196
Gilles Peskine30524eb2020-11-13 17:02:26 +01007197/** Initialize the PSA random generator.
7198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007199static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007200{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007201#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007203#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7204
Gilles Peskine30524eb2020-11-13 17:02:26 +01007205 /* Set default configuration if
7206 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 if (rng->entropy_init == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007208 rng->entropy_init = mbedtls_entropy_init;
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 }
7210 if (rng->entropy_free == NULL) {
Gilles Peskine30524eb2020-11-13 17:02:26 +01007211 rng->entropy_free = mbedtls_entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 rng->entropy_init(&rng->entropy);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007215#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
7216 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
7217 /* The PSA entropy injection feature depends on using NV seed as an entropy
7218 * source. Add NV seed as an entropy source for PSA entropy injection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 mbedtls_entropy_add_source(&rng->entropy,
7220 mbedtls_nv_seed_poll, NULL,
7221 MBEDTLS_ENTROPY_BLOCK_SIZE,
7222 MBEDTLS_ENTROPY_SOURCE_STRONG);
Gilles Peskine30524eb2020-11-13 17:02:26 +01007223#endif
7224
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007226#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007227}
7228
7229/** Deinitialize the PSA random generator.
7230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007231static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007232{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007233#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 memset(rng, 0, sizeof(*rng));
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007235#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
7237 rng->entropy_free(&rng->entropy);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007238#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007239}
7240
7241/** Seed the PSA random generator.
7242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007243static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +01007244{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007245#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7246 /* Do nothing: the external RNG seeds itself. */
7247 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007249#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007250 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 int ret = mbedtls_psa_drbg_seed(&rng->entropy,
7252 drbg_seed, sizeof(drbg_seed) - 1);
7253 return mbedtls_to_psa_error(ret);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007254#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01007255}
7256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257psa_status_t psa_generate_random(uint8_t *output,
7258 size_t output_size)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007259{
Gilles Peskinee59236f2018-01-27 23:32:46 +01007260 GUARD_MODULE_INITIALIZED;
7261
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007262#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7263
7264 size_t output_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 psa_status_t status = mbedtls_psa_external_get_random(&global_data.rng,
7266 output, output_size,
7267 &output_length);
7268 if (status != PSA_SUCCESS) {
7269 return status;
7270 }
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007271 /* Breaking up a request into smaller chunks is currently not supported
Tom Cosgrove1797b052022-12-04 17:19:59 +00007272 * for the external RNG interface. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 if (output_length != output_size) {
7274 return PSA_ERROR_INSUFFICIENT_ENTROPY;
7275 }
7276 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007277
7278#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 while (output_size > 0) {
Gilles Peskine71ddab92021-01-04 21:01:07 +01007281 size_t request_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
7283 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
7284 output_size);
7285 int ret = mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
7286 output, request_size);
7287 if (ret != 0) {
7288 return mbedtls_to_psa_error(ret);
7289 }
Gilles Peskine71ddab92021-01-04 21:01:07 +01007290 output_size -= request_size;
7291 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02007292 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 return PSA_SUCCESS;
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007294#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007295}
7296
Gilles Peskine8814fc42020-12-14 15:33:44 +01007297/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007298 *
7299 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
7300 * `psa_generate_random(...)`. The state parameter is ignored since the
7301 * PSA API doesn't support passing an explicit state.
7302 *
7303 * In the non-external case, psa_generate_random() calls an
7304 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
7305 * and semantics as mbedtls_psa_get_random(). As an optimization,
7306 * instead of doing this back-and-forth between the PSA API and the
7307 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
7308 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01007309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007310#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
7311int mbedtls_psa_get_random(void *p_rng,
7312 unsigned char *output,
7313 size_t output_size)
Gilles Peskine8814fc42020-12-14 15:33:44 +01007314{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01007315 /* This function takes a pointer to the RNG state because that's what
7316 * classic mbedtls functions using an RNG expect. The PSA RNG manages
7317 * its own state internally and doesn't let the caller access that state.
7318 * So we just ignore the state parameter, and in practice we'll pass
7319 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01007320 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 psa_status_t status = psa_generate_random(output, output_size);
7322 if (status == PSA_SUCCESS) {
7323 return 0;
7324 } else {
7325 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
7326 }
Gilles Peskine8814fc42020-12-14 15:33:44 +01007327}
7328#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
7329
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007330#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +01007331psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
7332 size_t seed_size)
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007333{
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 if (global_data.initialized) {
7335 return PSA_ERROR_NOT_PERMITTED;
7336 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 if (((seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM) ||
7339 (seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE)) ||
7340 (seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE)) {
7341 return PSA_ERROR_INVALID_ARGUMENT;
7342 }
Netanel Gonen21f37cb2018-11-19 11:53:55 +02007343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 return mbedtls_psa_storage_inject_entropy(seed, seed_size);
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007345}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01007346#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02007347
Ronald Cron3772afe2021-02-08 16:10:05 +01007348/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02007349 *
Ronald Cron3772afe2021-02-08 16:10:05 +01007350 * \param type The key type
7351 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02007352 *
7353 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01007354 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02007355 * \retval #PSA_ERROR_INVALID_ARGUMENT
7356 * The size in bits of the key is not valid.
7357 * \retval #PSA_ERROR_NOT_SUPPORTED
7358 * The type and/or the size in bits of the key or the combination of
7359 * the two is not supported.
7360 */
Ronald Cron3772afe2021-02-08 16:10:05 +01007361static psa_status_t psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 psa_key_type_t type, size_t bits)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007363{
Ronald Cronf3bb7612020-10-02 20:11:59 +02007364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007365
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 if (key_type_is_raw_bytes(type)) {
7367 status = psa_validate_unstructured_key_bit_size(type, bits);
7368 if (status != PSA_SUCCESS) {
7369 return status;
7370 }
7371 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007372#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 if (PSA_KEY_TYPE_IS_RSA(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7374 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
7375 return PSA_ERROR_NOT_SUPPORTED;
7376 }
Waleed Elmelegyd7bdbbe2023-07-20 16:26:58 +00007377 if (bits < PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS) {
Waleed Elmelegyab570712023-07-05 16:40:58 +00007378 return PSA_ERROR_NOT_SUPPORTED;
7379 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007380
Ronald Cron01b2aba2020-10-05 09:42:02 +02007381 /* Accept only byte-aligned keys, for the same reasons as
7382 * in psa_import_rsa_key(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 if (bits % 8 != 0) {
7384 return PSA_ERROR_NOT_SUPPORTED;
7385 }
7386 } else
Valerio Settif6d4dfb2023-07-10 10:55:12 +02007387#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007388
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007389#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Crond81ab562021-02-16 09:01:16 +01007391 /* To avoid empty block, return successfully here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return PSA_SUCCESS;
7393 } else
Valerio Setti6a9d0ee2023-06-21 10:07:21 +02007394#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007395
Valerio Setti8bb57632023-05-26 13:48:07 +02007396#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007397 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Przemek Stekield1cf1ba2023-04-27 12:04:21 +02007398 if (psa_is_dh_key_size_valid(bits) == 0) {
Przemek Stekielfedd1342022-12-01 15:00:02 +01007399 return PSA_ERROR_NOT_SUPPORTED;
7400 }
7401 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007402#endif /* defined(MBEDTLS_PSA_WANT_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02007403 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007405 }
7406
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 return PSA_SUCCESS;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007408}
7409
Ronald Cron55ed0592020-10-05 10:30:40 +02007410psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007411 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron01b2aba2020-10-05 09:42:02 +02007413{
7414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02007415 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02007416
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if ((attributes->domain_parameters == NULL) &&
7418 (attributes->domain_parameters_size != 0)) {
7419 return PSA_ERROR_INVALID_ARGUMENT;
7420 }
Ronald Cron01b2aba2020-10-05 09:42:02 +02007421
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 if (key_type_is_raw_bytes(type)) {
7423 status = psa_generate_random(key_buffer, key_buffer_size);
7424 if (status != PSA_SUCCESS) {
7425 return status;
7426 }
Steven Cooreman81be2fa2020-07-24 22:04:59 +02007427
David Brown12ca5032021-02-11 11:02:00 -07007428#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 if (type == PSA_KEY_TYPE_DES) {
7430 psa_des_set_key_parity(key_buffer, key_buffer_size);
7431 }
David Brown8107e312021-02-12 08:08:46 -07007432#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 } else
Gilles Peskinee59236f2018-01-27 23:32:46 +01007434
Valerio Setti76df8c12023-07-11 14:11:28 +02007435#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
7437 return mbedtls_psa_rsa_generate_key(attributes,
7438 key_buffer,
7439 key_buffer_size,
7440 key_buffer_length);
7441 } else
Valerio Setti76df8c12023-07-11 14:11:28 +02007442#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007443
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007444#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (PSA_KEY_TYPE_IS_ECC(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7446 return mbedtls_psa_ecp_generate_key(attributes,
7447 key_buffer,
7448 key_buffer_size,
7449 key_buffer_length);
7450 } else
Valerio Settibfeaf5b2023-06-20 13:27:46 +02007451#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR_GENERATE) */
Przemek Stekielfedd1342022-12-01 15:00:02 +01007452
Valerio Setti8bb57632023-05-26 13:48:07 +02007453#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY)
Przemek Stekielfedd1342022-12-01 15:00:02 +01007454 if (PSA_KEY_TYPE_IS_DH(type) && PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
7455 return mbedtls_psa_ffdh_generate_key(attributes,
7456 key_buffer,
7457 key_buffer_size,
7458 key_buffer_length);
7459 } else
Valerio Setti8bb57632023-05-26 13:48:07 +02007460#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DH_KEY_PAIR_LEGACY) */
Steven Cooreman29149862020-08-05 15:43:42 +02007461 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 (void) key_buffer_length;
7463 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman29149862020-08-05 15:43:42 +02007464 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007465
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 return PSA_SUCCESS;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007467}
Darryl Green0c6575a2018-11-07 16:05:30 +00007468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
7470 mbedtls_svc_key_id_t *key)
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007471{
7472 psa_status_t status;
7473 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02007474 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02007475 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02007476
Ronald Cron81709fc2020-11-14 12:10:32 +01007477 *key = MBEDTLS_SVC_KEY_ID_INIT;
7478
Gilles Peskine0f84d622019-09-12 19:03:13 +02007479 /* Reject any attempt to create a zero-length key so that we don't
7480 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 if (psa_get_key_bits(attributes) == 0) {
7482 return PSA_ERROR_INVALID_ARGUMENT;
7483 }
Gilles Peskine0f84d622019-09-12 19:03:13 +02007484
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007485 /* Reject any attempt to create a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 if (PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type)) {
7487 return PSA_ERROR_INVALID_ARGUMENT;
7488 }
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02007489
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 status = psa_start_key_creation(PSA_KEY_CREATION_GENERATE, attributes,
7491 &slot, &driver);
7492 if (status != PSA_SUCCESS) {
Gilles Peskine11792082019-08-06 18:36:36 +02007493 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 }
Gilles Peskine11792082019-08-06 18:36:36 +02007495
Ronald Cron977c2472020-10-13 08:32:21 +02007496 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05307497 * storage ( thus not in the case of generating a key in a secure element
7498 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
7499 * buffer to hold the generated key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 if (slot->key.data == NULL) {
7501 if (PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime) ==
7502 PSA_KEY_LOCATION_LOCAL_STORAGE) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007503 status = psa_validate_key_type_and_size_for_key_generation(
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 attributes->core.type, attributes->core.bits);
7505 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007506 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 }
Ronald Cron3772afe2021-02-08 16:10:05 +01007508
7509 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 attributes->core.type,
7511 attributes->core.bits);
7512 } else {
Ronald Cron977c2472020-10-13 08:32:21 +02007513 status = psa_driver_wrapper_get_key_buffer_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 attributes, &key_buffer_size);
7515 if (status != PSA_SUCCESS) {
Ronald Cron3772afe2021-02-08 16:10:05 +01007516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 }
Ronald Cron977c2472020-10-13 08:32:21 +02007518 }
Ronald Cron977c2472020-10-13 08:32:21 +02007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
7521 if (status != PSA_SUCCESS) {
Ronald Cron977c2472020-10-13 08:32:21 +02007522 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 }
Ronald Cron977c2472020-10-13 08:32:21 +02007524 }
7525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 status = psa_driver_wrapper_generate_key(attributes,
7527 slot->key.data, slot->key.bytes, &slot->key.bytes);
Gilles Peskine11792082019-08-06 18:36:36 +02007528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 if (status != PSA_SUCCESS) {
7530 psa_remove_key_data_from_memory(slot);
7531 }
Ronald Cron2b56bc82020-10-05 10:02:26 +02007532
Gilles Peskine11792082019-08-06 18:36:36 +02007533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (status == PSA_SUCCESS) {
7535 status = psa_finish_key_creation(slot, driver, key);
7536 }
7537 if (status != PSA_SUCCESS) {
7538 psa_fail_key_creation(slot, driver);
7539 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02007540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007542}
7543
Gilles Peskinee59236f2018-01-27 23:32:46 +01007544/****************************************************************/
7545/* Module setup */
7546/****************************************************************/
7547
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007548#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01007549psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 void (* entropy_init)(mbedtls_entropy_context *ctx),
7551 void (* entropy_free)(mbedtls_entropy_context *ctx))
Gilles Peskine5e769522018-11-20 21:59:56 +01007552{
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7554 return PSA_ERROR_BAD_STATE;
7555 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01007556 global_data.rng.entropy_init = entropy_init;
7557 global_data.rng.entropy_free = entropy_free;
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 return PSA_SUCCESS;
Gilles Peskine5e769522018-11-20 21:59:56 +01007559}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01007560#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01007561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562void mbedtls_psa_crypto_free(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007563{
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 psa_wipe_all_key_slots();
7565 if (global_data.rng_state != RNG_NOT_INITIALIZED) {
7566 mbedtls_psa_random_free(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007567 }
7568 /* Wipe all remaining data, including configuration.
7569 * In particular, this sets all state indicator to the value
7570 * indicating "uninitialized". */
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 mbedtls_platform_zeroize(&global_data, sizeof(global_data));
Ronald Cron9ba76912021-04-10 16:57:30 +02007572
7573 /* Terminate drivers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 psa_driver_wrapper_free();
Gilles Peskinee59236f2018-01-27 23:32:46 +01007575}
7576
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007577#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
7578/** Recover a transaction that was interrupted by a power failure.
7579 *
7580 * This function is called during initialization, before psa_crypto_init()
7581 * returns. If this function returns a failure status, the initialization
7582 * fails.
7583 */
7584static psa_status_t psa_crypto_recover_transaction(
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 const psa_crypto_transaction_t *transaction)
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007586{
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 switch (transaction->unknown.type) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007588 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
7589 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 /* TODO - fall through to the failure case until this
7591 * is implemented.
7592 * https://github.com/ARMmbed/mbed-crypto/issues/218
7593 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007594 default:
7595 /* We found an unsupported transaction in the storage.
7596 * We don't know what state the storage is in. Give up. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 return PSA_ERROR_DATA_INVALID;
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007598 }
7599}
7600#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
7601
Gilles Peskine449bd832023-01-11 14:50:10 +01007602psa_status_t psa_crypto_init(void)
Gilles Peskinee59236f2018-01-27 23:32:46 +01007603{
Gilles Peskine66fb1262018-12-10 16:29:04 +01007604 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007605
Gilles Peskinec6b69072018-11-20 21:42:52 +01007606 /* Double initialization is explicitly allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (global_data.initialized != 0) {
7608 return PSA_SUCCESS;
7609 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01007610
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01007611 /* Init drivers */
7612 status = psa_driver_wrapper_init();
7613 if (status != PSA_SUCCESS) {
7614 goto exit;
7615 }
7616 global_data.drivers_initialized = 1;
7617
Gilles Peskine30524eb2020-11-13 17:02:26 +01007618 /* Initialize and seed the random generator. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 mbedtls_psa_random_init(&global_data.rng);
Gilles Peskinec6b69072018-11-20 21:42:52 +01007620 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 status = mbedtls_psa_random_seed(&global_data.rng);
7622 if (status != PSA_SUCCESS) {
Gilles Peskinee59236f2018-01-27 23:32:46 +01007623 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007625 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 status = psa_initialize_key_slots();
7628 if (status != PSA_SUCCESS) {
Gilles Peskine66fb1262018-12-10 16:29:04 +01007629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01007631
Gilles Peskine4b734222019-07-24 15:56:31 +02007632#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 status = psa_crypto_load_transaction();
7634 if (status == PSA_SUCCESS) {
7635 status = psa_crypto_recover_transaction(&psa_crypto_transaction);
7636 if (status != PSA_SUCCESS) {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02007637 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 }
7639 status = psa_crypto_stop_transaction();
7640 } else if (status == PSA_ERROR_DOES_NOT_EXIST) {
Gilles Peskinefc762652019-07-22 19:30:34 +02007641 /* There's no transaction to complete. It's all good. */
7642 status = PSA_SUCCESS;
7643 }
Gilles Peskine4b734222019-07-24 15:56:31 +02007644#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02007645
Gilles Peskinec6b69072018-11-20 21:42:52 +01007646 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01007647 global_data.initialized = 1;
7648
7649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 if (status != PSA_SUCCESS) {
7651 mbedtls_psa_crypto_free();
7652 }
7653 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01007654}
7655
Yanray Wangffc3c482023-07-11 12:01:04 +08007656#if defined(PSA_WANT_ALG_SOME_PAKE)
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007657psa_status_t psa_crypto_driver_pake_get_password_len(
7658 const psa_crypto_driver_pake_inputs_t *inputs,
7659 size_t *password_len)
7660{
7661 if (inputs->password_len == 0) {
7662 return PSA_ERROR_BAD_STATE;
7663 }
7664
7665 *password_len = inputs->password_len;
7666
7667 return PSA_SUCCESS;
7668}
7669
7670psa_status_t psa_crypto_driver_pake_get_password(
7671 const psa_crypto_driver_pake_inputs_t *inputs,
7672 uint8_t *buffer, size_t buffer_size, size_t *buffer_length)
7673{
7674 if (inputs->password_len == 0) {
7675 return PSA_ERROR_BAD_STATE;
7676 }
7677
7678 if (buffer_size < inputs->password_len) {
7679 return PSA_ERROR_BUFFER_TOO_SMALL;
7680 }
7681
7682 memcpy(buffer, inputs->password, inputs->password_len);
7683 *buffer_length = inputs->password_len;
7684
7685 return PSA_SUCCESS;
7686}
7687
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007688psa_status_t psa_crypto_driver_pake_get_user_len(
7689 const psa_crypto_driver_pake_inputs_t *inputs,
7690 size_t *user_len)
7691{
7692 if (inputs->user_len == 0) {
7693 return PSA_ERROR_BAD_STATE;
7694 }
7695
7696 *user_len = inputs->user_len;
7697
7698 return PSA_SUCCESS;
7699}
7700
7701psa_status_t psa_crypto_driver_pake_get_user(
7702 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007703 uint8_t *user_id, size_t user_id_size, size_t *user_id_len)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007704{
7705 if (inputs->user_len == 0) {
7706 return PSA_ERROR_BAD_STATE;
7707 }
7708
Przemek Stekielc0e62502023-03-14 11:49:36 +01007709 if (user_id_size < inputs->user_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007710 return PSA_ERROR_BUFFER_TOO_SMALL;
7711 }
7712
Przemek Stekielc0e62502023-03-14 11:49:36 +01007713 memcpy(user_id, inputs->user, inputs->user_len);
7714 *user_id_len = inputs->user_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007715
7716 return PSA_SUCCESS;
7717}
7718
7719psa_status_t psa_crypto_driver_pake_get_peer_len(
7720 const psa_crypto_driver_pake_inputs_t *inputs,
7721 size_t *peer_len)
7722{
7723 if (inputs->peer_len == 0) {
7724 return PSA_ERROR_BAD_STATE;
7725 }
7726
7727 *peer_len = inputs->peer_len;
7728
7729 return PSA_SUCCESS;
7730}
7731
7732psa_status_t psa_crypto_driver_pake_get_peer(
7733 const psa_crypto_driver_pake_inputs_t *inputs,
Przemek Stekielc0e62502023-03-14 11:49:36 +01007734 uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length)
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007735{
7736 if (inputs->peer_len == 0) {
7737 return PSA_ERROR_BAD_STATE;
7738 }
7739
Przemek Stekielc0e62502023-03-14 11:49:36 +01007740 if (peer_id_size < inputs->peer_len) {
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007741 return PSA_ERROR_BUFFER_TOO_SMALL;
7742 }
7743
Przemek Stekielc0e62502023-03-14 11:49:36 +01007744 memcpy(peer_id, inputs->peer, inputs->peer_len);
7745 *peer_id_length = inputs->peer_len;
Przemek Stekiel1e7a9272023-02-28 14:38:58 +01007746
7747 return PSA_SUCCESS;
7748}
7749
Przemek Stekielca8d2b22023-01-17 16:21:33 +01007750psa_status_t psa_crypto_driver_pake_get_cipher_suite(
7751 const psa_crypto_driver_pake_inputs_t *inputs,
7752 psa_pake_cipher_suite_t *cipher_suite)
7753{
7754 if (inputs->cipher_suite.algorithm == PSA_ALG_NONE) {
7755 return PSA_ERROR_BAD_STATE;
7756 }
7757
7758 *cipher_suite = inputs->cipher_suite;
7759
7760 return PSA_SUCCESS;
7761}
7762
Neil Armstronga7d08c32022-06-01 18:21:20 +02007763psa_status_t psa_pake_setup(
7764 psa_pake_operation_t *operation,
7765 const psa_pake_cipher_suite_t *cipher_suite)
7766{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007768
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007769 if (operation->stage != PSA_PAKE_OPERATION_STAGE_SETUP) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007770 status = PSA_ERROR_BAD_STATE;
7771 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007772 }
7773
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01007774 if (PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
Przemek Stekiel51eac532022-12-07 11:04:51 +01007775 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007776 status = PSA_ERROR_INVALID_ARGUMENT;
7777 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007778 }
7779
Przemek Stekiel51eac532022-12-07 11:04:51 +01007780 memset(&operation->data.inputs, 0, sizeof(operation->data.inputs));
7781
Przemek Stekiele12ed362022-12-21 12:54:46 +01007782 operation->alg = cipher_suite->algorithm;
Przemek Stekiel656b2592023-03-22 13:15:33 +01007783 operation->primitive = PSA_PAKE_PRIMITIVE(cipher_suite->type,
7784 cipher_suite->family, cipher_suite->bits);
Przemek Stekiel51eac532022-12-07 11:04:51 +01007785 operation->data.inputs.cipher_suite = *cipher_suite;
7786
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007787#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01007788 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007789 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekieldde6a912023-01-26 08:46:37 +01007790 &operation->computation_stage.jpake;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01007791
David Horstmann16f01512023-06-14 17:21:07 +01007792 memset(computation_stage, 0, sizeof(*computation_stage));
David Horstmanne7f21e62023-05-12 18:17:21 +01007793 computation_stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel80a88492023-02-20 13:32:22 +01007794 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007795#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01007796 {
7797 status = PSA_ERROR_NOT_SUPPORTED;
7798 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01007799 }
7800
Przemek Stekiel1c3cfb42023-01-26 10:35:02 +01007801 operation->stage = PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS;
7802
Przemek Stekiel51eac532022-12-07 11:04:51 +01007803 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007804exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007805 psa_pake_abort(operation);
7806 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007807}
7808
7809psa_status_t psa_pake_set_password_key(
7810 psa_pake_operation_t *operation,
7811 mbedtls_svc_key_id_t password)
7812{
Neil Armstrong5ae60962022-09-15 11:29:46 +02007813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +01007814 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
7815 psa_key_slot_t *slot = NULL;
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007816 psa_key_attributes_t attributes;
7817 psa_key_type_t type;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007818
Przemek Stekiel51eac532022-12-07 11:04:51 +01007819 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007820 status = PSA_ERROR_BAD_STATE;
7821 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007822 }
7823
Przemek Stekiel6c764412022-11-22 14:05:12 +01007824 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
7825 PSA_KEY_USAGE_DERIVE,
Przemek Stekield5d28a22023-01-26 10:46:05 +01007826 operation->alg);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007827 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007828 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007829 }
7830
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007831 attributes = (psa_key_attributes_t) {
Przemek Stekiel6c764412022-11-22 14:05:12 +01007832 .core = slot->attr
7833 };
Neil Armstrong5ae60962022-09-15 11:29:46 +02007834
Agathiyan Bragadeesh4d47cea2023-07-17 16:57:24 +01007835 type = psa_get_key_type(&attributes);
Neil Armstrong5ae60962022-09-15 11:29:46 +02007836
Przemek Stekiel51eac532022-12-07 11:04:51 +01007837 if (type != PSA_KEY_TYPE_PASSWORD &&
7838 type != PSA_KEY_TYPE_PASSWORD_HASH) {
7839 status = PSA_ERROR_INVALID_ARGUMENT;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007840 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007841 }
7842
Przemek Stekiel51eac532022-12-07 11:04:51 +01007843 operation->data.inputs.password = mbedtls_calloc(1, slot->key.bytes);
7844 if (operation->data.inputs.password == NULL) {
Przemek Stekiele12ed362022-12-21 12:54:46 +01007845 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007846 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007847 }
7848
7849 memcpy(operation->data.inputs.password, slot->key.data, slot->key.bytes);
7850 operation->data.inputs.password_len = slot->key.bytes;
Przemek Stekiel9dd24402023-01-26 15:06:09 +01007851 operation->data.inputs.attributes = attributes;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007852exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007853 if (status != PSA_SUCCESS) {
7854 psa_pake_abort(operation);
7855 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007856 unlock_status = psa_unlock_key_slot(slot);
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007857 return (status == PSA_SUCCESS) ? unlock_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007858}
7859
7860psa_status_t psa_pake_set_user(
7861 psa_pake_operation_t *operation,
7862 const uint8_t *user_id,
7863 size_t user_id_len)
7864{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007865 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007866
7867 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007868 status = PSA_ERROR_BAD_STATE;
7869 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007870 }
7871
Przemek Stekiel51eac532022-12-07 11:04:51 +01007872 if (user_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007873 status = PSA_ERROR_INVALID_ARGUMENT;
7874 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007875 }
7876
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007877 if (operation->data.inputs.user_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007878 status = PSA_ERROR_BAD_STATE;
7879 goto exit;
7880 }
7881
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007882 operation->data.inputs.user = mbedtls_calloc(1, user_id_len);
7883 if (operation->data.inputs.user == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007884 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7885 goto exit;
7886 }
7887
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007888 memcpy(operation->data.inputs.user, user_id, user_id_len);
7889 operation->data.inputs.user_len = user_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007890
7891 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007892exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007893 psa_pake_abort(operation);
7894 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007895}
7896
7897psa_status_t psa_pake_set_peer(
7898 psa_pake_operation_t *operation,
7899 const uint8_t *peer_id,
7900 size_t peer_id_len)
7901{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007902 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007903
7904 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007905 status = PSA_ERROR_BAD_STATE;
7906 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007907 }
7908
Przemek Stekiel51eac532022-12-07 11:04:51 +01007909 if (peer_id_len == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007910 status = PSA_ERROR_INVALID_ARGUMENT;
7911 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007912 }
7913
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007914 if (operation->data.inputs.peer_len != 0) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007915 status = PSA_ERROR_BAD_STATE;
7916 goto exit;
7917 }
7918
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007919 operation->data.inputs.peer = mbedtls_calloc(1, peer_id_len);
7920 if (operation->data.inputs.peer == NULL) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007921 status = PSA_ERROR_INSUFFICIENT_MEMORY;
7922 goto exit;
7923 }
7924
Przemek Stekielf309d6b2023-03-10 09:54:45 +01007925 memcpy(operation->data.inputs.peer, peer_id, peer_id_len);
7926 operation->data.inputs.peer_len = peer_id_len;
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007927
7928 return PSA_SUCCESS;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007929exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007930 psa_pake_abort(operation);
7931 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007932}
7933
7934psa_status_t psa_pake_set_role(
7935 psa_pake_operation_t *operation,
7936 psa_pake_role_t role)
7937{
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007938 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007939
Przemek Stekiel51eac532022-12-07 11:04:51 +01007940 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel26c909d2023-02-28 12:34:03 +01007941 status = PSA_ERROR_BAD_STATE;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007942 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01007943 }
7944
Przemek Stekiel09104b82023-03-06 14:11:51 +01007945 switch (operation->alg) {
7946#if defined(PSA_WANT_ALG_JPAKE)
7947 case PSA_ALG_JPAKE:
7948 if (role == PSA_PAKE_ROLE_NONE) {
7949 return PSA_SUCCESS;
7950 }
7951 status = PSA_ERROR_INVALID_ARGUMENT;
7952 break;
7953#endif
7954 default:
7955 (void) role;
7956 status = PSA_ERROR_NOT_SUPPORTED;
7957 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02007958 }
Przemek Stekiel3e784d82023-02-08 09:12:42 +01007959exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007960 psa_pake_abort(operation);
7961 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02007962}
7963
David Horstmanne7f21e62023-05-12 18:17:21 +01007964/* Auxiliary function to convert core computation stage to single driver step. */
Przemek Stekiel4aa99402023-02-27 13:00:57 +01007965#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel251e86a2023-02-17 14:30:50 +01007966static psa_crypto_driver_pake_step_t convert_jpake_computation_stage_to_driver_step(
Przemek Stekieldde6a912023-01-26 08:46:37 +01007967 psa_jpake_computation_stage_t *stage)
Przemek Stekielb09c4872023-01-17 12:05:38 +01007968{
David Horstmann74a3d8c2023-06-14 18:28:19 +01007969 psa_crypto_driver_pake_step_t key_share_step;
David Horstmann5da95602023-06-08 15:37:12 +01007970 if (stage->round == PSA_JPAKE_FIRST) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007971 int is_x1;
David Horstmann74a3d8c2023-06-14 18:28:19 +01007972
David Horstmann024e5c52023-06-14 15:48:21 +01007973 if (stage->io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01007974 is_x1 = (stage->outputs < 1);
7975 } else {
7976 is_x1 = (stage->inputs < 1);
7977 }
7978
David Horstmann74a3d8c2023-06-14 18:28:19 +01007979 key_share_step = is_x1 ?
7980 PSA_JPAKE_X1_STEP_KEY_SHARE :
7981 PSA_JPAKE_X2_STEP_KEY_SHARE;
David Horstmann5da95602023-06-08 15:37:12 +01007982 } else if (stage->round == PSA_JPAKE_SECOND) {
David Horstmann74a3d8c2023-06-14 18:28:19 +01007983 key_share_step = (stage->io_mode == PSA_JPAKE_OUTPUT) ?
7984 PSA_JPAKE_X2S_STEP_KEY_SHARE :
7985 PSA_JPAKE_X4S_STEP_KEY_SHARE;
7986 } else {
7987 return PSA_JPAKE_STEP_INVALID;
Przemek Stekielb09c4872023-01-17 12:05:38 +01007988 }
Agathiyan Bragadeesh387bfa52023-07-17 17:01:33 +01007989 return (psa_crypto_driver_pake_step_t) (key_share_step + stage->step - PSA_PAKE_STEP_KEY_SHARE);
Przemek Stekielb09c4872023-01-17 12:05:38 +01007990}
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01007991#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekielb09c4872023-01-17 12:05:38 +01007992
Przemek Stekiel2797d372022-12-22 11:19:22 +01007993static psa_status_t psa_pake_complete_inputs(
7994 psa_pake_operation_t *operation)
7995{
Przemek Stekiel2797d372022-12-22 11:19:22 +01007996 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel18620a32023-01-17 16:34:52 +01007997 /* Create copy of the inputs on stack as inputs share memory
7998 with the driver context which will be setup by the driver. */
7999 psa_crypto_driver_pake_inputs_t inputs = operation->data.inputs;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008000
Przemek Stekielfde11282023-03-13 16:06:09 +01008001 if (inputs.password_len == 0) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008002 return PSA_ERROR_BAD_STATE;
8003 }
8004
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008005 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekielfde11282023-03-13 16:06:09 +01008006 if (inputs.user_len == 0 || inputs.peer_len == 0) {
8007 return PSA_ERROR_BAD_STATE;
8008 }
Przemek Stekieldff21d32023-02-14 20:09:10 +01008009 }
8010
Przemek Stekiel18620a32023-01-17 16:34:52 +01008011 /* Clear driver context */
8012 mbedtls_platform_zeroize(&operation->data, sizeof(operation->data));
8013
8014 status = psa_driver_wrapper_pake_setup(operation, &inputs);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008015
8016 /* Driver is responsible for creating its own copy of the password. */
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008017 mbedtls_platform_zeroize(inputs.password, inputs.password_len);
8018 mbedtls_free(inputs.password);
Przemek Stekiel2797d372022-12-22 11:19:22 +01008019
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008020 /* User and peer are translated to role. */
8021 mbedtls_free(inputs.user);
8022 mbedtls_free(inputs.peer);
8023
Przemek Stekiel2797d372022-12-22 11:19:22 +01008024 if (status == PSA_SUCCESS) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008025#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiel2797d372022-12-22 11:19:22 +01008026 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekield93de322023-02-24 08:39:04 +01008027 operation->stage = PSA_PAKE_OPERATION_STAGE_COMPUTATION;
Przemek Stekiel80a88492023-02-20 13:32:22 +01008028 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008029#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008030 {
8031 status = PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiel2797d372022-12-22 11:19:22 +01008032 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008033 }
Przemek Stekiel2797d372022-12-22 11:19:22 +01008034 return status;
8035}
8036
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008037#if defined(PSA_WANT_ALG_JPAKE)
David Horstmanne7f21e62023-05-12 18:17:21 +01008038static psa_status_t psa_jpake_prologue(
Przemek Stekiele12ed362022-12-21 12:54:46 +01008039 psa_pake_operation_t *operation,
David Horstmanne7f21e62023-05-12 18:17:21 +01008040 psa_pake_step_t step,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008041 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008042{
Przemek Stekiele12ed362022-12-21 12:54:46 +01008043 if (step != PSA_PAKE_STEP_KEY_SHARE &&
8044 step != PSA_PAKE_STEP_ZK_PUBLIC &&
8045 step != PSA_PAKE_STEP_ZK_PROOF) {
8046 return PSA_ERROR_INVALID_ARGUMENT;
8047 }
8048
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008049 psa_jpake_computation_stage_t *computation_stage =
8050 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008051
David Horstmann5da95602023-06-08 15:37:12 +01008052 if (computation_stage->round != PSA_JPAKE_FIRST &&
8053 computation_stage->round != PSA_JPAKE_SECOND) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008054 return PSA_ERROR_BAD_STATE;
8055 }
8056
David Horstmanne7f21e62023-05-12 18:17:21 +01008057 /* Check that the step we are given is the one we were expecting */
8058 if (step != computation_stage->step) {
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008059 return PSA_ERROR_BAD_STATE;
8060 }
8061
David Horstmanne7f21e62023-05-12 18:17:21 +01008062 if (step == PSA_PAKE_STEP_KEY_SHARE &&
8063 computation_stage->inputs == 0 &&
8064 computation_stage->outputs == 0) {
8065 /* Start of the round, so function decides whether we are inputting
8066 * or outputting */
David Horstmann024e5c52023-06-14 15:48:21 +01008067 computation_stage->io_mode = io_mode;
8068 } else if (computation_stage->io_mode != io_mode) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008069 /* Middle of the round so the mode we are in must match the function
8070 * called by the user */
8071 return PSA_ERROR_BAD_STATE;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008072 }
8073
8074 return PSA_SUCCESS;
8075}
8076
David Horstmanne7f21e62023-05-12 18:17:21 +01008077static psa_status_t psa_jpake_epilogue(
8078 psa_pake_operation_t *operation,
David Horstmann00ad6bf2023-06-14 15:44:24 +01008079 psa_jpake_io_mode_t io_mode)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008080{
David Horstmanne7f21e62023-05-12 18:17:21 +01008081 psa_jpake_computation_stage_t *stage =
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008082 &operation->computation_stage.jpake;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008083
David Horstmanne7f21e62023-05-12 18:17:21 +01008084 if (stage->step == PSA_PAKE_STEP_ZK_PROOF) {
8085 /* End of an input/output */
David Horstmann00ad6bf2023-06-14 15:44:24 +01008086 if (io_mode == PSA_JPAKE_INPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008087 stage->inputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008088 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008089 stage->io_mode = PSA_JPAKE_OUTPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008090 }
8091 }
David Horstmann00ad6bf2023-06-14 15:44:24 +01008092 if (io_mode == PSA_JPAKE_OUTPUT) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008093 stage->outputs++;
David Horstmann246ec5a2023-06-27 10:33:06 +01008094 if (stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmann024e5c52023-06-14 15:48:21 +01008095 stage->io_mode = PSA_JPAKE_INPUT;
David Horstmanne7f21e62023-05-12 18:17:21 +01008096 }
8097 }
David Horstmann246ec5a2023-06-27 10:33:06 +01008098 if (stage->inputs == PSA_JPAKE_EXPECTED_INPUTS(stage->round) &&
8099 stage->outputs == PSA_JPAKE_EXPECTED_OUTPUTS(stage->round)) {
David Horstmanne7f21e62023-05-12 18:17:21 +01008100 /* End of a round, move to the next round */
8101 stage->inputs = 0;
8102 stage->outputs = 0;
8103 stage->round++;
8104 }
8105 stage->step = PSA_PAKE_STEP_KEY_SHARE;
Przemek Stekiel5eff1032023-02-21 19:10:36 +01008106 } else {
David Horstmanne7f21e62023-05-12 18:17:21 +01008107 stage->step++;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008108 }
Przemek Stekiele12ed362022-12-21 12:54:46 +01008109 return PSA_SUCCESS;
8110}
David Horstmanne7f21e62023-05-12 18:17:21 +01008111
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008112#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008113
Neil Armstronga7d08c32022-06-01 18:21:20 +02008114psa_status_t psa_pake_output(
8115 psa_pake_operation_t *operation,
8116 psa_pake_step_t step,
8117 uint8_t *output,
8118 size_t output_size,
8119 size_t *output_length)
8120{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008122 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008123 *output_length = 0;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008124
8125 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008126 status = psa_pake_complete_inputs(operation);
8127 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008128 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008129 }
8130 }
8131
8132 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008133 status = PSA_ERROR_BAD_STATE;
8134 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008135 }
8136
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008137 if (output_size == 0) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008138 status = PSA_ERROR_INVALID_ARGUMENT;
8139 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008140 }
8141
Przemek Stekiele12ed362022-12-21 12:54:46 +01008142 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008143#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008144 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008145 status = psa_jpake_prologue(operation, step, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008146 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008147 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008148 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008149 driver_step = convert_jpake_computation_stage_to_driver_step(
8150 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008151 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008152#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008153 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008154 (void) step;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008155 status = PSA_ERROR_NOT_SUPPORTED;
8156 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008157 }
8158
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008159 status = psa_driver_wrapper_pake_output(operation, driver_step,
8160 output, output_size, output_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008161
8162 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008163 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008164 }
8165
8166 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008167#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008168 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008169 status = psa_jpake_epilogue(operation, PSA_JPAKE_OUTPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008170 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008171 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008172 }
8173 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008174#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008175 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008176 status = PSA_ERROR_NOT_SUPPORTED;
8177 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008178 }
8179
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008180 return PSA_SUCCESS;
8181exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008182 psa_pake_abort(operation);
8183 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008184}
8185
8186psa_status_t psa_pake_input(
8187 psa_pake_operation_t *operation,
8188 psa_pake_step_t step,
8189 const uint8_t *input,
8190 size_t input_length)
8191{
Przemek Stekiel51eac532022-12-07 11:04:51 +01008192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008193 psa_crypto_driver_pake_step_t driver_step = PSA_JPAKE_STEP_INVALID;
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008194 const size_t max_input_length = (size_t) PSA_PAKE_INPUT_SIZE(operation->alg,
8195 operation->primitive,
8196 step);
Przemek Stekiel51eac532022-12-07 11:04:51 +01008197
8198 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
Przemek Stekiel2797d372022-12-22 11:19:22 +01008199 status = psa_pake_complete_inputs(operation);
8200 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008201 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008202 }
8203 }
8204
8205 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008206 status = PSA_ERROR_BAD_STATE;
8207 goto exit;
Przemek Stekiel51eac532022-12-07 11:04:51 +01008208 }
8209
Przemek Stekiel256c75d2023-03-23 14:09:34 +01008210 if (input_length == 0 || input_length > max_input_length) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008211 status = PSA_ERROR_INVALID_ARGUMENT;
8212 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008213 }
8214
Przemek Stekiele12ed362022-12-21 12:54:46 +01008215 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008216#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008217 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008218 status = psa_jpake_prologue(operation, step, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008219 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008220 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008221 }
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008222 driver_step = convert_jpake_computation_stage_to_driver_step(
8223 &operation->computation_stage.jpake);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008224 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008225#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008226 default:
Przemek Stekiel80a88492023-02-20 13:32:22 +01008227 (void) step;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008228 status = PSA_ERROR_NOT_SUPPORTED;
8229 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008230 }
8231
Przemek Stekiel691e91a2023-03-07 16:26:37 +01008232 status = psa_driver_wrapper_pake_input(operation, driver_step,
8233 input, input_length);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008234
8235 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008236 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008237 }
8238
8239 switch (operation->alg) {
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008240#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008241 case PSA_ALG_JPAKE:
David Horstmann5da95602023-06-08 15:37:12 +01008242 status = psa_jpake_epilogue(operation, PSA_JPAKE_INPUT);
Przemek Stekiele12ed362022-12-21 12:54:46 +01008243 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008244 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008245 }
8246 break;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008247#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiele12ed362022-12-21 12:54:46 +01008248 default:
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008249 status = PSA_ERROR_NOT_SUPPORTED;
8250 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008251 }
8252
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008253 return PSA_SUCCESS;
8254exit:
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008255 psa_pake_abort(operation);
8256 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008257}
8258
8259psa_status_t psa_pake_get_implicit_key(
8260 psa_pake_operation_t *operation,
8261 psa_key_derivation_operation_t *output)
8262{
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008264 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel251e86a2023-02-17 14:30:50 +01008265 uint8_t shared_key[MBEDTLS_PSA_JPAKE_BUFFER_SIZE];
Przemek Stekiel0c781802022-11-29 14:53:13 +01008266 size_t shared_key_len = 0;
8267
Przemek Stekielf62b3bb2023-01-31 19:51:24 +01008268 if (operation->stage != PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008269 status = PSA_ERROR_BAD_STATE;
8270 goto exit;
Neil Armstrong5ae60962022-09-15 11:29:46 +02008271 }
8272
Przemek Stekiel4aa99402023-02-27 13:00:57 +01008273#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekiele12ed362022-12-21 12:54:46 +01008274 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008275 psa_jpake_computation_stage_t *computation_stage =
Przemek Stekiel083745e2023-02-23 17:28:23 +01008276 &operation->computation_stage.jpake;
David Horstmann5da95602023-06-08 15:37:12 +01008277 if (computation_stage->round != PSA_JPAKE_FINISHED) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008278 status = PSA_ERROR_BAD_STATE;
8279 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008280 }
Przemek Stekiel80a88492023-02-20 13:32:22 +01008281 } else
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008282#endif /* PSA_WANT_ALG_JPAKE */
Przemek Stekiel80a88492023-02-20 13:32:22 +01008283 {
8284 status = PSA_ERROR_NOT_SUPPORTED;
8285 goto exit;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008286 }
8287
Przemek Stekiel0c781802022-11-29 14:53:13 +01008288 status = psa_driver_wrapper_pake_get_implicit_key(operation,
8289 shared_key,
Przemek Stekiel6b648622023-02-19 22:55:33 +01008290 sizeof(shared_key),
Przemek Stekiel0c781802022-11-29 14:53:13 +01008291 &shared_key_len);
8292
8293 if (status != PSA_SUCCESS) {
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008294 goto exit;
Przemek Stekiel0c781802022-11-29 14:53:13 +01008295 }
8296
8297 status = psa_key_derivation_input_bytes(output,
8298 PSA_KEY_DERIVATION_INPUT_SECRET,
8299 shared_key,
8300 shared_key_len);
8301
Przemek Stekiele3ef3a12023-02-27 10:20:06 +01008302 mbedtls_platform_zeroize(shared_key, sizeof(shared_key));
Przemek Stekiel3e784d82023-02-08 09:12:42 +01008303exit:
8304 abort_status = psa_pake_abort(operation);
8305 return status == PSA_SUCCESS ? abort_status : status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008306}
8307
8308psa_status_t psa_pake_abort(
8309 psa_pake_operation_t *operation)
8310{
Przemek Stekield69dca92023-01-31 19:59:20 +01008311 psa_status_t status = PSA_SUCCESS;
Przemek Stekiele12ed362022-12-21 12:54:46 +01008312
Przemek Stekield69dca92023-01-31 19:59:20 +01008313 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COMPUTATION) {
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008314 status = psa_driver_wrapper_pake_abort(operation);
Neil Armstrong5ae60962022-09-15 11:29:46 +02008315 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008316
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008317 if (operation->stage == PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS) {
8318 if (operation->data.inputs.password != NULL) {
8319 mbedtls_platform_zeroize(operation->data.inputs.password,
Przemek Stekielaa183422023-03-06 14:21:44 +01008320 operation->data.inputs.password_len);
Przemek Stekiel26c909d2023-02-28 12:34:03 +01008321 mbedtls_free(operation->data.inputs.password);
8322 }
8323 if (operation->data.inputs.user != NULL) {
8324 mbedtls_free(operation->data.inputs.user);
8325 }
8326 if (operation->data.inputs.peer != NULL) {
8327 mbedtls_free(operation->data.inputs.peer);
8328 }
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008329 }
Przemek Stekield69dca92023-01-31 19:59:20 +01008330 memset(operation, 0, sizeof(psa_pake_operation_t));
Przemek Stekiel9a5b8122022-12-22 13:34:47 +01008331
Przemek Stekield69dca92023-01-31 19:59:20 +01008332 return status;
Neil Armstronga7d08c32022-06-01 18:21:20 +02008333}
Tom Cosgrove6d62fac2023-05-10 14:40:05 +01008334#endif /* PSA_WANT_ALG_SOME_PAKE */
Neil Armstronga7d08c32022-06-01 18:21:20 +02008335
Gilles Peskinee59236f2018-01-27 23:32:46 +01008336#endif /* MBEDTLS_PSA_CRYPTO_C */